-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.c
More file actions
67 lines (62 loc) · 1.83 KB
/
reader.c
File metadata and controls
67 lines (62 loc) · 1.83 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "reader.h"
void data_read(struct data* self, char* name)
{
// to read the instance, the list of possible pieces is ignored by now
char path[128];
strcpy(path, "instances/");
strcat(path, name);
FILE *fd = fopen(path, "r");
fscanf(fd, "%d %d\n", &self->width, &self->height);
fscanf(fd, "%d", &self->n_pieces);
self->pieces = (int *)malloc(sizeof(int)*self->n_pieces);
fscanf(fd, "%s\n", path);
for (int i = 0; i < self->n_pieces; i++) {
fscanf(fd, "%d", &self->pieces[i]);
}
fclose(fd);
}
void data_destruct(struct data* self)
{
// freedom of memory
free(self->pieces);
}
void dump_generation(struct generation* gen)
{
// not in use, to save a generation
// intended for when more generations are needed, so the algorithm doesn't have to run from scratch
char path[128], buffer[128];
strcpy(path, "dumps/");
sprintf(buffer, "%ld.csv", time(0));
strcat(path, buffer);
FILE *fd = fopen(path, "w");
for (int i = 0; i < POPSIZE; i++) {
fprintf(fd, "%d,%d,%d,%d\n",
gen->population[i].holes,
gen->population[i].row_transitions,
gen->population[i].column_transitions,
gen->population[i].heights
);
}
fclose(fd);
}
void load_generation(struct generation* gen, char* name)
{
// not in use, to load a saved generation
char path[128];
strcpy(path, "dumps/");
strcat(path, name);
FILE *fd = fopen(path, "r");
for (int i = 0; i < POPSIZE; i++) {
fscanf(fd, "%d,%d,%d,%d\n",
&gen->population[i].holes,
&gen->population[i].row_transitions,
&gen->population[i].column_transitions,
&gen->population[i].heights
);
}
fclose(fd);
}