Repository navigation
Expand file tree
/
Copy pathLcVRPContest.cpp
More file actions
116 lines (98 loc) · 2.83 KB
/
Copy pathLcVRPContest.cpp
File metadata and controls
116 lines (98 loc) · 2.83 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
#include "Evaluator.hpp"
#include "Optimizer.hpp"
#include "ProblemLoader.hpp"
#include <algorithm>
#include <cctype>
#include <exception>
#include <iostream>
#include <string>
using namespace LcVRPContest;
void StartOptimization(const string &folder_name, const string &instance_name, int max_iterations, bool use_random_permutation = true)
{
ProblemLoader problem_loader(folder_name, instance_name, use_random_permutation);
ProblemData problem_data = problem_loader.LoadProblem();
int num_groups = problem_data.GetNumGroups();
Evaluator evaluator(problem_data, num_groups);
Optimizer optimizer(evaluator);
optimizer.Initialize();
for (int i = 0; i < max_iterations; ++i)
{
optimizer.RunIteration();
}
vector<int> *best_solution = optimizer.GetCurrentBest();
double best_fitness = evaluator.Evaluate(*best_solution);
cout << "final best fitness: " << best_fitness << endl;
}
static void PrintUsage(const string &program_name)
{
cout << "Usage: " << program_name << " [folder_name] [instance_name] [max_iterations] [use_random_permutation]\n";
cout << " folder_name: dataset folder under data/lcvrp (default: Vrp-Set-D)\n";
cout << " instance_name: instance file name without .lcvrp extension (default: ORTEC-n323-k21)\n";
cout << " max_iterations: number of optimization iterations (default: 10)\n";
cout << " use_random_permutation: 1/true or 0/false (default: true)\n";
}
static bool ParseBoolArg(const string &value)
{
if (value == "0" || value == "false" || value == "False" || value == "FALSE")
{
return false;
}
return true;
}
static string StripLcVrpExtension(const string &instance_name)
{
const string suffix = ".lcvrp";
if (instance_name.size() > suffix.size() && instance_name.substr(instance_name.size() - suffix.size()) == suffix)
{
return instance_name.substr(0, instance_name.size() - suffix.size());
}
return instance_name;
}
int main(int argc, char* argv[])
{
string folder_name = "Vrp-Set-D";
string instance_name = "ORTEC-n323-k21";
int max_iterations = 10;
bool use_random_permutation = true; // fixed seed for reproducibility, rand perm has more fair search space :)
if (argc == 2)
{
string arg = argv[1];
if (arg == "-h" || arg == "--help")
{
PrintUsage(argv[0]);
return 0;
}
}
if (argc > 5)
{
cout << "Error: too many arguments.\n";
PrintUsage(argv[0]);
return 1;
}
if (argc >= 2)
{
folder_name = argv[1];
}
if (argc >= 3)
{
instance_name = StripLcVrpExtension(argv[2]);
}
if (argc >= 4)
{
try
{
max_iterations = stoi(argv[3]);
}
catch (const std::exception &e) {
cout << "Error: invalid max_iterations value: " << argv[3] << "\n";
PrintUsage(argv[0]);
return 1;
}
}
if (argc == 5)
{
use_random_permutation = ParseBoolArg(argv[4]);
}
StartOptimization(folder_name, instance_name, max_iterations, use_random_permutation);
return 0;
}