Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add $Ex(1)$ sampler #327

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions heuristic/exponential_dist_sampler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <cmath>
#include <cstdint>
#include <array>

template <int D> struct ExponentialDistSampler {
std::array<double, (1 << D)> minuslogps;

constexpr ExponentialDistSampler() {
for (int i = 0; i < (1 << D); ++i) minuslogps.at(i) = -log((0.5 + i) / (1 << D));
}

double sample(uint32_t random_mask) const {
return minuslogps.at(random_mask & ((1 << D) - 1));
}

// p ~ U(0, 1) => -log(p) ~ Ex(1)
// P[exp(-|dx| / T) >= p] = P[|dx| <= -log(p) * T]
bool check_sa(double abs_dx, double T, uint32_t random_mask) const {
return abs_dx <= minuslogps.at(random_mask & ((1 << D) - 1)) * T;
}
};
// const ExponentialDistSampler<16> log_ps;
25 changes: 25 additions & 0 deletions heuristic/exponential_dist_sampler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Fast sampler of exponential distribution (高速指数分布サンプラー・擬似焼きなまし法の遷移判定)
documentation_of: ./exponential_dist_sampler.hpp
---

母数 $\lambda = 1$ の指数分布を模擬するサンプラー.

典型的な用途として,擬似焼きなまし法 (simulated annealing, SA) の遷移判定を(指数関数や対数関数の計算を毎回行うことなく)高速に行える.

## 使用方法

```cpp
constexpr int D = 18;
const ExponentialDistSampler<D> eds; // 2^D 個のサンプルを前計算

// 下位 D bit がランダムに分布した mask を与えると x ~ Ex(1) をサンプル
uint32_t mask;
double x = eds.sample(mask);

double dx;
double T;

// コストの変化が abs(dx), 温度 T のとき焼きなまし法の遷移を受理するか確率的に判定
bool upd = eds.check_sa(abs(dx), T, mask);
```
Loading