27 #include <boost/random/uniform_int.hpp>
28 #include <boost/random/uniform_real.hpp>
31 #include "../exceptions.h"
32 #include "../population.h"
33 #include "../problem/base.h"
39 namespace pagmo {
namespace algorithm {
51 firefly::firefly(
int gen,
double alpha,
double beta,
double gamma):
base(),m_iter(gen), m_alpha(alpha), m_beta(beta), m_gamma(gamma) {
53 pagmo_throw(value_error,
"number of iterations must be nonnegative");
55 if (alpha < 0 || alpha > 1) {
56 pagmo_throw(value_error,
"alpha should be in [0,1]");
58 if (beta < 0 || beta > 1) {
59 pagmo_throw(value_error,
"beta should be in [0,1]");
61 if (gamma < 0 || gamma > 1) {
62 pagmo_throw(value_error,
"gamma should be in [0,1] interval");
88 pagmo_throw(value_error,
"There is no continuous part in the problem decision vector for Firefly to optimise");
92 pagmo_throw(value_error,
"The problem is not single objective and Firefly is not suitable to solve it");
95 if ( prob_c_dimension != 0 ) {
96 pagmo_throw(value_error,
"The problem is not box constrained and Firefly is not suitable to solve it");
100 pagmo_throw(value_error,
"for Firefly at least 2 individuals in the population are needed");
110 std::vector<decision_vector> X(NP,dummy);
111 std::vector<decision_vector> X0(NP,dummy);
112 std::vector<fitness_vector> fit(NP);
121 double gamma_nominal_distance = 16.0;
122 double newgamma = gamma_nominal_distance * m_gamma;
125 for (
int j = 0; j < m_iter; ++j) {
128 double r_max_sqrd = 0;
131 double r_temp_sqrd = 0;
133 r_temp_sqrd += (X[ii][k] - X[jj][k])*(X[ii][k]-X[jj][k]);
135 if (r_temp_sqrd > r_max_sqrd) {
136 r_max_sqrd = r_temp_sqrd;
154 r_sqrd += (X[ii][k] - X[jj][k]) * (X[ii][k] - X[jj][k]) ;
157 b = m_beta * exp( -1 * newgamma * sqrt(r_sqrd/r_max_sqrd));
161 X[ii][k] = (1-b) * X[ii][k] + b * X[jj][k];
170 X[ii][k] += boost::uniform_real<double>(-m_alpha, m_alpha)(
m_drng) * (ub[k] - lb[k]);
173 if (X[ii][k] < lb[k]) {
176 else if (X[ii][k] > ub[k]) {
182 prob.
objfun(test_fit, X[ii]);
184 pop.set_x(ii, X[ii]);
195 std::transform(X[i].begin(), X[i].end(), X0[i].begin(), dummy.begin(), std::minus<double>());
204 return "Firefly optimization";
214 std::ostringstream s;
215 s <<
"iter:" << m_iter <<
' ';
216 s <<
"alpha:" << m_alpha <<
' ';
217 s <<
"beta:" << m_beta <<
' ';
218 s <<
"gamma:" << m_gamma <<
' ';
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::vector< double > decision_vector
Decision vector type.
std::string get_name() const
Algorithm name.
fitness_vector cur_f
Current fitness vector.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
std::string human_readable_extra() const
Extra human readable algorithm info.
void evolve(population &) const
Evolve implementation.
size_type get_dimension() const
Return global dimension.
bool compare_fitness(const fitness_vector &, const fitness_vector &) const
Compare fitness vectors.
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.
c_size_type get_c_dimension() const
Return global constraints dimension.
const decision_vector & get_ub() const
Upper bounds getter.
container_type::size_type size_type
Population size type.
decision_vector cur_x
Current decision vector.
f_size_type get_f_dimension() const
Return fitness dimension.
firefly(int gen=1, double alpha=0.01, double beta=1.0, double gamma=0.8)
Constructor.
base_ptr clone() const
Clone method.
const decision_vector & get_lb() const
Lower bounds getter.
rng_double m_drng
Random number generator for double-precision floating point values.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.