22template<
typename FloatType,
typename RandType>
23class SASystem<graph::BinaryPolynomialModel<FloatType>, RandType> {
32 using SeedType =
typename RandType::result_type;
42 system_size_(model.GetSystemSize()),
43 key_value_list_(model.GetKeyValueList()),
44 adjacency_list_(model.GetAdjacencyList()) {
45 SetRandomConfiguration(seed);
47 SetBaseEnergyDifference();
50 void SetSample(
const std::vector<VariableType> &sample) {
51 if (
static_cast<std::int32_t
>(sample.size()) != system_size_) {
52 throw std::runtime_error(
"The size of initial variables is not equal to the system size.");
54 for (std::int32_t i = 0; i < system_size_; ++i) {
55 if (!(sample[i] == 0 || sample[i] == 1)) {
56 throw std::runtime_error(
"The initial variables must be 0 or 1.");
61 SetBaseEnergyDifference();
66 void Flip(
const std::int32_t index) {
68 sample_[index] = 1 - sample_[index];
70 for (
const auto &index_key: adjacency_list_[index]) {
71 const ValueType val = key_value_list_[index_key].second;
72 const std::int32_t total_zero_count = zero_count_[index_key];
73 zero_count_[index_key] -= 1;
74 for (
const auto &v_index: key_value_list_[index_key].first) {
75 if (total_zero_count + sample_[v_index] == 2 && v_index != index) {
76 base_energy_difference_[v_index] += val;
82 for (
const auto &index_key: adjacency_list_[index]) {
83 const ValueType val = key_value_list_[index_key].second;
84 const std::int32_t total_zero_count = zero_count_[index_key];
85 zero_count_[index_key] += 1;
86 for (
const auto &v_index: key_value_list_[index_key].first) {
87 if (total_zero_count + sample_[v_index] == 1 && v_index != index) {
88 base_energy_difference_[v_index] -= val;
110 return base_energy_difference_;
117 return (1 - 2*sample_[index])*base_energy_difference_[index];
132 sample_.resize(system_size_);
133 std::uniform_int_distribution<short> dist(0, 1);
134 RandType random_number_engine(seed);
135 for (std::int32_t i = 0; i < system_size_; i++) {
136 sample_[i] = dist(random_number_engine);
141 zero_count_.resize(key_value_list_.size());
142 for (std::size_t i = 0; i < key_value_list_.size(); ++i) {
143 std::int32_t count = 0;
144 for (
const auto &index : key_value_list_[i].first) {
145 if (sample_[index] == 0) {
149 zero_count_[i] = count;
154 base_energy_difference_.clear();
155 base_energy_difference_.resize(system_size_);
156 for (std::size_t i = 0; i < key_value_list_.size(); ++i) {
157 const ValueType value = key_value_list_[i].second;
158 for (
const auto &index: key_value_list_[i].first) {
159 if (sample_[index] + zero_count_[i] == 1) {
160 base_energy_difference_[index] += value;
Definition binary_polynomial_model.hpp:27
std::int8_t VariableType
The variable type, which here represents binary variables .
Definition binary_polynomial_model.hpp:43
FloatType ValueType
The value type.
Definition binary_polynomial_model.hpp:34
const std::int32_t system_size_
Definition binary_polynomial_sa_system.hpp:121
std::vector< VariableType > sample_
Definition binary_polynomial_sa_system.hpp:125
SASystem(const ModelType &model, const SeedType seed)
Constructor of SASystem for BinaryPolynomialModel.
Definition binary_polynomial_sa_system.hpp:41
void SetRandomConfiguration(const SeedType seed)
Set initial binary variables.
Definition binary_polynomial_sa_system.hpp:131
void SetBaseEnergyDifference()
Definition binary_polynomial_sa_system.hpp:153
void Flip(const std::int32_t index)
Flip a variable.
Definition binary_polynomial_sa_system.hpp:66
const std::vector< ValueType > & GetBaseEnergyDifference() const
Get the energy difference when flipped and sample as list.
Definition binary_polynomial_sa_system.hpp:109
typename RandType::result_type SeedType
The type of seed in random number engine.
Definition binary_polynomial_sa_system.hpp:32
const std::vector< std::vector< std::size_t > > & adjacency_list_
Definition binary_polynomial_sa_system.hpp:123
ValueType GetEnergyDifference(const std::int32_t index) const
Get the energy difference when flipped.
Definition binary_polynomial_sa_system.hpp:116
void SetSample(const std::vector< VariableType > &sample)
Definition binary_polynomial_sa_system.hpp:50
void SetZeroCount()
Definition binary_polynomial_sa_system.hpp:140
std::vector< std::int32_t > zero_count_
Definition binary_polynomial_sa_system.hpp:127
typename ModelType::ValueType ValueType
The value type.
Definition binary_polynomial_sa_system.hpp:36
typename ModelType::VariableType VariableType
The variable type, which here represents binary variables .
Definition binary_polynomial_sa_system.hpp:29
std::vector< ValueType > base_energy_difference_
Definition binary_polynomial_sa_system.hpp:126
const std::vector< std::pair< std::vector< std::int32_t >, ValueType > > & key_value_list_
Definition binary_polynomial_sa_system.hpp:122
const std::vector< VariableType > & ExtractSample() const
Extract the sample.
Definition binary_polynomial_sa_system.hpp:103
std::int32_t GetSystemSize() const
Get the system size.
Definition binary_polynomial_sa_system.hpp:97
Definition sa_system.hpp:21
Definition algorithm.hpp:24