NTRT Simulator
 All Classes Files Functions Variables Typedefs Friends Pages
AnnealAdapter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The NASA Tensegrity Robotics Toolkit (NTRT) v1 platform is licensed
7  * under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * http://www.apache.org/licenses/LICENSE-2.0.
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language
16  * governing permissions and limitations under the License.
17 */
18 
28 #include <vector>
29 #include <iostream>
30 #include <sstream>
31 #include <fstream>
32 #include "AnnealAdapter.h"
34 
35 using namespace std;
36 
37 AnnealAdapter::AnnealAdapter() :
38 totalTime(0.0)
39 {
40 }
41 AnnealAdapter::~AnnealAdapter(){};
42 
43 void AnnealAdapter::initialize(AnnealEvolution *evo,bool isLearning,configuration configdata)
44 {
45  numberOfActions=configdata.getDoubleValue("numberOfActions");
46  numberOfStates=configdata.getDoubleValue("numberOfStates");
47  numberOfControllers=configdata.getDoubleValue("numberOfControllers");
48  totalTime=0.0;
49 
50  //This Function initializes the parameterset from evo.
51  this->annealEvo = evo;
52  if(isLearning)
53  {
54  currentControllers = this->annealEvo->nextSetOfControllers();
55  }
56  else
57  {
58  currentControllers = this->annealEvo->nextSetOfControllers();
59  for(int i=0;i<currentControllers.size();i++)
60  {
61  stringstream ss;
62  ss<<"logs/bestParameters-"<<this->annealEvo->suffix<<"-"<<i<<".nnw";
63  currentControllers[i]->loadFromFile(ss.str().c_str());
64 // currentControllers[i]->getNn()->loadWeights(ss.str().c_str());
65  }
66  }
67  errorOfFirstController=0.0;
68 }
69 
70 vector<vector<double> > AnnealAdapter::step(double deltaTimeSeconds,vector<double> state)
71 {
72  totalTime+=deltaTimeSeconds;
73 // cout<<"NN adapter, state: "<<state[0]<<" "<<state[1]<<" "<<state[2]<<" "<<state[3]<<" "<<state[4]<<" "<<endl;
74  vector< vector<double> > actions;
75 
76  for(int i=0;i<currentControllers.size();i++)
77  {
78  vector<double> tmpAct;
79  for(int j=0;j<currentControllers[i]->statelessParameters.size();j++)
80  {
81  tmpAct.push_back(currentControllers[i]->statelessParameters[j]);
82  }
83  actions.push_back(tmpAct);
84  }
85 
86  return actions;
87 }
88 
89 void AnnealAdapter::endEpisode(vector<double> scores)
90 {
91  if(scores.size()==0)
92  {
93  vector< double > tmp(1);
94  tmp[0]=-1;
95  annealEvo->updateScores(tmp);
96  cout<<"Exploded"<<endl;
97  }
98  else
99  {
100  cout<<"Dist Moved: "<<scores[0]<<" energy: "<<scores[1]<<endl;
101 // double combinedScore=scores[0]*1.0-scores[1]*1.0;
102  annealEvo->updateScores(scores);
103  }
104  return;
105 }
A class to read a learning configuration from a .ini file.
Defines a class AnnealAdapter to pass parameters from AnnealEvolution to a controller. Adapting NeuroEvolution to do Simulated Annealing.
void initialize(AnnealEvolution *evo, bool isLearning, configuration config)