File size: 10,081 Bytes
ea8c728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#pragma once

// standart imports
#include <string>
#include <vector>
#include <limits>
#include <iostream>


// global variables declaration
constexpr int null = -1;
constexpr double e = 2.718281;
constexpr double inf_double = std::numeric_limits<double>::infinity();
constexpr int inf_int32 = std::numeric_limits<int>::max();
constexpr long long inf_int64 = std::numeric_limits<long long>::max();


enum class CandidatesSource { KNN, HeatMap };
enum class DistanceType { Int32, Int64, Double };


struct Config {
    int cities_number;
    std::string input_path;
    std::string output_path;
    bool use_heat_map_as_initial_weights;
    CandidatesSource candidates_source;
    int candidates_number;
    int max_k_opt_depth;
    bool random_k_opt_depth_after_first_iteration;
    double min_potential_to_consider;
    double exploration_coefficient;
    double weight_delta_coefficient;
    bool use_sensitivity_decrease;
    double sensitivity_temperature;
    int max_k_opt_simulations_without_improve_to_stop;
    int restarts_number;
    DistanceType distance_type;
    double magnify_rate;

    Config(const json& config) {
        cities_number = config["cities_number"];
        input_path = config["input_path"];
        output_path = config["output_path"];
        use_heat_map_as_initial_weights = config["use_heat_map_as_initial_weights"];

        if (config["candidates_source"].get<std::string>() == "knn") {
            candidates_source = CandidatesSource::KNN;
        } else if (config["candidates_source"].get<std::string>() == "heat_map") {
            candidates_source = CandidatesSource::HeatMap;
        } else {
            throw std::invalid_argument("Unknown candidates source: '" + config["candidates_source"].get<std::string>() + "'.");
        }

        candidates_number = config["candidates_number"];
        max_k_opt_depth = config["max_k_opt_depth"];
        random_k_opt_depth_after_first_iteration = config["random_k_opt_depth_after_first_iteration"];
        min_potential_to_consider = config["min_potential_to_consider"];
        exploration_coefficient = config["exploration_coefficient"];
        weight_delta_coefficient = config["weight_delta_coefficient"];
        use_sensitivity_decrease = config["use_sensitivity_decrease"];
        sensitivity_temperature = config["sensitivity_temperature"];
        max_k_opt_simulations_without_improve_to_stop = config["max_k_opt_simulations_without_improve_to_stop"];
        restarts_number = config["restarts_number"];

        if (config["distance_type"].get<std::string>() == "int32") {
            distance_type = DistanceType::Int32;
        } else if (config["distance_type"].get<std::string>() == "int64") {
            distance_type = DistanceType::Int64;
        } else if (config["distance_type"].get<std::string>() == "double") {
            distance_type = DistanceType::Double;
        } else {
            throw std::invalid_argument("Unknown distance type: '" + config["distance_type"].get<std::string>() + "'.");
        }

        magnify_rate = config["magnify_rate"];

        // warnings
        if (distance_type == DistanceType::Double) {
            if (cities_number > 1'000'000) {
                std::cout << "Warning: `cities_number` is greater than 1'000'000. Try to use with `distance_type` = 'int64' to avoid precision errors.\n";
            }
        } else {
            if (magnify_rate < 10'000.0) {
                std::cout << "Warning: `magnify_rate` is better to take more than 10'000 when using with `distance_type` = 'int32' or 'int64'.\n";
            }
            if (distance_type == DistanceType::Int32 && static_cast<long long>(cities_number) * magnify_rate > 1'000'000'000) {
                std::cout << "Warning: distance might be out of bounds for a 32 bit integer. Consider switching to doubles for distance calculation or using 64 bit integers.\n";
            }
            if (distance_type == DistanceType::Int64 && magnify_rate > 1'000'000'000) {
                std::cout << "Warning: distance might be out of bounds for a 64 bit integer. Consider switching to doubles for distance calculation.\n";
            }
        }
    }
};


struct City {
  int prev;
  int next;
};


struct Context {
    // coordinates
    double* coordinates_double_x = nullptr;
    double* coordinates_double_y = nullptr;

    int* coordinates_int32_x = nullptr;
    int* coordinates_int32_y = nullptr;

    long long* coordinates_int64_x = nullptr;
    long long* coordinates_int64_y = nullptr;

    // distances
    double* distance_double = nullptr;
    int* distance_int32 = nullptr;
    long long* distance_int64 = nullptr;

    // weights
    double* heat_map = nullptr;
    double* weight = nullptr;
    double* total_weight = nullptr;
    double* potential = nullptr;

    // candidates
    int* candidates = nullptr;

