MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
ann_sim.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/ann_sim.c,v 1.16 2017/01/12 15:10:27 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /*
33  * Copyright (C) 2008
34  *
35  * Mattia Mattaboni <mattaboni@aero.polimi.it>
36  */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <math.h>
40 #include <getopt.h>
41 #include <string.h>
42 
43 #include "ann.h"
44 
45 static char *ANNfile = "data/ann.dat";
46 static char *INPUTfile = "data/Input.dat";
47 static char *NN_OUTPUTfile = "data/NNOutput.dat";
48 
49 void
51 {
52  fprintf(stdout, "\nUSAGE OPTIONS:\n"
53  " -u, --usage\n"
54  " print usage\n"
55  " -A, --ann\n"
56  " filename of initialized neural network (default data/ann.dat)\n"
57  " -I, --input\n"
58  " filename of network training input (default data/Input.dat)\n"
59  " -N, --nn_output\n"
60  " filename where to save trained neural network output (default data/NNOutput.dat)\n"
61  );
62  exit(0);
63 
64 }
65 
66 int
67 main(int argc, char *argv[])
68 {
69  ANN net;
70  matrix INPUT, NN_OUTPUT;
71  int i, j, N_sample;
72  int opt;
73  extern char *optarg;
74 
75 
76  /* 0. Training options */
77  do {
78 #ifdef HAVE_GETOPT_LONG
79  static struct option options[] = {
80  { "usage", 0, 0, 'u' },
81  { "ann", 1, 0, 'A' },
82  { "input", 1, 0, 'I' },
83  { "nn_output", 1, 0, 'N' }
84  };
85  opt = getopt_long(argc, argv, "uA:I:T:N:", options, NULL);
86 #else /* ! HAVE_GETOPT_LONG */
87  opt = getopt(argc, argv, "uA:I:T:N:");
88 #endif /* ! HAVE_GETOPT_LONG */
89  switch (opt) {
90  case 'u': print_usage();
91  break;
92  case 'A': ANNfile = optarg;
93  break;
94  case 'I': INPUTfile = optarg;
95  break;
96  case 'N': NN_OUTPUTfile = optarg;
97  break;
98  default: break;
99  }
100  } while (opt >= 0);
101 
102  /* Artificial Neural Network inizialization*/
103  printf("LOADING DATA...\n");
104  if (ANN_init(&net, ANNfile)) {
105  fprintf(stderr, "Initialization error\n");
106  return 1;
107  }
108  /* Input data acquisition*/
109  N_sample = 0;
110  if (ANN_DataRead(&INPUT, &N_sample, INPUTfile)) {
111  fprintf(stderr, "Data input acquisition error\n");
112  return 1;
113  }
114 
115 
116  if (matrix_init(&NN_OUTPUT, N_sample, net.N_output)) {
117  fprintf(stderr, "MAtrix initailization error\n");
118  return 1;
119  }
120  matrix_write(&INPUT, stdout, W_M_BIN);
121 
122  ANN_write(&net, stdout, ANN_W_A_TEXT);
123  fprintf(stdout, "SIMULATION....\n");
124  for (i = 0; i < N_sample; i++) {
125  /* aggiorno il vettore degli ingressi */
126  for (j = 0; j < net.N_input; j++) {
127  net.input.vec[j] = INPUT.mat[i][j];
128  }
129  /* simulo la rete */
130  if (ANN_sim(&net, &net.input, &net.output, ANN_FEEDBACK_UPDATE)) {
131  fprintf(stderr, "Network simulation error\n");
132  return 1;
133  }
134  /* aggiorno la matrice delle uscite */
135  for (j = 0; j < net.N_output; j++) {
136  NN_OUTPUT.mat[i][j] = net.output.vec[j];
137  }
138  }
139 
140  ANN_DataWrite(&NN_OUTPUT, NN_OUTPUTfile);
141 
142  /* dynamic memory free*/
143  ANN_destroy(&net);
144  matrix_destroy(&INPUT);
145  matrix_destroy(&NN_OUTPUT);
146 
147  return 0;
148 }
149 
void print_usage(void)
Definition: ann_sim.c:50
mat_res_t matrix_write(matrix *MAT, FILE *fh, unsigned flags)
Definition: matrix.c:467
ann_res_t ANN_DataWrite(matrix *MAT, char *FileName)
Definition: ann.c:504
ann_res_t ANN_write(ANN *net, FILE *fh, unsigned flags)
Definition: ann.c:341
ann_res_t ANN_sim(ANN *net, vector *input, vector *output, unsigned flags)
Definition: ann.c:425
int main(int argc, char *argv[])
Definition: ann_sim.c:67
ann_res_t ANN_DataRead(matrix *MAT, int *N_sample, char *FileName)
Definition: ann.c:475
int N_output
Definition: ann.h:85
ann_res_t ANN_init(ANN *net, const char *FileName)
Definition: ann.c:48
static char * NN_OUTPUTfile
Definition: ann_sim.c:47
#define W_M_BIN
Definition: matrix.h:48
Definition: matrix.h:61
vector input
Definition: ann.h:116
#define ANN_W_A_TEXT
Definition: ann.h:48
static char * ANNfile
Definition: ann_sim.c:45
vector output
Definition: ann.h:116
static char * INPUTfile
Definition: ann_sim.c:46
mat_res_t matrix_destroy(matrix *MAT)
Definition: matrix.c:84
#define ANN_FEEDBACK_UPDATE
Definition: ann.h:52
int N_input
Definition: ann.h:84
Definition: ann.h:74
double ** mat
Definition: matrix.h:62
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
double * vec
Definition: matrix.h:69
char * optarg
Definition: getopt.c:74
mat_res_t matrix_init(matrix *MAT, unsigned Nrow, unsigned Ncolumn)
Definition: matrix.c:43
ann_res_t ANN_destroy(ANN *net)
Definition: ann.c:218