31template <
class ModelType,
class RandType,
class StateUpdater>
34 const std::int64_t num_sweeps,
const typename RandType::result_type seed,
35 const double min_T,
const double max_T,
const bool log_history) {
41 auto state_updater = StateUpdater{};
43 auto get_T = [&](
const std::int64_t sweep) {
46 (min_T - max_T) * (
static_cast<double>(sweep) / (num_sweeps - 1));
48 return max_T * std::pow(min_T / max_T,
49 static_cast<double>(sweep) / (num_sweeps - 1));
51 throw std::runtime_error(
"Unknown temperature schedule");
55 const std::int64_t num_variables = model.GetNumVariables();
58 for (std::int64_t sweep = 0; sweep < num_sweeps; ++sweep) {
59 const double T = get_T(sweep);
60 const double progress =
static_cast<double>(sweep) / (num_sweeps - 1);
61 for (std::int64_t i = 0; i < num_variables; ++i) {
63 state_updater.GenerateNewValue(sa_system, i, T, progress);
64 sa_system.SetValue(i, new_x);
72 result.
energy = sa_system.GetEnergy();
73 result.
solution.resize(num_variables);
74 const auto &state = sa_system.GetState();
75 for (std::int64_t i = 0; i < num_variables; ++i) {
82template <
class ModelType,
class UpdaterType>
84 const std::int64_t num_sweeps,
87 const std::int64_t seed,
const double min_T,
88 const double max_T,
const bool log_history) {
91 return BaseSA<ModelType, utility::Xorshift, UpdaterType>(
92 model, schedule, num_sweeps,
96 return BaseSA<ModelType, std::mt19937, UpdaterType>(
97 model, schedule, num_sweeps,
98 static_cast<std::mt19937::result_type
>(seed), min_T, max_T,
101 return BaseSA<ModelType, std::mt19937_64, UpdaterType>(
102 model, schedule, num_sweeps, seed, min_T, max_T, log_history);
104 throw std::runtime_error(
"Unknown random number engine");
108template <
class ModelType>
110 const std::int64_t num_sweeps,
114 const std::int64_t seed,
const double min_T,
115 const double max_T,
const bool log_history) {
117 switch (update_method) {
119 return SolveByIntegerSAImpl<ModelType, updater::MetropolisUpdater>(
120 model, num_sweeps, rand_type, schedule, seed, min_T, max_T, log_history);
122 return SolveByIntegerSAImpl<ModelType, updater::HeatBathUpdater>(
123 model, num_sweeps, rand_type, schedule, seed, min_T, max_T, log_history);
125 return SolveByIntegerSAImpl<ModelType, updater::SuwaTodoUpdater>(
126 model, num_sweeps, rand_type, schedule, seed, min_T, max_T, log_history);
128 return SolveByIntegerSAImpl<ModelType, updater::OptMetropolisUpdater>(
129 model, num_sweeps, rand_type, schedule, seed, min_T, max_T, log_history);
131 throw std::runtime_error(
"Unknown update method");
135template <
class ModelType>
136std::vector<IntegerSAResult>
141 const std::int64_t num_reads,
const std::int64_t seed,
142 const std::int32_t num_threads,
const double min_T,
143 const double max_T,
const bool log_history) {
145 std::vector<IntegerSAResult> results(num_reads);
147#pragma omp parallel for schedule(guided) num_threads(num_threads)
148 for (std::int64_t i = 0; i < num_reads; ++i) {
151 seed + i, min_T, max_T, log_history);
Definition sa_system.hpp:24
uint_fast32_t result_type
Definition random.hpp:41
UpdateMethod
Definition algorithm.hpp:63
@ OPT_METROPOLIS
Metropolis update with optimal transition.
@ HEAT_BATH
Heat bath update.
@ METROPOLIS
Metropolis update.
@ SUWA_TODO
Suwa-Todo update.
RandomNumberEngine
Definition algorithm.hpp:78
@ MT
32-bit Mersenne Twister
@ XORSHIFT
32-bit Xorshift
@ MT_64
64-bit Mersenne Twister
IntegerSAResult SolveByIntegerSA(const ModelType &model, const std::int64_t num_sweeps, const algorithm::UpdateMethod update_method, const algorithm::RandomNumberEngine rand_type, const utility::TemperatureSchedule schedule, const std::int64_t seed, const double min_T, const double max_T, const bool log_history)
Definition integer_sa_sampler.hpp:109
IntegerSAResult BaseSA(const ModelType &model, const utility::TemperatureSchedule schedule, const std::int64_t num_sweeps, const typename RandType::result_type seed, const double min_T, const double max_T, const bool log_history)
Definition integer_sa_sampler.hpp:33
IntegerSAResult SolveByIntegerSAImpl(const ModelType &model, const std::int64_t num_sweeps, const algorithm::RandomNumberEngine rand_type, const utility::TemperatureSchedule schedule, const std::int64_t seed, const double min_T, const double max_T, const bool log_history)
Definition integer_sa_sampler.hpp:83
std::vector< IntegerSAResult > SampleByIntegerSA(const ModelType &model, const std::int64_t num_sweeps, const algorithm::UpdateMethod update_method, const algorithm::RandomNumberEngine rand_type, const utility::TemperatureSchedule schedule, const std::int64_t num_reads, const std::int64_t seed, const std::int32_t num_threads, const double min_T, const double max_T, const bool log_history)
Definition integer_sa_sampler.hpp:137
TemperatureSchedule
Definition schedule_list.hpp:259
@ GEOMETRIC
Geometric cooling.
Definition algorithm.hpp:24
Definition integer_sa_sampler.hpp:24
std::vector< std::int64_t > solution
Definition integer_sa_sampler.hpp:26
std::vector< double > temperature_history
Definition integer_sa_sampler.hpp:28
double energy
Definition integer_sa_sampler.hpp:25
std::vector< double > energy_history
Definition integer_sa_sampler.hpp:27