vrpRouting  0.3
vrprouting::problem::Solution Class Reference

#include "solution.h"

Inheritance diagram for vrprouting::problem::Solution:
Collaboration diagram for vrprouting::problem::Solution:

Public Member Functions

 Solution ()=delete
 constructor More...
 
 Solution (const Solution &sol)=default
 copy constructor More...
 
template<typename P >
 Solution (P *p_problem)
 constructor More...
 
std::tuple< int, int, size_t, TInterval, TInterval, TIntervalcost () const
 Get all statistics in one cycle. More...
 
std::string cost_str () const
 
int cvTot () const
 Total number of capacity constraint violations of the solution. More...
 
TInterval duration () const
 Total duration of the solution. More...
 
const std::deque< Vehicle_pickDeliver > & fleet () const
 Get the current fleet solution. More...
 
std::vector< Solution_rtget_postgres_result () const
 get solution like postgres needs it More...
 
std::vector< Short_vehicleget_stops () const
 get solution like postgres needs it More...
 
bool is_feasible () const
 is the solution feasible? More...
 
Pgr_messagesmsg ()
 
double objective () const
 Get the value of the objective function. More...
 
bool operator< (const Solution &) const
 
Solutionoperator= (const Solution &sol)=default
 copy assignment More...
 
const Ordersorders () const
 
std::string tau (const std::string &title="Tau") const
 writing the solution in compact form into a string More...
 
TInterval total_service_time () const
 Total service time of the solution. More...
 
TInterval total_travel_time () const
 Total travel time of the solution. More...
 
int twvTot () const
 Total number of time windows constraint violations of the solution. More...
 
Fleetvehicles ()
 
TInterval wait_time () const
 Total waiting time of the solution. More...
 

Protected Attributes

std::deque< Vehicle_pickDeliverm_fleet
 The current solution. More...
 
Pgr_messages m_msg
 
Orders m_orders
 the problem info More...
 
Fleet m_trucks
 

Friends

std::ostream & operator<< (std::ostream &log, const Solution &solution)
 printing function More...
 

Detailed Description

Definition at line 50 of file solution.h.

Constructor & Destructor Documentation

◆ Solution() [1/3]

vrprouting::problem::Solution::Solution ( )
delete

constructor

◆ Solution() [2/3]

template<typename P >
vrprouting::problem::Solution::Solution ( P *  p_problem)
inlineexplicit

constructor

Definition at line 57 of file solution.h.

57  :
58  m_orders(p_problem->orders()),
59  m_trucks(p_problem->vehicles()),
60  m_msg(p_problem->msg) { }

◆ Solution() [3/3]

vrprouting::problem::Solution::Solution ( const Solution sol)
default

copy constructor

Member Function Documentation

◆ cost()

std::tuple< int, int, size_t, TInterval, TInterval, TInterval > Solution::cost ( ) const

Get all statistics in one cycle.

Returns
totals of
idx variable
0 twv
1 cv
2 fleet size
3 waiting time
4 duration
5 travel time
std::get<idx>

Definition at line 218 of file solution.cpp.

218  {
219  TInterval total_duration(0);
220  TInterval total_wait_time(0);
221  TInterval total_tt(0);
222  int total_twv(0);
223  int total_cv(0);
224  /*
225  * Cycle the fleet
226  */
227  for (const auto& v : m_fleet) {
228  total_duration += v.duration();
229  total_wait_time += v.total_wait_time();
230  total_twv += v.twvTot();
231  total_cv += v.cvTot();
232  total_tt += v.total_travel_time();
233  }
234  /*
235  * build the tuple
236  */
237  return std::make_tuple(
238  total_twv, total_cv, m_fleet.size(),
239  total_wait_time, total_duration,
240  total_tt);
241 }

References m_fleet.

Referenced by cost_str(), and operator<().

◆ cost_str()

std::string Solution::cost_str ( ) const
Returns
The costs in one string

Definition at line 246 of file solution.cpp.

246  {
247  /*
248  * get the cost
249  */
250  auto s_cost(cost());
251  std::ostringstream log;
252 
253  /*
254  * Build the string
255  */
256  log << std::fixed << std::setprecision(4)
257  << "twv=" << std::get<0>(s_cost)
258  << " cv=" << std::get<1>(s_cost)
259  << " wait=" << std::get<3>(s_cost)
260  << " duration=" << std::get<4>(s_cost)
261  << " tt=" << std::get<5>(s_cost);
262 
263  return log.str();
264 }

References cost().

Referenced by tau().

◆ cvTot()

int Solution::cvTot ( ) const

Total number of capacity constraint violations of the solution.

Returns
Total number of capacity violations The total capacity violations of the solution is the sum of the capacity violations of all vehicles

