PaGMO  1.1.5
monte_carlo.cpp
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 #include <boost/numeric/conversion/cast.hpp>
26 #include <boost/random/uniform_int.hpp>
27 #include <boost/random/uniform_real.hpp>
28 #include <cstddef>
29 
30 #include "../population.h"
31 #include "../types.h"
32 #include "base.h"
33 #include "monte_carlo.h"
34 
35 namespace pagmo { namespace algorithm {
36 
38 monte_carlo::monte_carlo(int n):base(),m_max_eval(boost::numeric_cast<std::size_t>(n)) {}
39 
42 {
43  return base_ptr(new monte_carlo(*this));
44 }
45 
48 {
49  // Let's store some useful variables.
50  const problem::base &prob = pop.problem();
51  const problem::base::size_type prob_dimension = prob.get_dimension(), prob_i_dimension = prob.get_i_dimension();
52  const decision_vector &lb = prob.get_lb(), &ub = prob.get_ub();
53  const population::size_type pop_size = pop.size();
54  // Get out if there is nothing to do.
55  if (pop_size == 0 || m_max_eval == 0) {
56  return;
57  }
58  // Initialise temporary decision vector, fitness vector and decision vector.
59  decision_vector tmp_x(prob_dimension);
60  fitness_vector tmp_f(prob.get_f_dimension());
61  constraint_vector tmp_c(prob.get_c_dimension());
62  // Main loop.
63  for (std::size_t i = 0; i < m_max_eval; ++i) {
64  // Generate a random decision vector.
65  for (problem::base::size_type j = 0; j < prob_dimension - prob_i_dimension; ++j) {
66  tmp_x[j] = boost::uniform_real<double>(lb[j],ub[j])(m_drng);
67  }
68  for (problem::base::size_type j = prob_dimension - prob_i_dimension; j < prob_dimension; ++j) {
69  tmp_x[j] = boost::uniform_int<int>(lb[j],ub[j])(m_urng);
70  }
71  // Compute fitness and constraints.
72  prob.objfun(tmp_f,tmp_x);
73  prob.compute_constraints(tmp_c,tmp_x);
74  // Locate the worst individual.
75  const population::size_type worst_idx = pop.get_worst_idx();
76  if (prob.compare_fc(tmp_f,tmp_c,pop.get_individual(worst_idx).cur_f,pop.get_individual(worst_idx).cur_c)) {
77  pop.set_x(worst_idx,tmp_x);
78  }
79  }
80 }
81 
83 std::string monte_carlo::get_name() const
84 {
85  return "Monte Carlo";
86 }
87 
89 
92 std::string monte_carlo::human_readable_extra() const
93 {
94  std::ostringstream s;
95  s << "max_eval:" << m_max_eval;
96  return s.str();
97 }
98 
99 }}
100 
101 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::algorithm::monte_carlo)
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
Root PaGMO namespace.
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
base_ptr clone() const
Clone method.
Definition: monte_carlo.cpp:41
fitness_vector cur_f
Current fitness vector.
Definition: population.h:89
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
Definition: population.cpp:277
Base algorithm class.
Monte Carlo algorithm.
Definition: monte_carlo.h:44
constraint_vector cur_c
Current constraint vector.
Definition: population.h:87
STL namespace.
Base problem class.
Definition: problem/base.h:148
Population class.
Definition: population.h:70
size_type get_dimension() const
Return global dimension.
fitness_vector objfun(const decision_vector &) const
Return fitness of pagmo::decision_vector.
size_type get_i_dimension() const
Return integer dimension.
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
constraint_vector compute_constraints(const decision_vector &) const
Compute constraints and return constraint vector.
c_size_type get_c_dimension() const
Return global constraints dimension.
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
const decision_vector & get_ub() const
Upper bounds getter.
monte_carlo(int=1)
Constructor from number of iterations.
Definition: monte_carlo.cpp:38
container_type::size_type size_type
Population size type.
Definition: population.h:192
std::string get_name() const
Algorithm name.
Definition: monte_carlo.cpp:83
rng_uint32 m_urng
Random number generator for unsigned integer values.
f_size_type get_f_dimension() const
Return fitness dimension.
bool compare_fc(const fitness_vector &, const constraint_vector &, const fitness_vector &, const constraint_vector &) const
Simultaneous fitness-constraint comparison.
const decision_vector & get_lb() const
Lower bounds getter.
rng_double m_drng
Random number generator for double-precision floating point values.
void evolve(population &) const
Evolve method.
Definition: monte_carlo.cpp:47
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
Definition: problem/base.h:160