27 #include <boost/random/uniform_int.hpp>
28 #include <boost/random/uniform_real.hpp>
29 #include <boost/random/variate_generator.hpp>
32 #include "../config.h"
33 #include "../serialization.h"
34 #include "../population.h"
35 #include "../problem/base_tsp.h"
36 #include "../algorithm/nn_tsp.h"
38 #include "inverover.h"
40 namespace pagmo {
namespace algorithm {
50 :
base(),m_gen(gen),m_ri(ri),m_ini_type(ini_type)
53 pagmo_throw(value_error,
"number of generations must be nonnegative");
55 if (ri > 1 || ri < 0) {
56 pagmo_throw(value_error,
"random invert probability must be in the [0,1] range");
82 catch (
const std::bad_cast& e) {
83 pagmo_throw(value_error,
"Problem not of type pagmo::problem::base_tsp");
91 boost::uniform_real<double> uniform(0.0, 1.0);
92 boost::variate_generator<boost::lagged_fibonacci607 &, boost::uniform_real<double> > unif_01(
m_drng, uniform);
93 boost::uniform_int<int> NPless1(0, NP - 2);
94 boost::variate_generator<boost::mt19937 &, boost::uniform_int<int> > unif_NPless1(
m_urng, NPless1);
95 boost::uniform_int<int> Nv_(0, Nv - 1);
96 boost::variate_generator<boost::mt19937 &, boost::uniform_int<int> > unif_Nv(
m_urng, Nv_);
97 boost::uniform_int<int> Nvless1(0, Nv - 2);
98 boost::variate_generator<boost::mt19937 &, boost::uniform_int<int> > unif_Nvless1(
m_urng, Nvless1);
105 std::vector<int> not_feasible;
106 for (
size_t i = 0; i < NP; i++) {
121 not_feasible.push_back(i);
127 switch (m_ini_type) {
131 for (
size_t ii = 0; ii < not_feasible.size(); ii++) {
132 i = not_feasible[ii];
133 for (
size_t j = 0; j < Nv; j++) {
139 for (
size_t j = 1; j < Nv-1; j++) {
140 boost::uniform_int<int> dist_(j, Nv - 1);
141 boost::variate_generator<boost::mt19937 &, boost::uniform_int<int> > dist(
m_urng,dist_);
143 for (
size_t ii = 0; ii < not_feasible.size(); ii++) {
144 i = not_feasible[ii];
147 my_pop[i][j] = my_pop[i][rnd_idx];
148 my_pop[i][rnd_idx] = tmp;
157 std::vector<int> starting_notes(std::max(Nv,not_feasible.size()));
158 for (
size_t j = 0; j < starting_notes.size(); j++) {
159 starting_notes[j] = j;
162 for (
size_t ii = 0; ii < not_feasible.size(); ii++) {
163 i = not_feasible[ii];
165 std::cout << starting_notes[i] <<
' ';
170 my_pop[i] = prob->
full2cities(one_ind_pop.get_individual(0).cur_x);
176 my_pop[i] = one_ind_pop.get_individual(0).cur_x;
179 std::cout << i <<
' ' << one_ind_pop.get_individual(0).cur_f << std::endl;
184 pagmo_throw(value_error,
"Invalid initialization type");
188 for(
size_t i=0; i < NP; i++){
197 fitness[i] = prob->
objfun(my_pop[i]);
205 size_t rnd_num, i2, pos1_c1, pos1_c2, pos2_c1, pos2_c2;
209 for(
int iter = 0; iter < m_gen; iter++) {
210 for(
size_t i1 = 0; i1 < NP; i1++) {
211 tmp_tour = my_pop[i1];
216 if(unif_01() < m_ri) {
217 rnd_num = unif_Nvless1();
218 pos1_c2 = (rnd_num == pos1_c1? Nv-1:rnd_num);
221 i2 = (i2 == i1? NP-1:i2);
222 pos2_c1 = std::find(my_pop[i2].begin(),my_pop[i2].end(),tmp_tour[pos1_c1])-my_pop[i2].begin();
223 pos2_c2 = (pos2_c1 == Nv-1? 0:pos2_c1+1);
224 pos1_c2 = std::find(tmp_tour.begin(),tmp_tour.end(),my_pop[i2][pos2_c2])-tmp_tour.begin();
229 if(pos1_c1<pos1_c2) {
230 for(
size_t l=0; l < (double (pos1_c2-pos1_c1-1)/2); l++) {
231 std::swap(tmp_tour[pos1_c1+1+l],tmp_tour[pos1_c2-l]);
236 for(
size_t l=0; l < (double (pos1_c1-pos1_c2-1)/2); l++) {
237 std::swap(tmp_tour[pos1_c2+l],tmp_tour[pos1_c1-l-1]);
239 pos1_c1 = (pos1_c2 == 0? Nv-1:pos1_c2-1);
253 fitness_tmp = prob->
objfun(tmp_tour);
257 my_pop[i1] = tmp_tour;
258 fitness[i1][0] = fitness_tmp[0];
265 for (
size_t ii = 0; ii < NP; ii++) {
274 pop.set_x(ii,my_pop[ii]);
284 return "InverOver Algorithm";
293 std::ostringstream s;
294 s <<
"generations: " << m_gen <<
" ";
295 s <<
"mutation probability: " << m_ri <<
" ";
296 std::string ini_str = (m_ini_type==0) ? (
"Random") : (
"Nearest Neighbour");
297 s <<
"initialization method: " << ini_str;
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
bool feasibility_x(const decision_vector &) const
Test feasibility of decision vector.
base_ptr clone() const
Clone method.
std::vector< double > decision_vector
Decision vector type.
inverover(int gen=10000, double ri=0.05, initialization_type ini_type=random)
Constructor.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
std::string get_name() const
Algorithm name.
pagmo::decision_vector full2cities(const pagmo::decision_vector &) const
From FULL to CITIES encoding.
bool compare_fitness(const fitness_vector &, const fitness_vector &) const
Compare fitness vectors.
As a vector of doubles in [0,1].
decision_vector::size_type get_n_cities() const
Getter for m_n_cities.
fitness_vector objfun(const decision_vector &) const
Return fitness of pagmo::decision_vector.
pagmo::decision_vector cities2full(const pagmo::decision_vector &) const
From CITIES to FULL encoding.
Nearest Neighbor Algorithm (NN)
Inver-Over Algorithm (IO)
std::vector< double > fitness_vector
Fitness vector type.
void evolve(population &) const
Evolve implementation.
container_type::size_type size_type
Population size type.
std::string human_readable_extra() const
Extra human readable algorithm info.
decision_vector cur_x
Current decision vector.
rng_uint32 m_urng
Random number generator for unsigned integer values.
As a matrix with ones and zeros.
Base TSP (Travelling Salesman Problem).
rng_double m_drng
Random number generator for double-precision floating point values.
As a sequence of cities ids.
pagmo::decision_vector cities2randomkeys(const pagmo::decision_vector &, const pagmo::decision_vector &) const
From CITIES to RANDOMKEYS encoding.
pagmo::decision_vector randomkeys2cities(const pagmo::decision_vector &) const
From RANDOMKEYS to CITIES encoding.
void evolve(population &) const
Evolve implementation.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
encoding_type get_encoding() const
Getter for m_encoding.