-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_dependent_orienteering_main.cpp
More file actions
183 lines (154 loc) · 5.33 KB
/
time_dependent_orienteering_main.cpp
File metadata and controls
183 lines (154 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* Time-dependent orienteering problem
*
* Problem description:
* See https://github.com/fontanf/orproblems/blob/main/orproblems/time_dependent_orienteering.hpp
*
*/
#include "read_args.hpp"
#include "localsearchsolver/sequencing.hpp"
#include "orproblems/routing/time_dependent_orienteering.hpp"
using namespace localsearchsolver;
using namespace orproblems::time_dependent_orienteering;
class SequencingScheme
{
public:
static sequencing::Parameters sequencing_parameters()
{
sequencing::Parameters parameters;
parameters.shift_block_maximum_length = 7;
parameters.swap_block_maximum_length = 5;
parameters.reverse = true;
parameters.shift_reverse_block_maximum_length = 6;
parameters.add_remove = true;
parameters.replace = true;
parameters.force_add = true;
return parameters;
}
/**
* Global cost:
* - Overtime
* - Profit
* - Total time
*/
using GlobalCost = std::tuple<Time, Profit, Time>;
struct SequenceData
{
sequencing::ElementId element_id_last = -1;
Time time_cur = 0;
Time time_full = 0;
Profit profit = 0;
};
SequencingScheme(const Instance& instance): instance_(instance) { }
inline sequencing::ElementPos number_of_elements() const { return instance_.number_of_locations() - 2; }
inline GlobalCost global_cost(const SequenceData& sequence_data) const
{
return {
std::max((Time)0, sequence_data.time_full - instance_.maximum_duration()),
-sequence_data.profit,
sequence_data.time_full,
};
}
inline GlobalCost bound(const SequenceData& sequence_data) const
{
return {
std::max((Time)0, sequence_data.time_full - instance_.maximum_duration()),
std::numeric_limits<Profit>::lowest(),
sequence_data.time_full,
};
}
inline void append(
SequenceData& sequence_data,
sequencing::ElementId element_id) const
{
// Update time_cur.
sequence_data.time_cur = instance_.arrival_time(
sequence_data.element_id_last + 1,
element_id + 1,
sequence_data.time_cur);
// Update profit.
sequence_data.profit += instance_.location(element_id + 1).profit;
// Update time_full.
sequence_data.time_full = instance_.arrival_time(
element_id + 1,
instance_.number_of_locations() - 1,
sequence_data.time_cur);
// Update element_id_last.
sequence_data.element_id_last = element_id;
}
void instance_format(
std::ostream& os,
int verbosity_level) const
{
os << "Time-dependent orienteering problem" << std::endl;
instance_.format(os, verbosity_level);
}
void solution_write(
const sequencing::LocalScheme<SequencingScheme>::Solution& solution,
const std::string& certificate_path)
{
if (certificate_path.empty())
return;
std::ofstream file(certificate_path);
if (!file.good()) {
throw std::runtime_error(
"Unable to open file \"" + certificate_path + "\".");
}
for (auto se: solution.sequences[0].elements)
file << se.element_id + 1 << std::endl;
}
private:
/** Instance. */
const Instance& instance_;
};
int main(int argc, char *argv[])
{
// Create command line options.
boost::program_options::options_description desc = setup_args();
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
if (vm.count("help")) {
std::cout << desc << std::endl;;
throw "";
}
try {
boost::program_options::notify(vm);
} catch (const boost::program_options::required_option& e) {
std::cout << desc << std::endl;;
throw "";
}
// Create instance.
InstanceBuilder instance_builder;
instance_builder.read(
vm["input"].as<std::string>(),
vm["format"].as<std::string>());
const Instance instance = instance_builder.build();
// Create local scheme.
SequencingScheme sequencing_scheme(instance);
auto sequencing_parameters = read_sequencing_args<SequencingScheme>(vm);
sequencing::LocalScheme<SequencingScheme> local_scheme(
sequencing_scheme,
sequencing_parameters);
// Run algorithm.
std::string algorithm = vm["algorithm"].as<std::string>();
auto output =
(algorithm == "multi-start-local-search")?
run_multi_start_local_search(local_scheme, vm):
(algorithm == "iterated-local-search")?
run_iterated_local_search(local_scheme, vm):
(algorithm == "best-first-local-search")?
run_best_first_local_search(local_scheme, vm):
run_genetic_local_search(local_scheme, vm);
// Run checker.
if (vm["print-checker"].as<int>() > 0
&& vm["certificate"].as<std::string>() != "") {
std::cout << std::endl
<< "Checker" << std::endl
<< "-------" << std::endl;
instance.check(
vm["certificate"].as<std::string>(),
std::cout,
vm["print-checker"].as<int>());
}
return 0;
}