27 #include <boost/random/uniform_int.hpp>
28 #include <boost/random/uniform_real.hpp>
30 #include "bee_colony.h"
31 #include "../exceptions.h"
32 #include "../population.h"
33 #include "../problem/base.h"
39 namespace pagmo {
namespace algorithm {
51 pagmo_throw(value_error,
"number of generations must be nonnegative");
55 pagmo_throw(value_error,
"limit value must be nonnegative");
83 pagmo_throw(value_error,
"There is no continuous part in the problem decision vector for ABC to optimise");
87 pagmo_throw(value_error,
"The problem is not single objective and ABC is not suitable to solve it");
90 if ( prob_c_dimension != 0 ) {
91 pagmo_throw(value_error,
"The problem is not box constrained and ABC is not suitable to solve it");
95 pagmo_throw(value_error,
"for ABC at least 2 individuals in the population are needed");
106 std::vector<decision_vector > X(NP,dummy);
107 std::vector<fitness_vector> fit(NP);
111 std::vector<int> trial(NP,0);
113 std::vector<double> probability(NP);
117 decision_vector::size_type param2change = 0;
119 std::vector<double> selectionfitness(NP), cumsum(NP), cumsumTemp(NP);
120 std::vector <population::size_type> selection(NP);
132 for (
int j = 0; j < m_iter; ++j) {
136 param2change = boost::uniform_int<decision_vector::size_type>(0,Dc-1)(
m_urng);
140 neighbour = boost::uniform_int<population::size_type>(0,NP-1)(
m_urng);
142 while(neighbour == ii);
146 temp_solution[i] = X[ii][i];
150 temp_solution[param2change] = X[ii][param2change] + boost::uniform_real<double>(-1,1)(
m_drng) * (X[ii][param2change] - X[neighbour][param2change]);
153 if (temp_solution[param2change]<lb[param2change]) {
154 temp_solution[param2change] = lb[param2change];
156 if (temp_solution[param2change]>ub[param2change]) {
157 temp_solution[param2change] = ub[param2change];
162 prob.
objfun(fnew,temp_solution);
165 X[ii][param2change] = temp_solution[param2change];
167 prob.
objfun(fit[ii], X[ii]);
183 selectionfitness[i] = fabs(worstfit[0] - fit[i][0]) + 1.;
187 cumsumTemp[0] = selectionfitness[0];
189 cumsumTemp[i] = cumsumTemp[i - 1] + selectionfitness[i];
192 cumsum[i] = cumsumTemp[i]/cumsumTemp[NP-1];
209 param2change = boost::uniform_int<decision_vector::size_type>(0,Dc-1)(
m_urng);
213 neighbour = boost::uniform_int<population::size_type>(0,NP-1)(
m_urng);
215 while(neighbour == ii);
219 temp_solution[i] = X[ii][i];
223 temp_solution[param2change] = X[ii][param2change] + boost::uniform_real<double>(-1,1)(
m_drng) * (X[ii][param2change] - X[neighbour][param2change]);
226 if (temp_solution[param2change]<lb[param2change]) {
227 temp_solution[param2change] = lb[param2change];
229 if (temp_solution[param2change]>ub[param2change]) {
230 temp_solution[param2change] = ub[param2change];
235 prob.
objfun(fnew,temp_solution);
238 X[ii][param2change] = temp_solution[param2change];
240 prob.
objfun(fit[ii], X[ii]);
249 int maxtrialindex = 0;
252 if (trial[ii] > trial[maxtrialindex]) {
256 if(trial[maxtrialindex] >= m_limit)
260 X[maxtrialindex][jj] = boost::uniform_real<double>(lb[jj],ub[jj])(
m_drng);
262 trial[maxtrialindex] = 0;
263 pop.set_x(maxtrialindex,X[maxtrialindex]);
273 return "Artificial Bee Colony optimization";
283 std::ostringstream s;
284 s <<
"gen:" << m_iter <<
' ';
285 s <<
"limit:" << m_limit <<
' ';
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
bee_colony(int gen=1, int limit=20)
Constructor.
std::string get_name() const
Algorithm name.
std::vector< double > decision_vector
Decision vector type.
fitness_vector cur_f
Current fitness vector.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
base_ptr clone() const
Clone method.
size_type get_dimension() const
Return global dimension.
bool compare_fitness(const fitness_vector &, const fitness_vector &) const
Compare fitness vectors.
void evolve(population &) const
Evolve implementation.
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.
The Artificial Bee Colony Solver (ABC)
decision_vector cur_x
Current decision vector.
rng_uint32 m_urng
Random number generator for unsigned integer values.
f_size_type get_f_dimension() const
Return fitness dimension.
const decision_vector & get_lb() const
Lower bounds getter.
rng_double m_drng
Random number generator for double-precision floating point values.
std::string human_readable_extra() const
Extra human readable algorithm info.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.