25 #include "tsp_vrplc.h"
26 #include "../population.h"
28 namespace pagmo {
namespace problem {
37 std::vector<double> dumb(3,0);
38 m_weights = std::vector<std::vector<double> > (3,dumb);
56 compute_dimensions(weights.size(), encoding)[0],
57 compute_dimensions(weights.size(), encoding)[1],
59 ), m_weights(weights), m_capacity(capacity)
63 pagmo_throw(value_error,
"Maximum vehicle capacity needs to be strictly positive");
65 check_weights(m_weights);
83 void tsp_vrplc::check_weights(
const std::vector<std::vector<double> > &matrix)
const
85 decision_vector::size_type n_cols = matrix.size();
87 for (decision_vector::size_type i = 0; i < n_cols; ++i) {
88 decision_vector::size_type n_rows = matrix.at(i).size();
91 pagmo_throw(value_error,
"adjacency matrix is not square");
93 for (
size_t j = 0; j < n_rows; ++j) {
94 if (i == j && matrix.at(i).at(j) != 0)
95 pagmo_throw(value_error,
"main diagonal elements must all be zeros.");
96 if (i != j && !matrix.at(i).at(j))
97 pagmo_throw(value_error,
"adjacency matrix contains zero values.");
98 if (i != j && (!matrix.at(i).at(j)) == matrix.at(i).at(j))
99 pagmo_throw(value_error,
"adjacency matrix contains NaN values.");
104 boost::array<int, 2> tsp_vrplc::compute_dimensions(decision_vector::size_type n_cities,
base_tsp::encoding_type encoding)
106 boost::array<int,2> retval;
109 retval[0] = n_cities*(n_cities-1)+2;
110 retval[1] = (n_cities-1)*(n_cities-2);
147 for (decision_vector::size_type i=0; i<n_cities-1; ++i) {
148 stl += m_weights[tour[i]][tour[i+1]];
156 f[0] += (m_weights[tour[i]][tour[i+1]])/(n_cities*m_capacity);
171 std::vector<std::vector<double> > tours;
174 std::vector<double> cur_tour;
175 for (decision_vector::size_type i=0; i<n_cities-1; ++i)
177 cur_tour.push_back(x[i]);
178 stl += m_weights[x[i]][x[i+1]];
182 tours.push_back(cur_tour);
183 cur_tour.erase(cur_tour.begin(),cur_tour.end());
186 cur_tour.push_back(x[n_cities-1]);
187 tours.push_back(cur_tour);
192 size_t tsp_vrplc::compute_idx(
const size_t i,
const size_t j,
const size_t n)
const
194 pagmo_assert( i!=j && i<n && j<n );
195 return i*(n-1) + j - (j>i? 1:0);
207 for (
size_t i = 0; i < n_cities; i++) {
210 for (
size_t j = 0; j < n_cities; j++)
213 decision_vector::size_type rows = compute_idx(i, j, n_cities);
214 decision_vector::size_type cols = compute_idx(j, i, n_cities);
216 c[i+n_cities] += x[cols];
219 c[i+n_cities] = c[i+n_cities]-1;
225 size_t next_city = 0,current_city = 0;
226 std::vector<int> u(n_cities);
227 for (
size_t i = 0; i < n_cities; i++)
229 u[current_city] = i+1;
230 for (
size_t j = 0; j < n_cities; j++)
232 if (current_city==j)
continue;
233 if (x[compute_idx(current_city, j, n_cities)] == 1)
239 current_city = next_city;
242 for (
size_t i = 1; i < n_cities; i++) {
243 for (
size_t j = 1; j < n_cities; j++)
246 c[2*n_cities+count] = u[i]-u[j] + (n_cities+1) * x[compute_idx(i, j, n_cities)] - n_cities;
256 std::vector<population::size_type> range(n_cities);
257 for (std::vector<population::size_type>::size_type i=0; i<range.size(); ++i)
261 c[0] = !std::is_permutation(x.begin(),x.end(),range.begin());
271 return m_weights[i][j];
295 return "Vehicle Routing Problem with Limited Vehicle Capacity";
304 std::ostringstream oss;
305 oss <<
"\n\tNumber of cities: " <<
get_n_cities() <<
'\n';
306 oss <<
"\tEncoding: ";
309 oss <<
"FULL" <<
'\n';
312 oss <<
"RANDOMKEYS" <<
'\n';
315 oss <<
"CITIES" <<
'\n';
318 oss <<
"\tMaximum vehicle capacity: " << m_capacity << std::endl;
319 oss <<
"\tWeight Matrix: \n";
320 for (decision_vector::size_type i=0; i<
get_n_cities() ; ++i)
322 oss <<
"\t\t" << m_weights.at(i) <<
'\n';
325 oss <<
"\t\t..." <<
'\n';
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
std::vector< double > decision_vector
Decision vector type.
A static Travelling Salesman Problem.
std::vector< std::vector< double > > return_tours(const decision_vector &x) const
Returns the tours.
base_ptr clone() const
Copy constructor for polymorphic objects (deep copy)
pagmo::decision_vector full2cities(const pagmo::decision_vector &) const
From FULL to CITIES encoding.
std::string get_name() const
Returns the problem name.
As a vector of doubles in [0,1].
decision_vector::size_type get_n_cities() const
Getter for m_n_cities.
const std::vector< std::vector< double > > & get_weights() const
Getter for m_weights.
encoding_type
Mechanism used to encode the sequence of vertices to be visited.
double distance(decision_vector::size_type, decision_vector::size_type) const
Definition of the distance function.
std::vector< double > fitness_vector
Fitness vector type.
std::vector< double > constraint_vector
Constraint vector type.
tsp_vrplc()
Default constructor.
std::string human_readable_extra() const
Extra human readable info for the problem.
const double & get_capacity() const
Getter for m_capacity.
As a matrix with ones and zeros.
Base TSP (Travelling Salesman Problem).
As a sequence of cities ids.
pagmo::decision_vector randomkeys2cities(const pagmo::decision_vector &) const
From RANDOMKEYS to CITIES encoding.
encoding_type get_encoding() const
Getter for m_encoding.