NTRT Simulator  Version: Master
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
AnnealEvoPopulation.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 "AnnealEvoPopulation.h"
29 #include <string>
30 #include <vector>
31 #include <iostream>
32 #include <numeric>
33 #include <fstream>
34 #include <algorithm>
35 
36 using namespace std;
37 
38 AnnealEvoPopulation::AnnealEvoPopulation(int populationSize,configuration config)
39 {
40  compareAverageScores=true;
41  clearScoresBetweenGenerations=false;
42  this->compareAverageScores=config.getintvalue("compareAverageScores");
43  this->clearScoresBetweenGenerations=config.getintvalue("clearScoresBetweenGenerations");
44 
45  for(int i=0;i<populationSize;i++)
46  {
47  //cout<<" creating members"<<endl;
48  controllers.push_back(new AnnealEvoMember(config));
49  }
50 }
51 
52 AnnealEvoPopulation::~AnnealEvoPopulation()
53 {
54  for(std::size_t i=0;i<controllers.size();i++)
55  {
56  delete controllers[i];
57  }
58 }
59 
60 void AnnealEvoPopulation::mutate(std::tr1::ranlux64_base_01 *engPntr,std::size_t numMutate, double T)
61 {
62  for(std::size_t i=0;i<numMutate;i++)
63  {
64  int copyFrom = 0; // Always copy from the best
65  int copyTo = this->controllers.size()-1-i;
66  controllers.at(copyTo)->copyFrom(controllers.at(copyFrom));
67  controllers.at(copyTo)->mutate(engPntr, T);
68  }
69  return;
70 }
71 
72 bool AnnealEvoPopulation::comparisonFuncForAverage(AnnealEvoMember * elm1, AnnealEvoMember * elm2)
73 {
74  return elm1->averageScore > elm2->averageScore;
75 }
76 bool AnnealEvoPopulation::comparisonFuncForMax(AnnealEvoMember * elm1, AnnealEvoMember * elm2)
77 {
78  return elm1->maxScore > elm2->maxScore;
79 }
80 
81 
82 void AnnealEvoPopulation::orderPopulation()
83 {
84  //calculate each member's average score
85  for(std::size_t i=0;i<this->controllers.size();i++)
86  {
87  double ave = std::accumulate(controllers[i]->pastScores.begin(),controllers[i]->pastScores.end(),0);
88  ave /= (double) controllers[i]->pastScores.size();
89  controllers[i]->averageScore=ave;
90  if(clearScoresBetweenGenerations)
91  controllers[i]->pastScores.clear();
92  }
93 // cout<<"ordering the whole population"<<endl;
94  if(compareAverageScores)
95  sort(controllers.begin(),controllers.end(),this->comparisonFuncForAverage);
96  else
97  sort(controllers.begin(),controllers.end(),this->comparisonFuncForMax);
98 
99 }
100 
101 void AnnealEvoPopulation::readConfigFromXML(std::string configFile)
102 {
103  int intValue;
104  string elementTxt;
105  ifstream in;
106  in.open(configFile.c_str());
107  if(in.fail()==true)
108  {
109  cerr << endl << "impossible to read file " << configFile << endl;
110  in.close();
111  exit(1);
112  return;
113  }
114  do
115  {
116  in >> ws >> elementTxt;
117  if(elementTxt == "<!--")
118  {
119  do
120  {
121  in >> ws >> elementTxt;
122  }while(elementTxt != "-->");
123  }
124  else if(elementTxt == "compareAverageScores")
125  {
126  in >> ws >> intValue;
127  this->compareAverageScores=intValue;
128  }
129  else if(elementTxt == "clearScoresBetweenGenerations")
130  {
131  in >> ws >> intValue;
132  this->clearScoresBetweenGenerations=intValue;
133  }
134  else if(elementTxt == "populationSize")
135  {
136  in >> ws >> intValue;
137  this->populationSize=intValue;
138  }
139  }while(elementTxt != "</configuration>" || in.eof());
140  in.close();
141 }
Contains the definition of class AnnealEvoPopulation Adapting NeuroEvolution to do Simulated Annealin...