vrpRouting  0.3
tabu_list.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: tabu_list.cpp
4 
5 Copyright (c) 2021 pgRouting developers
7 
8 Developer:
9 Copyright (c) 2021 Joseph Emile Honour Percival
10 
11 ------
12 
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17 
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22 
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 
27  ********************************************************************PGR-GNU*/
28 
29 #include "optimizers/tabu_list.h"
30 
31 #include <string>
32 #include <iostream>
33 #include <algorithm>
34 
35 namespace vrprouting {
36 namespace optimizers {
37 namespace tabu {
38 
42 void TabuList::add(const Move &m) {
43  if (has(m)) return;
44  while (size() >= max_length()) m_tabu_list.pop_front();
45  m_tabu_list.push_back(m);
46 }
47 
51  m_tabu_list.clear();
52 }
53 
59 bool TabuList::has(const Move &m) const {
60  return std::find(m_tabu_list.begin(), m_tabu_list.end(), m) != m_tabu_list.end();
61 }
62 
74  const problem::Vehicle_pickDeliver& to_vehicle,
75  const problem::Order& order, double obj1, double obj2) const {
76  return has({from_vehicle, to_vehicle, order, obj1, obj2}) ||
77  has({to_vehicle, from_vehicle, order, obj1, obj2});
78 }
79 
92  const problem::Vehicle_pickDeliver& to_vehicle,
93  const problem::Order& from_order,
94  const problem::Order& to_order, double obj1, double obj2) const {
95  return has({from_vehicle, to_vehicle, from_order, to_order, obj1, obj2}) ||
96  has({from_vehicle, to_vehicle, to_order, from_order, obj1, obj2}) ||
97  has({to_vehicle, from_vehicle, from_order, to_order, obj1, obj2}) ||
98  has({to_vehicle, from_vehicle, to_order, from_order, obj1, obj2});
99 }
100 
101 
102 
108 bool TabuList::is_empty() const {
109  return m_tabu_list.empty();
110 }
111 
116 size_t TabuList::size() const {
117  return m_tabu_list.size();
118 }
119 
124 size_t TabuList::max_length() const {
125  return m_max_length;
126 }
127 
131 void TabuList::set_max_length(size_t length) {
132  m_max_length = length;
133 }
134 
139 std::ostream&
140 operator << (std::ostream &log, const TabuList &list) {
141  log << "\nTabuList length: " << list.size()
142  << "\n *><* *** tabu list start *** ";
143 
144  for (const auto &m : list) log << m << ' ';
145 
146  log << " *** tabu list end ***\n";
147  return log;
148 }
149 
150 bool TabuList::has_infeasible(const std::string& candidate) {
151  return (m_infeasible_list.find(candidate) != m_infeasible_list.end());
152 }
153 
154 void TabuList::add_infeasible(const std::string& candidate) {
155  m_infeasible_list.insert(candidate);
156 }
157 
158 bool TabuList::has_seen(const std::string& candidate) {
159  return (m_seen_list.find(candidate) != m_seen_list.end());
160 }
161 
162 void TabuList::add_seen(const std::string& candidate) {
163  m_seen_list.insert(candidate);
164 }
165 
166 
167 } // namespace tabu
168 } // namespace optimizers
169 } // namespace vrprouting
vrprouting::optimizers::tabu::TabuList::m_seen_list
std::set< std::string > m_seen_list
the seen list
Definition: tabu_list.h:117
vrprouting::optimizers::tabu::TabuList::has_seen
bool has_seen(const std::string &candidate)
Checks to see if a move candidate has already been done before (in the seen list)
Definition: tabu_list.cpp:158
vrprouting::optimizers::tabu::TabuList::has_move
bool has_move(const problem::Vehicle_pickDeliver &, const problem::Vehicle_pickDeliver &, const problem::Order &from_order, double obj1, double obj2) const
Checks to see if a "move" move is "tabu" (in the tabu list)
Definition: tabu_list.cpp:73
tabu_list.h
vrprouting::optimizers::tabu::TabuList::m_tabu_list
std::deque< Move > m_tabu_list
the tabu list
Definition: tabu_list.h:111
vrprouting::optimizers::tabu::TabuList::max_length
size_t max_length() const
returns the maximum length (tabu_length) of the tabu list
Definition: tabu_list.cpp:124
vrprouting::optimizers::tabu::Move
The Move class defines moves that are evaluated and set as tabu.
Definition: move.h:46
vrprouting::optimizers::tabu::operator<<
std::ostream & operator<<(std::ostream &log, const Move &m)
Definition: move.cpp:66
vrprouting::optimizers::tabu::TabuList::clear
void clear()
removes all moves from the tabu list
Definition: tabu_list.cpp:50
vrprouting::optimizers::tabu::TabuList::size
size_t size() const
returns the current length (number of moves) in the tabu list
Definition: tabu_list.cpp:116
vrprouting::optimizers::tabu::TabuList::is_empty
bool is_empty() const
returns true if the tabu list is empty
Definition: tabu_list.cpp:108
vrprouting::optimizers::tabu::TabuList::add_infeasible
void add_infeasible(const std::string &candidate)
Adds a move candidate to the infeasible list.
Definition: tabu_list.cpp:154
vrprouting::problem::Vehicle_pickDeliver
Definition: vehicle_pickDeliver.h:46
vrprouting::optimizers::tabu::TabuList::has
bool has(const Move &) const
Checks to see if a (general) move is "tabu" (in the tabu list)
Definition: tabu_list.cpp:59
vrprouting::optimizers::tabu::TabuList::has_swap
bool has_swap(const problem::Vehicle_pickDeliver &, const problem::Vehicle_pickDeliver &, const problem::Order &, const problem::Order &, double obj1, double obj2) const
Checks to see if a "swap" move is "tabu" (in the tabu list)
Definition: tabu_list.cpp:91
vrprouting::optimizers::tabu::TabuList::set_max_length
void set_max_length(size_t length)
sets the maximum length (tabu_length) of the tabu list
Definition: tabu_list.cpp:131
vrprouting::optimizers::tabu::TabuList::add_seen
void add_seen(const std::string &candidate)
Adds a move candidate to the seen list.
Definition: tabu_list.cpp:162
vrprouting::optimizers::tabu::TabuList::add
void add(const Move &m)
Make a move "tabu" by adding it to the tabu list.
Definition: tabu_list.cpp:42
vrprouting::problem::Order
Definition: order.h:40
vrprouting::optimizers::tabu::TabuList::m_max_length
size_t m_max_length
the maximum length (tabu_length) of the tabu list
Definition: tabu_list.h:108
vrprouting::optimizers::tabu::TabuList
The TabuList class defines the tabu list to be used in optimization.
Definition: tabu_list.h:45
vrprouting::optimizers::tabu::TabuList::has_infeasible
bool has_infeasible(const std::string &candidate)
Checks to see if a move candidate is infeasible (in the infeasible list)
Definition: tabu_list.cpp:150
vrprouting
Definition: base_matrix.cpp:46
vrprouting::optimizers::tabu::TabuList::m_infeasible_list
std::set< std::string > m_infeasible_list
the infeasible list
Definition: tabu_list.h:114