PaGMO  1.1.5
base_tsp.cpp
1 /*****************************************************************************
2  * Copyright (C) 2004-2014 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 <algorithm>
26 
27 #include "base_tsp.h"
28 #include "../population.h"
29 
30 namespace pagmo { namespace problem {
31 
33 
39  base_tsp::base_tsp(int n_cities, int nc, int nic, encoding_type encoding):
40  base(
41  (encoding==FULL ? n_cities*(n_cities-1): n_cities),
42  (encoding==RANDOMKEYS ? 0 : (encoding==FULL ? n_cities*(n_cities-1):n_cities)),
43  1, nc, nic, 0.0
44  ),
45  m_encoding(encoding),
46  m_n_cities(n_cities)
47  {
48  switch( m_encoding ) {
49  case FULL:
50  set_lb(0);
51  set_ub(1);
52  break;
53  case RANDOMKEYS:
54  set_lb(0);
55  set_ub(1);
56  break;
57  case CITIES:
58  set_lb(0);
59  set_ub(m_n_cities-1);
60  break;
61  }
62  }
63 
64 
66 
75  {
76  pagmo::decision_vector retval(m_n_cities,0);
77  pagmo::population::size_type next_city,cur_city = 0;
78  retval[0]=cur_city;
79  for(size_t j = 1; j < m_n_cities; j++){
80  pagmo::decision_vector::const_iterator iter = std::find(x.begin() + cur_city*(m_n_cities-1), x.begin() + (cur_city+1)*(m_n_cities-1),1);
81  next_city = iter - (x.begin() + cur_city*(m_n_cities-1));
82  next_city = next_city + ( (next_city >= cur_city) ? 1:0 );
83  cur_city=next_city;
84  retval.at(j) = std::min(next_city,m_n_cities-1); //the min is to prevent cases where the 1 is not found (unfeasible chromosomes) and thus the city _idx returned would be invalid
85  }
86  return retval;
87  }
88 
90 
99  {
100  if (x.size() != m_n_cities)
101  {
102  pagmo_throw(value_error,"input representation of a tsp solution (CITIES encoding) looks unfeasible [wrong length]");
103  }
104 
105  pagmo::decision_vector retval(m_n_cities*(m_n_cities-1),0);
106  for (std::vector<population::size_type>::size_type i=0; i<x.size()-1; ++i)
107  {
108  retval.at( x[i]*(m_n_cities-1) + x[i+1] - (x[i+1]>x[i]?1:0) ) = 1;
109  }
110  retval.at( x[x.size()-1]*(m_n_cities-1) + x[0] - (x[0]>x[x.size()-1]?1:0) ) = 1;
111  return retval;
112  }
113 
114  bool comparator ( const std::pair<double,int>& l, const std::pair<double,int>& r)
115  { return l.first < r.first; }
116 
118 
128  {
129  if (x.size() != m_n_cities)
130  {
131  pagmo_throw(value_error,"input representation of a tsp solution (RANDOMKEYS encoding) looks unfeasible [wrong length]");
132  }
133  pagmo::decision_vector retval(m_n_cities);
134  std::vector<std::pair<double,int> > pairs(m_n_cities);
135  for (pagmo::decision_vector::size_type i=0;i<m_n_cities;++i) {
136  pairs[i].first = x[i];
137  pairs[i].second = i;
138  }
139  std::sort(pairs.begin(),pairs.end(),comparator);
140  for (pagmo::decision_vector::size_type i=0;i<m_n_cities;++i) {
141  retval[i] = pairs[i].second;
142  }
143  return retval;
144  }
145 
147 
157  {
158  if (cities.size() != orig_random_keys.size())
159  {
160  pagmo_throw(value_error,"the random keys original vector and the cities vector need to have the same length");
161  }
162  if (cities.size() != m_n_cities)
163  {
164  pagmo_throw(value_error,"input representation of a tsp solution (CITIES encoding) looks unfeasible [wrong length]");
165  }
166  if ( (*std::max_element(cities.begin(),cities.end()) >= m_n_cities) || (*std::min_element(cities.begin(),cities.end()) < 0) )
167  {
168  pagmo_throw(value_error,"city indexes outside the allowed bounds");
169  }
170 
171  pagmo::decision_vector rk(orig_random_keys);
172  pagmo::decision_vector retval(m_n_cities);
173  std::sort(rk.begin(),rk.end());
174  for (pagmo::decision_vector::size_type i=0;i<m_n_cities;++i) {
175  retval[cities[i]] = rk[i];
176  }
177  return retval;
178  }
179 
181 
185  {
186  return m_encoding;
187  }
188 
190 
193  decision_vector::size_type base_tsp::get_n_cities() const
194  {
195  return m_n_cities;
196  }
197 
198 }} //namespaces
Root PaGMO namespace.
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
Base problem class.
Definition: problem/base.h:148
void set_lb(const decision_vector &)
Set lower bounds from pagmo::decision_vector.
pagmo::decision_vector full2cities(const pagmo::decision_vector &) const
From FULL to CITIES encoding.
Definition: base_tsp.cpp:74
As a vector of doubles in [0,1].
Definition: base_tsp.h:69
decision_vector::size_type get_n_cities() const
Getter for m_n_cities.
Definition: base_tsp.cpp:193
pagmo::decision_vector cities2full(const pagmo::decision_vector &) const
From CITIES to FULL encoding.
Definition: base_tsp.cpp:98
encoding_type
Mechanism used to encode the sequence of vertices to be visited.
Definition: base_tsp.h:68
void set_ub(const decision_vector &)
Set upper bounds from pagmo::decision_vector.
container_type::size_type size_type
Population size type.
Definition: population.h:192
As a matrix with ones and zeros.
Definition: base_tsp.h:70
As a sequence of cities ids.
Definition: base_tsp.h:71
pagmo::decision_vector cities2randomkeys(const pagmo::decision_vector &, const pagmo::decision_vector &) const
From CITIES to RANDOMKEYS encoding.
Definition: base_tsp.cpp:156
pagmo::decision_vector randomkeys2cities(const pagmo::decision_vector &) const
From RANDOMKEYS to CITIES encoding.
Definition: base_tsp.cpp:127
base_tsp(int n_cities, int nc, int nic, encoding_type=CITIES)
Constructor from dimensins and encoding.
Definition: base_tsp.cpp:39
encoding_type get_encoding() const
Getter for m_encoding.
Definition: base_tsp.cpp:184