28 #include "../population.h"
30 namespace pagmo {
namespace problem {
41 (encoding==FULL ? n_cities*(n_cities-1): n_cities),
42 (encoding==RANDOMKEYS ? 0 : (encoding==FULL ? n_cities*(n_cities-1):n_cities)),
48 switch( m_encoding ) {
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 );
84 retval.at(j) = std::min(next_city,m_n_cities-1);
100 if (x.size() != m_n_cities)
102 pagmo_throw(value_error,
"input representation of a tsp solution (CITIES encoding) looks unfeasible [wrong length]");
106 for (std::vector<population::size_type>::size_type i=0; i<x.size()-1; ++i)
108 retval.at( x[i]*(m_n_cities-1) + x[i+1] - (x[i+1]>x[i]?1:0) ) = 1;
110 retval.at( x[x.size()-1]*(m_n_cities-1) + x[0] - (x[0]>x[x.size()-1]?1:0) ) = 1;
114 bool comparator (
const std::pair<double,int>& l,
const std::pair<double,int>& r)
115 {
return l.first < r.first; }
129 if (x.size() != m_n_cities)
131 pagmo_throw(value_error,
"input representation of a tsp solution (RANDOMKEYS encoding) looks unfeasible [wrong length]");
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];
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;
158 if (cities.size() != orig_random_keys.size())
160 pagmo_throw(value_error,
"the random keys original vector and the cities vector need to have the same length");
162 if (cities.size() != m_n_cities)
164 pagmo_throw(value_error,
"input representation of a tsp solution (CITIES encoding) looks unfeasible [wrong length]");
166 if ( (*std::max_element(cities.begin(),cities.end()) >= m_n_cities) || (*std::min_element(cities.begin(),cities.end()) < 0) )
168 pagmo_throw(value_error,
"city indexes outside the allowed bounds");
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];
std::vector< double > decision_vector
Decision vector type.
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.
As a vector of doubles in [0,1].
decision_vector::size_type get_n_cities() const
Getter for m_n_cities.
pagmo::decision_vector cities2full(const pagmo::decision_vector &) const
From CITIES to FULL encoding.
encoding_type
Mechanism used to encode the sequence of vertices to be visited.
void set_ub(const decision_vector &)
Set upper bounds from pagmo::decision_vector.
container_type::size_type size_type
Population size type.
As a matrix with ones and zeros.
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.
base_tsp(int n_cities, int nc, int nic, encoding_type=CITIES)
Constructor from dimensins and encoding.
encoding_type get_encoding() const
Getter for m_encoding.