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