69 MarkovPole(
bool single_pole, std::size_t hidden,
bool shortcuts,
bool bias,
70 bool normalize =
true, std::size_t max_pole_evaluations = 100000)
71 : m_single(single_pole),
72 m_maxPoleEvals(max_pole_evaluations),
73 m_normalize(normalize) {
75 std::size_t inputs = 0;
88 if (bias && shortcuts){
89 m_dimensions = hidden * (inputs + 1) + inputs + hidden + 1;
92 m_dimensions = hidden * (inputs + 1) + inputs;
95 m_dimensions = hidden * (inputs + 1) + hidden + 1;
98 m_dimensions = hidden * (inputs + 1);
102 mp_net =
new FFNet<HiddenNeuron, OutputNeuron>();
103 FFNetStructures::ConnectionType type = shortcuts ?
104 FFNetStructures::InputOutputShortcut : FFNetStructures::Normal;
105 mp_net->setStructure(inputs, hidden, 1, type, bias);
108 if(m_dimensions != mp_net->numberOfParameters()) {
109 std::cerr <<
"Markov pole FFNet: Dimensions do not match, " << m_dimensions
110 <<
" != " << mp_net->numberOfParameters() << std::endl;
124 return "Objective Function for Markovian pole balancing.";
135 for(std::size_t i = 0; i != m_dimensions; i++) {
136 startingPoint(i) = 0.0;
138 return startingPoint;
150 return evalSingle(input);
153 return evalDouble(input);
162 double convertToPoleMovement(
double output)
const{
163 if (
typeid(mp_net->outputActivationFunction())
167 else if (
typeid(mp_net->outputActivationFunction())
169 return (output + 1.) / 2.;
171 else if (
typeid(mp_net->outputActivationFunction()) ==
typeid(TanhNeuron)) {
172 return (output + 1.) / 2.;
175 std::cerr <<
"Unsupported neuron type in Markov pole FFNet." << std::endl;
184 double init_angle = 0.07;
185 SinglePole pole(
true, m_normalize);
187 RealVector output(1);
188 std::size_t eval_count = 0;
191 pole.init(init_angle);
193 mp_net->setParameterVector(input);
195 while(!failed && eval_count < m_maxPoleEvals) {
196 pole.getState(state);
197 mp_net->eval(state,output);
198 pole.move(convertToPoleMovement(output(0)));
199 failed = pole.failure();
204 return m_maxPoleEvals - eval_count;
211 double init_angle = 0.07;
212 DoublePole pole(
true, m_normalize);
214 RealVector output(1);
215 std::size_t eval_count = 0;
218 pole.init(init_angle);
219 mp_net->setParameterVector(input);
221 while(!failed && eval_count < m_maxPoleEvals) {
222 pole.getState(state);
223 mp_net->eval(state,output);
224 pole.move(convertToPoleMovement(output(0)));
225 failed = pole.failure();
230 return m_maxPoleEvals - eval_count;
238 std::size_t m_dimensions;
240 std::size_t m_maxPoleEvals;
243 FFNet<HiddenNeuron, OutputNeuron> *mp_net;
244 HiddenNeuron m_hiddenNeuron;
245 OutputNeuron m_outputNeuron;