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);
67 result.energy_history.push_back(sa_system.GetEnergy());
68 result.temperature_history.push_back(T);
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) {
76 result.solution[i] = state[i].value;
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) {
92 model, schedule, num_sweeps,
97 model, schedule, num_sweeps,
98 static_cast<std::mt19937::result_type
>(seed), min_T, max_T,
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) {
120 model, num_sweeps, rand_type, schedule, seed, min_T, max_T, log_history);
123 model, num_sweeps, rand_type, schedule, seed, min_T, max_T, log_history);
126 model, num_sweeps, rand_type, schedule, seed, min_T, max_T, log_history);
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.
Definition algorithm.hpp:75
@ HEAT_BATH
Heat bath update.
Definition algorithm.hpp:69
@ METROPOLIS
Metropolis update.
Definition algorithm.hpp:66
@ SUWA_TODO
Suwa-Todo update.
Definition algorithm.hpp:72
RandomNumberEngine
Definition algorithm.hpp:78
@ MT
32-bit Mersenne Twister
Definition algorithm.hpp:84
@ XORSHIFT
32-bit Xorshift
Definition algorithm.hpp:81
@ MT_64
64-bit Mersenne Twister
Definition algorithm.hpp:87
Definition model/__init__.py:1
Definition get_solution.hpp:29
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:285
@ GEOMETRIC
Geometric cooling.
Definition schedule_list.hpp:291
@ LINEAR
Linear cooling.
Definition schedule_list.hpp:288
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