37 #ifndef SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_POLE_NONMARKOV_OBJECTIVE_FUNCTION 38 #define SHARK_OBJECTIVEFUNCTIONS_BENCHMARKS_POLE_NONMARKOV_OBJECTIVE_FUNCTION 44 #include <shark/Models/OnlineRNNet.h> 69 RecurrentStructure::SigmoidType sigmoidType = RecurrentStructure::FastSigmoid,
70 bool normalize =
true,
71 std::size_t max_pole_evaluations = 100000)
73 m_maxPoleEvals(max_pole_evaluations),
74 m_normalize(normalize) {
75 if (sigmoidType == RecurrentStructure::Linear) {
76 std::cerr <<
"Cannot use linear activation function for pole balancing." 82 std::size_t inputs = 0;
96 m_dimensions = (hidden + 1) * (hidden + 1) +
97 inputs * (hidden + 1) + hidden + 1;
100 m_dimensions = (hidden + 1) * (hidden + 1) + inputs * (hidden + 1);
104 mp_struct =
new RecurrentStructure();
105 mp_struct->setStructure(inputs, hidden, 1, bias, sigmoidType);
106 mp_net =
new PoleRNNet(mp_struct);
109 if(m_dimensions != mp_net->numberOfParameters()) {
110 std::cerr <<
"Non-Markov pole RNNet: Dimensions do not match, " 111 << m_dimensions <<
" != " << mp_net->numberOfParameters() << std::endl;
126 return "Objective Function for Non-Markovian pole balancing.";
137 for(std::size_t i = 0; i != m_dimensions; i++) {
138 startingPoint(i) = 0.0;
140 return startingPoint;
152 return evalSingle(input);
155 return evalDouble(input);
162 class PoleRNNet :
public OnlineRNNet {
164 PoleRNNet(RecurrentStructure* structure) : OnlineRNNet(structure){}
165 boost::shared_ptr<State> createState()
const{
166 throw std::logic_error(
"State not available for PoleRNNet.");
168 void eval(BatchInputType
const & patterns, BatchOutputType &outputs,
170 throw std::logic_error(
"Batch not available for PoleRNNet.");
177 double convertToPoleMovement(
double output)
const{
178 switch(mp_struct->sigmoidType())
180 case RecurrentStructure::Logistic:
182 case RecurrentStructure::FastSigmoid:
183 return (output + 1.) / 2.;
184 case RecurrentStructure::Tanh:
185 return (output + 1.) / 2.;
187 std::cerr <<
"Unsupported activation function for pole balancing." << std::endl;
197 double init_angle = 0.07;
200 RealMatrix output(1,1);
201 RealMatrix inState(1,2);
202 std::size_t eval_count = 0;
205 pole.
init(init_angle);
206 mp_net->resetInternalState();
207 mp_net->setParameterVector(input);
209 while(!failed && eval_count < m_maxPoleEvals) {
211 row(inState,0) = state;
212 mp_net->eval(inState,output);
213 pole.
move(convertToPoleMovement(output(0,0)));
219 return m_maxPoleEvals - eval_count;
226 double init_angle = 0.07;
229 RealMatrix output(1,1);
230 RealMatrix inState(1,3);
231 std::size_t eval_count = 0;
234 pole.
init(init_angle);
235 mp_net->resetInternalState();
236 mp_net->setParameterVector(input);
238 while(!failed && eval_count < m_maxPoleEvals) {
240 row(inState,0) = state;
241 mp_net->eval(inState,output);
242 pole.
move(convertToPoleMovement(output(0,0)));
247 return m_maxPoleEvals - eval_count;
255 std::size_t m_dimensions;
257 std::size_t m_maxPoleEvals;
260 RecurrentStructure *mp_struct;