    // local k opt search
    int* pairs = nullptr;
    int* saved_pairs = nullptr;
    int saved_depth = 0;

    double current_best_delta_double = -inf_double;
    int current_best_delta_int32 = -inf_int32;
    long long current_best_delta_int64 = -inf_int64;

    long long* chosen_times = nullptr;
    long long total_simulations = 0;

    // path
    City* path = nullptr;
    double path_distance_double = inf_double;
    int path_distance_int32 = inf_int32;
    long long path_distance_int64 = inf_int64;

    City* best_path = nullptr;
    double best_path_distance_double = inf_double;
    int best_path_distance_int32 = inf_int32;
    long long best_path_distance_int64 = inf_int64;

    int* solution;
    std::vector<bool> is_city_selected;

    // other utils/tmp variables
    std::vector<int> buffer;

    Context(const Config& config) : buffer(config.cities_number, 0), is_city_selected(config.cities_number, false) {
        coordinates_double_x = new double[config.cities_number];
        coordinates_double_y = new double[config.cities_number];

        if (config.distance_type == DistanceType::Int32) {
            coordinates_int32_x = new int[config.cities_number];
            coordinates_int32_y = new int[config.cities_number];
        }
        if (config.distance_type == DistanceType::Int64) {
            coordinates_int64_x = new long long[config.cities_number];
            coordinates_int64_y = new long long[config.cities_number];
        }

        distance_double = new double[config.cities_number * config.cities_number];
        if (config.distance_type == DistanceType::Int32) {
            distance_int32 = new int[config.cities_number * config.cities_number];
        }
        if (config.distance_type == DistanceType::Int64) {
            distance_int64 = new long long[config.cities_number * config.cities_number];
        }

        if (config.use_heat_map_as_initial_weights) {
            heat_map = new double[config.cities_number * config.cities_number];
        }
        weight = new double[config.cities_number * config.cities_number];
        total_weight = new double[config.cities_number];
        potential = new double[config.cities_number * config.cities_number];

        candidates = new int[config.cities_number * config.candidates_number];

        pairs = new int[config.cities_number];  // because currently we have a random k opt depth change option
        saved_pairs = new int[config.cities_number];

        chosen_times = new long long[config.cities_number * config.cities_number];
        for (int i = 0; i < config.cities_number * config.cities_number; ++i) {
            chosen_times[i] = 0;
        }

        path = new City[config.cities_number];
        best_path = new City[config.cities_number];
        solution = new int[config.cities_number];
    }

    ~Context() {
        delete[] coordinates_double_x;
        delete[] coordinates_double_y;

        if (coordinates_int32_x) { delete[] coordinates_int32_x; }
        if (coordinates_int32_y) { delete[] coordinates_int32_y; }

        if (coordinates_int64_x) { delete[] coordinates_int64_x; }
        if (coordinates_int64_y) { delete[] coordinates_int64_y; }

        delete[] distance_double;
        if (distance_int32) { delete[] distance_int32; }
        if (distance_int64) { delete[] distance_int64; }

        if (heat_map) { delete[] heat_map; }
        delete[] weight;
        delete[] total_weight;
        delete[] potential;

        delete[] candidates;

        delete[] pairs;
        delete[] saved_pairs;

        delete[] chosen_times;

        delete[] path;
        delete[] best_path;
        delete[] solution;
    }
};


void convert_solution_to_path(const Config& config, Context& context) {
    for (int i = 0; i < config.cities_number; ++i) {
        int current_city = context.solution[i];

        context.path[current_city].prev = context.solution[(i + config.cities_number - 1) % config.cities_number];
        context.path[current_city].next = context.solution[(i + 1) % config.cities_number];
    }
}

void convert_path_to_solution(const Config& config, Context& context) {
    int current_city = 0;

    for (int i = 0; i < config.cities_number; ++i) {
        context.solution[i] = current_city;

        current_city = context.path[current_city].next;
    }
}

void store_path_as_best(const Config& config, Context& context) {
    for (int i = 0; i < config.cities_number; ++i) {
        context.best_path[i].prev = context.path[i].prev;
        context.best_path[i].next = context.path[i].next;
    }

    if (config.distance_type == DistanceType::Double) {
        context.best_path_distance_double = context.path_distance_double;
    }
    if (config.distance_type == DistanceType::Int32) {
        context.best_path_distance_int32 = context.path_distance_int32;
    }
    if (config.distance_type == DistanceType::Int64) {
        context.best_path_distance_int64 = context.path_distance_int64;
    }
}

void restore_best_path(const Config& config, Context& context) {
    for (int i = 0; i < config.cities_number; ++i) {
        context.path[i].prev = context.best_path[i].prev;
        context.path[i].next = context.best_path[i].next;
    }
}