Definition at line 196 of file solution.cpp.

196  {
197  return std::accumulate(begin(m_fleet), end(m_fleet), 0,
198  [](int i, const Vehicle_pickDeliver& v) {return v.cvTot() + i;});
199 }

References vrprouting::problem::Vehicle::cvTot(), and m_fleet.

Referenced by get_postgres_result().

◆ duration()

TInterval Solution::duration ( ) const

Total duration of the solution.

Returns
the solution's duration

The solution duration is the sum of the durations of all vehicles

Definition at line 140 of file solution.cpp.

140  {
141  return std::accumulate(begin(m_fleet), end(m_fleet), static_cast<TInterval>(0),
142  [](TInterval i, const Vehicle_pickDeliver& v) {return v.duration() + i;});
143 }

References vrprouting::problem::Vehicle::duration(), and m_fleet.

Referenced by get_postgres_result(), vrprouting::optimizers::simple::Optimize::save_if_best(), and vrprouting::optimizers::simple::Optimize::swap_worse().

◆ fleet()

const std::deque<Vehicle_pickDeliver>& vrprouting::problem::Solution::fleet ( ) const
inline

Get the current fleet solution.

Definition at line 107 of file solution.h.

107 {return m_fleet;}

References m_fleet.

Referenced by vrprouting::optimizers::tabu::Optimize::intensify(), and vrprouting::optimizers::tabu::Optimize::Optimize().

◆ get_postgres_result()

std::vector< Solution_rt > Solution::get_postgres_result ( ) const

get solution like postgres needs it

Returns
container for postgres

Definition at line 61 of file solution.cpp.

61  {
62  std::vector<Solution_rt> result;
63 
64  /* postgres numbering starts with 1 */
65  int i(1);
66 
67  for (auto v : m_fleet) {
68  v.evaluate(0);
69  auto data = v.get_postgres_result(i);
70  /*
71  * Results adjusted for the depot and the first stop
72  * So the vehicle waits on the depot not on the first stop
73  */
74  data[1].arrivalTime = data[1].operationTime;
75  data[0].waitDuration = data[1].waitDuration;
76  data[1].waitDuration = 0;
77  data[0].departureTime = data[0].arrivalTime + data[0].waitDuration;
78 
79 
80  result.insert(result.end(), data.begin(), data.end());
81  ++i;
82  }
83 
84  Solution_rt aggregates = {
85  /*
86  * Vehicle id = -2 indicates its an aggregate row
87  *
88  * (twv, cv, fleet, wait, duration)
89  */
90  -2, // summary row on vehicle_number
91  twvTot(), // on vehicle_id
92  cvTot(), // on vehicle_seq
93  -1, // on order_id
94  -1, // on stop_id
95  -2, // on stop_type (gets increased later by one so it gets -1)
96  -1, // not accounting total loads
97  static_cast<int64_t>(total_travel_time()),
98  -1, // not accounting arrival_travel_time
99  static_cast<int64_t>(wait_time()),
100  -1, // not accounting operation time
101  static_cast<int64_t>(total_service_time()),
102  static_cast<int64_t>(duration()),
103  cvTot(),
104  twvTot()
105  };
106  result.push_back(aggregates);
107 
108  return result;
109 }

References cvTot(), duration(), m_fleet, total_service_time(), total_travel_time(), twvTot(), and wait_time().

◆ get_stops()

std::vector< Short_vehicle > Solution::get_stops ( ) const

get solution like postgres needs it

Returns
container for postgres

Definition at line 49 of file solution.cpp.

49  {
50  std::vector<Short_vehicle> result;
51  for (auto v : m_fleet) {
52  result.push_back(Short_vehicle{v.id(), v.get_stops()});
53  }
54  return result;
55 }

References Short_vehicle::id, and m_fleet.

◆ is_feasible()

bool Solution::is_feasible ( ) const

is the solution feasible?

The solution is feasible when all vehicles are feasible.

Returns
true when all vehicles in solution are feasible

Definition at line 129 of file solution.cpp.

129  {
130  return std::find_if(m_fleet.begin(), m_fleet.end(),
131  [] (const Vehicle_pickDeliver& v) -> bool {return !v.is_feasible();}) == m_fleet.end();
132 }

References m_fleet.

Referenced by vrprouting::optimizers::simple::Optimize::decrease_truck(), vrprouting::initialsol::simple::Initial_solution::do_while_feasible(), vrprouting::initialsol::simple::Initial_solution::do_while_foo(), vrprouting::initialsol::tabu::Initial_solution::Initial_solution(), vrprouting::initialsol::tabu::Initial_solution::process_given_solution_from_user(), and vrprouting::initialsol::tabu::Initial_solution::process_unassigned().

