NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
NeuroEvoMember.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 "NeuroEvoMember.h"
28 #include "neuralNet/Neural Network v2/neuralNetwork.h"
29 #include <fstream>
30 #include <iostream>
31 #include <assert.h>
32 #include <stdexcept>
33 
34 using namespace std;
35 
36 NeuroEvoMember::NeuroEvoMember(configuration config)
37 {
38  this->numInputs=config.getintvalue("numberOfStates");
39  this->numOutputs=config.getintvalue("numberOfActions");
40  int numHidden = config.getintvalue("numberHidden");
41  assert(numOutputs > 0);
42  cout<<"creating NN"<<endl;
43  if(numInputs>0)
44  nn = new neuralNetwork(numInputs, numHidden,numOutputs);
45  else
46  {
47  statelessParameters.resize(numOutputs);
48  for(int i=0;i<numOutputs;i++)
49  statelessParameters[i]=rand()*1.0/RAND_MAX;
50  }
51  maxScore=-1000;
52 }
53 
54 NeuroEvoMember::~NeuroEvoMember()
55 {
56  delete nn;
57 }
58 
59 void NeuroEvoMember::mutate(std::tr1::ranlux64_base_01 *eng){
60  std::tr1::uniform_real<double> unif(0, 1);
61  if(unif(*eng) > 0.5)
62  {
63  return;
64  }
65  //TODO: for each weight of the NN with 0.5 probability mutate it
66 
67  if(numInputs>0)
68  this->nn->mutate(eng);
69  else
70  {
71  double dev = 3.0 / 100.0; // 10 percent of interval 0-1
72  std::tr1::normal_distribution<double> normal(0, dev);
73  for(std::size_t i=0;i<statelessParameters.size();i++)
74  {
75  if(unif(*eng) > 0.5)
76  {
77  continue;
78  }
79  double mutAmount = normal(*eng);
80 // cout<<"param: "<<i<<" dev: "<<dev<<" rand: "<<mutAmount<<endl;
81  double newParam= statelessParameters[i] + mutAmount;
82  if(newParam < 0.0)
83  statelessParameters[i] = 0.0;
84  else if(newParam > 1.0)
85  statelessParameters[i] = 1.0;
86  else
87  statelessParameters[i] =newParam;
88  }
89 
90  }
91 }
92 
93 void NeuroEvoMember::copyFrom(NeuroEvoMember* otherMember)
94 {
95  if(numInputs>0)
96  {
97  this->nn->copyWeightFrom(otherMember->getNn());
98  this->maxScore=-10000;
99  this->pastScores.clear();
100  }
101  else
102  {
103  this->statelessParameters=otherMember->statelessParameters;
104  }
105 }
106 
107 void NeuroEvoMember::copyFrom(NeuroEvoMember *otherMember1, NeuroEvoMember *otherMember2, std::tr1::ranlux64_base_01 *eng)
108 {
109  if(numInputs>0)
110  {
111  this->nn->combineWeights(otherMember1->getNn(), otherMember2->getNn(), eng);
112  this->maxScore=-10000;
113  this->pastScores.clear();
114  }
115  else
116  {
117  std::tr1::uniform_real<double> unif(0, 1);
118  for (int i = 0; i < numOutputs; i++)
119  {
120  if (unif(*eng) > 0.5)
121  {
122  this->statelessParameters[i] = otherMember1->statelessParameters[i];
123  }
124  else
125  {
126  this->statelessParameters[i] = otherMember2->statelessParameters[i];
127  }
128  }
129  }
130 }
131 
132 void NeuroEvoMember::saveToFile(const char * outputFilename)
133 {
134  if(numInputs > 0 )
135  this->getNn()->saveWeights(outputFilename);
136  else
137  {
138  ofstream ss(outputFilename);
139  for(std::size_t i=0;i<statelessParameters.size();i++)
140  {
141  ss<<statelessParameters[i];
142  if(i!=statelessParameters.size()-1)
143  ss<<",";
144  }
145  ss.close();
146  }
147 }
148 
149 void NeuroEvoMember::loadFromFile(const char * outputFilename)
150 {
151  if(numInputs > 0 )
152  this->getNn()->loadWeights(outputFilename);
153  else
154  {
155  //cout<<"loading parameters from file "<<outputFilename<<endl;
156  ifstream ss(outputFilename);
157  int i=0;
158  string value;
159 #if (0)
160  // Disable definition of unused variable to suppress compiler warning
161  double valueDbl;
162 #endif
163  if(ss.is_open())
164  {
165  while(!ss.eof())
166  {
167  //cout<<"success opening file"<<endl;
168  if(getline ( ss, value, ',' )>0)
169  {
170  //cout<<"value read as string: "<<value<<endl;
171  statelessParameters[i++]=atof(value.c_str());
172  //cout<<statelessParameters[i-1]<<",";
173  }
174  }
175  //cout<<"reading complete"<<endl;
176  cout<<endl;
177  ss.close();
178  }
179  else
180  {
181  cout << "File of name " << outputFilename << " does not exist" << std::endl;
182  cout << "Try turning learning on in config.ini to generate parameters" << std::endl;
183  throw std::invalid_argument("Parameter file does not exist");
184  }
185  //cout<<"reading complete"<<endl;
186  //cout<<endl;
187  ss.close();
188  }
189 
190 }
Single set of params for NeuroEvolution.