NTRT Simulator  Version: Master
 All Classes Namespaces 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 #include "helpers/FileHelpers.h"
35 
36 using namespace std;
37 
38 AnnealAdapter::AnnealAdapter() :
39 totalTime(0.0)
40 {
41 }
42 AnnealAdapter::~AnnealAdapter(){};
43 
44 void AnnealAdapter::initialize(AnnealEvolution *evo,bool isLearning,configuration configdata)
45 {
46  numberOfActions=configdata.getDoubleValue("numberOfActions");
47  numberOfStates=configdata.getDoubleValue("numberOfStates");
48  numberOfControllers=configdata.getDoubleValue("numberOfControllers");
49  totalTime=0.0;
50 
51  //This Function initializes the parameterset from evo.
52  this->annealEvo = evo;
53  if(isLearning)
54  {
55  currentControllers = this->annealEvo->nextSetOfControllers();
56  }
57  else
58  {
59  currentControllers = this->annealEvo->nextSetOfControllers();
60  for(int i=0;i<currentControllers.size();i++)
61  {
62  stringstream ss;
63  ss << annealEvo->resourcePath << "logs/bestParameters-" << this->annealEvo->suffix << "-" << i << ".nnw";
64  currentControllers[i]->loadFromFile(ss.str().c_str());
65 // currentControllers[i]->getNn()->loadWeights(ss.str().c_str());
66  }
67  }
68  errorOfFirstController=0.0;
69 }
70 
71 vector<vector<double> > AnnealAdapter::step(double deltaTimeSeconds,vector<double> state)
72 {
73  totalTime+=deltaTimeSeconds;
74 // cout<<"NN adapter, state: "<<state[0]<<" "<<state[1]<<" "<<state[2]<<" "<<state[3]<<" "<<state[4]<<" "<<endl;
75  vector< vector<double> > actions;
76 
77  for(int i=0;i<currentControllers.size();i++)
78  {
79  vector<double> tmpAct;
80  for(int j=0;j<currentControllers[i]->statelessParameters.size();j++)
81  {
82  tmpAct.push_back(currentControllers[i]->statelessParameters[j]);
83  }
84  actions.push_back(tmpAct);
85  }
86 
87  return actions;
88 }
89 
90 void AnnealAdapter::endEpisode(vector<double> scores)
91 {
92  if(scores.size()==0)
93  {
94  vector< double > tmp(1);
95  tmp[0]=-1;
96  annealEvo->updateScores(tmp);
97  cout<<"Exploded"<<endl;
98  }
99  else
100  {
101  cout<<"Dist Moved: "<<scores[0]<<" energy: "<<scores[1]<<endl;
102 // double combinedScore=scores[0]*1.0-scores[1]*1.0;
103  annealEvo->updateScores(scores);
104  }
105  return;
106 }
std::string resourcePath
A class to read a learning configuration from a .ini file.
A series of functions to assist with file input/output.
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)