I am trying to code heuristic in branch&Cut code but, he can't modify the best integer solution in the main i put this instruction to call the heuristic "status = CPXsetheuristiccallbackfunc (env, mst_heuri, NULL);" and this is the code of the heuristic : static int CPXPUBLIC mst_heuri (CPXCENVptr env, void cbdata, int wherefrom, void cbhandle, double objval_p, double x, int checkfeas_p, int useraction_p) { int status = 0;

int i,j,i0,j0, qq, cols; int nodecnt, kkk; int a=0,b=0,u=0,v=0,n_mst,ne=1; double min,mincost=0; double ** cost; double * p_cal =NULL;

double opt_remain, opt_int, opt_mst,roundobjval; int feas = NULL; CPXCLPptr lp; double objcoefs = NULL;

int ** aretes_mst =NULL; double * poids_mst =NULL; double pp;

double optt = 0.0;

*useraction_p = CPX_CALLBACK_DEFAULT; opt_mst=0;

/ Heuristic motivated by knapsack constrained problems. Rounding down all fractional values will give an integer solution that is feasible, since all constraints are <= with positive coefficients / //printf("heuristique dans\n"); status = CPXgetcallbacklp (env, cbdata, wherefrom, &lp); if ( status ) { printf ("Can't get lp pointer."); goto TERMINATE; }

cols = CPXgetnumcols (env, lp); if ( cols <= 0 ) { printf ("numcols = %d. \n", cols); status = CPXERR_CALLBACK; goto TERMINATE; }

aretes_mst = malloc ( nb_a * sizeof(int ) ); for (i0=0; i0 <nb_a ; i0++) aretes_mst[i0] = malloc ( 2 * sizeof(int) ); poids_mst = malloc ( nb_a * sizeof(double) ); for (i0=0; i0 <nb_a ; i0++){ aretes_mst[i0][0]=-1; aretes_mst[i0][1]=-1; poids_mst[i0]=-1; }
objcoefs = (double
) malloc (cols * sizeof (double)); feas = (int *) malloc (cols * sizeof (int));

parent = malloc ((nb_n+1) * sizeof (int));

cost = malloc ( (nb_n+1) * sizeof(double *) ); for (i=0; i <(nb_n+1) ; i++) cost[i] = malloc ( (nb_n+1) * sizeof(double) ); for (i=0; i <(nb_n+1) ; i++) parent[i]=0;

if ( feas == NULL ) { printf ("Out of memory."); status = CPXERR_CALLBACK; goto TERMINATE; }

status = CPXgetobj (env, lp, objcoefs, 0, cols-1); if ( status ) { printf ("Can't get objective."); goto TERMINATE; }

status = CPXgetcallbacknodeintfeas (env, cbdata, wherefrom, feas, 0, cols-1);

if ( status ) { printf ("Can't get variable feasible status for node."); goto TERMINATE; }

status = CPXgetcallbackinfo (env, cbdata, wherefrom,CPX_CALLBACK_INFO_NODE_COUNT, &nodecnt);
if ( status )  goto TERMINATE;


status = CPXgetcallbackinfo (env, cbdata, wherefrom,CPX_CALLBACK_INFO_BEST_REMAINING, &opt_remain);
    if ( status )  goto TERMINATE;
//printf("OPT_REMAIN = %lf \n",opt_remain);
status = CPXgetcallbackinfo (env, cbdata, wherefrom,CPX_CALLBACK_INFO_BEST_INTEGER, &opt_int);
    if ( status )  goto TERMINATE;
printf("OPT_INTEGER = %lf \n",opt_int);

j = 0;
bool entier = TRUE;
while(j < cols){
    if(feas[j] == CPX_INTEGER_INFEASIBLE)
    entier = FALSE,j = cols+1;
    else j++;
}


 /*******************************/
 for (j = 0; j < cols; j++)
 {  x[j]=arrondie (x[j]);
   optt=optt+coeff[j]*x[j];
 //if(j<nb_a)
 //printf("***heur func X[%d] = %lf **************  \n",j, x[j]);
 }
printf("optt heuris = %lf \n",optt);    
 /*******************************/


if(nodecnt == 0){

    opt1=optt;
    printf("opt1 heu = %lf \n",opt1);       
    if(premierefois){
        opt0=optt;
        printf("opt0 heu = %lf \n",opt0);
        premierefois = FALSE;
    }

}

