GHOST
Loading...
Searching...
No Matches
variable.hpp
Go to the documentation of this file.
1/*
2 * GHOST (General meta-Heuristic Optimization Solving Tool) is a C++ framework
3 * designed to help developers to model and implement optimization problem
4 * solving. It contains a meta-heuristic solver aiming to solve any kind of
5 * combinatorial and optimization real-time problems represented by a CSP/COP/EF-CSP/EF-COP.
6 *
7 * First developed to solve game-related optimization problems, GHOST can be used for
8 * any kind of applications where solving combinatorial and optimization problems. In
9 * particular, it had been designed to be able to solve not-too-complex problem instances
10 * within some milliseconds, making it very suitable for highly reactive or embedded systems.
11 * Please visit https://github.com/richoux/GHOST for further information.
12 *
13 * Copyright (C) 2014-2025 Florian Richoux
14 *
15 * This file is part of GHOST.
16 * GHOST is free software: you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as published
18 * by the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20
21 * GHOST is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25
26 * You should have received a copy of the GNU General Public License
27 * along with GHOST. If not, see http://www.gnu.org/licenses/.
28 */
29
30
31#pragma once
32
33#include <iostream>
34#include <vector>
35#include <string>
36#include <algorithm>
37
38#include "thirdparty/randutils.hpp"
39
40namespace ghost
41{
56 class Variable final
57 {
58 friend class SearchUnit;
59 friend class ModelBuilder;
60
61 std::vector<int> _domain; // The domain, i.e., the vector of values the variable can take.
62 int _id; // Unique ID integer
63 std::string _name; // String to give a name to the variable, helpful to debug/trace.
64
65 int _current_value; // Current value assigned to the variable.
66 int _min_value; // minimal value in the domain
67 int _max_value; // maximal value in the domain
68
69 struct valueException : std::exception
70 {
71 int value;
72 int min;
73 int max;
74 valueException( int value, int min, int max ) : value( value ), min( min ), max( max ) {}
75 std::string message = "Wrong value " + std::to_string( value ) + " passed to Variable::set_value. The given value does not belong to the domain and/or is not be between "
76 + std::to_string( min ) + " (included) and "
77 + std::to_string( max ) + " (included).\n";
78 const char* what() const noexcept { return message.c_str(); }
79 };
80
81 // Assign to the variable a random values from its domain.
82 inline void pick_random_value( randutils::mt19937_rng& rng ) { _current_value = rng.pick( _domain ); }
83
84 public:
86 Variable() = default;
87
102 Variable( const std::vector<int>& domain,
103 int index = 0,
104 const std::string& name = std::string() );
105
119 Variable( int starting_value,
120 std::size_t size,
121 int index = 0,
122 const std::string& name = std::string() );
123
133 Variable( const std::vector<int>& domain,
134 const std::string& name );
135
146 Variable( int starting_value,
147 std::size_t size,
148 const std::string& name );
149
155 inline std::vector<int> get_full_domain() const { return _domain; }
156
164 std::vector<int> get_partial_domain( int range ) const;
165
171 inline int get_value() const { return _current_value; }
172
179 inline void set_value( int value )
180 {
181 if( std::find( _domain.cbegin(), _domain.cend(), value ) == _domain.cend() )
182 throw valueException( value, get_domain_min_value(), get_domain_max_value() );
183
184 _current_value = value;
185 }
186
192 inline std::size_t get_domain_size() const { return _domain.size(); }
193
199 inline int get_domain_min_value() const { return _min_value; }
200
206 inline int get_domain_max_value() const { return _max_value; }
207
209 inline std::string get_name() const { return _name; }
210
212 inline int get_id() const { return _id; }
213
215 friend std::ostream& operator<<( std::ostream& os, const Variable& v )
216 {
217 std::string domain = "";
218 for( auto value : v.get_full_domain() )
219 domain += std::to_string( value ) + std::string( ", " );
220
221 return os
222 << "Variable name: " << v._name
223 << "\nId: " << v._id
224 << "\nValue: " << v._current_value
225 << "\nDomain: " << domain
226 << "\n--------";
227 }
228 };
229}
Definition model_builder.hpp:63
Definition variable.hpp:57
void set_value(int value)
Definition variable.hpp:179
Variable(int starting_value, std::size_t size, int index=0, const std::string &name=std::string())
friend class SearchUnit
Definition variable.hpp:58
Variable(int starting_value, std::size_t size, const std::string &name)
int get_id() const
Inline method to get the unique id of the Variable object.
Definition variable.hpp:212
int get_value() const
Definition variable.hpp:171
std::string get_name() const
Inline accessor to get the variable name.
Definition variable.hpp:209
Variable(const std::vector< int > &domain, int index=0, const std::string &name=std::string())
Variable(const std::vector< int > &domain, const std::string &name)
friend std::ostream & operator<<(std::ostream &os, const Variable &v)
To have a nicer stream of Variable.
Definition variable.hpp:215
int get_domain_min_value() const
Definition variable.hpp:199
int get_domain_max_value() const
Definition variable.hpp:206
std::vector< int > get_full_domain() const
Definition variable.hpp:155
std::vector< int > get_partial_domain(int range) const
std::size_t get_domain_size() const
Definition variable.hpp:192
Variable()=default
Default constructor.
Definition auxiliary_data.hpp:38