3 #include "../problem/base_stochastic.h"
7 namespace pagmo {
namespace util {
namespace racing {
9 namespace metrics_algos {
15 for(std::vector<problem::base_ptr>::size_type i = 0; i < probs.size(); i++){
16 max_c_dim = std::max(max_c_dim, probs[i]->get_c_dimension());
25 for(std::vector<problem::base_ptr>::size_type i = 0; i < probs.size(); i++){
26 max_ic_dim = std::max(max_ic_dim, probs[i]->get_ic_dimension());
54 standard(
const std::vector<problem::base_ptr> &probs = std::vector<problem::base_ptr>(),
const std::vector<algorithm::base_ptr> &algos = std::vector<algorithm::base_ptr>(),
unsigned int seed = 0,
unsigned int pop_size = 100);
65 friend class boost::serialization::access;
66 template <
class Archive>
67 void serialize(Archive &ar,
const unsigned int)
69 ar & boost::serialization::base_object<base_stochastic>(*this);
75 void setup(
const std::vector<problem::base_ptr> &probs,
const std::vector<algorithm::base_ptr> &algos);
78 void evaluate_algorithm(
unsigned int)
const;
80 std::vector<algorithm::base_ptr> m_algos;
81 std::vector<problem::base_ptr> m_probs;
82 unsigned int m_pop_size;
86 mutable std::vector<bool> m_is_first_evaluation;
87 mutable std::vector<unsigned int> m_database_seed;
88 mutable std::vector<fitness_vector> m_database_f;
89 mutable std::vector<constraint_vector> m_database_c;
109 standard::standard(
const std::vector<problem::base_ptr> &probs,
const std::vector<algorithm::base_ptr> &algos,
unsigned int seed,
unsigned int pop_size): base_stochastic(1, 1, probs.front()->get_f_dimension(), get_max_c_dimension(probs), get_max_ic_dimension(probs), 0, seed), m_pop_size(pop_size), m_is_first_evaluation(algos.size(), true), m_database_seed(algos.size()), m_database_f(algos.size()), m_database_c(algos.size())
120 void standard::setup(
const std::vector<problem::base_ptr> &probs,
const std::vector<algorithm::base_ptr> &algos)
123 if(algos.size() == 0){
124 pagmo_throw(value_error,
"Empty algorithm set in race_algo");
126 for(
unsigned int i = 0; i < probs.size(); i++){
128 pagmo_throw(value_error,
"Racing of multi-objective algorithms is not supported yet");
133 for(
unsigned int i = 0; i < algos.size(); i++){
134 m_algos.push_back(algos[i]->
clone());
136 for(
unsigned int i = 0; i < probs.size(); i++){
137 m_probs.push_back(probs[i]->
clone());
146 base_stochastic(1, 1, standard_copy.get_f_dimension(),
147 standard_copy.get_c_dimension(),
148 standard_copy.get_ic_dimension(), 0, standard_copy.m_seed),
149 m_algos(standard_copy.m_algos),
150 m_probs(standard_copy.m_probs),
151 m_pop_size(standard_copy.m_pop_size),
152 m_is_first_evaluation(standard_copy.m_is_first_evaluation),
153 m_database_seed(standard_copy.m_database_seed),
154 m_database_f(standard_copy.m_database_f),
155 m_database_c(standard_copy.m_database_c)
173 evaluate_algorithm(x[0]);
174 f = m_database_f[x[0]];
184 evaluate_algorithm(x[0]);
185 c = m_database_c[x[0]];
218 void standard::evaluate_algorithm(
unsigned int algo_idx)
const
220 if(algo_idx >= m_algos.size()){
221 pagmo_throw(value_error,
"Out of bound algorithm index");
225 if(!m_is_first_evaluation[algo_idx] && m_database_seed[algo_idx] ==
m_seed){
230 for(
unsigned int i = 0; i < m_algos.size(); i++){
231 m_algos[i]->reset_rngs(
m_seed);
236 unsigned int prob_idx;
237 if(m_probs.size() == 1){
241 prob_idx = (
unsigned int)(
m_drng() * 100000) % m_probs.size();
246 population pop(*m_probs[prob_idx], m_pop_size,
m_seed);
247 m_algos[algo_idx]->evolve(pop);
250 m_is_first_evaluation[algo_idx] =
false;
251 m_database_seed[algo_idx] =
m_seed;
252 m_database_f[algo_idx] = pop.champion().f;
253 m_database_c[algo_idx] = zero_pad_constraint(pop.champion().c, m_probs[prob_idx]->get_ic_dimension());
271 for(
unsigned int i = 0; i < algos.size(); i++){
272 m_algos.push_back(algos[i]->clone());
274 m_probs.push_back(prob.
clone());
286 race_algo::race_algo(
const std::vector<algorithm::base_ptr> &algos,
const std::vector<problem::base_ptr> &probs,
unsigned int pop_size,
unsigned int seed): m_pop_size(pop_size), m_seed(seed)
288 for(
unsigned int i = 0; i < algos.size(); i++){
289 m_algos.push_back(algos[i]->clone());
291 for(
unsigned int i = 0; i < probs.size(); i++){
292 m_probs.push_back(probs[i]->clone());
311 const unsigned int n_final,
312 const unsigned int min_trials,
313 const unsigned int max_count,
315 const std::vector<unsigned int> &active_set,
316 const bool race_best,
317 const bool screen_output)
335 for(
unsigned int i = 0; i < m_algos.size(); i++){
338 algos_pop.push_back(algo_idx);
342 std::vector<population::size_type> pop_race_active_set(active_set.size());
343 for(
unsigned int i = 0; i < active_set.size(); i++){
344 pop_race_active_set[i] = active_set[i];
348 std::pair<std::vector<population::size_type>,
unsigned int> res =
349 algos_pop.race(n_final, min_trials, max_count, delta,
350 pop_race_active_set, race_best, screen_output);
353 std::pair<std::vector<unsigned int>,
unsigned int> res_algo_race;
354 res_algo_race.first.clear();
355 for(
unsigned int i = 0; i < res.first.size(); i++){
356 res_algo_race.first.push_back(res.first[i]);
358 res_algo_race.second = res.second;
360 return res_algo_race;
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
std::vector< double > decision_vector
Decision vector type.
c_size_type get_ic_dimension() const
Return inequality constraints dimension.
race_algo(const std::vector< algorithm::base_ptr > &algos=std::vector< algorithm::base_ptr >(), const problem::base &prob=problem::ackley(), unsigned int pop_size=100, unsigned int seed=0)
Constructor of the racing mechanism for algorithms.
rng_double m_drng
Random number generator for double-precision floating point values.
unsigned int m_seed
Seed of the random number generator.
problem::base_ptr clone() const
Clone method.
virtual base_ptr clone() const =0
Clone method.
standard(const std::vector< problem::base_ptr > &probs=std::vector< problem::base_ptr >(), const std::vector< algorithm::base_ptr > &algos=std::vector< algorithm::base_ptr >(), unsigned int seed=0, unsigned int pop_size=100)
Constructor of the performance metrics of algorithms on a single problem.
std::vector< double > fitness_vector
Fitness vector type.
void objfun_impl(fitness_vector &, const decision_vector &) const
The performance of an algorithm encoded in the fitness function.
c_size_type get_c_dimension() const
Return global constraints dimension.
std::pair< std::vector< unsigned int >, unsigned int > run(const unsigned int n_final, const unsigned int min_trials, const unsigned int max_count, double delta, const std::vector< unsigned int > &, const bool race_best, const bool screen_output)
Juice of racing mechanisms for algorithms.
std::vector< double > constraint_vector
Constraint vector type.
const decision_vector & get_ub() const
Upper bounds getter.
Base Stochastic Optimization Problem.
void compute_constraints_impl(constraint_vector &, const decision_vector &) const
The performance of an algorithm in terms of a constraint vector.
f_size_type get_f_dimension() const
Return fitness dimension.
constraint_vector::size_type c_size_type
Constraints' size type: the same as pagmo::constraint_vector's size type.
const decision_vector & get_lb() const
Lower bounds getter.
void set_bounds(const decision_vector &, const decision_vector &)
Bounds setter from pagmo::decision_vector.