roundobjval = *objval_p;    
printf("objval_p AVANT = %lf \n",roundobjval);
if (entier == FALSE){

if(roundobjval < opt_int ){

//roundobjval = objval_p; /*****/ n_mst=nb_n; for(i=1;i<=n_mst;i++) cost[i][i]=INFI; j=0; while (j<nb_a){ cost[(table_graphe[j][0])][table_graphe[j][1]]= ((table_graphe_poids[j]) arrondie(1-x[j])); cost[(table_graphe[j][1])][table_graphe[j][0]]= ((table_graphe_poids[j])* arrondie(1-x[j])); j++; } // for(i=0;i<=cols;i++)printf("x(%d)= %lf \n",i,x[i]); // for(i=1;i<=n_mst;i++){ // for(j=1;j<=n_mst;j++) printf("cost(%d,%d)= %d \t",i,j,cost[i][j]); // printf("\n"); // }

kkk=0; //printf("\nThe edges of Minimum Cost Spanning Tree are\n\n"); while(ne<n_mst) { for(i=1,min=INFI;i<=n_mst;i++) { for(j=1;j<=n_mst;j++) { if(cost[i][j]<min) { min=cost[i][j]; a=u=i; b=v=j; } } } u=find(u); v=find(v);

if(uni(u,v)) { //printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min); ne++; mincost +=min; aretes_mst[kkk][0]=a; aretes_mst[kkk][1]=b; // poids_mst[kkk]=x[indice_arete(a, b)]*table_graphe_poids[indice_arete(a, b)]; //printf("indice_arete(%d, %d) = %d \n",a,b, indice_arete(a, b)); poids_mst[kkk]=table_graphe_poids[indice_arete(a, b)]; kkk++; } cost[a][b]=cost[b][a]=INFI; } //printf("\n\tMST = %ld \n",mincost); p_cal = malloc ( nb_n * sizeof(double) ); for (i0=0 ; i0 < nb_n ; i0++) p_cal[i0] = 0; for (j0=0 ; j0 < kkk ; j0++){ if(poids_mst[j0] > p_cal[aretes_mst[j0][0]-1]) {pp=poids_mst[j0]; p_cal[aretes_mst[j0][0]-1]= pp;}

    if(poids_mst[j0] > p_cal[aretes_mst[j0][1]-1]) {pp=poids_mst[j0]; p_cal[aretes_mst[j0][1]-1]= pp;}

} opt_mst=0; for (i0=0 ; i0 < nb_n ; i0++) opt_mst=opt_mst+p_cal[i0]; pp=0; for (j0=0 ; j0 < kkk ; j0++)pp=pp+poids_mst[j0];

printf("\n c(MST) = %lf \t MESNC(MST)= %lf \n",pp,opt_mst); heuris++; if(opt_mst < opt_int){ for (i0=0 ; i0 < cols ; i0++) x[i0] = 0; for (i0=0 ; i0 < kkk ; i0++) x[indice_arete(aretes_mst[i0][0], aretes_mst[i0][1])]=1; /%%%%%%%%%%%%%%%%%%/ j0=nb_a; for (i0=0 ; i0 < nb_n ; i0++){ pp=0; qq=j0; while((pp+coeff[qq]) <= p_cal[i0]){ x[qq]=1; pp=pp+coeff[qq]; qq++; } j0=j0+(nb_n-1); } /%%%%%%%%%%%%%%%%%%/ pp=0; for (i0=0 ; i0 < cols ; i0++) pp=pp+objcoefs[i0]*x[i0]; //for (i0=0 ; i0 < nb_n ; i0++) x[i0+nb_a] = p_cal[i0]; //roundobjval=opt_mst; roundobjval=pp; heuris_eff++;

     *useraction_p = CPX_CALLBACK_SET;
     //for (i0=0 ; i0 < cols ; i0++){printf("***heur func z[%d] = %lf **************  \n",i0, x[i0]);}
 }

}

} /*****/ // for (j = 0; j < nb_a; j++) {

  // /* Set the fractional variable to zero and update
     // the objective value */

  // if ( feas[j] == CPX_INTEGER_INFEASIBLE ) {
     // roundobjval -= x[j] * objcoefs[j];
     // x[j] = 0.0;
  // }

// } objval_p = roundobjval; printf("objval_p APRES = %lf \n",roundobjval); / Have CPLEX check the solution for integer feasibility */

*checkfeas_p = 1;

/ Tell CPLEX that a solution is being returned /

*useraction_p = CPX_CALLBACK_SET;

TERMINATE: free_and_null ((char ) &aretes_mst); free_and_null ((char ) &poids_mst); free_and_null ((char ) & p_cal); free_and_null ((char ) &cost); free_and_null ((char ) &parent); free_and_null ((char ) &objcoefs); free_and_null ((char **) &feas);

return (status);

}

Please help me

asked 14 Nov '17, 10:33

Salsabil's gravatar image

Salsabil
111
accept rate: 0%

I use cplex12.7 in c

(14 Nov '17, 10:37) Salsabil
Be the first one to answer this question!
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×191
×18

Asked: 14 Nov '17, 10:33

Seen: 183 times

Last updated: 14 Nov '17, 10:37

OR-Exchange! Your site for questions, answers, and announcements about operations research.