NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
AnnealEvoMember.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 "AnnealEvoMember.h"
29 #include <fstream>
30 #include <iostream>
31 #include <assert.h>
32 #include <stdexcept>
33 
34 using namespace std;
35 
36 AnnealEvoMember::AnnealEvoMember(configuration config)
37 {
38  //readConfigFromXML(configFile);
39  this->numOutputs=config.getintvalue("numberOfActions");
40  this->devBase=config.getDoubleValue("deviation");
41  this->monteCarlo=config.getintvalue("MonteCarlo");
42 
43  statelessParameters.resize(numOutputs);
44  for(int i=0;i<numOutputs;i++)
45  statelessParameters[i]=rand()*1.0/RAND_MAX;
46 
47  maxScore=-1000;
48 }
49 
50 AnnealEvoMember::~AnnealEvoMember()
51 {
52 }
53 
54 void AnnealEvoMember::mutate(std::tr1::ranlux64_base_01 *eng, double T){
55 
56  assert (T <= 1.0);
57  std::tr1::uniform_real<double> unif(0, 1);
58 
59  //TODO: for each weight of the NN with 0.5 probability mutate it
60 
61  double dev = devBase * T / 100.0;
62  std::tr1::normal_distribution<double> normal(0, dev);
63  for(std::size_t i=0;i<statelessParameters.size();i++)
64  {
65  double newParam;
66  if (monteCarlo)
67  {
68  newParam= unif(*eng);
69  }
70  else
71  {
72  double mutAmount = normal(*eng);
73  //std::cout<<"param: "<<i<<" dev: "<<dev<<" rand: "<<mutAmount<<endl;
74  newParam= statelessParameters[i] + mutAmount;
75  }
76 
77  if(newParam < 0.0)
78  statelessParameters[i] = 0.0;
79  else if(newParam > 1.0)
80  statelessParameters[i] = 1.0;
81  else
82  statelessParameters[i] =newParam;
83  }
84 
85 
86 }
87 
88 void AnnealEvoMember::copyFrom(AnnealEvoMember* otherMember)
89 {
90 
91  this->statelessParameters=otherMember->statelessParameters;
92 
93 }
94 
95 void AnnealEvoMember::saveToFile(const char * outputFilename)
96 {
97 
98  ofstream ss(outputFilename);
99  for(std::size_t i=0;i<statelessParameters.size();i++)
100  {
101  ss<<statelessParameters[i];
102  if(i!=statelessParameters.size()-1)
103  ss<<",";
104  }
105  ss.close();
106 
107 }
108 
109 void AnnealEvoMember::loadFromFile(const char * outputFilename)
110 {
111  //cout<<"loading parameters from file "<<outputFilename<<endl;
112  ifstream ss(outputFilename);
113  int i=0;
114  string value;
115 #if (0)
116  // Disable definition of unused variable to suppress compiler warning
117  double valueDbl;
118 #endif
119  if(ss.is_open())
120  {
121  while(!ss.eof())
122  {
123  //cout<<"success opening file"<<endl;
124  // @todo fix the infinite loop that occurs here!
125  if(getline ( ss, value, ',' )>0)
126  {
127  //cout<<"value read as string: "<<value<<endl;
128  statelessParameters[i++]=atof(value.c_str());
129  //cout<<statelessParameters[i-1]<<",";
130  }
131  }
132  }
133  else
134  {
135  cout << "File of name " << outputFilename << " does not exist" << std::endl;
136  cout << "Try turning learning on in config.ini to generate parameters" << std::endl;
137  throw std::invalid_argument("Parameter file does not exist");
138  }
139  //cout<<"reading complete"<<endl;
140  //cout<<endl;
141  ss.close();
142 
143 }
Contains the definition of class AnnealEvoMember Adapting NeuroEvolution to do Simulated Annealing...