openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
sa_sampler.hpp
Go to the documentation of this file.
1// Copyright 2023 Jij Inc.
2
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include "openjij/graph/all.hpp"
20
21namespace openjij {
22namespace sampler {
23
26template<class ModelType>
27class SASampler {
28
30 using ValueType = typename ModelType::ValueType;
31
33 using VariableType = typename ModelType::VariableType;
34
35public:
38 SASampler(const ModelType &model): model_(model) {}
39
42 void SetNumSweeps(const std::int32_t num_sweeps) {
43 if (num_sweeps <= 0) {
44 throw std::runtime_error("num_sweeps must be larger than zero.");
45 }
46 num_sweeps_ = num_sweeps;
47 }
48
51 void SetNumReads(const std::int32_t num_reads) {
52 if (num_reads <= 0) {
53 throw std::runtime_error("num_reads must be larger than zero.");
54 }
55 num_reads_ = num_reads;
56 }
57
60 void SetNumThreads(const std::int32_t num_threads) {
61 if (num_threads <= 0) {
62 throw std::runtime_error("num_threads must be non-negative integer.");
63 }
64 num_threads_ = num_threads;
65 }
66
69 void SetBetaMin(const ValueType beta_min) {
70 if (beta_min < 0) {
71 throw std::runtime_error("beta_min must be positive number");
72 }
73 beta_min_ = beta_min;
74 }
75
78 void SetBetaMax(const ValueType beta_max) {
79 if (beta_max < 0) {
80 throw std::runtime_error("beta_max must be positive number");
81 }
82 beta_max_ = beta_max;
83 }
84
87 beta_min_ = std::log(2.0)/model_.GetEstimatedMaxEnergyDifference();
88 }
89
92 beta_max_ = std::log(100.0)/model_.GetEstimatedMinEnergyDifference();
93 }
94
97 void SetUpdateMethod(const algorithm::UpdateMethod update_method) {
98 update_method_ = update_method;
99 }
100
103 void SetRandomNumberEngine(const algorithm::RandomNumberEngine random_number_engine) {
104 random_number_engine_ = random_number_engine;
105 }
106
110 schedule_ = schedule;
111 }
112
115 const ModelType &GetModel() const {
116 return model_;
117 }
118
121 std::int32_t GetNumSweeps() const {
122 return num_sweeps_;
123 }
124
127 std::int32_t GetNumReads() const {
128 return num_reads_;
129 }
130
133 std::int32_t GetNumThreads() const {
134 return num_threads_;
135 }
136
140 return beta_min_;
141 }
142
146 return beta_max_;
147 }
148
154
160
166
169 std::uint64_t GetSeed() const {
170 return seed_;
171 }
172
173 const std::vector<typename ModelType::IndexType> &GetIndexList() const {
174 return model_.GetIndexList();
175 }
176
179 const std::vector<std::vector<VariableType>> &GetSamples() const {
180 return samples_;
181 }
182
183 std::vector<ValueType> CalculateEnergies() const {
184 if (samples_.size() == 0) {
185 throw std::runtime_error("The sample size is zero. It seems that sampling has not been carried out.");
186 }
187 std::vector<ValueType> energies(num_reads_);
188
189 try {
190#pragma omp parallel for schedule(guided) num_threads(num_threads_)
191 for (std::int32_t i = 0; i < num_reads_; ++i) {
192 energies[i] = model_.CalculateEnergy(samples_[i]);
193 }
194 }
195 catch (const std::exception &e) {
196 std::cerr << e.what() << std::endl;
197 }
198
199 return energies;
200 }
201
204 void Sample() {
205 Sample(std::random_device()());
206 }
207
210 void Sample(const std::uint64_t seed) {
211 seed_ = seed;
212
213 samples_.clear();
214 samples_.shrink_to_fit();
215 samples_.resize(num_reads_);
216
218 TemplateSampler<system::SASystem<ModelType, utility::Xorshift>, utility::Xorshift>();
219 }
221 TemplateSampler<system::SASystem<ModelType, std::mt19937>, std::mt19937>();
222 }
224 TemplateSampler<system::SASystem<ModelType, std::mt19937_64>, std::mt19937_64>();
225 }
226 else {
227 throw std::runtime_error("Unknown RandomNumberEngine");
228 }
229
230 }
231
232private:
234 const ModelType model_;
235
237 std::int32_t num_sweeps_ = 1000;
238
240 std::int32_t num_reads_ = 1;
241
243 std::int32_t num_threads_ = 1;
244
247
250
253
256
259
261 std::uint64_t seed_ = std::random_device()();
262
264 std::vector<std::vector<VariableType>> samples_;
265
266 template<typename RandType>
267 std::vector<std::pair<typename RandType::result_type, typename RandType::result_type>>
268 GenerateSeedPairList(const typename RandType::result_type seed, const std::int32_t num_reads) const {
269 RandType random_number_engine(seed);
270 std::vector<std::pair<typename RandType::result_type, typename RandType::result_type>> seed_pair_list(num_reads);
271
272 for (std::int32_t i = 0; i < num_reads; ++i) {
273 seed_pair_list[i].first = random_number_engine();
274 seed_pair_list[i].second = random_number_engine();
275 }
276
277 return seed_pair_list;
278 }
279
280 template<class SystemType, class RandType>
282 const auto seed_pair_list = GenerateSeedPairList<RandType>(static_cast<typename RandType::result_type>(seed_), num_reads_);
283 std::vector<ValueType> beta_list = utility::GenerateBetaList(schedule_, beta_min_, beta_max_, num_sweeps_);
284
285#pragma omp parallel for schedule(guided) num_threads(num_threads_)
286 for (std::int32_t i = 0; i < num_reads_; ++i) {
287 auto system = SystemType{model_, seed_pair_list[i].first};
288 updater::SingleFlipUpdater<SystemType, RandType>(&system, num_sweeps_, beta_list, seed_pair_list[i].second, update_method_);
289 samples_[i] = system.ExtractSample();
290 }
291 }
292
293};
294
295template<class ModelType>
296auto make_sa_sampler(const ModelType &model) {
297 return SASampler<ModelType>{model};
298};
299
300
301
302} //sampler
303} //openjij
Class for executing simulated annealing.
Definition sa_sampler.hpp:27
algorithm::RandomNumberEngine random_number_engine_
Random number engine for updating and initializing state.
Definition sa_sampler.hpp:255
void SetNumSweeps(const std::int32_t num_sweeps)
Set the number of sweeps.
Definition sa_sampler.hpp:42
void SetNumThreads(const std::int32_t num_threads)
Set the number of threads in the calculation.
Definition sa_sampler.hpp:60
std::int32_t num_sweeps_
The number of sweeps.
Definition sa_sampler.hpp:237
const ModelType & GetModel() const
Get the model.
Definition sa_sampler.hpp:115
void SetBetaMin(const ValueType beta_min)
Set the minimum inverse temperature.
Definition sa_sampler.hpp:69
ValueType GetBetaMin() const
Get the minimum inverse temperature.
Definition sa_sampler.hpp:139
void SetNumReads(const std::int32_t num_reads)
Set the number of samples.
Definition sa_sampler.hpp:51
algorithm::RandomNumberEngine GetRandomNumberEngine() const
Get the random number engine for updating and initializing state.
Definition sa_sampler.hpp:157
const ModelType model_
The model.
Definition sa_sampler.hpp:234
const std::vector< typename ModelType::IndexType > & GetIndexList() const
Definition sa_sampler.hpp:173
void Sample()
Execute sampling.
Definition sa_sampler.hpp:204
std::int32_t GetNumSweeps() const
Get the number of sweeps.
Definition sa_sampler.hpp:121
algorithm::UpdateMethod GetUpdateMethod() const
Get the update method used in the state update.
Definition sa_sampler.hpp:151
std::int32_t GetNumReads() const
Get the number of reads.
Definition sa_sampler.hpp:127
const std::vector< std::vector< VariableType > > & GetSamples() const
Get the samples.
Definition sa_sampler.hpp:179
typename ModelType::ValueType ValueType
The value type.
Definition sa_sampler.hpp:30
std::vector< std::vector< VariableType > > samples_
The samples.
Definition sa_sampler.hpp:264
std::uint64_t seed_
The seed to be used in the calculation.
Definition sa_sampler.hpp:261
void SetBetaMax(const ValueType beta_max)
Set the minimum inverse temperature.
Definition sa_sampler.hpp:78
std::uint64_t GetSeed() const
Get the seed to be used in the calculation.
Definition sa_sampler.hpp:169
SASampler(const ModelType &model)
Constructor for SASampler class.
Definition sa_sampler.hpp:38
utility::TemperatureSchedule GetTemperatureSchedule() const
Get the temperature schedule.
Definition sa_sampler.hpp:163
utility::TemperatureSchedule schedule_
Cooling schedule.
Definition sa_sampler.hpp:258
void SetRandomNumberEngine(const algorithm::RandomNumberEngine random_number_engine)
Set random number engine for updating initializing state.
Definition sa_sampler.hpp:103
ValueType beta_max_
The end inverse temperature.
Definition sa_sampler.hpp:249
std::int32_t num_threads_
The number of threads in the calculation.
Definition sa_sampler.hpp:243
void SetTemperatureSchedule(const utility::TemperatureSchedule schedule)
Set the cooling schedule.
Definition sa_sampler.hpp:109
void Sample(const std::uint64_t seed)
Execute sampling.
Definition sa_sampler.hpp:210
algorithm::UpdateMethod update_method_
The update method used in the state update.
Definition sa_sampler.hpp:252
void SetUpdateMethod(const algorithm::UpdateMethod update_method)
Set update method used in the state update.
Definition sa_sampler.hpp:97
ValueType beta_min_
The start inverse temperature.
Definition sa_sampler.hpp:246
void TemplateSampler()
Definition sa_sampler.hpp:281
typename ModelType::VariableType VariableType
The variable type.
Definition sa_sampler.hpp:33
std::vector< std::pair< typename RandType::result_type, typename RandType::result_type > > GenerateSeedPairList(const typename RandType::result_type seed, const std::int32_t num_reads) const
Definition sa_sampler.hpp:268
void SetBetaMinAuto()
Set the minimum inverse temperature automatically.
Definition sa_sampler.hpp:86
void SetBetaMaxAuto()
Set the maximum inverse temperature automatically.
Definition sa_sampler.hpp:91
ValueType GetBetaMax() const
Get the maximum inverse temperature.
Definition sa_sampler.hpp:145
std::int32_t GetNumThreads() const
Get the number of threads.
Definition sa_sampler.hpp:133
std::int32_t num_reads_
The number of reads (samples).
Definition sa_sampler.hpp:240
std::vector< ValueType > CalculateEnergies() const
Definition sa_sampler.hpp:183
xorshift random generator for c++11 random
Definition random.hpp:39
UpdateMethod
Definition algorithm.hpp:63
@ METROPOLIS
Metropolis update.
RandomNumberEngine
Definition algorithm.hpp:73
@ MT_64
64-bit Mersenne Twister
auto make_sa_sampler(const ModelType &model)
Definition sa_sampler.hpp:296
TemperatureSchedule
Definition schedule_list.hpp:259
std::vector< FloatType > GenerateBetaList(const TemperatureSchedule schedule_type, const FloatType beta_min, const FloatType beta_max, const std::int32_t num_sweeps)
Generate temperature schedule from specific schedule.
Definition schedule_list.hpp:316
Definition algorithm.hpp:24