◆ msg()

◆ objective()

double vrprouting::problem::Solution::objective ( ) const
inline

◆ operator<()

bool Solution::operator< ( const Solution s_rhs) const

Definition at line 267 of file solution.cpp.

267  {
268  auto lhs(cost());
269  auto rhs(s_rhs.cost());
270 
271  /*
272  * time window violations
273  */
274  if (std::get<0>(lhs) < std::get<0>(rhs))
275  return true;
276  if (std::get<0>(lhs) > std::get<0>(rhs))
277  return false;
278 
279  /*
280  * capacity violations
281  */
282  if (std::get<1>(lhs) < std::get<1>(rhs))
283  return true;
284  if (std::get<1>(lhs) > std::get<1>(rhs))
285  return false;
286 
287  /*
288  * fleet size
289  */
290  if (std::get<2>(lhs) < std::get<2>(rhs))
291  return true;
292  if (std::get<2>(lhs) > std::get<2>(rhs))
293  return false;
294 
295  /*
296  * waiting time
297  */
298  if (std::get<3>(lhs) < std::get<3>(rhs))
299  return true;
300  if (std::get<3>(lhs) > std::get<3>(rhs))
301  return false;
302 
303  /*
304  * duration
305  */
306  if (std::get<4>(lhs) < std::get<4>(rhs))
307  return true;
308  if (std::get<4>(lhs) > std::get<4>(rhs))
309  return false;
310 
311  return false;
312 }

References cost().

◆ operator=()

Solution& vrprouting::problem::Solution::operator= ( const Solution sol)
default

copy assignment

◆ orders()

◆ tau()

std::string Solution::tau ( const std::string &  title = "Tau") const

writing the solution in compact form into a string

Parameters
[in]titleTitle to Use for the tau
Returns
The solution in one compacted string

Definition at line 116 of file solution.cpp.

116  {
117  std::string str {"\n" + title + ": " + '\n'};
118  for (const auto& v : m_fleet) str += ("\n" + v.tau());
119  str += "\n" + cost_str() + "\n";
120  return str;
121 }

References cost_str(), and m_fleet.

Referenced by vrprouting::optimizers::simple::Optimize::inter_swap(), vrprouting::optimizers::simple::Optimize::Optimize(), and vrprouting::optimizers::tabu::Optimize::Optimize().

◆ total_service_time()

TInterval Solution::total_service_time ( ) const

Total service time of the solution.

Returns
the total service time of the solution

The total service time of the solution is the sum of the service times of all vehicles

Definition at line 174 of file solution.cpp.

174  {
175  return std::accumulate(begin(m_fleet), end(m_fleet), static_cast<TInterval>(0),
176  [](TInterval i, const Vehicle_pickDeliver& v) {return v.total_service_time() + i;});
177 }

References m_fleet, and vrprouting::problem::Vehicle::total_service_time().

Referenced by get_postgres_result().

◆ total_travel_time()

TInterval Solution::total_travel_time ( ) const

Total travel time of the solution.

Returns
the total waiting time

The total travel time of the solution is the sum of the travel times of all vehicles

Definition at line 162 of file solution.cpp.

162  {
163  return std::accumulate(begin(m_fleet), end(m_fleet), static_cast<TInterval>(0),
164  [](TInterval i, const Vehicle_pickDeliver& v) {return v.total_travel_time() + i;});
165 }

References m_fleet, and vrprouting::problem::Vehicle::total_travel_time().

Referenced by get_postgres_result(), and objective().

◆ twvTot()

int Solution::twvTot ( ) const

Total number of time windows constraint violations of the solution.

Returns
the total number of time window violations

The total time window violations of the solution is the sum of the time window violations of all vehicles

Definition at line 185 of file solution.cpp.

185  {
186  return std::accumulate(begin(m_fleet), end(m_fleet), 0,
187  [](int i, const Vehicle_pickDeliver& v) {return v.twvTot() + i;});
188 }

References m_fleet, and vrprouting::problem::Vehicle::twvTot().

Referenced by get_postgres_result().

◆ vehicles()

◆ wait_time()

TInterval Solution::wait_time ( ) const

Total waiting time of the solution.

Returns
the total waiting time

The total waiting time of the solution is the sum of the waiting time of all vehicles

Definition at line 151 of file solution.cpp.

151  {
152  return std::accumulate(begin(m_fleet), end(m_fleet), static_cast<TInterval>(0),
153  [](TInterval i, const Vehicle_pickDeliver& v) {return v.total_wait_time() + i;});
154 }

References m_fleet, and vrprouting::problem::Vehicle::total_wait_time().

Referenced by get_postgres_result().

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  log,
const Solution solution 
)
friend

