void *BVNS(void *arg){//VNS com HJ pVNS *vns = arg; int t = 0,k=1,j; int best_aval = 0; int hooke_cont = 0; bool stop = false; int avalmax_aux=0; int shake_cont =0; int hooke_geral_cont = 0; int vns_cont = 0; double *x,*x2,*y,*r; double fx,fy; int run=vns->RUN; int p = vns->P; x = (double*) malloc (vns->DIM * sizeof(double)); x2 = (double*) malloc (vns->DIM * sizeof(double)); y = (double*)malloc (vns->DIM * sizeof(double)); r = (double*)malloc ( (vns->KMAX+1) *sizeof(double)); vns->r = (double*)malloc ( (vns->KMAX+1) *sizeof(double)); //set wich radii value r[0] = 0.0f; for(j=1;j<=vns->KMAX;j++){ r[j] = vns->RADII; vns->RADII*=vns->Q; } memcpy(vns->r,r,sizeof(double)*vns->KMAX+1); run = vns->RUN+1; t=0; k=1; vns_cont=0; hooke_cont=0; hooke_geral_cont=0; avalmax_aux=0; shake_cont=0; stop=false; memcpy(x,vns->best,sizeof(double)*vns->DIM); memcpy(x2,x,vns->DIM*sizeof(double));//set as start value, just for initialize memcpy(y,x,vns->DIM*sizeof(double));//set as start value, just for initialize fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t);//initialize fx avalmax_aux = vns->AVAL_MAX; time (&(vns->solv.stime)); while(t < vns->TMAX -1 && !stop){ k=1; while(k<=vns->KMAX && t< vns->TMAX && !stop){ /* t++; */ vns_cont++; //do shake */ shake_one(x, y, r, k,vns->DIM,vns->LB,vns->UB,&shake_cont,vns->RADII_T_FLAG,p); if( (vns->TMAX - t) < avalmax_aux){ avalmax_aux = vns->TMAX -t; } shake_cont = hooke(vns->DIM, y, x2, vns->RHO, vns->EPSILON, avalmax_aux, vns->FUNCTION,&hooke_cont); /* checkisLbUb(x2,vns->DIM,vns->LB,vns->UB); */ t += hooke_cont; hooke_geral_cont+=hooke_cont; hooke_cont=0; avalmax_aux = vns->AVAL_MAX; //change neighborhood, x<= receive the best or the same neighborhoodChange(x,x2,&fx,vns->DIM,&k,vns->FUNCTION,&best_aval,&t,&fy,vns->LB,vns->UB); /* if(vns->FUNCTION == 8){ */ /* if(-418.0 > fx){ */ /* printf("Find the best solution before finis2h!\n"); */ /* stop = true; */ /* }else{ */ /* continue; */ /* } */ /* }else if(fx < (1E-10)){ */ /* printf("Find the best solution before finish!\n"); */ /* stop = true; */ /* } */ } } time (&(vns->solv.etime)); vns->solv.t_total = difftime(vns->solv.etime,vns->solv.stime); printf("\n==RUN: %d\n",run); printf("VNS total number of avaliations : %d\n",vns_cont); printf("Hooke total number of avaliations: %d\n",hooke_geral_cont); printf("Total number of avaliation: %d\n",t); printf("Best solution found == %.10f\n",fx); printf("Time: == %.0f seconds\n",vns->solv.t_total); vns->solv.c_aval=t; vns->solv.c_aval_best=best_aval; vns->solv.bestfo=fx; printf("\n"); free(x); free(x2); free(y); free(r); return (void*)arg; }
void *HJ(void *arg){//Only Hooke and Jaevees pVNS *vns = arg; int t = 0,i; bool stop = false; int avalmax_aux=0; double *x; double fx; int run=vns->RUN; x = (double*) malloc (vns->DIM * sizeof(double)); run = vns->RUN+1; t=0; avalmax_aux=0; stop=false; for (i=0; i<vns->DIM;i++) //each dimension { x[i] = randon(vns->LB,vns->UB); } fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t);//initialize fx avalmax_aux = vns->AVAL_MAX; time (&(vns->solv.stime)); while(t < vns->TMAX && !stop){ /* t++; */ if( (vns->TMAX - t) < avalmax_aux){ avalmax_aux = vns->TMAX -t; } hooke(vns->DIM, x, x, vns->RHO, vns->EPSILON, avalmax_aux, vns->FUNCTION,&t); checkisLbUb(x,vns->DIM,vns->LB,vns->UB); /* t = t + hooke_cont; */ //change neighborhood, x<= receive the best or the same fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t); /* if(fx == 0.0f){ */ /* if(vns->FUNCTION == 8){ */ /* if(-418.0 > fx){ */ /* printf("Find the best solution before finis2h!\n"); */ /* stop = true; */ /* }else{ */ /* continue; */ /* } */ /* }else if(fx < (1E-10)){ */ /* printf("Find the best solution before finish!\n"); */ /* stop = true; */ /* } */ /* printf("Improved with %d avaliation and %.20f value\n",t,fx); */ } time (&(vns->solv.etime)); printf("\n==RUN: %d\n",run); printf("Total number of avaliation with HJ: %d\n",t); printf("Best solution found == %.10f\n",fx); vns->solv.c_aval=t; vns->solv.c_aval_best=t; vns->solv.bestfo=fx; printf("\n"); free(x); vns->solv.t_total = difftime(vns->solv.etime,vns->solv.stime); return (void*)arg; }
void TransformerSPRINGEMBEDDER::SpringEmbed(Graph& G, vector<float> dimension_limits, IntermediateStepHandler* intermediatestephandler) { vector<VectorND> tractions; int dimensions = dimension_limits.size(); unstressed_spring_length = dimension_limits[0]; for(int i = 1; i < dimensions; i++) unstressed_spring_length *= dimension_limits[i]; unstressed_spring_length /= G.NumberOfVertices(); unstressed_spring_length = pow(unstressed_spring_length, 1.0f/dimensions) / (dimensions * 2); // init srand ( time(NULL) ); for(VertexIterator a = G.BeginVertices(); a != G.EndVertices(); a++) { Coordinates coordinates; for(int i = 0; i < dimensions; i++) coordinates.push_back(fmod(rand(), dimension_limits[i])); (*a)->SetCoordinates(coordinates); // should make sure that no two vertices have the same position tractions.push_back(VectorND(dimensions)); } float max_movement; iteration = 0; do { // compute movement max_movement = 0; int i = 0; for(VertexIterator a = G.BeginVertices(); a != G.EndVertices(); a++, i++) { VectorND traction = tractions[i] * friction; // compute forces on a by the other vertices VectorND aCoordinates((*a)->GetCoordinates(0)); for(VertexIterator b = G.BeginVertices(); b != G.EndVertices(); b++) { if(b==a) continue; // force on a by vertex b VectorND bCoordinates((*b)->GetCoordinates(0)); traction += coulomb(aCoordinates, bCoordinates); if((*a)->Adjacent(*b)) traction += hooke(aCoordinates, bCoordinates); } tractions[i] = traction; } // execute movement VertexIterator a = G.BeginVertices(); for(int i = 0; a != G.EndVertices(); a++, i++) { Coordinates OldCoordinates = (*a)->GetCoordinates(0); Coordinates NewCoordinates(dimensions); for(int j = 0; j < dimensions; j++) NewCoordinates[j] = max(0.0f,min(dimension_limits[j], OldCoordinates[j] + delta * tractions[i][j] )); (*a)->SetCoordinates( NewCoordinates ); // for the loop-condition float current_movement = 0.0f; for(int j = 0; j < dimensions; j++) current_movement += (OldCoordinates[j] - NewCoordinates[j]) * (OldCoordinates[j] - NewCoordinates[j]); if(sqrt(current_movement) > max_movement) max_movement = sqrt(current_movement); } if(intermediatestephandler != NULL) intermediatestephandler->Handle(&G); if(++iteration > nextincrease) { movement_threshold += 0.01f; nextincrease *= 4; } if(iteration > max_iterations) break; } while(max_movement > movement_threshold*unstressed_spring_length); }