NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
NeuroAdapter.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 
27 #include "NeuroAdapter.h"
29 #include "helpers/FileHelpers.h"
30 #include "neuralNet/Neural Network v2/neuralNetwork.h"
31 
32 #include <vector>
33 #include <iostream>
34 #include <fstream>
35 #include <sstream>
36 #include <assert.h>
37 
38 using namespace std;
39 
40 NeuroAdapter::NeuroAdapter() :
41 totalTime(0.0)
42 {
43 }
44 NeuroAdapter::~NeuroAdapter(){};
45 
46 void NeuroAdapter::initialize(NeuroEvolution *evo,bool isLearning,configuration configdata)
47 {
48  numberOfActions=configdata.getDoubleValue("numberOfActions");
49  numberOfStates=configdata.getDoubleValue("numberOfStates");
50  numberOfControllers=configdata.getDoubleValue("numberOfControllers");
51  totalTime=0.0;
52 
53  //This Function initializes the parameterset from evo.
54  this->neuroEvo = evo;
55  if(isLearning)
56  {
57  currentControllers = this->neuroEvo->nextSetOfControllers();
58  }
59  else
60  {
61  currentControllers = this->neuroEvo->nextSetOfControllers();
62  for(std::size_t i=0;i<currentControllers.size();i++)
63  {
64  stringstream ss;
65  ss<< neuroEvo->resourcePath << "logs/bestParameters-"<<this->neuroEvo->suffix<<"-"<<i<<".nnw";
66  currentControllers[i]->loadFromFile(ss.str().c_str());
67  }
68  }
69  errorOfFirstController=0.0;
70 }
71 
72 vector<vector<double> > NeuroAdapter::step(double deltaTimeSeconds,vector<double> state)
73 {
74  totalTime+=deltaTimeSeconds;
75 // cout<<"NN adapter, state: "<<state[0]<<" "<<state[1]<<" "<<state[2]<<" "<<state[3]<<" "<<state[4]<<" "<<endl;
76  vector< vector<double> > actions;
77  if(numberOfStates>0)
78  {
79  double *inputs = new double[numberOfStates];
80 
81  //scale inputs to 0-1 from -1 to 1 (unit vector provided from the controller).
82  // Assumes inputs are already scaled -1 to 1
83  assert (state.size() == numberOfStates);
84  for (int i = 0; i < numberOfStates; i++)
85  {
86  inputs[i]=state[i] / 2.0 + 0.5;
87  }
88  for(std::size_t i=0;i<currentControllers.size();i++)
89  {
90  double *output=currentControllers[i]->getNn()->feedForwardPattern(inputs);
91  vector<double> tmpAct;
92  for(int j=0;j<numberOfActions;j++)
93  {
94  tmpAct.push_back(output[j]);
95  }
96  actions.push_back(tmpAct);
97  }
98  delete[]inputs;
99  }
100  else
101  {
102  for(std::size_t i=0;i<currentControllers.size();i++)
103  {
104  vector<double> tmpAct;
105  for(std::size_t j=0;j<currentControllers[i]->statelessParameters.size();j++)
106  {
107  tmpAct.push_back(currentControllers[i]->statelessParameters[j]);
108  }
109  actions.push_back(tmpAct);
110  }
111  }
112 
113 
114  return actions;
115 }
116 
117 void NeuroAdapter::endEpisode(vector<double> scores)
118 {
119  if(scores.size()==0)
120  {
121  vector< double > tmp(1);
122  tmp[0]=-1;
123  neuroEvo->updateScores(tmp);
124  cout<<"Exploded"<<endl;
125  }
126  else
127  {
128  cout<<"Dist Moved: "<<scores[0]<<" energy: "<<scores[1]<<endl;
129 // double combinedScore=scores[0]*1.0-scores[1]*1.0;
130  neuroEvo->updateScores(scores);
131  }
132  return;
133 }
void initialize(NeuroEvolution *evo, bool isLearning, configuration config)
A class to read a learning configuration from a .ini file.
A series of functions to assist with file input/output.
Defines a class NeuroAdapter to pass parameters from NeuroEvolution to a controller.
std::string resourcePath