25 #include <boost/random/uniform_int.hpp>
26 #include <boost/random/uniform_real.hpp>
30 #include "../exceptions.h"
31 #include "../population.h"
32 #include "../problem/base.h"
37 namespace pagmo {
namespace algorithm {
50 mbh::mbh(
const base & local,
int stop,
double perturb):
base(),m_stop(stop),m_perturb(1,perturb)
52 m_local = local.
clone();
54 pagmo_throw(value_error,
"number of consecutive step allowed without any improvement needs to be positive");
56 if ((perturb < 0) || (perturb > 1)) {
57 pagmo_throw(value_error,
"perturb must be positive");
72 mbh::mbh(
const base & local,
int stop,
const std::vector<double> &perturb):
base(),m_stop(stop),m_perturb(perturb)
74 m_local = local.
clone();
76 pagmo_throw(value_error,
"number of consecutive step allowed without any improvement needs to be positive");
78 for (
size_t i=0;i<perturb.size();++i)
80 if ((perturb[i] < 0 ) || (perturb[i] > 1 )) {
81 pagmo_throw(value_error,
"perturb[.] must be in [0,1]");
84 if (perturb.size()==0) pagmo_throw(value_error,
"perturbation vector appears empty!!");
88 mbh::mbh(
const mbh &algo):
base(algo),m_local(algo.m_local->clone()),m_stop(algo.m_stop),m_perturb(algo.m_perturb)
115 if (m_perturb.size()==1)
121 if (m_perturb.size()!=D)
123 pagmo_throw(value_error,
"perturbation vector size does not match the problem size");
127 if (m_stop == 0 || NP == 0) {
147 for (decision_vector::size_type k=0; k < Dc; ++k)
150 width = m_perturb[k];
151 tmp_x[k] = boost::uniform_real<double>(std::max(dummy-width*(ub[k]-lb[k]),lb[k]),std::min(dummy+width*(ub[k]-lb[k]),ub[k]))(
m_drng);
153 tmp_v[k] = boost::uniform_real<double>(dummy-width*(ub[k]-lb[k]),dummy+width*(ub[k]-lb[k]))(
m_drng);
156 for (decision_vector::size_type k=Dc; k < D; ++k)
159 width = m_perturb[k];
160 tmp_x[k] = boost::uniform_int<int>(std::max(dummy-std::floor(width*(ub[k]-lb[k])),lb[k]),std::min(dummy+std::floor(width*(ub[k]-lb[k])),ub[k]))(
m_urng);
162 tmp_v[k] = boost::uniform_int<int>(std::max(dummy-std::floor(width*(ub[k]-lb[k])),lb[k]),std::min(dummy+std::floor(width*(ub[k]-lb[k])),ub[k]))(
m_urng);
164 pert_pop.push_back(tmp_x);
169 m_local->evolve(pert_pop); i++;
172 std::cout << i <<
". " <<
"\tLocal solution: " << pert_pop.champion().
f <<
"\tGlobal best: " << pop.champion().
f;
176 std::cout << std::endl;
180 if (pert_pop.problem().
compare_fc(pert_pop.champion().
f,pert_pop.champion().
c,pop.champion().
f,pop.champion().
c) )
184 std::cout <<
"New solution accepted. Constraints vector: " << pert_pop.champion().
c <<
'\n';
201 return "Generalized Monotonic Basin Hopping";
210 return m_local->clone();
221 m_local = algo.
clone();
230 std::ostringstream s;
231 s <<
"algorithm: " << m_local->get_name() <<
' ';
232 s <<
"stop:" << m_stop <<
' ';
233 s <<
"perturb:" << m_perturb <<
' ';
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::string human_readable_extra() const
Extra human readable algorithm info.
bool feasibility_x(const decision_vector &) const
Test feasibility of decision vector.
std::vector< double > decision_vector
Decision vector type.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
void evolve(population &) const
Evolve implementation.
base_ptr clone() const
Clone method.
std::string get_name() const
Algorithm name.
size_type get_dimension() const
Return global dimension.
decision_vector x
Decision vector.
virtual base_ptr clone() const =0
Clone method.
fitness_vector f
Fitness vector.
bool m_screen_output
Indicates to the derived class whether to print stuff on screen.
size_type get_i_dimension() const
Return integer dimension.
decision_vector cur_v
Current velocity vector.
const decision_vector & get_ub() const
Upper bounds getter.
constraint_vector c
Constraint vector.
base_ptr get_algorithm() const
Get a copy of the internal local algorithm.
container_type::size_type size_type
Population size type.
rng_uint32 m_urng
Random number generator for unsigned integer values.
Monotonic Basin Hopping (generalized)
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 set_algorithm(const base &)
Set algorithm.
mbh(const base &=cs(), int stop=5, double perturb=5e-2)
Constructor.
decision_vector best_x
Best decision vector so far.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.