XCSF  1.4.7
XCSF learning classifier system
env_csv.c
Go to the documentation of this file.
1 /*
2  * This program is free software: you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 
24 #include "env_csv.h"
25 #include "param.h"
26 
27 #define MAX_ROWS (100000)
28 #define MAX_COLS (200)
29 #define MAX_NAME (200)
30 #define DELIM (",")
31 
37 static int
38 env_csv_samples(FILE *fin)
39 {
40  int n_samples = 0;
41  char line[MAX_COLS];
42  while (fgets(line, MAX_COLS, fin) != NULL) {
43  ++n_samples;
44  }
45  return n_samples;
46 }
47 
53 static int
54 env_csv_dim(FILE *fin)
55 {
56  rewind(fin);
57  int n_dim = 0;
58  char line[MAX_COLS];
59  char *saveptr = NULL;
60  if (fgets(line, MAX_COLS, fin) != NULL) {
61  const char *ptok = strtok_r(line, DELIM, &saveptr);
62  while (ptok != NULL) {
63  if (strnlen(ptok, MAX_COLS) > 0) {
64  ++n_dim;
65  }
66  ptok = strtok_r(NULL, DELIM, &saveptr);
67  }
68  }
69  return n_dim;
70 }
71 
79 static void
80 env_csv_read_data(FILE *fin, double **data, const int n_samples,
81  const int n_dim)
82 {
83  rewind(fin);
84  *data = malloc(sizeof(double) * n_dim * n_samples);
85  char line[MAX_COLS];
86  const char *str = NULL;
87  char *saveptr = NULL;
88  char *endptr = NULL;
89  int i = 0;
90  while (fgets(line, MAX_COLS, fin) != NULL && i < n_samples) {
91  str = strtok_r(line, DELIM, &saveptr);
92  (*data)[i * n_dim] = strtod(str, &endptr);
93  for (int j = 1; j < n_dim; ++j) {
94  str = strtok_r(NULL, DELIM, &saveptr);
95  (*data)[i * n_dim + j] = strtod(str, &endptr);
96  }
97  ++i;
98  }
99 }
100 
109 static void
110 env_csv_read(const char *filename, double **data, int *n_samples, int *n_dim)
111 {
112  FILE *fin = fopen(filename, "rt");
113  if (fin == 0) {
114  printf("Error opening file: %s. %s.\n", filename, strerror(errno));
115  exit(EXIT_FAILURE);
116  }
117  *n_samples = env_csv_samples(fin);
118  *n_dim = env_csv_dim(fin);
119  if (*n_samples > 0 && *n_dim > 0) {
120  env_csv_read_data(fin, data, *n_samples, *n_dim);
121  fclose(fin);
122  } else {
123  printf("Error reading file: %s. No samples found\n", filename);
124  fclose(fin);
125  exit(EXIT_FAILURE);
126  }
127  printf("Loaded: %s: samples=%d, dim=%d\n", filename, *n_samples, *n_dim);
128 }
129 
137 static void
138 env_csv_input_read(const char *infile, struct Input *train_data,
139  struct Input *test_data)
140 {
141  char name[MAX_NAME];
142  snprintf(name, MAX_NAME, "%s_train_x.csv", infile);
143  env_csv_read(name, &train_data->x, &train_data->n_samples,
144  &train_data->x_dim);
145  snprintf(name, MAX_NAME, "%s_train_y.csv", infile);
146  env_csv_read(name, &train_data->y, &train_data->n_samples,
147  &train_data->y_dim);
148  snprintf(name, MAX_NAME, "%s_test_x.csv", infile);
149  env_csv_read(name, &test_data->x, &test_data->n_samples, &test_data->x_dim);
150  snprintf(name, MAX_NAME, "%s_test_y.csv", infile);
151  env_csv_read(name, &test_data->y, &test_data->n_samples, &test_data->y_dim);
152 }
153 
159 void
160 env_csv_init(struct XCSF *xcsf, const char *filename)
161 {
162  struct EnvCSV *env = malloc(sizeof(struct EnvCSV));
163  env->train_data = malloc(sizeof(struct Input));
164  env->test_data = malloc(sizeof(struct Input));
165  env_csv_input_read(filename, env->train_data, env->test_data);
166  xcsf->env = env;
167  const int x_dim = env->train_data->x_dim;
168  const int y_dim = env->train_data->y_dim;
169  param_init(xcsf, x_dim, y_dim, 1);
170 }
171 
176 void
177 env_csv_free(const struct XCSF *xcsf)
178 {
179  struct EnvCSV *env = xcsf->env;
180  free(env->train_data->x);
181  free(env->train_data->y);
182  free(env->test_data->x);
183  free(env->test_data->y);
184  free(env->train_data);
185  free(env->test_data);
186  free(env);
187 }
188 
193 void
194 env_csv_reset(const struct XCSF *xcsf)
195 {
196  (void) xcsf;
197 }
198 
204 bool
205 env_csv_is_done(const struct XCSF *xcsf)
206 {
207  (void) xcsf;
208  return true;
209 }
210 
216 const double *
218 {
219  (void) xcsf;
220  return 0;
221 }
222 
229 double
230 env_csv_execute(const struct XCSF *xcsf, const int action)
231 {
232  (void) xcsf;
233  (void) action;
234  return 0;
235 }
236 
242 bool
244 {
245  (void) xcsf;
246  return false;
247 }
248 
254 double
256 {
257  (void) xcsf;
258  return 0;
259 }
#define DELIM
File delimiter.
Definition: env_csv.c:30
bool env_csv_multistep(const struct XCSF *xcsf)
Returns whether the csv environment is a multistep problem.
Definition: env_csv.c:243
const double * env_csv_get_state(const struct XCSF *xcsf)
Dummy method since no state is returned by the csv environment.
Definition: env_csv.c:217
static int env_csv_samples(FILE *fin)
Returns the number of samples in a csv file.
Definition: env_csv.c:38
#define MAX_COLS
Maximum line length.
Definition: env_csv.c:28
static int env_csv_dim(FILE *fin)
Returns the number of dimensions in a csv file.
Definition: env_csv.c:54
static void env_csv_input_read(const char *infile, struct Input *train_data, struct Input *test_data)
Parses specified csv files into training and testing data sets.
Definition: env_csv.c:138
double env_csv_execute(const struct XCSF *xcsf, const int action)
Dummy method since no action is executed by the csv environment.
Definition: env_csv.c:230
bool env_csv_is_done(const struct XCSF *xcsf)
Returns whether the csv environment is in a terminal state.
Definition: env_csv.c:205
void env_csv_reset(const struct XCSF *xcsf)
Dummy method since no csv environment reset is necessary.
Definition: env_csv.c:194
void env_csv_init(struct XCSF *xcsf, const char *filename)
Initialises a CSV input environment from a specified filename.
Definition: env_csv.c:160
#define MAX_NAME
Maximum file name length.
Definition: env_csv.c:29
double env_csv_maxpayoff(const struct XCSF *xcsf)
Returns the maximum payoff value possible in the csv environment.
Definition: env_csv.c:255
static void env_csv_read(const char *filename, double **data, int *n_samples, int *n_dim)
Parses a specified csv file.
Definition: env_csv.c:110
static void env_csv_read_data(FILE *fin, double **data, const int n_samples, const int n_dim)
Reads the data from a csv file.
Definition: env_csv.c:80
void env_csv_free(const struct XCSF *xcsf)
Frees the csv environment.
Definition: env_csv.c:177
CSV input file handling functions.
Definition: __init__.py:1
void param_init(struct XCSF *xcsf, const int x_dim, const int y_dim, const int n_actions)
Initialises default XCSF parameters.
Definition: param.c:45
Functions for setting and printing parameters.
CSV environment data structure.
Definition: env_csv.h:32
struct Input * train_data
Definition: env_csv.h:33
struct Input * test_data
Definition: env_csv.h:34
Input data structure.
Definition: xcsf.h:146
double * x
Feature variables.
Definition: xcsf.h:147
int y_dim
Number of target variables.
Definition: xcsf.h:150
int x_dim
Number of feature variables.
Definition: xcsf.h:149
int n_samples
Number of instances.
Definition: xcsf.h:151
double * y
Target variables.
Definition: xcsf.h:148
XCSF data structure.
Definition: xcsf.h:85