-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
194 lines (156 loc) · 3.95 KB
/
main.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "game.h"
#include "astar.h"
#include "gen.h"
/* matrix is cost from prize to prize, from start to prizes and prizes to end.
* index 0: start
* 1 .. num_prizes: prizes
* index num_prizes+1: end
*/
TSP_Node *graph;
void swap_pos(int *pos1, int *pos2)
{
int tmp;
tmp = pos1[0];
pos1[0] = pos2[0];
pos2[0] = tmp;
tmp = pos1[1];
pos1[1] = pos2[1];
pos2[1] = tmp;
}
static inline void print_path(int *vet, int sz)
{
int i;
for(i = 0; i < sz; i++)
{
printf("DEBUG: (%d, %d)\n", vet[i*2 + 0], vet[i*2 + 1]);
}
}
static inline int comp_pos(int *pos1, int *pos2)
{
if(pos1[0] != pos2[0]) return 0;
if(pos1[1] != pos2[1]) return 0;
return 1;
}
int *inv_vet(int *vet, int sz)
{
int *new_vet = malloc(sizeof(int)*sz*2);
int i;
for(i = 0; i < sz; i++)
{
new_vet[2*(sz-i-1) + 0] = vet[2*i + 0];
new_vet[2*(sz-i-1) + 1] = vet[2*i + 1];
}
return new_vet;
}
void calc_cost_matrix(void)
{
int i,j;
int bkp_num_prizes = G.gi.num_prizes;
TSP_Node *start_graph = malloc(sizeof(TSP_Node) * G.gi.num_prizes);
for (i=0; i<G.gi.num_prizes; i++)
{
TSP_Node *node = &start_graph[i];
node->path = a_star_search(G.gi.start, G.gi.prizes[i], &node->cost, &node->steps);
}
for (i=0; i<G.gi.num_prizes; i++) {
if(start_graph[i].cost < 0)
{
/* swap them */
TSP_Node tmp = start_graph[i];
start_graph[i] = start_graph[G.gi.num_prizes-1];
start_graph[G.gi.num_prizes-1] = tmp;
swap_pos(G.gi.prizes[i], G.gi.prizes[G.gi.num_prizes-1]);
G.gi.num_prizes--;
i--;
}
}
graph = malloc(sizeof(TSP_Node) * (G.gi.num_prizes+2) * (G.gi.num_prizes+2));
for (i=j=0; i<G.gi.num_prizes; i++) {
graph[mkcostidx(0, j+1)].path = start_graph[i].path;
graph[mkcostidx(j+1, 0)].path = inv_vet(start_graph[i].path, start_graph[i].steps);
graph[mkcostidx(0, j+1)].cost = start_graph[i].cost;
graph[mkcostidx(j+1, 0)].cost = start_graph[i].cost;
graph[mkcostidx(0, j+1)].steps = start_graph[i].steps;
graph[mkcostidx(j+1, 0)].steps = start_graph[i].steps;
j++;
}
free(start_graph);
for (i=0; i<G.gi.num_prizes; i++) {
for (j=0; j<i; j++) {
TSP_Node *node = &graph[mkcostidx(i+1, j+1)];
TSP_Node *node_inv = &graph[mkcostidx(j+1, i+1)];
node->path = a_star_search(G.gi.prizes[i], G.gi.prizes[j], &node->cost, &node->steps);
node_inv->cost = node->cost;
node_inv->steps = node->steps;
node_inv->path = inv_vet(node->path, node->steps);
}
}
for (i=0; i<G.gi.num_prizes; i++) {
TSP_Node *node = &graph[mkcostidx(i+1, G.gi.num_prizes+1)];
node->path = a_star_search(G.gi.prizes[i], G.gi.end, &node->cost, &node->steps);
}
#if 1
for (i=0; i<G.gi.num_prizes+2; i++) {
for (j=0; j<G.gi.num_prizes+2; j++) {
printf("% 7.1f", graph[mkcostidx(i, j)].cost);
}
printf("\n");
}
#endif
}
int main(int argc, const char *argv[])
{
#if defined(ALLEGRO)
int i;
int *path;
int *goal, *ptr;
float distance;
float min = 100000;
#endif
srand(time(NULL));
game_read();
calc_cost_matrix();
#if defined(ALLEGRO)
gfx_init(600, 600, 10);
while (1) {
int j=0;
if (G.gi.num_prizes != 0) {
path = ga_solve_tsp(1000, &distance);
} else {
TSP_Node *node = &graph[mkcostidx(0, 1)];
path = malloc(sizeof(int) * 2);
path[0] = 0;
path[1] = 1;
node->path = a_star_search(G.gi.start, G.gi.end, &node->cost, &node->steps);
distance = node->cost;
}
min = MIN(distance, min);
for (i = 0; i < G.gi.num_prizes+2; i++) {
printf(" %d", path[i]);
}
printf("\n");
printf("distance: %f %f\n", distance, min);
memset(G.gi.prizes_visited, 0, sizeof(int) * G.gi.num_prizes+2);
for (j=0; j<G.gi.num_prizes+1; j++) {
TSP_Node *node = &graph[mkcostidx(path[j], path[j+1])];
if (path[j] > 0) {
G.gi.prizes_visited[path[j]-1] = 1;
}
goal = node->path;
for(i = 0; i < node->steps; i++)
{
G.gi.cur[0] = node->path[i*2 + 0];
G.gi.cur[1] = node->path[i*2 + 1];
gfx_step();
}
gfx_step();
}
}
gfx_end();
free(graph);
#endif
return 0;
}