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
|
#include "../tool.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define SOL_BUFFER_LEN 1000
#define MAX_SCRAMBLES 100
char *scrambles[MAX_SCRAMBLES + 1];
int scrambles_count = 0;
char *solver;
int64_t size = 0;
char *buf;
struct ScrambleArgs {
int scramble_idx;
};
static double timerun_single_scramble(void (*run)(struct ScrambleArgs *, char *, int *, long long *), struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) {
struct timespec start, end;
double tdiff, tdsec, tdnano;
fflush(stdout);
clock_gettime(CLOCK_MONOTONIC, &start);
run(args, solution, solution_length, stats);
clock_gettime(CLOCK_MONOTONIC, &end);
tdsec = end.tv_sec - start.tv_sec;
tdnano = end.tv_nsec - start.tv_nsec;
tdiff = tdsec + 1e-9 * tdnano;
return tdiff;
}
void run_scramble(struct ScrambleArgs *args, char *solution, int *solution_length, long long *stats) {
int scramble_idx = args->scramble_idx;
int64_t n;
char sol[SOL_BUFFER_LEN], cube[22];
printf("%d. %s\n", scramble_idx + 1, scrambles[scramble_idx]);
printf("Solving scramble %s\n", scrambles[scramble_idx]);
if (nissy_applymoves(NISSY_SOLVED_CUBE, scrambles[scramble_idx], cube) == -1) {
printf("Invalid scramble\n");
strcpy(solution, "Invalid scramble");
*solution_length = 0;
return;
}
n = nissy_solve(cube, solver, NISSY_NISSFLAG_NORMAL, 0, 20, 1, -1, size, buf, SOL_BUFFER_LEN, sol, stats);
if (n == 0) {
printf("No solution found\n");
strcpy(solution, "No solution found");
*solution_length = 0;
} else {
printf("Solutions:\n%s\n", sol);
strcpy(solution, sol);
*solution_length = nissy_countmoves(solution);
printf("Nodes visited: %lld\n", stats[0]);
printf("Table fallbacks: %lld\n", stats[1]);
}
}
void run_all_scrambles(void) {
double total_time = 0.0, average_time = 0.0;
long long total_nodes = 0, average_nodes = 0;
long long total_fallbacks = 0, average_fallbacks = 0;
long long stats[scrambles_count+1][NISSY_SIZE_SOLVE_STATS];
printf("Solved the following scrambles with timings:\n\n");
FILE *output_file = fopen("./tools/402_benchmark_nodes/compressed_results.txt", "w");
if (!output_file) {
printf("Error: Could not open output file for writing\n");
return;
}
int i;
for (i = 0; scrambles[i] != NULL; i++) {
struct ScrambleArgs args;
args.scramble_idx = i;
char solution[SOL_BUFFER_LEN];
int solution_length = 0;
double scramble_time = timerun_single_scramble(run_scramble, &args, solution, &solution_length, stats[i]);
total_time += scramble_time;
total_nodes += stats[i][0];
total_fallbacks += stats[i][1];
fprintf(output_file, "%d. %s\n", i + 1, scrambles[i]);
fprintf(output_file, "Solution: %s\n", solution);
fprintf(output_file, "Length of solution: %d\n", solution_length);
fprintf(output_file, "Time for scramble %d: %.4fs\n", i + 1, scramble_time);
fprintf(output_file, "Nodes visited: %lld\n", stats[i][0]);
fprintf(output_file, "Table fallbacks %lld\n", stats[i][1]);
fprintf(output_file, "-------------------------\n");
printf("Time for scramble %d: %.4fs\n", i + 1, scramble_time);
printf("Lenght: %d\n", solution_length);
}
if (i > 0) {
average_time = total_time / i;
average_nodes = total_nodes / i;
average_fallbacks = total_fallbacks / i;
}
printf("---------\n");
printf("Total time for all scrambles: %.4fs\n", total_time);
printf("Average time per scramble: %.4fs\n", average_time);
printf("---------\n");
printf("Total nodes for all scrambles: %lld\n", total_nodes);
printf("Average nodes per scramble: %lld\n", average_nodes);
printf("Average Table fallbacks: %lld\n", average_fallbacks);
printf("---------\n");
printf("Average Time/Node: %f\n", total_time/total_nodes*1e9);
fprintf(output_file, "---------\n");
fprintf(output_file, "Total time for all scrambles: %.4fs\n", total_time);
fprintf(output_file, "Average time per scramble: %.4fs\n", average_time);
fprintf(output_file, "---------\n");
fprintf(output_file, "Total nodes for all scrambles: %lld\n", total_nodes);
fprintf(output_file, "Average nodes per scramble: %lld\n", average_nodes);
fprintf(output_file, "Average Table fallbacks: %lld\n", average_fallbacks);
fprintf(output_file, "---------\n");
fprintf(output_file, "Average Time/Node: %f\n", total_time/total_nodes*1e9);
fclose(output_file);
}
int read_scrambles_from_file(const char *filename, char **scrambles) {
FILE *file = fopen(filename, "r");
if (!file) {
printf("Error: Could not open file %s\n", filename);
return -1;
}
char line[256];
while (fgets(line, sizeof(line), file)) {
line[strcspn(line, "\n")] = 0;
if (strncmp(line, "//", 2) == 0 || strlen(line) == 0)
continue;
scrambles[scrambles_count] = (char *)malloc(strlen(line) + 1);
if (scrambles[scrambles_count] == NULL) {
printf("Error: Memory allocation failed\n");
fclose(file);
return -1;
}
strcpy(scrambles[scrambles_count], line);
scrambles_count++;
if (scrambles_count >= MAX_SCRAMBLES) {
printf("Warning: Reached maximum number of scrambles (%d)\n", MAX_SCRAMBLES);
break;
}
}
scrambles[scrambles_count] = NULL;
fclose(file);
return 0;
}
int main(int argc, char **argv) {
char tables_filename[255];
char scrambles_filename[255] = "./tools/402_benchmark_nodes/scrambles.txt";
if (argc < 2) {
printf("Error: not enough arguments. A solver must be given.\n");
return 1;
}
solver = argv[1];
srand(time(NULL));
nissy_setlogger(log_stderr);
sprintf(tables_filename, "tables/%s", solver);
if (getdata(solver, &buf, tables_filename) != 0)
return 1;
size = nissy_datasize(solver);
if (read_scrambles_from_file(scrambles_filename, scrambles) != 0) {
printf("Error: failed to read scrambles from file\n");
free(buf);
return 1;
}
run_all_scrambles();
free(buf);
for (int i = 0; scrambles[i] != NULL; i++) {
free(scrambles[i]);
}
return 0;
}
|