1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libann/ann.h,v 1.16 2017/01/12 14:43:23 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  * Copyright (C) 2008
33  *
34  * Mattia Mattaboni	<mattaboni@aero.polimi.it>
35  */
36 
37 #ifndef ANN_H
38 #define ANN_H
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif /* __cplusplus */
43 
44 #include "ActivationFunction.h"
45 #include "matrix.h"
46 
47 #define ANN_W_A_NONE		(0x0U)
48 #define ANN_W_A_TEXT		(0x1U)
49 #define ANN_W_A_BIN		(0x2U)
50 
51 #define ANN_FEEDBACK_NONE	(0x0U)
52 #define ANN_FEEDBACK_UPDATE	(0x1U)
53 
54 /* diagnostics */
55 typedef enum {
56         ANN_OK = 0,
57         ANN_NO_MEMORY,
58 	ANN_NO_FILE,
59 	ANN_DATA_ERROR,
60         ANN_MATRIX_ERROR,
61 	ANN_GEN_ERROR
62 } ann_res_t;
63 
64 typedef enum {
65 	ANN_TM_BATCH,
66 	ANN_TM_SEQUENTIAL
67 } ann_training_mode_t;
68 
69 typedef matrix*  ANN_vector_matrix;
70 typedef vector*  ANN_vector_vector;
71 
72 
73 /* Artificial Neural Network structure*/
74 typedef struct ANN {
75 	/* NEURON'S ACTIVATION FUNCTION */
76         w_init_f        	w_init;
77         w_destroy_f		w_destroy;
78         w_read_f  		w_read;
79         w_write_f       	w_write;
80         w_eval_f        	w_eval;
81         void            	*w_priv;
82 
83 	/* NETWORK ARCHITECTURE */
84         int 			N_input;    	/* network input number*/
85         int 			N_output;	/* network (visible) output number*/
86         int 			N_layer;    	/* network layer number*/
87         int 			*N_neuron;  	/* neuron number*/
88         int 			r;          	/* number of time delay*/
89 
90 	/* TRAINING PARAMETERS */
91         double 			eta;     	/* learning rate*/
92         double 			rho;     	/* momentum term*/
93 
94 	/* SYNAPTIC WEIGHTS */
95         ANN_vector_matrix 	W;    		/* network synaptic weights*/
96 
97 	/* JCOBIAN MATRIX */
98 	matrix			jacobian;
99 
100 	/* SCALE FACTORS */
101 	matrix			input_scale;	/* input scale factors */
102 	matrix			output_scale;	/* output scale factors */
103 
104 	/* SIMULATION DATA */
105         ANN_vector_vector	v;	     	/* neurons' internal activity*/
106         ANN_vector_vector	Y_neuron;  	/* neurons' output*/
107         vector 			yD;      	/* output */
108 
109 	/* TRAINING DATA */
110         ANN_vector_matrix 	dEdW;		/* error gradient*/
111         ANN_vector_matrix	**dy; 		/* output network gradient*/
112 
113 	/* PRIVATE TRAINING DATA */
114 	ANN_vector_matrix	*dydW, dW;
115 	ANN_vector_vector	dXdW, temp, dydV, dEdV, dXdu;
116 	vector 			input, output, error;
117 
118 
119 } ANN;
120 
121 
122 /* FUNCTIONS' PROTOTYPES  */
123 ann_res_t ANN_init( ANN *, const char * );
124 ann_res_t ANN_destroy( ANN * );
125 ann_res_t ANN_write( ANN *, FILE *, unsigned );
126 ann_res_t ANN_sim( ANN *, vector *, vector *, unsigned );
127 ann_res_t ANN_DataRead( matrix *, int *, char * );
128 ann_res_t ANN_DataWrite( matrix *, char * );
129 double ANN_InternalFunction( double , ANN * );
130 double ANN_InternalFunctionDer( double , ANN * );
131 ann_res_t ANN_vector_matrix_init( ANN_vector_matrix *, int *, int );
132 ann_res_t ANN_vector_vector_init( ANN_vector_vector *, int *, int );
133 ann_res_t ANN_dEdW( ANN *, vector *);
134 ann_res_t ANN_dXdW( ANN *, int , int , int );
135 ann_res_t ANN_WeightUpdate( ANN *, ANN_vector_matrix, double );
136 ann_res_t ANN_TrainingEpoch( ANN *, matrix *, matrix *, matrix *, int, ann_training_mode_t);
137 ann_res_t ANN_reset( ANN * );
138 ann_res_t ANN_TotalError( matrix *, matrix *, double *);
139 ann_res_t ANN_vector_matrix_ass( ANN_vector_matrix *, ANN_vector_matrix *, int* , int, double );
140 ann_res_t ANN_jacobian_matrix( ANN *, matrix * );
141 
142 void ANN_error( ann_res_t, char * );
143 
144 #ifdef __cplusplus
145 }
146 #endif /* __cplusplus */
147 
148 #endif /* ANN_H */
149