TORCS  1.3.9
The Open Racing Car Simulator
ANN.h
Go to the documentation of this file.
1 /* -*- Mode: C++; -*- */
2 // VER: $Id$
3 // copyright (c) 2004 by Christos Dimitrakakis <dimitrak@idiap.ch>
4 /***************************************************************************
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  ***************************************************************************/
12 
13 #ifndef ANN_H
14 #define ANN_H
15 
16 #include <assert.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <math.h>
20 #include <learning/learn_debug.h>
21 #include <learning/string_utils.h>
22 #include <learning/List.h>
23 #include <learning/real.h>
24 
48 typedef struct Connection_ {
49  int c;
50  real w;
51  real dw;
52  real e;
53  real v;
54 } Connection;
55 
62 typedef struct RBFConnection_ {
63  real w;
64  real m;
66 
69 typedef struct Layer_ {
70  int n_inputs;
71  int n_outputs;
72  real* x;
73  real* y;
74  real* z;
75  real* d;
78  real a;
81  bool batch_mode;
82  void (*forward) (struct Layer_* current_layer, bool stochastic);
83  real (*backward) (LISTITEM* p, real* d, bool use_eligibility, real TD);
84  real (*f) (real x);
85  real (*f_d) (real x);
86 } Layer;
87 
89 typedef struct ANN_ {
90  int n_inputs;
91  int n_outputs;
92  LIST* c;
93  real* x;
94  real* y;
95  real* t;
96  real* d;
97  real a;
101  bool batch_mode;
103 } ANN;
104 
105 
106 /**************** User-level API ******************/
107 /* Object Management Interface */
108 extern ANN* NewANN(int n_inputs, int n_outputs);
109 extern int DeleteANN(ANN* ann);
110 extern ANN* LoadANN(char* filename);
111 extern ANN* LoadANN(FILE* f);
112 extern int SaveANN(ANN* ann, char* filename);
113 extern int SaveANN(ANN* ann, FILE* f);
114 
115 /* Setup Inteface */
116 extern int ANN_AddHiddenLayer(ANN* ann, int n_nodes);
117 extern int ANN_AddRBFHiddenLayer (ANN* ann, int n_nodes);
118 extern int ANN_Init(ANN* ann);
119 extern void ANN_SetOutputsToTanH(ANN* ann);
120 extern void ANN_SetOutputsToLinear(ANN* ann);
121 extern void ANN_SetLearningRate(ANN* ann, real a);
122 extern void ANN_SetLambda(ANN * ann, real lambda);
123 extern void ANN_SetZeta(ANN * ann, real lambda);
124 extern void ANN_Reset(ANN* ann);
125 
126 /* Functionality Interface */
127 extern real ANN_Input(ANN* ann, real* x);
128 extern real ANN_StochasticInput(ANN * ann, real * x);
129 extern real ANN_Train(ANN* ann, real* x, real* t);
130 extern real ANN_Delta_Train(ANN * ann, real* delta, real TD = 0.0);
131 extern void ANN_SetBatchMode(ANN* ann, bool batch);
132 extern void ANN_BatchAdapt(ANN* ann);
133 extern real ANN_Test(ANN* ann, real* x, real* t);
134 extern real* ANN_GetOutput(ANN* ann);
135 extern real ANN_GetError(ANN* ann);
136 extern real* ANN_GetErrorVector(ANN* ann);
137 
138 /********* Low-level code **********/
139 
140 /* Sub-object management functions */
141 extern Layer* ANN_AddLayer (ANN* ann, int n_inputs, int n_outputs, real* x);
142 extern Layer* ANN_AddRBFLayer (ANN* ann, int n_inputs, int n_outputs, real* x);
143 extern void ANN_FreeLayer (void* l);
144 extern void ANN_FreeLayer (Layer* l);
145 
146 /* Calculations */
147 extern void ANN_CalculateLayerOutputs (Layer* current_layer, bool stochastic=false);
148 extern real ANN_Backpropagate (LISTITEM* p, real* d, bool use_eligibility=false, real TD = 0.0);
149 extern void ANN_RBFCalculateLayerOutputs (Layer* current_layer, bool stochastic=false);
150 extern real ANN_RBFBackpropagate (LISTITEM* p, real* d, bool use_eligibility=false, real TD = 0.0);
151 extern void ANN_LayerBatchAdapt (Layer* l);
152 
153 /* Output functions and derivatives */
154 extern real Exp (real x);
155 extern real Exp_d (real x);
156 extern real htan (real x);
157 extern real htan_d (real x);
158 extern real dtan (real x);
159 extern real dtan_d (real x);
160 extern real linear (real x);
161 extern real linear_d (real x);
162 
163 /* Debugging functions */
164 extern real ANN_LayerShowWeights (Layer* l);
165 extern real ANN_ShowWeights(ANN* ann);
166 extern void ANN_ShowOutputs(ANN* ann);
167 extern real ANN_ShowInputs(ANN* ann);
168 extern real ANN_LayerShowInputs(Layer* l);
169 #endif /* ANN_H */
real dw
Weight-change.
Definition: ANN.h:51
void ANN_BatchAdapt(ANN *ann)
Adapt the parameters after a series of patterns has been seen.
Definition: ANN.cpp:923
real htan(real x)
Hyperbolic tangent hook.
Definition: ANN.cpp:1086
real e
eligibility;
Definition: ANN.h:52
real Exp(real x)
Exponential hook.
Definition: ANN.cpp:1068
A list item.
Definition: List.h:20
real * x
inputs;
Definition: ANN.h:72
Connection * c
connections
Definition: ANN.h:76
real ANN_GetError(ANN *ann)
Get the error for the current input/output pair.
Definition: ANN.cpp:833
real * z
activation
Definition: ANN.h:74
int ANN_AddRBFHiddenLayer(ANN *ann, int n_nodes)
Add an RBF layer with n_nodes.
Definition: ANN.cpp:131
void ANN_SetZeta(ANN *ann, real lambda)
Set zeta, parameter variance smoothing.
Definition: ANN.cpp:890
bool eligibility_traces
use eligibility
Definition: ANN.h:102
real ANN_LayerShowInputs(Layer *l)
Dump inputs to a particular layer on stdout.
Definition: ANN.cpp:996
real m
mean
Definition: ANN.h:64
void ANN_CalculateLayerOutputs(Layer *current_layer, bool stochastic=false)
Calculate layer outputs.
Definition: ANN.cpp:449
int c
connected?
Definition: ANN.h:49
real lambda
eligibility decay
Definition: ANN.h:79
void ANN_SetBatchMode(ANN *ann, bool batch)
Set batch updates.
Definition: ANN.cpp:906
real(* f_d)(real x)
derivative of activation function
Definition: ANN.h:85
real ANN_LayerShowWeights(Layer *l)
Dump the weights of a particular layer on stdout.
Definition: ANN.cpp:958
real ANN_ShowInputs(ANN *ann)
Dump inputs to all layers on stdout.
Definition: ANN.cpp:979
real linear(real x)
linear hook
Definition: ANN.cpp:1133
int ANN_Init(ANN *ann)
Initialise neural network.
Definition: ANN.cpp:346
real ANN_StochasticInput(ANN *ann, real *x)
Stochastically generate an output, depending on parameter distributions.
Definition: ANN.cpp:429
struct ANN_ ANN
ANN management structure.
RBFConnection * rbf
rbf connections (if any)
Definition: ANN.h:77
real w
weight
Definition: ANN.h:50
real ANN_RBFBackpropagate(LISTITEM *p, real *d, bool use_eligibility=false, real TD=0.0)
Backpropagation for an RBF layer.
Definition: ANN.cpp:727
real * d
delta vector
Definition: ANN.h:96
int ANN_AddHiddenLayer(ANN *ann, int n_nodes)
Add a hidden layer with n_nodes.
Definition: ANN.cpp:111
Some simple functions for string operations.
void ANN_RBFCalculateLayerOutputs(Layer *current_layer, bool stochastic=false)
Calculate layer outputs.
Definition: ANN.cpp:503
real(* backward)(LISTITEM *p, real *d, bool use_eligibility, real TD)
partial derivative calculation
Definition: ANN.h:83
ANN * LoadANN(char *filename)
Load an ANN from a filename.
Definition: ANN.cpp:1171
An RBF connection between two neural elements.
Definition: ANN.h:62
real ANN_Train(ANN *ann, real *x, real *t)
Perform mean square error training, where the aim is to minimise the cost function ...
Definition: ANN.cpp:544
real dtan(real x)
Discrete htan hook.
Definition: ANN.cpp:1105
A linear connection between two neural elements.
Definition: ANN.h:48
real * ANN_GetOutput(ANN *ann)
Get the output for the current input.
Definition: ANN.cpp:824
void ANN_ShowOutputs(ANN *ann)
Dump outputs to stdout.
Definition: ANN.cpp:1017
void ANN_FreeLayer(void *l)
Free this layer - low level.
Definition: ANN.cpp:313
void ANN_SetOutputsToTanH(ANN *ann)
Set outputs to hyperbolic tangent.
Definition: ANN.cpp:1050
void ANN_LayerBatchAdapt(Layer *l)
Perform batch adaptation.
Definition: ANN.cpp:773
int DeleteANN(ANN *ann)
Delete a neural network.
Definition: ANN.cpp:77
real ANN_Input(ANN *ann, real *x)
Give an input vector to the neural network.
Definition: ANN.cpp:406
real * x
unit inputs
Definition: ANN.h:93
struct Layer_ Layer
A collection of connections from one layer to another, plus management functions and data...
static Point p[4]
Definition: Convex.cpp:54
real zeta
variance update smoothness.
Definition: ANN.h:80
real * t
targets
Definition: ANN.h:95
void ANN_SetLearningRate(ANN *ann, real a)
Set the learning rate to a.
Definition: ANN.cpp:856
void ANN_SetLambda(ANN *ann, real lambda)
Set lambda, eligibility decay.
Definition: ANN.cpp:872
Layer * ANN_AddLayer(ANN *ann, int n_inputs, int n_outputs, real *x)
Low-level code to add a weighted sum layer.
Definition: ANN.cpp:152
real w
weight (= )
Definition: ANN.h:63
real(* f)(real x)
activation function
Definition: ANN.h:84
real zeta
variance update smoothness
Definition: ANN.h:99
real Exp_d(real x)
Exponential derivative hook.
Definition: ANN.cpp:1077
struct Connection_ Connection
A linear connection between two neural elements.
real * y
unit activations
Definition: ANN.h:94
void ANN_Reset(ANN *ann)
Resets the eligbility traces and batch updates.
Definition: ANN.cpp:379
ANN * NewANN(int n_inputs, int n_outputs)
Create a new ANN.
Definition: ANN.cpp:25
void ANN_SetOutputsToLinear(ANN *ann)
Set outputs to linear.
Definition: ANN.cpp:1033
bool batch_mode
do not update weights immediately
Definition: ANN.h:81
real a
learning rate
Definition: ANN.h:78
real * ANN_GetErrorVector(ANN *ann)
Return the error vector for pattern.
Definition: ANN.cpp:847
real * error
errors
Definition: ANN.h:100
real a
learning rate
Definition: ANN.h:97
real ANN_ShowWeights(ANN *ann)
Dump the weights on stdout.
Definition: ANN.cpp:941
bool batch_mode
use batch mode
Definition: ANN.h:101
real * d
derivatives
Definition: ANN.h:75
A collection of connections from one layer to another, plus management functions and data...
Definition: ANN.h:69
A very simple list structure.
Definition: List.h:37
int SaveANN(ANN *ann, char *filename)
Save the ANN to a filename.
Definition: ANN.cpp:1182
real dtan_d(real x)
Discrete htan derivative hook.
Definition: ANN.cpp:1119
int n_outputs
number of outputs
Definition: ANN.h:71
int n_inputs
number of inputs
Definition: ANN.h:90
int n_outputs
number of outputs
Definition: ANN.h:91
real v
variance estimate
Definition: ANN.h:53
void(* forward)(struct Layer_ *current_layer, bool stochastic)
forward calculation
Definition: ANN.h:82
real htan_d(real x)
Hyperbolic tangent derivative hook.
Definition: ANN.cpp:1095
real ANN_Backpropagate(LISTITEM *p, real *d, bool use_eligibility=false, real TD=0.0)
d are the derivatives at the outputs.
Definition: ANN.cpp:610
int n_inputs
number of inputs
Definition: ANN.h:70
real linear_d(real x)
linear derivative hook
Definition: ANN.cpp:1142
float real
Definition: real.h:13
Layer * ANN_AddRBFLayer(ANN *ann, int n_inputs, int n_outputs, real *x)
Low-level code to add an RBF layer.
Definition: ANN.cpp:240
real ANN_Test(ANN *ann, real *x, real *t)
Given an input and test pattern, return the MSE between the network&#39;s output and the test pattern...
Definition: ANN.cpp:802
real lambda
eligibility trace decay
Definition: ANN.h:98
ANN management structure.
Definition: ANN.h:89
real * y
outputs
Definition: ANN.h:73
struct RBFConnection_ RBFConnection
An RBF connection between two neural elements.
real ANN_Delta_Train(ANN *ann, real *delta, real TD=0.0)
Minimise a custom cost function.
Definition: ANN.cpp:584
LIST * c
connection layers
Definition: ANN.h:92