printing function

Definition at line 76 of file solution.h.

76  {
77  for (const auto& vehicle : solution.m_fleet) log << vehicle;
78  log << "\n SOLUTION:\n\n " << solution.tau();
79  return log;
80  }

Member Data Documentation

◆ m_fleet

std::deque<Vehicle_pickDeliver> vrprouting::problem::Solution::m_fleet
protected

The current solution.

Definition at line 125 of file solution.h.

Referenced by cost(), cvTot(), vrprouting::optimizers::simple::Optimize::decrease_truck(), vrprouting::optimizers::simple::Optimize::delete_empty_truck(), vrprouting::optimizers::tabu::Optimize::diversify(), vrprouting::initialsol::simple::Initial_solution::do_while_foo(), duration(), fleet(), get_postgres_result(), get_stops(), vrprouting::initialsol::tabu::Initial_solution::Initial_solution(), vrprouting::optimizers::tabu::Optimize::intensify(), vrprouting::optimizers::simple::Optimize::inter_swap(), is_feasible(), vrprouting::optimizers::tabu::Optimize::move_2_real(), vrprouting::initialsol::simple::Initial_solution::one_truck_all_orders(), vrprouting::optimizers::simple::Optimize::Optimize(), vrprouting::optimizers::tabu::Optimize::Optimize(), vrprouting::initialsol::tabu::Initial_solution::process_given_solution_from_user(), vrprouting::initialsol::tabu::Initial_solution::process_unassigned(), vrprouting::optimizers::simple::Optimize::save_if_best(), vrprouting::optimizers::tabu::Optimize::single_pair_insertion(), vrprouting::optimizers::simple::Optimize::sort_by_duration(), vrprouting::optimizers::simple::Optimize::sort_by_id(), vrprouting::optimizers::simple::Optimize::sort_by_size(), vrprouting::optimizers::tabu::Optimize::sort_by_size(), vrprouting::optimizers::simple::Optimize::sort_for_move(), vrprouting::optimizers::tabu::Optimize::swap_between_routes(), vrprouting::optimizers::tabu::Optimize::tabu_search(), tau(), total_service_time(), total_travel_time(), twvTot(), and wait_time().

◆ m_msg

Pgr_messages vrprouting::problem::Solution::m_msg
protected

Definition at line 130 of file solution.h.

Referenced by msg().

◆ m_orders

Orders vrprouting::problem::Solution::m_orders
protected

the problem info

Definition at line 128 of file solution.h.

Referenced by vrprouting::initialsol::simple::Initial_solution::do_while_feasible(), and orders().

◆ m_trucks

Fleet vrprouting::problem::Solution::m_trucks
protected

Definition at line 129 of file solution.h.

Referenced by vehicles().


The documentation for this class was generated from the following files:
vrprouting::problem::Solution::total_service_time
TInterval total_service_time() const
Total service time of the solution.
Definition: solution.cpp:174
vrprouting::problem::Solution::m_fleet
std::deque< Vehicle_pickDeliver > m_fleet
The current solution.
Definition: solution.h:125
vrprouting::problem::Solution::cvTot
int cvTot() const
Total number of capacity constraint violations of the solution.
Definition: solution.cpp:196
vrprouting::problem::Solution::total_travel_time
TInterval total_travel_time() const
Total travel time of the solution.
Definition: solution.cpp:162
Short_vehicle::id
Id id
Definition: short_vehicle.h:42
vrprouting::problem::Solution::twvTot
int twvTot() const
Total number of time windows constraint violations of the solution.
Definition: solution.cpp:185
vrprouting::problem::Solution::wait_time
TInterval wait_time() const
Total waiting time of the solution.
Definition: solution.cpp:151
vrprouting::problem::Solution::duration
TInterval duration() const
Total duration of the solution.
Definition: solution.cpp:140
Short_vehicle
short_vehicle
Definition: short_vehicle.h:41
Solution_rt
Solution schedule when twv & cw are hard restrictions.
Definition: solution_rt.h:56
vrprouting::problem::Solution::m_msg
Pgr_messages m_msg
Definition: solution.h:130
vrprouting::problem::Solution::cost
std::tuple< int, int, size_t, TInterval, TInterval, TInterval > cost() const
Get all statistics in one cycle.
Definition: solution.cpp:218
TInterval
int64_t TInterval
Definition: typedefs.h:72
vrprouting::problem::Solution::m_trucks
Fleet m_trucks
Definition: solution.h:129
vrprouting::problem::Solution::m_orders
Orders m_orders
the problem info
Definition: solution.h:128
vrprouting::problem::Solution::cost_str
std::string cost_str() const
Definition: solution.cpp:246