openjij
Framework for the Ising model and QUBO.
Loading...
Searching...
No Matches
algorithm.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 <functional>
18
22
23
24namespace openjij {
25namespace algorithm {
26template <template <typename> class Updater> struct Algorithm {
27 template <typename System, typename RandomNumberEngine>
28 static void
29 run(System &system, RandomNumberEngine &random_number_engine,
31 typename system::get_system_type<System>::type> &schedule_list,
32 const std::function<void(
33 const System &,
35 typename system::get_system_type<System>::type> &)> &callback =
36 nullptr) {
37 if (callback) {
38 // with callback
39 for (auto &&schedule : schedule_list) {
40 for (std::size_t i = 0; i < schedule.one_mc_step; ++i) {
41 Updater<System>::update(system, random_number_engine,
42 schedule.updater_parameter);
43 callback(system, schedule.updater_parameter);
44 }
45 }
46 } else {
47 // without callback
48 for (auto &&schedule : schedule_list) {
49 for (std::size_t i = 0; i < schedule.one_mc_step; ++i) {
50 Updater<System>::update(system, random_number_engine,
51 schedule.updater_parameter);
52 }
53 }
54 }
55 }
56};
57
58// type alias (Monte Carlo method)
59// TODO: Algorithm class will be deprecated shortly.
60template <template <typename> class Updater> using MCMC = Algorithm<Updater>;
61
62
63enum class UpdateMethod {
64
67
70
71};
72
74
77
79 MT,
80
82 MT_64
83
84};
85
86
87std::variant<utility::Xorshift, std::mt19937, std::mt19937_64>
89 if (random_number_engine == RandomNumberEngine::XORSHIFT) {
90 return utility::Xorshift();
91 }
92 else if (random_number_engine == RandomNumberEngine::MT) {
93 return std::mt19937();
94 }
95 else if (random_number_engine == RandomNumberEngine::MT) {
96 return std::mt19937_64();
97 }
98 else {
99 throw std::runtime_error("Unknown RandomNumberEngine");
100 }
101}
102
103} // namespace algorithm
104} // namespace openjij
xorshift random generator for c++11 random
Definition random.hpp:39
std::variant< utility::Xorshift, std::mt19937, std::mt19937_64 > GenerateRandomNumberEngineClass(const RandomNumberEngine random_number_engine)
Definition algorithm.hpp:88
UpdateMethod
Definition algorithm.hpp:63
@ METROPOLIS
Metropolis update.
RandomNumberEngine
Definition algorithm.hpp:73
@ MT_64
64-bit Mersenne Twister
std::vector< Schedule< SystemType > > ScheduleList
schedule list alias
Definition schedule_list.hpp:135
Definition algorithm.hpp:24
Definition algorithm.hpp:26
static void run(System &system, RandomNumberEngine &random_number_engine, const utility::ScheduleList< typename system::get_system_type< System >::type > &schedule_list, const std::function< void(const System &, const utility::UpdaterParameter< typename system::get_system_type< System >::type > &)> &callback=nullptr)
Definition algorithm.hpp:29
typename System::system_type type
Definition system.hpp:77
updater parameter for monte carlo simulation
Definition schedule_list.hpp:32