PaGMO  1.1.5
race_pop.h
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #ifndef PAGMO_UTIL_RACE_POP_H
26 #define PAGMO_UTIL_RACE_POP_H
27 
28 #include <iostream>
29 #include <string>
30 #include <vector>
31 
32 #include "../config.h"
33 #include "../serialization.h"
34 #include "../problem/base.h"
35 #include "racing.h"
36 
37 namespace pagmo{ namespace util {
38 
40 
43 namespace racing{
44 
46 
60 class __PAGMO_VISIBLE race_pop
61 {
62 public:
63 
64  race_pop(const population &, unsigned int seed = 0);
65  race_pop(unsigned int seed = 0);
66 
70  MAX_DATA_COUNT
71  };
72 
73  // Main method containing all the juice
74  std::pair<std::vector<population::size_type>, unsigned int> run(
75  const population::size_type n_final,
76  const unsigned int min_trials,
77  const unsigned int max_count,
78  double delta,
79  const std::vector<population::size_type> &,
80  termination_condition term_cond,
81  const bool race_best,
82  const bool screen_output
83  );
84 
85  population::size_type size() const;
86  void reset_cache();
87  void register_population(const population &);
88  void inherit_memory(const race_pop&);
89  std::vector<fitness_vector> get_mean_fitness(const std::vector<population::size_type> &active_set = std::vector<population::size_type>()) const;
90  void set_seed(unsigned int);
91 
92 private:
93  // Helper methods to validate input data
94  void _validate_active_set(const std::vector<population::size_type>& active_set, unsigned int pop_size) const;
95  void _validate_problem_stochastic(const problem::base& prob) const;
96  void _validate_racing_params(const population& pop, const population::size_type n_final, double delta) const;
97  void _validate_budget(const unsigned int min_trials, const unsigned int max_f_evals, const std::vector<population::size_type>& in_race) const;
98 
99  unsigned int prepare_population_friedman(const std::vector<population::size_type> &in_race, unsigned int count_iter);
100  unsigned int prepare_population_wilcoxon(const std::vector<population::size_type> &in_race, unsigned int count_iter);
101 
102  unsigned int compute_required_fevals(const std::vector<population::size_type>& in_race, unsigned int num_iter) const;
103 
104  // Atoms of the cache
105  struct eval_data
106  {
107  eval_data(const fitness_vector& _f = fitness_vector(), const constraint_vector& _c = constraint_vector()): f(_f), c(_c) { }
108  fitness_vector f;
110  };
111 
112  std::vector<population::size_type> construct_output_list(
113  const std::vector<racer_type>& racers,
114  const std::vector<population::size_type>& decided,
115  const std::vector<population::size_type>& in_race,
116  const std::vector<population::size_type>& discarded,
117  const population::size_type n_final,
118  const bool race_best);
119 
120  // Caching routines
121  void cache_insert_data(unsigned int, const fitness_vector &, const constraint_vector &);
122  void cache_delete_entry(unsigned int);
123  bool cache_data_exist(unsigned int, unsigned int) const;
124  const eval_data &cache_get_entry(unsigned int, unsigned int) const;
125  void cache_register_signatures(const population&);
126  void print_cache_stats(const std::vector<population::size_type> &) const;
127 
128  // Seeding control
129  void generate_seeds(unsigned int);
130  unsigned int get_current_seed(unsigned int);
131  unsigned int m_race_seed;
132 
133  // Data members
134  racing_population m_pop;
135  racing_population m_pop_wilcoxon;
136  bool m_pop_registered;
137  std::vector<unsigned int> m_seeds;
138  rng_uint32 m_seeder;
139  bool m_use_caching;
140  std::vector<std::vector<eval_data> > m_cache_data;
141  std::vector<eval_data> m_cache_averaged_data;
142  std::vector<decision_vector> m_cache_signatures;
143 };
144 
145 }}}
146 
147 #endif
Root PaGMO namespace.
This rng returns an unsigned integer in the [0,2**32-1] range.
Definition: rng.h:47
Base problem class.
Definition: problem/base.h:148
Population class.
Definition: population.h:70
Fixed number of function evaluations.
Definition: race_pop.h:69
termination_condition
Method to stop the race.
Definition: race_pop.h:68
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
container_type::size_type size_type
Population size type.
Definition: population.h:192
Racing mechanism for a population.
Definition: race_pop.h:60