File size: 6,122 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
#pragma once

// standart imports
#include <queue>
#include <cmath>
#include <numeric>
#include <algorithm>

// other imports
#include "context.hpp"


constexpr double BHH_CONSTANT_2D = 0.7120;  // Beardwood–Halton–Hammersley (BHH) constant


long long int64_sqrt(long long value) {
    if (value < 0) return null;  // invalid for negatives
    if (value < 2) return value;

    constexpr long long MAX_SQRT_LL = 3037000499LL;

    long long left = 1;
    long long right = std::min<long long>(value, MAX_SQRT_LL);
    long long floor_root = 1;

    while (left <= right) {
        long long candidate = left + (right - left) / 2;

        if (candidate <= value / candidate) {
            floor_root = candidate;
            left = candidate + 1;
        } else {
            right = candidate - 1;
        }
    }
    return floor_root;
}

double smooth_relu(double x) {
    if (x < 0) { return pow(e, x); }
    return x + 1.0;
}


double calc_distance_double(Context& context, int i, int j) {
    if (i == j) { return inf_double; }
    double diff_x = (context.coordinates_double_x[i] - context.coordinates_double_x[j]);
    double diff_y = (context.coordinates_double_y[i] - context.coordinates_double_y[j]);
    return sqrt(diff_x * diff_x + diff_y * diff_y);
}

int calc_distance_int32(Context& context, int i, int j) {
    if (i == j) { return inf_int32; }
    long long diff_x = static_cast<long long>(context.coordinates_int32_x[i] - context.coordinates_int32_x[j]);
    long long diff_y = static_cast<long long>(context.coordinates_int32_y[i] - context.coordinates_int32_y[j]);
    return static_cast<int>(int64_sqrt(diff_x * diff_x + diff_y * diff_y));
}

long long calc_distance_int64(Context& context, int i, int j) {
    if (i == j) { return inf_int64; }
    long long diff_x = context.coordinates_int64_x[i] - context.coordinates_int64_x[j];
    long long diff_y = context.coordinates_int64_y[i] - context.coordinates_int64_y[j];
    return int64_sqrt(diff_x * diff_x + diff_y * diff_y);
}


double get_distance_double(const Config& config, Context& context, int i, int j) {
    return context.distance_double[i * config.cities_number + j];
}

int get_distance_int32(const Config& config, Context& context, int i, int j) {
    return context.distance_int32[i * config.cities_number + j];
}

long long get_distance_int64(const Config& config, Context& context, int i, int j) {
    return context.distance_int64[i * config.cities_number + j];
}


double calc_total_distance_double(const Config& config, Context& context) {
    double total_distance = 0.0;

    for (int i = 0; i < config.cities_number; ++i) {
        total_distance += get_distance_double(config, context, i, context.path[i].next);
    }

    return total_distance;
}

int calc_total_distance_int32(const Config& config, Context& context) {
    int total_distance = 0.0;

    for (int i = 0; i < config.cities_number; ++i) {
        total_distance += get_distance_int32(config, context, i, context.path[i].next);
    }

    return total_distance;
}

long long calc_total_distance_int64(const Config& config, Context& context) {
    long long total_distance = 0.0;

    for (int i = 0; i < config.cities_number; ++i) {
        total_distance += get_distance_int64(config, context, i, context.path[i].next);
    }

    return total_distance;
}


void calc_and_save_total_distance(const Config& config, Context& context) {
    if (config.distance_type == DistanceType::Double) {
        context.path_distance_double = calc_total_distance_double(config, context);
    }
    if (config.distance_type == DistanceType::Int32) {
        context.path_distance_int32 = calc_total_distance_int32(config, context);
    }
    if (config.distance_type == DistanceType::Int64) {
        context.path_distance_int64 = calc_total_distance_int64(config, context);
    }
}


void update_weight_undirected(const Config& config, Context& context, int i, int j, double weight_delta) {
    context.total_weight[i] -= smooth_relu(context.weight[i * config.cities_number + j]);
    context.total_weight[j] -= smooth_relu(context.weight[j * config.cities_number + i]);

    context.weight[i * config.cities_number + j] += weight_delta;
    context.weight[j * config.cities_number + i] += weight_delta;

    context.total_weight[i] += smooth_relu(context.weight[i * config.cities_number + j]);
    context.total_weight[j] += smooth_relu(context.weight[j * config.cities_number + i]);
}


void identify_candidates_for_each_node(const Config& config, Context& context, const double* metric, bool is_reversed) {
	for (int i = 0; i < config.cities_number; ++i) {
        std::iota(context.buffer.begin(), context.buffer.end(), 0);  // just a simple range(0, n), vector should be filled to use std::iota

        std::nth_element(context.buffer.begin(), context.buffer.begin() + config.candidates_number, context.buffer.end(), [&](int u, int v) {
            if (i == u) { return false; }
            if (i == v) { return true; }
            return static_cast<bool>((metric[i * config.cities_number + u] < metric[i * config.cities_number + v]) ^ is_reversed);
        });
		
		for (int j = 0; j < config.candidates_number; ++j) {
			context.candidates[i * config.candidates_number + j] = context.buffer[j];
	    }
	}
}


int get_random_int_by_module(int mod) {
	return rand() % mod;
}


bool is_cities_same_or_adjacent(const Config& config, Context& context, int i, int j) {
    return (i == j || context.path[i].next == j || context.path[j].next == i);
}


void reverse_sub_path(Context& context, int i, int j) {
    int current_city = i;

    while (true) {
        std::swap(context.path[current_city].prev, context.path[current_city].next);

        if (current_city == j) { return; }

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


double expected_optimal_tsp_length_2d(long long n, double width, double height) {
    if (n <= 1 || width <= 0.0 || height <= 0.0) {
        return 0.0;
    }

    double area = width * height;
    double expected_length = BHH_CONSTANT_2D * std::sqrt(static_cast<double>(n) * area);
    return expected_length;
}