vrpRouting  0.3
initialsol/tabu.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 FILE: tabu.cpp
3 
4 Copyright (c) 2021 pgRouting developers
6 
7 Developer:
8 Copyright (c) 2021 Copyright (c) 2021 Joseph Emile Honour Percival
9 
10 ------
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26  ********************************************************************PGR-GNU*/
27 
28 
29 #include "initialsol/tabu.h"
30 #include <deque>
31 #include <algorithm>
32 #include <limits>
33 
34 #include "cpp_common/pgr_assert.h"
36 #include "problem/pickDeliver.h"
37 #if 0
38 #include "cpp_common/fleet.h"
39 #endif
40 
41 namespace vrprouting {
42 namespace initialsol {
43 namespace tabu {
44 
49 void
52  pgassert((m_assigned * m_unassigned).empty());
53 }
54 
61  TTimestamp execution_date,
62  bool optimize,
63  problem::PickDeliver* problem_ptr) :
64  Solution(problem_ptr),
65  m_all_orders(),
66  m_unassigned(),
67  m_assigned() {
68  invariant();
69 
77 
78  process_given_solution_from_user(execution_date, optimize);
80 
87  if (!m_unassigned.empty()) {
88  for (auto &v : m_fleet) {
89  bool is_usable(false);
90  for (const auto &o : m_unassigned) {
91  if (v.feasible_orders().has(o)) {
92  /*
93  * found an order to be inserted that fits on the vehicle
94  */
95  is_usable = true;
96  break;
97  }
98  }
99  /*
100  * No order to be inserted fits on the vehicle
101  */
102  if (!is_usable) v.set_unmovable(std::numeric_limits<TTimestamp>::max());
103  }
104  }
105 
109  auto unused = vehicles().get_unused_trucks();
110  m_fleet.insert(m_fleet.end(), unused.begin(), unused.end());
112 
118 
119  invariant();
120  }
121 
130 void
132  pgassert(m_fleet.empty());
133  invariant();
134 
138  vehicles().set_initial_solution(orders(), m_assigned, m_unassigned, execution_date, optimize);
139 
143  auto used_v = vehicles().get_used_trucks();
144 
148  m_fleet.insert(m_fleet.end(), used_v.begin(), used_v.end());
149 
151  invariant();
152 }
153 
161 void
166  invariant();
168 
169  Identifiers<size_t> notused;
170 
171  auto not_assigned = m_unassigned;
172 
176  for (const auto o : not_assigned) {
180  auto phony_v = vehicles().get_phony();
181  if (!phony_v.is_order_feasible(orders()[o])) {
182  phony_v.push_back(orders()[o]);
183  m_unassigned -= o;
184  m_all_orders -= o;
185  msg().error << "\n**Illegal Order** pick.opens() + tt > drop.closes() can not be inserted on any vehicle";
186  continue;
187  }
188 
192  phony_v.push_back(orders()[o]);
193  pgassert(phony_v.is_feasible());
194 
198  m_assigned += o;
199  m_unassigned -= o;
200 
204  m_fleet.push_back(phony_v);
205  invariant();
206  }
207 
213  invariant();
214 }
215 
216 } // namespace tabu
217 } // namespace initialsol
218 } // namespace vrprouting
vrprouting::problem::Solution::orders
const Orders & orders() const
Definition: solution.h:120
pgr_messages.h
vrprouting::problem::Solution::m_fleet
std::deque< Vehicle_pickDeliver > m_fleet
The current solution.
Definition: solution.h:125
vrprouting::problem::Fleet::get_used_trucks
std::vector< Vehicle_pickDeliver > get_used_trucks()
Get all the used vehicles.
Definition: fleet.cpp:64
vrprouting::problem::Fleet::set_initial_solution
void set_initial_solution(const Orders &, Identifiers< size_t > &, Identifiers< size_t > &, TTimestamp, bool)
set the vehicle's user's initial solution
Definition: fleet.cpp:296
vrprouting::problem::Fleet::get_phony
Vehicle_pickDeliver get_phony() const
Get a vehicle from the user's defined vehicles.
Definition: fleet.h:91
vrprouting::problem::PickDeliver
the pick deliver problem
Definition: pickDeliver.h:50
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::Pgr_messages::error
std::ostringstream error
Stores the error information.
Definition: pgr_messages.h:95
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
tabu.h
vrprouting::initialsol::tabu::Initial_solution::m_assigned
Identifiers< size_t > m_assigned
set of assigned orders
Definition: initialsol/tabu.h:72
pickDeliver.h
vrprouting::problem::Fleet::feasible_orders
Identifiers< size_t > feasible_orders() const
Definition: fleet.cpp:97
Identifiers::empty
bool empty() const
Definition: identifiers.hpp:78
vrprouting::initialsol::tabu::Initial_solution::m_unassigned
Identifiers< size_t > m_unassigned
set of unassigned orders
Definition: initialsol/tabu.h:69
pgr_assert.h
An assert functionality that uses C++ throw().
vrprouting::problem::Solution::msg
Pgr_messages & msg()
Definition: solution.h:119
TTimestamp
int64_t TTimestamp
Definition: typedefs.h:71
vrprouting::initialsol::tabu::Initial_solution::invariant
void invariant() const
class invariant about the orders that have been assigned
Definition: initialsol/tabu.cpp:50
vrprouting::initialsol::tabu::Initial_solution::process_unassigned
void process_unassigned()
Adds unassigned orders to phony vehicles.
Definition: initialsol/tabu.cpp:162
vrprouting::problem::Solution::is_feasible
bool is_feasible() const
is the solution feasible?
Definition: solution.cpp:129
vrprouting::initialsol::tabu::Initial_solution::Initial_solution
Initial_solution()=delete
Initial solution without information is not valid.
vrprouting::initialsol::tabu::Initial_solution::m_all_orders
Identifiers< size_t > m_all_orders
set of all orders
Definition: initialsol/tabu.h:66
vrprouting::initialsol::tabu::Initial_solution::process_given_solution_from_user
void process_given_solution_from_user(TTimestamp, bool)
processes the initial solution given by the user
Definition: initialsol/tabu.cpp:131
vrprouting::problem::Fleet::get_unused_trucks
std::vector< Vehicle_pickDeliver > get_unused_trucks()
Get all the unused vehicles.
Definition: fleet.cpp:80
Identifiers< size_t >
vrprouting
Definition: base_matrix.cpp:46