26template<
class ModelType>
43 if (num_sweeps <= 0) {
44 throw std::runtime_error(
"num_sweeps must be larger than zero.");
53 throw std::runtime_error(
"num_reads must be larger than zero.");
61 if (num_threads <= 0) {
62 throw std::runtime_error(
"num_threads must be non-negative integer.");
71 throw std::runtime_error(
"beta_min must be positive number");
80 throw std::runtime_error(
"beta_max must be positive number");
173 const std::vector<typename ModelType::IndexType> &
GetIndexList()
const {
174 return model_.GetIndexList();
179 const std::vector<std::vector<VariableType>> &
GetSamples()
const {
185 throw std::runtime_error(
"The sample size is zero. It seems that sampling has not been carried out.");
190#pragma omp parallel for schedule(guided) num_threads(num_threads_)
191 for (std::int32_t i = 0; i <
num_reads_; ++i) {
195 catch (
const std::exception &e) {
196 std::cerr << e.what() << std::endl;
205 Sample(std::random_device()());
218 TemplateSampler<system::SASystem<ModelType, utility::Xorshift>,
utility::Xorshift>();
221 TemplateSampler<system::SASystem<ModelType, std::mt19937>, std::mt19937>();
224 TemplateSampler<system::SASystem<ModelType, std::mt19937_64>, std::mt19937_64>();
227 throw std::runtime_error(
"Unknown RandomNumberEngine");
261 std::uint64_t
seed_ = std::random_device()();
266 template<
typename RandType>
267 std::vector<std::pair<typename RandType::result_type, typename RandType::result_type>>
269 RandType random_number_engine(seed);
270 std::vector<std::pair<typename RandType::result_type, typename RandType::result_type>> seed_pair_list(num_reads);
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();
277 return seed_pair_list;
280 template<
class SystemType,
class RandType>
282 const auto seed_pair_list = GenerateSeedPairList<RandType>(
static_cast<typename RandType::result_type
>(
seed_),
num_reads_);
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();
295template<
class ModelType>
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
32-bit Mersenne Twister
@ XORSHIFT
32-bit Xorshift
@ MT_64
64-bit Mersenne Twister
auto make_sa_sampler(const ModelType &model)
Definition sa_sampler.hpp:296
TemperatureSchedule
Definition schedule_list.hpp:259
@ GEOMETRIC
Geometric cooling.
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