-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathobjective.cpp
More file actions
123 lines (103 loc) · 4.03 KB
/
Copy pathobjective.cpp
File metadata and controls
123 lines (103 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* GHOST (General meta-Heuristic Optimization Solving Tool) is a C++ framework
* designed to help developers to model and implement optimization problem
* solving. It contains a meta-heuristic solver aiming to solve any kind of
* combinatorial and optimization real-time problems represented by a CSP/COP/EF-CSP/EF-COP.
*
* First developed to solve game-related optimization problems, GHOST can be used for
* any kind of applications where solving combinatorial and optimization problems. In
* particular, it had been designed to be able to solve not-too-complex problem instances
* within some milliseconds, making it very suitable for highly reactive or embedded systems.
* Please visit https://github.com/richoux/GHOST for further information.
*
* Copyright (C) 2014-2026 Florian Richoux
*
* This file is part of GHOST.
* GHOST is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* GHOST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with GHOST. If not, see http://www.gnu.org/licenses/.
*/
#include "objective.hpp"
using ghost::Objective;
Objective::Objective( const std::vector<int>& variables_index, bool is_maximization, const std::string& name )
: _variables_index( variables_index ),
_is_optimization( true ),
_is_maximization( is_maximization ),
_name( name )
{ }
Objective::Objective( const std::vector<Variable>& variables, bool is_maximization, const std::string& name )
: _variables_index( std::vector<int>( variables.size() ) ),
_is_optimization( true ),
_is_maximization( is_maximization ),
_name( name )
{
std::transform( variables.begin(),
variables.end(),
_variables_index.begin(),
[&](const auto& v){ return v.get_id(); } );
}
double Objective::cost() const
{
double value = required_cost( _variables );
if( std::isnan( value ) )
throw nanException( _variables );
if( _is_maximization )
value = -value;
return value;
}
void Objective::conditional_update_data_structures( const std::vector<Variable*>& variables, int index, int new_value )
{ }
int Objective::expert_heuristic_value( const std::vector<Variable*>& variables,
int variable_index,
const std::vector<int>& possible_values,
randutils::mt19937_rng& rng ) const
{
double min_cost = std::numeric_limits<double>::max();
double simulated_cost;
auto var = variables[ variable_index ];
int backup = var->get_value();
std::vector<int> best_values;
for( auto v : possible_values )
{
var->set_value( v );
simulated_cost = required_cost( variables );
if( _is_maximization )
simulated_cost = -simulated_cost;
if( min_cost > simulated_cost )
{
min_cost = simulated_cost;
best_values.clear();
best_values.push_back( v );
}
else
if( min_cost == simulated_cost )
best_values.push_back( v );
}
var->set_value( backup );
if( !best_values.empty() )
return rng.pick( best_values );
else
if( !possible_values.empty() )
return rng.pick( possible_values );
else
return backup;
}
int Objective::expert_heuristic_value_permutation( const std::vector<Variable*>& variables,
int variable_index,
const std::vector<int>& bad_variables,
randutils::mt19937_rng& rng ) const
{
return rng.pick( bad_variables );
}
double Objective::expert_postprocess( const std::vector<Variable*>& variables,
double best_cost ) const
{
return best_cost;
}