-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.cpp
153 lines (141 loc) · 4.09 KB
/
utils.cpp
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
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "global.h"
/**
* @brief helper function used to print
* @return void
*/
void logger(FILE *file, char *message)
{
if (LOGGING) {
fprintf(file,"%s",message);
fflush(file);
}
}
int process_config_line(char *line)
{
// Ignore comments.
if (line[0] == CONFIG_COMMENT_CHAR || strncmp(line,"\n",1)==0)
return 0;
// Extract config parameter name and value.
char name[100];
char value[50];
int items = sscanf(line, "%s %s\n", name, value);
// Line wasn't as expected.
if (items != 2)
return 1;
// Process this line.
if (strcmp(name, "CUE_SEED") == 0) {
CUE_SEED = atoi(value);
} else if (strcmp(name, "NUM_RESERVOIRS") == 0) {
NUM_RESERVOIRS = atoi(value);
} else if (strcmp(name, "NUM_SENSORS") == 0) {
NUM_SENSORS = atoi(value);
} else if (strcmp(name, "NUM_MOTORS") == 0) {
NUM_MOTORS = atoi(value);
} else if (strcmp(name, "MAX_AXON_LENGTH") == 0) {
MAX_AXON_LENGTH = atoi(value);
} else if (strcmp(name, "MAX_SYNAPSES") == 0) {
MAX_SYNAPSES = atoi(value);
} else if (strcmp(name, "MAX_WEIGHT") == 0) {
MAX_WEIGHT = atoi(value);
} else if (strcmp(name, "MAX_CUE") == 0) {
MAX_CUE = atoi(value);
}
else if (strcmp(name, "SEARCH_RADIUS") == 0) {
SEARCH_RADIUS = atoi(value);
}
else if (strcmp(name, "ACTION_POTENTIAL") == 0) {
ACTION_POTENTIAL = atoi(value);
}
else if (strcmp(name, "THRESHOLD") == 0) {
THRESHOLD = atoi(value);
}
else if (strcmp(name, "INITIAL_SYNAPSE_WEIGHT") == 0) {
INITIAL_SYNAPSE_WEIGHT = atoi(value);
}
else if (strcmp(name, "CUE_CHANGE") == 0) {
CUE_CHANGE = (float)atof(value);
}
else if (strcmp(name, "WEIGHT_CHANGE") == 0) {
WEIGHT_CHANGE = (float)atof(value);
}
else if (strcmp(name, "LIFESPAN_INCREASE") == 0) {
LIFESPAN_INCREASE = (float)atof(value);
}
else if (strcmp(name, "LIFESPAN_DECREASE") == 0) {
LIFESPAN_DECREASE = (float)atof(value);
}
else {
// Ignore unknown config parameters.
}
return 0;
}
/**
* @brief Reads the config file to compare the values later on
* @return If successful, return 0 if found, -1 if not found
*/
int read_config(char *config_file)
{
int error_occurred = 0;
// Open file for reading.
FILE *file = fopen(config_file, "r");
if (file == NULL)
error_occurred = 1;
// Process the config file.
while (!error_occurred && !feof(file)) {
// Read a line from the file.
char line[150];
char *l = fgets(line, sizeof line, file);
// Process the line.
if (l == line)
error_occurred = process_config_line(line);
else if (!feof(file))
error_occurred = 1;
}
return error_occurred ? -1 : 0;
}
int readMNIST(char* image_filename, char* label_filename, int num, int*** images, int* labels) {
char buffimg[150];
char buff[num + 1];
int imageNum = 0;
int line = 0;
// Load labels from file.
FILE* label_file = fopen(label_filename, "r");
if (label_file == NULL) {
printf ("Error opening file: %s\n", label_filename);
return -1;
}
fgets(buff, num + 1, label_file);
for (int i = 0; i < strlen(buff); i++) {
char temp = buff[i];
labels[i] = temp - '0';
}
printf("Labels finished loading.\n");
// Load image data from file.
imageNum = 0;
line = 0;
FILE* image_file = fopen(image_filename, "r");
if (image_file == NULL) {
printf ("Error opening file: %s\n", image_filename);
return -1;
}
while ((fgets(buffimg, 150, image_file) != NULL) && (imageNum < num)) {
if (strcmp(buffimg, "\n") == 0) {
line = 0;
imageNum += 1;
}
else {
images[imageNum][line][0] = atoi(strtok(buffimg, "\t"));
for (int i = 1; i < 28; i++) {
// sscanf(buff, "%d\t", &images[imageNum][line][i]);
images[imageNum][line][i] = atoi(strtok(NULL, "\t"));
}
line += 1;
}
}
printf("Images finished loading.\n");
return 0;
}