PaGMO  1.1.5
ms.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/random/uniform_int.hpp>
26 #include <boost/random/uniform_real.hpp>
27 #include <string>
28 #include <vector>
29 
30 #include "../exceptions.h"
31 #include "../population.h"
32 #include "../problem/base.h"
33 #include "../types.h"
34 #include "base.h"
35 #include "ms.h"
36 
37 namespace pagmo { namespace algorithm {
38 
40 
47 ms::ms(const base &algorithm, int starts):base(),m_starts(starts)
48 {
49  m_algorithm = algorithm.clone();
50  if (starts < 0) {
51  pagmo_throw(value_error,"number of multistarts needs to be larger than zero");
52  }
53 }
54 
56 ms::ms(const ms &other):base(other),m_algorithm(other.m_algorithm->clone()),m_starts(other.m_starts) {}
57 
60 {
61  return base_ptr(new ms(*this));
62 }
63 
65 
71 void ms::evolve(population &pop) const
72 {
73  // Let's store some useful variables.
74  const population::size_type NP = pop.size();
75 
76  // Get out if there is nothing to do.
77  if (m_starts == 0 || NP == 0) {
78  return;
79  }
80 
81  // Local population used in the algorithm iterations.
82  population working_pop(pop);
83 
84  //ms main loop
85  for (int i=0; i< m_starts; ++i)
86  {
87  working_pop.reinit();
88  m_algorithm->evolve(working_pop);
89  if (working_pop.problem().compare_fc(working_pop.get_individual(working_pop.get_best_idx()).cur_f,working_pop.get_individual(working_pop.get_best_idx()).cur_c,
90  pop.get_individual(pop.get_worst_idx()).cur_f,pop.get_individual(pop.get_worst_idx()).cur_c
91  ) )
92  {
93  //update best population replacing its worst individual with the good one just produced.
94  pop.set_x(pop.get_worst_idx(),working_pop.get_individual(working_pop.get_best_idx()).cur_x);
95  pop.set_v(pop.get_worst_idx(),working_pop.get_individual(working_pop.get_best_idx()).cur_v);
96  }
97  if (m_screen_output)
98  {
99  std::cout << i << ". " << "\tCurrent iteration best: " << working_pop.get_individual(working_pop.get_best_idx()).cur_f << "\tOverall champion: " << pop.champion().f << std::endl;
100  }
101  }
102 }
103 
104 
106 std::string ms::get_name() const
107 {
108  return "Multi-start";
109 }
110 
112 
116 {
117  return m_algorithm->clone();
118 }
119 
121 
126 void ms::set_algorithm(const base &algo)
127 {
128  m_algorithm = algo.clone();
129 }
130 
132 
135 std::string ms::human_readable_extra() const
136 {
137  std::ostringstream s;
138  s << "algorithm: " << m_algorithm->get_name() << ' ';
139  s << "iter:" << m_starts << ' ';
140  return s.str();
141 }
142 
143 }} //namespaces
144 
145 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::algorithm::ms)
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
Root PaGMO namespace.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
Definition: population.cpp:277
base_ptr clone() const
Clone method.
Definition: ms.cpp:59
Base algorithm class.
Multistart.
Definition: ms.h:59
std::string get_name() const
Algorithm name.
Definition: ms.cpp:106
Population class.
Definition: population.h:70
virtual base_ptr clone() const =0
Clone method.
fitness_vector f
Fitness vector.
Definition: population.h:153
bool m_screen_output
Indicates to the derived class whether to print stuff on screen.
std::string human_readable_extra() const
Extra human readable algorithm info.
Definition: ms.cpp:135
void evolve(population &) const
Evolve implementation.
Definition: ms.cpp:71
ms(const base &=de(), int=1)
Constructor.
Definition: ms.cpp:47
base_ptr get_algorithm() const
Get a copy of the internal algorithm.
Definition: ms.cpp:115
container_type::size_type size_type
Population size type.
Definition: population.h:192
void reinit(const size_type &)
Re-initialise individual at position idx.
Definition: population.cpp:236
void set_algorithm(const base &)
Set algorithm.
Definition: ms.cpp:126
bool compare_fc(const fitness_vector &, const constraint_vector &, const fitness_vector &, const constraint_vector &) const
Simultaneous fitness-constraint comparison.