4#include "Cartesian_genotype.h"
22 std::vector<CrossoverOpP> crossoverOperators;
26 return crossoverOperators;
31 std::vector<MutationOpP> mutationOperators;
38 return mutationOperators;
43 registerParameter(state,
"numoutputs", (voidP) (
new uint(1)), ECF::UINT,
"number of functional outputs (default: 1)");
44 registerParameter(state,
"numrows", (voidP) (
new uint(1)), ECF::UINT,
"number of rows (default: 1)");
45 registerParameter(state,
"numcols", (voidP) (
new uint(100)), ECF::UINT,
"number of columns (default: 100)");
46 registerParameter(state,
"levelsback", (voidP) (
new uint(1)), ECF::UINT,
"number of previous columns to be used as possible inputs (default: 1)");
47 registerParameter(state,
"numvariables", (voidP) (
new uint(1)), ECF::UINT,
"number of input variables (default: 1)");
48 registerParameter(state,
"constantset", (voidP) (
new std::string), ECF::STRING,
"set of functions to use (default: none)");
49 registerParameter(state,
"functionset", (voidP) (
new std::string), ECF::STRING,
"set of input constants (default: none)");
57 std::string names,name;
61 functionSet_ =
static_cast<FunctionSetP
> (
new FunctionSet);
62 functionSet_->initialize(
state_);
68 number = *((uint*) sptr.get());
70 std::cerr <<
"Genotype initialization error: Number of variables is lesser than 1 or can not be parsed into a number.\n";
76 number = *((uint*) sptr.get());
78 std::cerr <<
"Genotype initialization error: Number of outputs is lesser than 1 or can not be parsed into a number.\n";
84 number = *((uint*) sptr.get());
86 std::cerr <<
"Genotype initialization error: Number of rows is lesser than 1 or can not be parsed into a number.\n";
92 number = *((uint*) sptr.get());
94 std::cerr <<
"Genotype initialization error: Number of columns is lesser than 1 or can not be parsed into a number.\n";
100 number = *((uint*) sptr.get());
102 std::cerr <<
"Genotype initialization error: Number of columns is lesser than 1 or can not be parsed into a number.\n";
111 names = *((std::string*) sptr.get());
124 names = *((std::string*) sptr.get());
131 functionSet_->addFunction(name);
135 std::map<std::string, FunctionP_basic>::iterator it;
136 for (it = functionSet_->mActiveFunctions_.begin(); it != functionSet_->mActiveFunctions_.end(); it++) {
137 uint nArgs = it->second->getNumOfArgs();
149 std::string s = xCart.getText(0);
150 std::istringstream ss(s);
155 std::vector<uint> operands;
157 while (getline(ss, token,
' '))
160 functionId = str2uint(token);
162 counter = functionSet_->vActiveFunctions_[functionId]->getNumOfArgs();
165 operands.push_back(str2uint(token));
173 for(uint j = this->size() - 1; j > this->size() - 1 -
nOutputs; j--) {
174 this->operator[](j).isOutput =
true;
180 xCart = XMLNode::createXMLTopNode(
"Cartesian");
181 std::stringstream sValue;
182 sValue << this->size();
183 xCart.addAttribute(
"size", sValue.str().c_str());
188 std::vector<CartesianGene>& genome = *
this;
189 for(uint i = 0; i < genome.size(); i++) {
192 xCart.addText(sValue.str().c_str());
195 uint Cartesian::getGenomeSize()
200 uint Cartesian::getRowNumber(uint index)
219 if(minimum <= firstElementOfARow) {
225 return state_->getRandomizer()->getRandomInteger(minimum, firstElementOfARow -1);
228 std::set<uint> Cartesian::allPossibleConnection(uint rowNumber)
233 if(minimum <= firstElementOfARow) {
240 for(uint i = minimum; i < firstElementOfARow; i++) {
253 std::vector<uint> trail;
254 std::deque<uint> working_deque;
255 uint start_index = this->size() -
nOutputs + output_number;
256 uint start_value = this->operator[](start_index).value;
257 working_deque.push_back(start_value);
258 while(!working_deque.empty()) {
259 uint working_index = working_deque.front();
260 working_deque.pop_front();
262 trail.push_back(working_index -
nInputs);
265 std::cout <<
"Something went wrong. Trail is considering an output index.\n";
267 for(uint i = 0; i < gene.inputConnections.size(); i++) {
268 working_deque.push_back(gene.inputConnections[i]);
277 std::vector<std::vector<uint> > allTrails;
278 for(uint i = 0; i <
nOutputs; i++) {
286 const std::vector<FunctionP_basic>& vRef = functionSet_->vActiveFunctions_;
287 for(uint i = 0; i <
nRows; i++) {
288 for(uint j = 0; j <
nCols; j++) {
289 uint functionID = get_random_int(0, functionSet_->vActiveFunctions_.size() - 1);
290 uint noOfOperands = vRef[functionID]->getNumOfArgs();
291 std::vector<uint> connections;
292 for(uint k = 0; k < noOfOperands; k++) {
298 for(uint i = 0; i <
nOutputs; i++) {
304 int Cartesian::get_random_int(
int from,
int to) {
305 return this->
state_->getRandomizer()->getRandomInteger(from,to);
309 double Cartesian::get_random_double(
int from,
int to) {
310 return rand() % (to - from + 1) + from;
313 void Cartesian::evaluate(
const std::vector<double> &inputData, std::vector<double> &results)
315 std::vector<double> working_vector(inputData);
316 std::vector<double> operands;
317 const std::vector<FunctionP_basic>& vRef = functionSet_->vActiveFunctions_;
320 for(uint i = 0; i < this->size() -
nOutputs; i++) {
321 const CartesianGene& gene = this->operator[](i);
322 for(uint j = 0; j < gene.inputConnections.size(); j++) {
323 operands.push_back(working_vector[gene.inputConnections[j]]);
325 vRef[gene.value]->evaluate(operands,result);
326 working_vector.push_back(result);
330 for(uint i = this->size() -
nOutputs; i < this->size(); i++) {
331 results.push_back(working_vector[this->
operator[](i).value]);
335 double Cartesian::static_random_double(
int from,
int to) {
336 return rand() % (to - from + 1) + from;
voidP getParameterValue(StateP state, std::string name)
Read single parameter value from Registry.
bool registerParameter(StateP state, std::string name, voidP value, enum ECF::type T, std::string description="")
Register a single parameter.
std::string name_
genotype's name
uint nInputs
total number of inputs (including constants)
uint randomConnectionGenerator(uint rowNumber)
uint nVariables
number of input variables
void write(XMLNode &xCart)
void evaluate(const vector< double > &inputData, vector< double > &results)
vector< MutationOpP > getMutationOp()
std::vector< uint > getActiveTrail(uint output_number)
void read(XMLNode &xCart)
vector< CrossoverOpP > getCrossoverOp()
uint nLevelsBack
levels back parameter
uint maxArity
max number of inputs for all function nodes (gates)
bool initialize(StateP state)
uint nOutputs
number of final outputs
uint nCols
number of columns
StateP state_
local copy of state
std::vector< std::vector< uint > > getActiveTrails()
uint nFunctions
number of functions
uint nConstants
number of input constants
void registerParameters(StateP state)