/** \file
  training a neural network

  it uses
  Fast artificial neural network library (http://fann.sourceforge.net)...
  load existing network, read input, calc output
  */

#include "local.h"

/** feed it with input-data and a trained ANN it outputs the calculated data */
int main(int argc, char * argv[]) {
	struct fann * ann;
	int year;
	int i;
	fann_type input[30];
	fann_type * output;
	fann_type expected_output;
#ifndef FLOATFANN
	/* assertion that we use float as fann_type */
#error "to avoid type conflicts, the float-variant of libfann must be used"
#endif
	if (3!=argc) {
		fprintf(stderr,"run_net\n");
		fprintf(stderr,"  reads data stream from file in FANN DAT format, and loads a\n");
		fprintf(stderr,"  neural network. On STDOUT calculated data will be written\n");
		fprintf(stderr,"call\n %s <network-file> <testdat>\n", argv[0]);
		exit(ERRARG);
	}
	/* create or load neural network */
	ann = fann_create_from_file(argv[1]);
	if (ann == NULL) {
		fprintf(stderr, "could not create network from file %s: %s\n", argv[1], strerror(errno));
		exit(ERRFANN);
	}
	net.num_input=fann_get_num_input(ann); /* -1 because on-neuron for bias should not be counted */
	fprintf(stderr, "total neurons: %u, total connections: %u\n", fann_get_total_neurons(ann), fann_get_total_connections(ann));
	net.test_data = fann_read_train_from_file(argv[2]);	
	fprintf(stderr, "input:%i output:%i\n", net.test_data->num_input, net.test_data->num_output);
	printf("#year,expected_data,predicted_data\n");
	for (year=0; year < net.test_data->num_data; year++) {
		for (i=0; i< 30; i++) {
			input[i]= net.test_data->input[year][i];
			//printf("test %i %lf\n", i, input[i]);
		}
		output=fann_run(ann, input);
		expected_output= net.test_data->output[year][0];
		printf("%i %lf %lf\n", year, (double) (expected_output), ((double) output[0]));
	}
	fann_destroy(ann);
	(void) fflush(stdout);
	(void) fflush(stderr);
	/* destroy network */
	return(0);
}


