diff --git a/cpp_frep/frep_tree.cpp b/cpp_frep/frep_tree.cpp index 2acf38ca188ca2677cf1ffaa5c6b9823123b7c76..e74fb262b9fadc2c6e4d2223d17bf122fd9d642a 100644 --- a/cpp_frep/frep_tree.cpp +++ b/cpp_frep/frep_tree.cpp @@ -195,13 +195,13 @@ namespace cpp_frep { void parseTreeSmooth(){ printGenome(); - parsedTree=morphNodes(0); + parsedTree=morphNodeTypes(0); std::cout<<"\n"; parsedTree->printName(); std::cout<<"\n"; } - Frep* morphNodes(int index){ + Frep* morphNodeTypes(int index){ int nodeIndex1,nodeIndex2; nodeIndex1=(int)(genome[index][0]*nodeLength); @@ -221,6 +221,31 @@ namespace cpp_frep { return &ArrayMorph[n]; + } + + Frep* morphNodeIndices(int index,float nextIndex){ //num is 1 or 2 + + int shapeIndex1,shapeIndex2; + //TODO CHECK IF RIGHT + + shapeIndex1=index+(int)ceilf(nextIndex* (maxLength-1-index)); + shapeIndex2=(shapeIndex1==(maxLength-1))?(maxLength-1):(shapeIndex1+1); + float orig=(float)index+ nextIndex* (float)(maxLength-1-index); + float weight=orig-(float)shapeIndex1+1.0; + + + int n=counterMorph; + ArrayMorph[n].weight=weight; + std::cout<<"Morph("; + counterMorph++; + ArrayMorph[n].setShape1(morphNodeTypes(shapeIndex1)); + std::cout<<","; + ArrayMorph[n].setShape2(morphNodeTypes(shapeIndex2)); + std::cout<<","<<weight<<")"; + + return &ArrayMorph[n]; + + } Frep* getNode(int index, int nodeIndex){ @@ -240,10 +265,10 @@ namespace cpp_frep { int n=counterAdd; std::cout<<"Add("; counterAdd++; - if (smooth) ArrayAdd[n].setShape1(morphNodes(shape1Index)); + if (smooth) ArrayAdd[n].setShape1(morphNodeIndices( index,genome[index][1])); else ArrayAdd[n].setShape1(getNode(shape1Index,0)); std::cout<<","; - if (smooth) ArrayAdd[n].setShape2(morphNodes(shape2Index)); + if (smooth) ArrayAdd[n].setShape2(morphNodeIndices( index,genome[index][2])); else ArrayAdd[n].setShape2(getNode(shape2Index,0)); std::cout<<")"; return &ArrayAdd[n]; @@ -254,10 +279,10 @@ namespace cpp_frep { int n=counterSubtract; std::cout<<"Subtract("; counterSubtract++; - if (smooth) ArraySubtract[n].setShape1(morphNodes(shape1Index)); + if (smooth) ArraySubtract[n].setShape1(morphNodeIndices( index,genome[index][1])); else ArraySubtract[n].setShape1(getNode(shape1Index,0)); std::cout<<","; - if (smooth) ArraySubtract[n].setShape2(morphNodes(shape2Index)); + if (smooth) ArraySubtract[n].setShape2(morphNodeIndices( index,genome[index][2])); else ArraySubtract[n].setShape2(getNode(shape2Index,0)); std::cout<<")"; return &ArraySubtract[n]; @@ -267,10 +292,10 @@ namespace cpp_frep { int n=counterIntersect; std::cout<<"Intersect("; counterIntersect++; - if (smooth) ArrayIntersect[n].setShape1(morphNodes(shape1Index)); + if (smooth) ArrayIntersect[n].setShape1(morphNodeIndices( index,genome[index][1])); else ArrayIntersect[n].setShape1(getNode(shape1Index,0)); std::cout<<","; - if (smooth) ArrayIntersect[n].setShape2(morphNodes(shape2Index)); + if (smooth) ArrayIntersect[n].setShape2(morphNodeIndices( index,genome[index][2])); else ArrayIntersect[n].setShape2(getNode(shape2Index,0)); std::cout<<")"; return &ArrayIntersect[n]; @@ -280,10 +305,10 @@ namespace cpp_frep { int n=counterMorph; std::cout<<"Morph("; counterMorph++; - if (smooth) ArrayMorph[n].setShape1(morphNodes(shape1Index)); + if (smooth) ArrayMorph[n].setShape1(morphNodeIndices( index,genome[index][1])); else ArrayMorph[n].setShape1(getNode(shape1Index,0)); std::cout<<","; - if (smooth) ArrayMorph[n].setShape2(morphNodes(shape2Index)); + if (smooth) ArrayMorph[n].setShape2(morphNodeIndices( index,genome[index][2])); else ArrayMorph[n].setShape2(getNode(shape2Index,0)); std::cout<<")"; ArrayMorph[n].weight=0.5; @@ -296,7 +321,7 @@ namespace cpp_frep { int n=counterTranslate; std::cout<<"Translate("; counterTranslate++; - if (smooth) ArrayTranslate[n].setShape(morphNodes(shapeIndex)); + if (smooth) ArrayTranslate[n].setShape(morphNodeIndices( index,genome[index][1])); else ArrayTranslate[n].setShape(getNode(shapeIndex,0)); std::cout<<")"; if (parametric) { @@ -313,7 +338,7 @@ namespace cpp_frep { int n=counterScale; std::cout<<"Scale("; counterScale++; - if (smooth) ArrayScale[n].setShape(morphNodes(shapeIndex)); + if (smooth) ArrayScale[n].setShape(morphNodeIndices( index,genome[index][1])); else ArrayScale[n].setShape(getNode(shapeIndex,0)); std::cout<<")"; if (parametric) { @@ -331,7 +356,7 @@ namespace cpp_frep { int n=counterRotate; std::cout<<"Rotate("; counterRotate++; - if (smooth) ArrayRotate[n].setShape(morphNodes(shapeIndex)); + if (smooth) ArrayRotate[n].setShape(morphNodeIndices( index,genome[index][1])); else ArrayRotate[n].setShape(getNode(shapeIndex,0)); std::cout<<")"; if (parametric) ArrayRotate[n].angle=genome[index][3]*360.0; @@ -343,7 +368,7 @@ namespace cpp_frep { int n=counterLinearArrayX; std::cout<<"LinearArrayX("; counterLinearArrayX++; - if (smooth) ArrayLinearArrayX[n].setShape(morphNodes(shapeIndex)); + if (smooth) ArrayLinearArrayX[n].setShape(morphNodeIndices( index,genome[index][1])); else ArrayLinearArrayX[n].setShape(getNode(shapeIndex,0)); std::cout<<")"; if (parametric) { @@ -360,7 +385,7 @@ namespace cpp_frep { int n=counterLinearArrayY; std::cout<<"LinearArrayY("; counterLinearArrayY++; - if (smooth) ArrayLinearArrayY[n].setShape(morphNodes(shapeIndex)); + if (smooth) ArrayLinearArrayY[n].setShape(morphNodeIndices( index,genome[index][1])); else ArrayLinearArrayY[n].setShape(getNode(shapeIndex,0)); std::cout<<")"; if (parametric) { @@ -378,7 +403,7 @@ namespace cpp_frep { int n=counterPolarArray; std::cout<<"PolarArray("; counterPolarArray++; - if (smooth) ArrayPolarArray[n].setShape(morphNodes(shapeIndex)); + if (smooth) ArrayPolarArray[n].setShape(morphNodeIndices( index,genome[index][1])); else ArrayPolarArray[n].setShape(getNode(shapeIndex,0)); std::cout<<")"; if (parametric) { diff --git a/cpp_frep/main.cpp b/cpp_frep/main.cpp index b123ad7ac31241c8896e37963420f7991f5324cf..a1f055ad4c1c740e510dbf370dd68557e588b4b4 100755 --- a/cpp_frep/main.cpp +++ b/cpp_frep/main.cpp @@ -397,6 +397,7 @@ void nelderMead(Frep_tree* tree,Frep* target,float precision, int maxSteps, bool int count=0; + int count1=0; float score=0.0f; // while ( !o.done(score)&&count<maxSteps) { //cap @@ -418,7 +419,7 @@ void nelderMead(Frep_tree* tree,Frep* target,float precision, int maxSteps, bool } } - std::cout<<"Step:"<<count<<"\n"; + std::cout<<"Step:"<<count<<", total:"<< count1<<"\n"; std::string name1 = "i"; if(tuneParameters) name1 = "j"; @@ -426,6 +427,7 @@ void nelderMead(Frep_tree* tree,Frep* target,float precision, int maxSteps, bool score=compareL1( v, target, tree, bound,res , name,tuneParameters); v = o.step(v,score ); count++; + count1++; } } @@ -437,7 +439,7 @@ int main(int const, char const**) { bool smooth=true; - bool parametric=false; + bool parametric=true; bool tuneParameters=false; float precision = 0.001; @@ -476,10 +478,8 @@ int main(int const, char const**) { // node3.push_back(0.2472); // node3.push_back(0.452); // node3.push_back(0.727); - // if (parametric){ - // node3.push_back(0.17); - // node3.push_back(0.227); - // } + // node3.push_back(0.17); + // node3.push_back(0.227); // genome.push_back(node3); Frep_tree tree(genome,smooth,parametric,bound); @@ -498,16 +498,16 @@ int main(int const, char const**) { tree.printGenome(); // //////////tune parameters////////// - std::cout<<"Tuning parameters!!!!!!!!!!!!!!!!!!!\n"; - parametric=true; - tree.parametric=parametric; - tuneParameters=true; - precision=0.0075; - maxSteps=20; + // std::cout<<"Tuning parameters!!!!!!!!!!!!!!!!!!!\n"; + // parametric=true; + // tree.parametric=parametric; + // tuneParameters=true; + // precision=0.0075; + // maxSteps=20; - nelderMead(&tree,&pa, precision, maxSteps, tuneParameters, bound, res); + // nelderMead(&tree,&pa, precision, maxSteps, tuneParameters, bound, res); - drawFrep(tree,minX, maxX, minY, maxY, dx, dy,"finalt",false); + // drawFrep(tree,minX, maxX, minY, maxY, dx, dy,"finalt",false);