37 #ifndef SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_POLE_MARKOV_OBJECTIVE_FUNCTION 38 #define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_POLE_MARKOV_OBJECTIVE_FUNCTION 45 #include <shark/Models/FFNet.h> 56 template<
class H
iddenNeuron,
class OutputNeuron>
72 MarkovPole(
bool single_pole, std::size_t hidden,
bool shortcuts,
bool bias,
73 bool normalize =
true, std::size_t max_pole_evaluations = 100000)
74 : m_single(single_pole),
75 m_maxPoleEvals(max_pole_evaluations),
76 m_normalize(normalize) {
78 std::size_t inputs = 0;
91 if (bias && shortcuts){
92 m_dimensions = hidden * (inputs + 1) + inputs + hidden + 1;
95 m_dimensions = hidden * (inputs + 1) + inputs;
98 m_dimensions = hidden * (inputs + 1) + hidden + 1;
101 m_dimensions = hidden * (inputs + 1);
105 mp_net =
new FFNet<HiddenNeuron, OutputNeuron>();
106 FFNetStructures::ConnectionType type = shortcuts ?
107 FFNetStructures::InputOutputShortcut : FFNetStructures::Normal;
108 mp_net->setStructure(inputs, hidden, 1, type, bias);
111 if(m_dimensions != mp_net->numberOfParameters()) {
112 std::cerr <<
"Markov pole FFNet: Dimensions do not match, " << m_dimensions
113 <<
" != " << mp_net->numberOfParameters() << std::endl;
127 return "Objective Function for Markovian pole balancing.";
138 for(std::size_t i = 0; i != m_dimensions; i++) {
139 startingPoint(i) = 0.0;
141 return startingPoint;
153 return evalSingle(input);
156 return evalDouble(input);
165 double convertToPoleMovement(
double output)
const{
166 if (
typeid(mp_net->outputActivationFunction())
170 else if (
typeid(mp_net->outputActivationFunction())
172 return (output + 1.) / 2.;
174 else if (
typeid(mp_net->outputActivationFunction()) ==
typeid(
TanhNeuron)) {
175 return (output + 1.) / 2.;
178 std::cerr <<
"Unsupported neuron type in Markov pole FFNet." << std::endl;
187 double init_angle = 0.07;
190 RealVector output(1);
191 std::size_t eval_count = 0;
194 pole.
init(init_angle);
196 mp_net->setParameterVector(input);
198 while(!failed && eval_count < m_maxPoleEvals) {
200 mp_net->eval(state,output);
201 pole.
move(convertToPoleMovement(output(0)));
207 return m_maxPoleEvals - eval_count;
214 double init_angle = 0.07;
217 RealVector output(1);
218 std::size_t eval_count = 0;
221 pole.
init(init_angle);
222 mp_net->setParameterVector(input);
224 while(!failed && eval_count < m_maxPoleEvals) {
226 mp_net->eval(state,output);
227 pole.
move(convertToPoleMovement(output(0)));
233 return m_maxPoleEvals - eval_count;
241 std::size_t m_dimensions;
243 std::size_t m_maxPoleEvals;
246 FFNet<HiddenNeuron, OutputNeuron> *mp_net;
247 HiddenNeuron m_hiddenNeuron;
248 OutputNeuron m_outputNeuron;