Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

Commit 684c6dae authored by moh's avatar moh
Browse files

Simple crossover function for tgp

parent 178ff2ac
......@@ -23,6 +23,7 @@ __________________________________________________________*/
#define TREE_DEPTH_MAX 6 // maximum size of (initial) trees (excluded)
#define GROW_FULL_RATIO 0 // ratio between grow and full construction method
#define DIV_ERR_VALUE 1 // Returned value, in case of non defined input value
#define MAX_XOVER_DEPTH 15
float** inputs;
float* outputs;
......@@ -88,9 +89,18 @@ int load_data(float*** inputs, float** outputs, string filename){
match = fscanf(file,"%f\n",(*outputs)+i);
assert(match==1);
}
fclose(file);
return loaded_size;
}
void free_data(){
for( int i=0 ; i<fitnessCasesSetLength ;i++ )
free( inputs[i] );
free( inputs );
free( outputs );
}
/**
Recursive construction method for trees.
Koza construction methods. Function set has to be ordered,
......@@ -295,6 +305,7 @@ int collectNodesDepth(const int goalDepth, GPNode** collection, int collected, i
toDotFile( ((IndividualImpl*)population->parents[i])->root[0],oss.str().c_str(),0);
//toDotFile( ((IndividualImpl*)population->parents[i])->root,oss.str().c_str(),1);
}
free_data();
}
\end
......@@ -356,6 +367,12 @@ GenomeClass {
\GenomeClass::crossover :
{
/**
Crossover for GP trees.
It selects depths of cross, then selects which nodes
will be exchanged at those depths.
*/
int depthP1 = depthOfTree(parent1.root[0]);
int depthP2 = depthOfTree(parent2.root[0]);
......@@ -384,9 +401,9 @@ GenomeClass {
collected2=1;
}
#if 1
cout << "dp1 : " << depthP1 << " xov1 : " << xoverD1 << " maxnodeCount1 : " << maxNode1 << " collected1 : "<< collected1 << endl;
cout << "dp2 : " << depthP2 << " xov2 : " << xoverD2 << " maxnodeCount2 : " << maxNode2 << " collected2 : "<< collected2 << endl;
#if 0
cout << "dp1 : " << depthP1 << " xovd1 : " << xoverD1 << " maxnodeCount1 : " << maxNode1 << " collected1 : "<< collected1 << endl;
cout << "dp2 : " << depthP2 << " xovd2 : " << xoverD2 << " maxnodeCount2 : " << maxNode2 << " collected2 : "<< collected2 << endl;
for( int i=0 ; i<collected1 ; i++ )
printf("%s | ",opCodeName[(int)d1Nodes[i]->opCode].c_str());
......@@ -407,9 +424,10 @@ GenomeClass {
stockPointCountP2+=d2Nodes[i]->currentArity;
}
#if 0
cout << "stock point count parent 1 : " << stockPointCountP1 << endl;
cout << "stock point count parent 2 : " << stockPointCountP2 << endl;
#endif
int xoverP1 = random(0,stockPointCountP1);
int xoverP2 = random(0,stockPointCountP2);
......@@ -433,26 +451,37 @@ GenomeClass {
}
else i+=d2Nodes[parentIndexP2++]->currentArity;
#if 0
cout << "r : " << reminderP1 << " parentIndex : " << parentIndexP1 << " xoverPt1 : " << xoverP1<<endl;
cout << "r : " << reminderP2 << " parentIndex : " << parentIndexP2 << " xoverPt2 : " << xoverP2<<endl;
cout << "xoverPt2 : " << xoverP2 << endl;
toDotFile( parent1.root[0],"out/xover/p1-o",0);
toDotFile( parent2.root[0],"out/xover/p2-o",0);
#endif
delete d1Nodes[parentIndexP1]->children[reminderP1];
d1Nodes[parentIndexP1]->children[reminderP1] = NULL;
#if 0
toDotFile( child1.root[0],"out/xover/p1",0);
#endif
d1Nodes[parentIndexP1]->children[reminderP1] = d2Nodes[parentIndexP2]->children[reminderP2];
#if 0
toDotFile(d2Nodes[parentIndexP2]->children[reminderP2],"out/xover/graft",0);
#endif
d2Nodes[parentIndexP2]->children[reminderP2] = NULL;
#if 0
toDotFile( parent2.root[0],"out/xover/p2",0);
toDotFile(d1Nodes[parentIndexP1]->children[reminderP1],"out/xover/stock",0);
toDotFile( child1.root[0],"out/xover/c",0);
#endif
child1.valid=false;
delete[] d1Nodes;
delete[] d2Nodes;
}
......@@ -477,7 +506,9 @@ GenomeClass {
//cout << evolvedValue << ":" << outputs[i] << " ";
fitness += fabs(evolvedValue-outputs[i]);
}
cout << "->" << fitness <<endl;
//cout << "->" << fitness <<endl;
Genome.valid = true;
Genome.fitness = fitness;
return fitness;
}
\end
......@@ -489,8 +520,8 @@ CPPFLAGS+=
\Default run parameters : // Please let the parameters appear in this order
Number of generations : 1 // NB_GEN
Time limit: 0 // In seconds, 0 to deactivate
Population size : 10 //POP_SIZE
Offspring size : 1 // 40%
Population size : 1000 //POP_SIZE
Offspring size : 100 // 40%
Mutation probability : 1 // MUT_PROB
Crossover probability : 1 // XOVER_PROB
Evaluator goal : minimise // Maximise
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment