TORCS  1.3.10
The Open Racing Car Simulator
Loading...
Searching...
No Matches
ann_policy.cpp
Go to the documentation of this file.
1// -*- Mode: c++ -*-
2// copyright (c) 2004 by Christos Dimitrakakis <dimitrak@idiap.ch>
3// $Id$
4
5
6/***************************************************************************
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 ***************************************************************************/
14
15#include <learning/ann_policy.h>
16
17ANN_Policy::ANN_Policy (int n_states, int n_actions, int n_hidden, real alpha, real gamma, real lambda, bool eligibility, bool softmax, real randomness, real init_eval, bool separate_actions) : DiscretePolicy (n_states, n_actions, alpha, gamma, lambda, softmax, randomness, init_eval)
18{
19 this->separate_actions = separate_actions;
20 this->eligibility = eligibility;
21 if (eligibility) {
22 message ("Using eligibility traces");
23 }
24 if (separate_actions) {
25 message ("Separate actions");
26 J = NULL;
27 Ja = new ANN* [n_actions];
28 JQs = new real [n_actions];
29 for (int i=0; i<n_actions; i++) {
30 Ja[i] = NewANN (n_states, 1);
31 if (n_hidden > 0) {
32 ANN_AddHiddenLayer (Ja[i], n_hidden);
33 }
34 ANN_Init (Ja[i]);
36 ANN_SetBatchMode(Ja[i], false);
37 Ja[i]->eligibility_traces = eligibility;
40 }
41 } else {
42 JQs = NULL;
43 Ja = NULL;
45 if (n_hidden > 0) {
46 ANN_AddHiddenLayer (J, n_hidden);
47 }
48 ANN_Init (J);
50 ANN_SetBatchMode(J, false);
51 J->eligibility_traces = eligibility;
54 }
55 ps = new real [n_states];
57 J_ps_pa = 0.0;
58}
59
61{
62 delete [] ps;
63 delete [] delta_vector;
64 if (separate_actions) {
65 for (int i=0; i<n_actions; i++) {
66 DeleteANN(Ja[i]);
67 }
68 delete [] Ja;
69 } else {
70 //ANN_ShowWeights(J);
71 DeleteANN (J);
72 }
73}
74
75int ANN_Policy::SelectAction (int s, real r, int forced_a)
76{
77 return DiscretePolicy::SelectAction(s, r, forced_a);
78}
79
80int ANN_Policy::SelectAction (real* s, real r, int forced_a)
81{
82 int a; // selected action
83 int amax; //maximum evaluated action
84 real* Q_s; // pointer to evaluations for state s
85 if (confidence) {
86 if (separate_actions) {
87 for (int i=0; i<n_actions; i++) {
88 ANN_StochasticInput (Ja[i], s);
89 JQs[i] = ANN_GetOutput(Ja[i])[0];
90 }
91 Q_s = JQs;
92 } else {
94 Q_s = ANN_GetOutput (J);
95 }
96 } else {
97 if (separate_actions) {
98 for (int i=0; i<n_actions; i++) {
99 ANN_Input (Ja[i], s);
100 JQs[i] = ANN_GetOutput(Ja[i])[0];
101 }
102 Q_s = JQs;
103 } else {
104 ANN_Input (J, s);
105 Q_s = ANN_GetOutput (J);
106 }
107 }
108 int argmax = argMax (Q_s);
109
110 if (forced_learning) {
111 a = forced_a;
112 } else if (confidence) {
113 a = argmax;
114 } else if (smax) {
115 a = softMax (Q_s);
116 //printf ("Q[%d][%d]=%f\n", s, a, Q[s][a]);
117 } else {
118 a = eGreedy (Q_s);
119 }
120
121 if (a<0 || a>=n_actions) {
122 fprintf (stderr, "Action %d out of bounds\n", a);
123 }
124
125 switch (learning_method) {
126
127 case Sarsa:
128 amax = a;
129 break;
130 case QLearning:
131 amax = argmax;
132 break;
133 default:
134 amax = a;
135 fprintf (stderr, "Unknown learning method\n");
136 }
137 if (pa>=0) { // do not update at start of episode
138 real delta = r + gamma*Q_s[amax] - J_ps_pa;
139 tdError = delta;
140 for (int j=0; j<n_actions; j++) {
141 delta_vector[j] = 0.0;
142 }
143 if (separate_actions) {
144 if (eligibility) {
145 delta_vector[0] = 1.0;
147 // Reset other actions' traces.
148 for (int i=0; i<n_actions; i++) {
149 if (i!=pa) {
150 ANN_Reset(Ja[i]);
151 }
152 }
153 } else {
154 delta_vector[0] = delta;
156 }
157 } else {
158 if (J->eligibility_traces) {
159 delta_vector[pa] = 1.0;
161 } else {
162 delta_vector[pa] = delta;
164 }
165 }
166
167
168 }
169
170 //printf ("%d %d #STATE\n", min_el_state, max_el_state);
171 // printf ("Q[%d,%d]=%f r=%f e=%f ad=%f gl=%f #QV\n",
172 // ps, pa, Q[ps][pa], r, e[ps][pa], ad, gl);
173
174 J_ps_pa = Q_s[a];
175 pa = a;
176
177 return a;
178}
179
181{
182 if (separate_actions) {
183 for (int i=0; i<n_actions; i++) {
184 ANN_Reset (Ja[i]);
185 }
186 } else {
187 ANN_Reset(J);
188 }
189}
190
195
199 this->confidence = confidence;
200 this->zeta = zeta;
201 if (separate_actions) {
202 for (int i=0; i<n_actions; i++) {
203 ANN_SetZeta(Ja[i], zeta);
204 }
205 } else {
207 }
208 if (confidence) {
209 logmsg ("#+[CONDIFENCE]\n");
210 } else {
211 logmsg ("#-[CONDIFENCE]\n");
212 }
213
214 return confidence;
215}
int ANN_AddHiddenLayer(ANN *ann, int n_nodes)
Add a hidden layer with n_nodes.
Definition ANN.cpp:111
void ANN_SetLearningRate(ANN *ann, real a)
Set the learning rate to a.
Definition ANN.cpp:856
ANN * NewANN(int n_inputs, int n_outputs)
Create a new ANN.
Definition ANN.cpp:25
real ANN_Input(ANN *ann, real *x)
Give an input vector to the neural network.
Definition ANN.cpp:406
int ANN_Init(ANN *ann)
Initialise neural network.
Definition ANN.cpp:346
void ANN_SetLambda(ANN *ann, real lambda)
Set lambda, eligibility decay.
Definition ANN.cpp:872
void ANN_SetBatchMode(ANN *ann, bool batch)
Set batch updates.
Definition ANN.cpp:906
void ANN_Reset(ANN *ann)
Resets the eligbility traces and batch updates.
Definition ANN.cpp:379
void ANN_SetZeta(ANN *ann, real zeta)
Set zeta, parameter variance smoothing.
Definition ANN.cpp:890
void ANN_SetOutputsToLinear(ANN *ann)
Set outputs to linear.
Definition ANN.cpp:1033
real ANN_Delta_Train(ANN *ann, real *delta, real TD)
Minimise a custom cost function.
Definition ANN.cpp:584
int DeleteANN(ANN *ann)
Delete a neural network.
Definition ANN.cpp:77
real ANN_StochasticInput(ANN *ann, real *x)
Stochastically generate an output, depending on parameter distributions.
Definition ANN.cpp:429
real * ANN_GetOutput(ANN *ann)
Get the output for the current input.
Definition ANN.cpp:824
struct ANN_ ANN
ANN management structure.
real J_ps_pa
Evaluation of last action.
Definition ann_policy.h:42
virtual int SelectAction(int s, real r, int forced_a=-1)
Keep the base discrete-state overload visible for callers using DiscretePolicy's interface.
ANN * J
Evaluation network.
Definition ann_policy.h:38
virtual void Reset()
Reset eligibility traces.
bool eligibility
eligibility option
Definition ann_policy.h:44
ANN_Policy(int n_states, int n_actions, int n_hidden=0, real alpha=0.1, real gamma=0.8, real lambda=0.8, bool eligibility=false, bool softmax=false, real randomness=0.1, real init_eval=0.0, bool separate_actions=false)
Make a new policy.
ANN ** Ja
Evaluation networks (for separate_actions case)
Definition ann_policy.h:39
virtual bool useConfidenceEstimates(bool confidence, real zeta, bool confidence_eligibility)
Use confidence estimates with the base class signature.
real * JQs
Placeholder for evaluation vector (separate_actions)
Definition ann_policy.h:41
bool separate_actions
Single/separate evaluation option.
Definition ann_policy.h:45
real * ps
Previous state vector.
Definition ann_policy.h:40
virtual ~ANN_Policy()
real * delta_vector
Scratch vector for TD error.
Definition ann_policy.h:43
int n_states
number of states
Definition policy.h:149
int softMax(real *Qs)
Softmax Gibbs sampling.
Definition policy.cpp:783
int argMax(real *Qs)
Get ID of maximum action.
Definition policy.cpp:816
real tdError
temporal difference error
Definition policy.h:160
DiscretePolicy(int n_states, int n_actions, real alpha=0.1, real gamma=0.8, real lambda=0.8, bool softmax=false, real randomness=0.1, real init_eval=0.0)
Create a new discrete policy.
Definition policy.cpp:42
int n_actions
number of actions
Definition policy.h:150
virtual int SelectAction(int s, real r, int forced_a=-1)
Select an action a, given state s and reward from previous action.
Definition policy.cpp:283
real zeta
Confidence smoothing.
Definition policy.h:179
enum LearningMethod learning_method
learning method to use;
Definition policy.h:148
bool forced_learning
Force agent to take supplied action.
Definition policy.h:173
real lambda
Eligibility trace decay.
Definition policy.h:165
int eGreedy(real *Qs)
e-greedy sampling
Definition policy.cpp:802
bool confidence
Confidence estimates option.
Definition policy.h:174
real alpha
learning rate
Definition policy.h:166
bool smax
softmax option
Definition policy.h:161
real r
reward
Definition policy.h:158
real gamma
Future discount parameter.
Definition policy.h:164
bool confidence_eligibility
Apply eligibility traces to confidence.
Definition policy.h:175
int pa
previous action
Definition policy.h:157
#define logmsg
Definition learn_debug.h:17
@ QLearning
Definition policy.h:140
@ Sarsa
Definition policy.h:140
float real
Definition real.h:13
void message(const char *msg,...)
Prints a message.