ILOHEURISTICCALLBACK1(Rounddown, IloNumVarArray, vars) { IntegerFeasibilityArray feas; IloNumArray obj; IloNumArray x; try { feas = IntegerFeasibilityArray(getEnv()); obj = IloNumArray(getEnv()); x = IloNumArray(getEnv()); getFeasibilities(feas, vars); getObjCoefs (obj , vars); getValues (x , vars); IloNum objval = getObjValue(); IloInt cols = vars.getSize(); for (IloInt j = 0; j < cols; j++) { // Set the fractional variable to zero and update the objective value if ( feas[j] == Infeasible ) { objval -= x[j] * obj[j]; x[j] = 0.0; } } setSolution(vars, x, objval); } catch (...) { feas.end(); obj.end(); x.end(); throw; } feas.end(); obj.end(); x.end(); }
ILOBRANCHCALLBACK1(MyBranch, IloNumVarArray, vars) { if ( getBranchType() != BranchOnVariable ) return; // Branch on var with largest objective coefficient // among those with largest infeasibility IloNumArray x; IloNumArray obj; IntegerFeasibilityArray feas; try { x = IloNumArray(getEnv()); obj = IloNumArray(getEnv()); feas = IntegerFeasibilityArray(getEnv()); getValues(x, vars); getObjCoefs(obj, vars); getFeasibilities(feas, vars); IloInt bestj = -1; IloNum maxinf = 0.0; IloNum maxobj = 0.0; IloInt cols = vars.getSize(); for (IloInt j = 0; j < cols; j++) { if ( feas[j] == Infeasible ) { IloNum xj_inf = x[j] - IloFloor (x[j]); if ( xj_inf > 0.5 ) xj_inf = 1.0 - xj_inf; if ( xj_inf >= maxinf && (xj_inf > maxinf || IloAbs (obj[j]) >= maxobj) ) { bestj = j; maxinf = xj_inf; maxobj = IloAbs (obj[j]); } } } if ( bestj >= 0 ) { makeBranch(vars[bestj], x[bestj], IloCplex::BranchUp, getObjValue()); makeBranch(vars[bestj], x[bestj], IloCplex::BranchDown, getObjValue()); } } catch (...) { x.end(); obj.end(); feas.end(); throw; } x.end(); obj.end(); feas.end(); }
ILOCPLEXGOAL1(MyBranchGoal, IloNumVarArray, vars) { IloNumArray x; IloNumArray obj; IntegerFeasibilityArray feas; x = IloNumArray(getEnv()); obj = IloNumArray(getEnv()); feas = IntegerFeasibilityArray(getEnv()); getValues(x, vars); getObjCoefs(obj, vars); getFeasibilities(feas, vars); IloInt bestj = -1; IloNum maxinf = 0.0; IloNum maxobj = 0.0; IloInt cols = vars.getSize(); for (IloInt j = 0; j < cols; j++) { if ( feas[j] == Infeasible ) { IloNum xj_inf = x[j] - IloFloor (x[j]); if ( xj_inf > 0.5 ) xj_inf = 1.0 - xj_inf; if ( xj_inf >= maxinf && (xj_inf > maxinf || IloAbs (obj[j]) >= maxobj) ) { bestj = j; maxinf = xj_inf; maxobj = IloAbs (obj[j]); } } } IloCplex::Goal res; if ( bestj >= 0 ) { res = AndGoal(OrGoal(vars[bestj] >= IloFloor(x[bestj])+1, vars[bestj] <= IloFloor(x[bestj])), this); } x.end(); obj.end(); feas.end(); return res; }
ILOUSERCUTCALLBACK4( CtCallback, IloNumVarArray, vars, int**, graph, int, num_cuts, int, max_deep ) { if( current_deep > max_deep ){ //cout << "aa " << current_deep << endl; return; } IloNumArray vals(getEnv()); getValues(vals, vars); IntegerFeasibilityArray feas( getEnv() ); getFeasibilities( feas, vars ); int old_win[vals.getSize()]; memset(old_win, 0, sizeof(old_win)); for(int n=0; n<num_cuts; n++){ static int cont=0; int cnt=0; for( int i = 0; i < 2; ++i ){ IloRange cut( getEnv(), 0, 1 ); if( getCut( vals, vars, CLQ2B, i, feas, cut, graph, old_win ) ){ add(cut); cnt++; } else if( getCut( vals, vars, CLQ2A, i, feas, cut, graph, old_win ) ){ cnt++; add(cut); }else{ //se nao encotrar algum corte, sai } cut.end(); } if(n>1 && cnt==0)break; } feas.end(); vals.end(); }
ILOHEURISTICCALLBACK3(Rounddown, IloNumVarArray, vars, int**, graph, vector<vector<int> >, adj) { //if(0){ int n = vars.getSize()/2; IntegerFeasibilityArray feas( getEnv() ); IloNumArray x( getEnv() ); getFeasibilities( feas, vars ); getValues( x, vars ); int newobj1 = 0; int newobj2 = 0; int newx1[2*n]; int newx2[2*n]; memset(newx1, 0, sizeof(newx1)); memset(newx2, 0, sizeof(newx2)); int acc[2*n]; memset(acc, 0, sizeof(acc)); for(int set=0; set<2; set++){ for(int i=0; i<n; i++){ acc[i+n*set]+=x[i+n*set]; for(int j=0; j<adj[i].size(); j++){ int v = adj[i][j]; acc[i]+= x[v+n*set]; } } } for(int s = 1; s <= 2;s++){ for( int set = 0; set < 2; ++set ){ bool marked[n]; int count_marked = 0; for( int i = 0; i < n; ++i ){ if(feas[i+n*set] == Infeasible) marked[i] = false; else{ marked[i]=true; count_marked++; } } int winner; while( count_marked < n ){ //choose the star node winner = -1; for( int i = 0; i < n; ++i ){ if( !marked[i] ){ if( winner == -1 ){ winner = i; } else if(s==1 && x[i+n*set] > x[winner+n*set] ){ winner = i; }else if(s==2 && acc[i+n*set] < acc[winner+n*set]) winner = i; } } marked[winner] = true; ++count_marked; if(set==1 && (s==1&&newx1[winner]==1 || s==2&&newx2[winner]==1)){ continue; } if(s==1){ newx1[winner+n*set] = 1; newobj1++; }else{ //s==2 newx2[winner+n*set] = 1; newobj2++; } for( int i = 0; i < n; ++i ){ if( graph[i][winner] ){ if( !marked[i] ){ marked[i] = true; if(s==1){ newx1[i+set*n] = 0; }else{ newx2[i+set*n] = 0; } ++count_marked; marked[winner] = true; } } } int temp = 0; for( int i = 0; i < n; i++) temp+=marked[i]; if( temp != count_marked ){ printf("error %d %d\n", temp, count_marked); exit(-1); } } } } //codigo do incumbent //ilomipex4 int *newx; int newobj; int op=-1; if(newobj1 > newobj2){ newx = newx1; newobj = newobj1; op=1; }else{ newx = newx2; newobj = newobj2; op=2; } if(newobj > bestIncumbent){ if(op==1)op1++; else op2++; int cont=0; for(int i=0; i<2*n; i++){ x[i] = newx[i]; cont += x[i]; } if(cont!=newobj){ puts("ERRO"); } setSolution( vars, x, newobj ); } x.end(); feas.end(); // } }