openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
binary_polynomial_sa_system.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 "./sa_system.hpp"
18
19namespace openjij {
20namespace system {
21
22template<typename FloatType, typename RandType>
23class SASystem<graph::BinaryPolynomialModel<FloatType>, RandType> {
24
27
30
32 using SeedType = typename RandType::result_type;
33
34public:
37
41 SASystem(const ModelType &model, const SeedType seed):
42 system_size_(model.GetSystemSize()),
43 key_value_list_(model.GetKeyValueList()),
44 adjacency_list_(model.GetAdjacencyList()) {
45 SetRandomConfiguration(seed);
46 SetZeroCount();
47 SetBaseEnergyDifference();
48 }
49
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.");
53 }
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.");
57 }
58 }
59 sample_ = sample;
60 SetZeroCount();
61 SetBaseEnergyDifference();
62 }
63
66 void Flip(const std::int32_t index) {
67 const VariableType state = sample_[index];
68 sample_[index] = 1 - sample_[index];
69 if (state == 0) {
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;
77 }
78 }
79 }
80 }
81 else { //state == 1
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;
89 }
90 }
91 }
92 }
93 }
94
97 std::int32_t GetSystemSize() const {
98 return system_size_;
99 }
100
103 const std::vector<VariableType> &ExtractSample() const {
104 return sample_;
105 }
106
109 const std::vector<ValueType> &GetBaseEnergyDifference() const {
110 return base_energy_difference_;
111 }
112
116 ValueType GetEnergyDifference(const std::int32_t index) const {
117 return (1 - 2*sample_[index])*base_energy_difference_[index];
118 }
119
120private:
121 const std::int32_t system_size_;
122 const std::vector<std::pair<std::vector<std::int32_t>, ValueType>> &key_value_list_;
123 const std::vector<std::vector<std::size_t>> &adjacency_list_;
124
125 std::vector<VariableType> sample_;
126 std::vector<ValueType> base_energy_difference_;
127 std::vector<std::int32_t> zero_count_;
128
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);
137 }
138 }
139
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) {
146 count++;
147 }
148 }
149 zero_count_[i] = count;
150 }
151 }
152
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;
161 }
162 }
163 }
164 }
165
166
167};
168
169
170} // namespace system
171} // namespace openjij
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