openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
variable.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
17namespace openjij {
18namespace utility {
19
21
22 IntegerVariable(const std::int64_t lower_bound,
23 const std::int64_t upper_bound) {
24 this->lower_bound = lower_bound;
25 this->upper_bound = upper_bound;
26 this->num_states = upper_bound - lower_bound + 1;
27 this->value = lower_bound;
28 }
29
30 template <typename RandomNumberEngine>
31 void SetRandomValue(RandomNumberEngine &random_number_engine) {
32 this->value = std::uniform_int_distribution<std::int64_t>(
33 this->lower_bound, this->upper_bound)(random_number_engine);
34 }
35
36 void SetValue(const std::int64_t value) {
37 if (value < this->lower_bound || value > this->upper_bound) {
38 throw std::runtime_error("Value out of bounds.");
39 }
40 this->value = value;
41 }
42
43 template <typename RandomNumberEngine>
44 std::int64_t
45 GenerateCandidateValue(RandomNumberEngine &random_number_engine) const {
46 std::int64_t candidate_value = std::uniform_int_distribution<std::int64_t>(
47 this->lower_bound, this->upper_bound - 1)(random_number_engine);
48 if (candidate_value >= this->value) {
49 candidate_value += 1; // Ensure candidate is different from current value
50 }
51 return candidate_value;
52 }
53
54 template <typename RandomNumberEngine>
55 std::int64_t
56 GenerateRandomValue(RandomNumberEngine &random_number_engine) const {
57 return std::uniform_int_distribution<std::int64_t>(
58 this->lower_bound, this->upper_bound)(random_number_engine);
59 }
60
61 std::int64_t GetValueFromState(std::int64_t state) const {
62 return this->lower_bound + state;
63 }
64
65 std::int64_t GetStateFromValue(std::int64_t value) const {
66 if (value < this->lower_bound || value > this->upper_bound) {
67 throw std::runtime_error("Value out of bounds.");
68 }
69 return value - this->lower_bound;
70 }
71
72 std::int64_t lower_bound;
73 std::int64_t upper_bound;
74 std::int64_t num_states;
75 std::int64_t value;
76};
77
78} // namespace utility
79} // namespace openjij
Definition algorithm.hpp:24
Definition variable.hpp:20
std::int64_t GetValueFromState(std::int64_t state) const
Definition variable.hpp:61
std::int64_t GetStateFromValue(std::int64_t value) const
Definition variable.hpp:65
std::int64_t value
Definition variable.hpp:75
std::int64_t GenerateRandomValue(RandomNumberEngine &random_number_engine) const
Definition variable.hpp:56
std::int64_t GenerateCandidateValue(RandomNumberEngine &random_number_engine) const
Definition variable.hpp:45
std::int64_t upper_bound
Definition variable.hpp:73
std::int64_t lower_bound
Definition variable.hpp:72
std::int64_t num_states
Definition variable.hpp:74
void SetValue(const std::int64_t value)
Definition variable.hpp:36
void SetRandomValue(RandomNumberEngine &random_number_engine)
Definition variable.hpp:31
IntegerVariable(const std::int64_t lower_bound, const std::int64_t upper_bound)
Definition variable.hpp:22