This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximize.c
94 lines (81 loc) · 2.11 KB
/
maximize.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
#include "main.h"
void
check_args(int argc, char *argv[])
{
if ((argc < 2) || (argc > 4)) {
printf("Usage: %s filename [seed] [order]\n", argv[0]);
exit(0);
}
}
void
init_seed(int argc, char *argv[])
{
int seed = 0; // Initial seed
if (argc == 3)
seed = atoi(argv[2]);
if (seed < 0)
seed = time(0);
srand(seed);
}
struct matrix *
init_cost_matrix(int argc, char *argv[])
{
struct matrix *M;
int order;
if(strcmp(argv[1], "random") == 0) {
if(argc == 4) {
order = atoi(argv[3]);
} else {
order = 10;
}
fprintf(stderr,"Creatin matrix order %d ", order);
fflush(stderr);
M = matrix_random(order, -10, 200, 1);
} else {
int *pvec = malloc(sizeof *pvec);
order = read_matrix_file(argv[1], &pvec);
M = matrix_from_vector(pvec, order);
free(pvec);
};
return M;
}
int
main(int argc,
char *argv[])
{
double time;
check_args(argc,argv);
init_seed(argc, argv);
struct matrix *M = init_cost_matrix(argc, argv);
struct solution *S = solution_new(M->order);
struct simulated_annealing_state sa = { .T = 0.0,
.t0 = 0.,
.a = 0.8,
.max_iter = 1000,
.eps = 0.001,
};
sa.t0 = sa_initial_temperature(M, M->order); // Initial temp
solution_init(S);
for(size_t i = 0; i<30; i++) {
fprintf(stderr,".");
fflush(stderr);
solution_shuffle(S, S->order);
solution_update(S, M);
tic(&time, TIME_ms);
sa_maximize(sa, S, M);
tac(&time, TIME_ms);
fprintf(stdout, "%d,%7.4f,%d\n", S->order+1, time, solution_cost(S));
fflush(stdout);
if (S->order < 53)
solution_print(S);
fflush(stdout);
}
fprintf(stderr,"\n");
fflush(stderr);
/*
* Closing
*/
solution_free(S);
matrix_free(M);
return 0;
}