vrpRouting  0.3
initialsol/simple.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 FILE: simple.cpp
3 
4 Copyright (c) 2015 pgRouting developers
6 
7 ------
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 Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
25 
26 #include "initialsol/simple.h"
27 #include <deque>
28 #include <algorithm>
29 #include <set>
30 #include "cpp_common/pgr_assert.h"
31 #include "problem/node_types.h"
32 #include "problem/orders.h"
33 #include "problem/pickDeliver.h"
34 
35 namespace vrprouting {
36 namespace initialsol {
37 namespace simple {
38 
39 void
41  /* this checks there is no order duplicated */
43  pgassert((assigned * unassigned).empty());
44 }
45 
47  Initials_code kind,
48  problem::PickDeliver* problem_ptr) :
49  problem::Solution(problem_ptr),
50  all_orders(m_orders.size()),
51  unassigned(m_orders.size()),
52  assigned() {
53  invariant();
54  pgassert(kind >= 0 && kind <= OneDepot);
55 
56  switch (kind) {
57  case OneTruck:
59  break;
60  case OnePerTruck:
61  case FrontTruck:
62  case BackTruck:
63  case BestInsert:
64  case BestBack:
65  case BestFront:
66  case OneDepot:
67  do_while_foo(kind);
68  break;
69  default: pgassert(false);
70  }
71 
72  invariant();
73 }
74 
75 void
77  invariant();
78  pgassert(kind > 0 && kind <= OneDepot);
79 
80  Identifiers<size_t> notused;
81 
82  while (!unassigned.empty()) {
83  auto current = unassigned.size();
84  auto truck = vehicles().get_truck(unassigned.front());
85  /*
86  * kind 1 to 7 work with the same code structure
87  */
89  pgassertwm(current > unassigned.size(), msg().get_log().c_str());
90 
91  m_fleet.push_back(truck);
92  invariant();
93  }
94 
95  pgassertwm(true, msg().get_log().c_str());
97  invariant();
98 }
99 
100 void
102  invariant();
103  msg().log << "\nInitial_solution::one_truck_all_orders\n";
104  auto truck = vehicles().get_truck();
105  while (!unassigned.empty()) {
106  auto order(truck.orders().at(*unassigned.begin()));
107 
108  truck.hillClimb(order);
109 
112 
113  invariant();
114  }
115  m_fleet.push_back(truck);
116  invariant();
117 }
118 
119 void
122  Initials_code kind,
123  Identifiers<size_t> &unassigned,
124  Identifiers<size_t> &assigned) {
126  auto current_feasible = vehicle.feasible_orders() * unassigned;
127 
128  while (!current_feasible.empty()) {
129  auto order = m_orders[current_feasible.front()];
130 
131  switch (kind) {
132  case OnePerTruck:
133  vehicle.push_back(order);
135  assigned += order.idx();
136  unassigned -= order.idx();
137  invariant();
138  return;
139  break;
140  case FrontTruck:
141  vehicle.push_front(order);
142  break;
143  case BackTruck:
144  vehicle.push_back(order);
145  break;
146  case BestInsert:
147  vehicle.hillClimb(order);
148  break;
149  case BestBack:
150  order = m_orders[m_orders.find_best_J(current_feasible)];
151  vehicle.hillClimb(order);
152  break;
153  case BestFront:
154  order = m_orders[m_orders.find_best_I(current_feasible)];
155  vehicle.hillClimb(order);
156  break;
157  case OneDepot:
158  vehicle.semiLIFO(order);
159  break;
160  default: pgassert(false);
161  }
162 
163  if (vehicle.orders_size() == 1 && !is_feasible()) {
164  pgassert(false);
165  }
166 
167  if (!is_feasible()) {
168  vehicle.erase(order);
169  } else if (vehicle.has_order(order)) {
170  assigned += order.idx();
171  unassigned -= order.idx();
172  if (kind == BestBack) {
173  current_feasible = m_orders[order.idx()].subsetJ(
174  current_feasible);
175  }
176  if (kind == BestFront) {
177  current_feasible = m_orders[order.idx()].subsetI(
178  current_feasible);
179  }
180  }
181 
182  current_feasible -= order.idx();
183  invariant();
184  }
185 
187  invariant();
188 }
189 
190 } // namespace simple
191 } // namespace initialsol
192 } // namespace vrprouting
node_types.h
vrprouting::problem::Vehicle_pickDeliver::erase
void erase(const Order &order)
erases the order from the vehicle
Definition: vehicle_pickDeliver.cpp:154
vrprouting::Pgr_messages::log
std::ostringstream log
Stores the hint information.
Definition: pgr_messages.h:91
vrprouting::problem::Vehicle_pickDeliver::feasible_orders
Identifiers< size_t > & feasible_orders()
returns the set of feasible orders for modification
Definition: vehicle_pickDeliver.h:100
vrprouting::problem::Solution::m_fleet
std::deque< Vehicle_pickDeliver > m_fleet
The current solution.
Definition: solution.h:125
Identifiers::pop_front
void pop_front()
Definition: identifiers.hpp:84
vrprouting::initialsol::simple::Initials_code
Initials_code
Different kinds to insert an order into the vehicle.
Definition: initialsol/initials_code.h:37
vrprouting::problem::Vehicle_pickDeliver::hillClimb
bool hillClimb(const Order &order)
Inserts an order with hill Climb approach.
Definition: vehicle_pickDeliver.cpp:444
vrprouting::problem::Fleet::get_truck
Vehicle_pickDeliver get_truck()
Finds an unused vehicle.
Definition: fleet.cpp:121
vrprouting::problem::PickDeliver
the pick deliver problem
Definition: pickDeliver.h:50
pgassertwm
#define pgassertwm(expr, msg)
Adds a message to the assertion.
Definition: pgr_assert.h:118
vrprouting::initialsol::simple::Initial_solution::unassigned
Identifiers< size_t > unassigned
Definition: initialsol/simple.h:67
vrprouting::initialsol::simple::Initial_solution::invariant
void invariant() const
Definition: initialsol/simple.cpp:40
vrprouting::problem::Solution::vehicles
Fleet & vehicles()
Definition: solution.h:121
vrprouting::problem::Vehicle_pickDeliver::push_back
void push_back(const Order &order)
puts an order at the end of the truck
Definition: vehicle_pickDeliver.cpp:57
vrprouting::initialsol::simple::BackTruck
@ BackTruck
Insetion at the front of the truck.
Definition: initialsol/initials_code.h:41
vrprouting::initialsol::simple::Initial_solution::do_while_foo
void do_while_foo(int kind)
Definition: initialsol/simple.cpp:76
vrprouting::initialsol::simple::OneDepot
@ OneDepot
Push front order that allows more orders to be inserted at the front.
Definition: initialsol/initials_code.h:45
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
vrprouting::initialsol::simple::Initial_solution::Initial_solution
Initial_solution()=delete
Identifiers::size
size_t size() const
Definition: identifiers.hpp:77
vrprouting::problem::Vehicle_pickDeliver::has_order
bool has_order(const Order &order) const
does the vehicle has the order?
Definition: vehicle_pickDeliver.h:108
vrprouting::problem::Vehicle_pickDeliver::semiLIFO
bool semiLIFO(const Order &order)
Inserts an order In semi-Lifo (almost last in first out) order.
Definition: vehicle_pickDeliver.cpp:361
Identifiers::begin
const_iterator begin() const
Definition: identifiers.hpp:81
pickDeliver.h
vrprouting::problem::Vehicle_pickDeliver
Definition: vehicle_pickDeliver.h:46
Identifiers::empty
bool empty() const
Definition: identifiers.hpp:78
pgr_assert.h
An assert functionality that uses C++ throw().
vrprouting::problem::Solution::msg
Pgr_messages & msg()
Definition: solution.h:119
vrprouting::problem::Vehicle_pickDeliver::push_front
void push_front(const Order &order)
Puts an order at the end front of the truck.
Definition: vehicle_pickDeliver.cpp:84
vrprouting::initialsol::simple::OnePerTruck
@ OnePerTruck
All orders in one truck.
Definition: initialsol/initials_code.h:39
vrprouting::initialsol::simple::FrontTruck
@ FrontTruck
One Order per truck.
Definition: initialsol/initials_code.h:40
vrprouting::initialsol::simple::OneTruck
@ OneTruck
Definition: initialsol/initials_code.h:38
vrprouting::initialsol::simple::Initial_solution::one_truck_all_orders
void one_truck_all_orders()
Definition: initialsol/simple.cpp:101
vrprouting::initialsol::simple::Initial_solution::assigned
Identifiers< size_t > assigned
Definition: initialsol/simple.h:68
Identifiers::front
T front() const
Definition: identifiers.hpp:79
vrprouting::problem::Solution::is_feasible
bool is_feasible() const
is the solution feasible?
Definition: solution.cpp:129
vrprouting::initialsol::simple::BestBack
@ BestBack
Best place to insert Order.
Definition: initialsol/initials_code.h:43
vrprouting::problem::Orders::find_best_J
size_t find_best_J(const Identifiers< size_t > &within_this_set) const
find the best order -> this -> order
Definition: orders.cpp:46
vrprouting::initialsol::simple::Initial_solution::do_while_feasible
void do_while_feasible(problem::Vehicle_pickDeliver &truck, Initials_code kind, Identifiers< size_t > &unassigned, Identifiers< size_t > &assigned)
Definition: initialsol/simple.cpp:120
simple.h
vrprouting::problem::Vehicle_pickDeliver::orders_size
size_t orders_size() const
Definition: vehicle_pickDeliver.h:110
orders.h
vrprouting::initialsol::simple::BestFront
@ BestFront
Push back order that allows more orders to be inserted at the back.
Definition: initialsol/initials_code.h:44
vrprouting::problem::Orders::find_best_I
size_t find_best_I(const Identifiers< size_t > &within_this_set) const
find the best order -> this
Definition: orders.cpp:68
vrprouting::initialsol::simple::BestInsert
@ BestInsert
Insetion at the back of the truck.
Definition: initialsol/initials_code.h:42
vrprouting::problem::Solution::m_orders
Orders m_orders
the problem info
Definition: solution.h:128
Identifiers< size_t >
vrprouting::initialsol::simple::Initial_solution::all_orders
Identifiers< size_t > all_orders
Definition: initialsol/simple.h:66
vrprouting
Definition: base_matrix.cpp:46