static void solveanddisplay (IloEnv env, IloCplex cplex, IloNumVarArray var, IloRangeArray con) { // Optimize the problem and obtain solution. if ( !cplex.solve() ) { env.error() << "Failed to optimize LP" << endl; throw(-1); } IloNumArray vals(env); env.out() << "Solution status = " << cplex.getStatus() << endl; env.out() << "Solution value = " << cplex.getObjValue() << endl; cplex.getValues(vals, var); env.out() << "Values = " << vals << endl; cplex.getSlacks(vals, con); env.out() << "Slacks = " << vals << endl; cplex.getDuals(vals, con); env.out() << "Duals = " << vals << endl; cplex.getReducedCosts(vals, var); env.out() << "Reduced Costs = " << vals << endl; } // END solveanddisplay
IloNum columnGeneration (IloCplex2 subSolver, IloCplex rmpSolver, IloNumVarArray3 x, IloNumVarArray2 z, IloNumVarArray2 lambda, IloObjective2 reducedCost, IloObjective rmpObj, IloRangeArray2 maintConEng, IloRangeArray2 removeMod, IloRangeArray2 convex, IloNumArray2 addXCol, IloNumArray addZCol, IloNumArray2 priceRemoveMod, IloNumArray2 priceMaintConEng, IloNumArray priceConvex, const IloNumArray2 compCosts, const IloNumArray convexityCoef, IloBool weightedDW_in, IloBool allowCustomTermination, IloInt relChangeSpan, IloNum relChange) { // extract optimization enviroment IloEnv env = rmpSolver.getEnv(); // ANALYSIS: write to file const char* output = "results/colGen_analysis.dat"; // variable declarations.. // loop counters IloInt i, m, t, testCounter; testCounter = 0; // declare two clock_t objects for start and end time. clock_t start, end; // declare an array of size NR_OF_MODULES to temporarily hold the // reduced costs from the NR_OF_MODULES different subproblems. IloNumArray redCosts(env, NR_OF_MODULES); IloBool weightedDW = weightedDW_in; // declare an array to hold the RMP objective values: e.g. add // one value to array for each iteration. // IloNumArray objValues(env); // declare an array to hold the lower bounds for the full MP, given // z(RMP)* + sum_[m in subproblems] redCost(m)^* <= z(full MP)^* <= z(RMP)^* // IloNumArray lowerBounds(env); // declare two temp IloNum objects for temporarily hold rmp objective value // and full MP lower bound. (full: MP with _all_ columns availible). IloNum tempRmpObj, tempLowerBound, bestLowerBound; bestLowerBound = -IloInfinity; // also use a num-array to hold the REL_CHANGE_SPAN most // recent RMP objective values. Used to the relative change // break criteria (if improvement by col-gen becomes to // "slow"/"flat"). IloNumArray recentObjValues(env, REL_CHANGE_SPAN); // declare and initiate col-gen iteration counter IloInt itNum = 0; // ---- weighted DW decomp - for less trailing in colgen. //IloBool weightedDW = IloTrue; // declare two IloNum-array to hold all 2x (NR_OF_MODULES x TIME_SPAN) // dual vars - or specific, best dual vars (giving highest lower bound) IloNumArray2 dualsModBest(env); IloNumArray2 dualsEngBest(env); // temp.. //IloBoolArray continueSub(env, NR_OF_MODULES); // allocate memory for sub-arrays for (m = 0; m < NR_OF_MODULES; m++) { dualsModBest.add(IloNumArray(env,TIME_SPAN)); dualsEngBest.add(IloNumArray(env,TIME_SPAN)); //continueSub[m] = IloTrue; // and maybe: initially set all dual vars to 0? for (t = 0; t < TIME_SPAN; t++) { dualsModBest[m][t] = 0.0; dualsEngBest[m][t] = 0.0; } } // also, a weight for weighted DW: IloNum weight = 2.0; // counter number of improvements of "best duals" IloInt nrOfImprov = 0; // const used in weigt IloNum weightConst = DW_WEIGHT; // initiate numarray for recent objective values // UPDATE: this is not really needed now as we check the // relative change break criteria only when we know this // vector has been filled by REL_CHANGE_SPAN "proper" values... recentObjValues[0] = 0.0; for (i = 1; i < REL_CHANGE_SPAN; i++) { recentObjValues[i] = 0.0; } // model before generation start: //rmpSolver.exportModel("rmpModel.lp"); //write results to file /* ofstream outFile(output); outFile << "Writing out results from initial column generation, T=" << TIME_SPAN << endl << "Form: [(itNum) (dual solution value) (upper bound = tempRmpObj)]" << endl << endl;*/ //cout << endl << "WE GOT OURSELVES A SEGMENTATION FAULT!" << endl; // start timer start = clock(); // also use an alternative time counter.. time_t start2, end2; start2 = time(NULL); // loop until break... for (;;) { // increase iteration counter itNum++; // solve master if ( !rmpSolver.solve() ) { env.error() << "Failed to optimize RMP." << endl; throw(-1); } // get the rmp objective value tempRmpObj = rmpSolver.getValue(rmpObj); // add to objective value array (size: iterations) // objValues.add(tempRmpObj); // update recentObjValues: recentObjValues.remove(0); // remove oldest: recentObjValues.add(tempRmpObj); // save newest // report something (... report1() ) // UPDATE: colgen is to quick for anything to see anything reported... so skip this // update weight: weight = min{ 2, if (itNum>1) { weight = (IloNum(itNum - 1) + IloNum(nrOfImprov))/2; } if ( weight > weightConst) { weight = weightConst; } // update duals // -> updates obj. functions for subproblems (w.r.t. duals) for (m = 0; m < NR_OF_MODULES; m++) { //cout << "weight = " << weight << endl; //cin.get(); for (t = 0; t < TIME_SPAN; t++) { // if we try weightedWD decomp: // for first iteration: just extract duals which will become best duals if (itNum > 1 && weightedDW) { // price associated with const. removeMod priceRemoveMod[m][t] = (rmpSolver.getDual(removeMod[m][t]))/weight + (weight-1)*(dualsModBest[m][t])/weight; // price associated with const. maintConEng priceMaintConEng[m][t] = (rmpSolver.getDual(maintConEng[m][t]))/weight + (weight-1)*(dualsEngBest[m][t])/weight; } // normally: else { // price associated with const. removeMod priceRemoveMod[m][t] = rmpSolver.getDual(removeMod[m][t]); // price associated with const. maintConEng priceMaintConEng[m][t] = rmpSolver.getDual(maintConEng[m][t]); } //cout << "priceRemoveMod[m][t] = " << priceRemoveMod[m][t] << endl; // update subproblem obj. function coefficients for z[m][t] vars: reducedCost[m].setLinearCoef(z[m][t],-(priceRemoveMod[m][t] + priceMaintConEng[m][t])); } //cin.get(); // rmpSolver.getDuals(priceRemoveMod[m], removeMod[m]); // rmpSolver.getDuals(priceMaintConEng[m], maintConEng[m]); // priceConvex[m] = -rmpSolver.getDual(convex[m][0]); // if we try weightedWD decomp: // for first iteration: just extract duals which will become best duals /* if (itNum > 1 && weightedDW) { // price associated with convexity constraint priceConvex[m] = -abs(rmpSolver.getDual(convex[m][0]))/weight - (weight-1)*(dualsConvBest[m])/weight; } */ // price associated with convexity constraint priceConvex[m] = -abs(rmpSolver.getDual(convex[m][0])); // update subproblem obj. function's constant term reducedCost[m].setConstant(priceConvex[m]); } // END of updating duals // solve subproblems for (m = 0; m < NR_OF_MODULES; m++) { //if (continueSub[m]) { // solve subproblem m cout << endl << "Solving subproblem #" << (m+1) << "." << endl; if ( !subSolver[m].solve() ) { env.error() << "Failed to optimize subproblem #" << (m+1) << "." << endl; throw(-1); } // extract reduced cost: subSolver[m].getValue(reducedCost[m]) // into a IloNumArray(env, NR_OF_MODULES) at position m. redCosts[m] = subSolver[m].getValue(reducedCost[m]); //env.out() << endl << "Reduced cost for problem #" << (m+1) << ":" << redCosts[m] << endl; // if reduced cost is negative, att optimal solution to // column pool Q(m) if (!(redCosts[m] > -RC_EPS)) { // add column addColumn(subSolver[m], x[m], z[m], lambda[m], rmpObj, maintConEng[m], removeMod[m], convex[m], addXCol, addZCol, compCosts[m], convexityCoef); //env.out() << endl << "Added a column to pool Q(" << (m+1) // << "). Lambda[" << (m+1) << "].size = " << lambda[m].getSize() << endl; testCounter++; } //else { // continueSub[m] = IloFalse; //} //} //else { // redCosts[m] = 0; //} } // END of solving subproblems (for this iteration) // calculate lower bound on full MP tempLowerBound = tempRmpObj + sumArray(redCosts); // update best lower bound if (tempLowerBound > bestLowerBound) { bestLowerBound = tempLowerBound; // increase counter for number of improved "best duals". nrOfImprov++; //cout << endl << "WHATEVAAAH" << endl; //cin.get(); // save best duals for (m = 0; m < NR_OF_MODULES; m++) { for (t = 0; t < TIME_SPAN; t++) { dualsModBest[m][t] = priceRemoveMod[m][t]; dualsEngBest[m][t] = priceMaintConEng[m][t]; } } } // update lower bounds vector //lowerBounds.add(bestLowerBound); // ANALYSIS: print out to file: // [(itNum) (dual solution value) (upper bound = tempRmpObj)] //outFile << itNum << " " << tempLowerBound << " " << tempRmpObj << endl; // if the smallest reduced cost in the updated red-cost vector // with NR_OF_MODULES entries is greater than -RC_EPS: break // otherwise, repeat. if (minMemberValue(redCosts) > -RC_EPS) { env.out() << endl << "All reduced costs non-negative: breaking." << endl; bestLowerBound = tempRmpObj; break; } // if relative change in objective value over the last REL_CHANGE_SPAN iterations is // less than REL_CHANGE, break. //if (allowCustomTermination && (itNum >= REL_CHANGE_SPAN) && (relativeImprovement(recentObjValues) < REL_CHANGE)) { if (allowCustomTermination && (itNum >= relChangeSpan) && (relativeImprovement(recentObjValues) < relChange)) { env.out() << endl << "Relative change smaller than pre-set acceptance: breaking." << endl; break; } // if the gap currentUpperBound - currentLowerBound is small enough, break if ((tempRmpObj - bestLowerBound) < RC_EPS) { env.out() << endl << "Lower<>Upper bound gap less than pre-set acceptance: breaking." << endl; cout << endl << "(tempRmpObj - bestLowerBound) = " << (tempRmpObj - bestLowerBound) << endl; bestLowerBound = tempRmpObj; break; } } // END of column generation. // alternative: print the REL_CHANGE_SPAN most recent RMP objective values env.out() << endl; /*for (i = 0; i < recentObjValues.getSize(); i++) { env.out() << "Recent #" << (i+1) << ": " << recentObjValues[i] << endl; }*/ // solve master using final column setup if ( !rmpSolver.solve() ) { env.error() << "Failed to optimize RMP." << endl; throw(-1); } // also add final objective value to objective value array (size: iterations) // objValues.add(rmpSolver.getValue(rmpObj)); // create IloNum object and extract the final RMP objective value. IloNum finalObj; finalObj = rmpSolver.getValue(rmpObj); // end program time counter(s) end = clock(); end2 = time(NULL); // print result to file //outFile << endl << endl << "Final objective value: " << finalObj << endl // << "Time required for execution: " << (double)(end-start)/CLOCKS_PER_SEC << " seconds." << endl; // outfile.close() //outFile.close(); itNum = 0; // re- use iteration counter to count total number of columns generated // nr of columns generated from each subproblem for (m = 0; m < NR_OF_MODULES; m++) { // ### outFile << "Number of columns generated from subproblem (m = " << m << "): " << (lambda[m].getSize() - 1) << endl; cout << "Number of columns generated from subproblem (m = " << m << "): " << (lambda[m].getSize() - 1) << endl; //outFile << "Pool (m = " << m << "): " << (lambda[m].getSize() - 1) << endl; itNum += lambda[m].getSize(); } // ### /* outFile << endl << "Total number of columns generated: " << itNum << endl; outFile << "Time required for column generation: " << (double)(end-start)/CLOCKS_PER_SEC << " seconds." << endl; outFile << "ALTERNATIVE: Time required for colgen: " << end2-start2 << " seconds." << endl; outFile << "RMP objective function cost: " << finalObj << endl << endl; //outFile << "Objective function values for each iteration: " << endl; //outFile << objValues; //outFile << endl << endl << "Lower bounds for each iteration: " << endl; //outFile << lowerBounds; outFile.close(); */ //cout << "Initial column generation complete, press enter to continue..." << endl; //cin.get(); // print some results.. /*env.out() << endl << "RMP objective function cost: " << finalObj << endl; env.out() << endl << "Size of objective value vector: " << objValues.getSize() << endl; env.out() << endl << "Nr of iterations: " << itNum << endl; cout << endl << "Number of new columns generated: " << testCounter << endl;*/ // Note: to explicit output (void function), ass columns are added implicitely via // income argument pointers. // clear memory assigned to temporary IloNumArrays: redCosts.end(); //objValues.end(); //lowerBounds.end(); recentObjValues.end(); // save best duals for (m = 0; m < NR_OF_MODULES; m++) { dualsModBest[m].end(); dualsEngBest[m].end(); } dualsModBest.end(); dualsEngBest.end(); //continueSub.end(); //cout << endl << "NR OF IMPROVEMENTS: " << nrOfImprov << endl; //cin.get(); // return lower bound return bestLowerBound; } // END of columnGeneration()
// This routine separates Benders' cuts violated by the current x solution. // Violated cuts are found by solving the worker LP // IloBool separate(const Arcs x, const IloNumArray2 xSol, IloCplex cplex, const IloNumVarArray v, const IloNumVarArray u, IloObjective obj, IloExpr cutLhs, IloNum& cutRhs) { IloBool violatedCutFound = IloFalse; IloEnv env = cplex.getEnv(); IloModel mod = cplex.getModel(); IloInt numNodes = xSol.getSize(); IloInt numArcs = numNodes * numNodes; IloInt i, j, k, h; // Update the objective function in the worker LP: // minimize sum(k in V0) sum((i,j) in A) x(i,j) * v(k,i,j) // - sum(k in V0) u(k,0) + sum(k in V0) u(k,k) mod.remove(obj); IloExpr objExpr = obj.getExpr(); objExpr.clear(); for (k = 1; k < numNodes; ++k) { for (i = 0; i < numNodes; ++i) { for (j = 0; j < numNodes; ++j) { objExpr += xSol[i][j] * v[(k-1)*numArcs + i*numNodes + j]; } } } for (k = 1; k < numNodes; ++k) { objExpr += u[(k-1)*numNodes + k]; objExpr -= u[(k-1)*numNodes]; } obj.setExpr(objExpr); mod.add(obj); objExpr.end(); // Solve the worker LP cplex.solve(); // A violated cut is available iff the solution status is Unbounded if ( cplex.getStatus() == IloAlgorithm::Unbounded ) { IloInt vNumVars = (numNodes-1) * numArcs; IloNumVarArray var(env); IloNumArray val(env); // Get the violated cut as an unbounded ray of the worker LP cplex.getRay(val, var); // Compute the cut from the unbounded ray. The cut is: // sum((i,j) in A) (sum(k in V0) v(k,i,j)) * x(i,j) >= // sum(k in V0) u(k,0) - u(k,k) cutLhs.clear(); cutRhs = 0.; for (h = 0; h < val.getSize(); ++h) { IloInt *index_p = (IloInt*) var[h].getObject(); IloInt index = *index_p; if ( index >= vNumVars ) { index -= vNumVars; k = index / numNodes + 1; i = index - (k-1)*numNodes; if ( i == 0 ) cutRhs += val[h]; else if ( i == k ) cutRhs -= val[h]; } else { k = index / numArcs + 1; i = (index - (k-1)*numArcs) / numNodes; j = index - (k-1)*numArcs - i*numNodes; cutLhs += val[h] * x[i][j]; } } var.end(); val.end(); violatedCutFound = IloTrue; } return violatedCutFound; } // END separate
bool FrontalSolverWithoutConnexity::solve(int borne_max, bool warmstart) { LOG(INFO) << name_ << " :: " << description_; //Extracting data VLOG(2) << "Extracting data"; int n = data_.n; int m = data_.m; double Ba = data_.Ba; double Bp = data_.Bp; const vector<vector<double> >& Ha = data_.Ha; const vector<vector<double> >& Ca = data_.Ca; const vector<vector<double> >& Hp = data_.Hp; const vector<vector<double> >& Cp = data_.Cp; //Environement VLOG(2) << "Creating environment"; IloEnv env; IloModel model = IloModel (env); IloCplex cplex = IloCplex(model); //Variables VLOG(2) << "Creating variables"; BoolVarMatrix x(env); NumVarMatrix y(env); NumVarMatrix z(env); VLOG(2) << "Creating variables"; for (int i = 0; i < m ; ++i) { x.add(IloBoolVarArray(env,n)); y.add(IloNumVarArray(env,n,0,Bp)); z.add(IloNumVarArray(env,n,0,Ba)); } IloNumVar ha_var(env); model.add(ha_var); IloNumVar hp_var(env); model.add(hp_var); //Objective function VLOG(2) << "Processing objective function"; IloExpr objective(env,0); for (int i = 0; i < m ; ++i) { for (int j = 0; j < n ; ++j) { objective += x[i][j]; } } model.add(IloMaximize(env, objective )); VLOG(2) << "Creating constraint expressions"; IloExpr sum_Cy(env,0); IloExpr sum_HCPx(env,0); IloExpr sum_Cz(env,0); IloExpr sum_HCAx(env,0); VLOG(2) << "Computing constraint expressions"; for (int i = 0; i < m ; ++i) { for (int j = 0; j < n ; ++j) { VLOG(5) << i << " " << j << " " << Cp[i][j] << endl; sum_Cy += y[i][j]*Cp[i][j]; sum_Cz += z[i][j]*Ca[i][j]; sum_HCPx += x[i][j]*Hp[i][j]*Cp[i][j]; sum_HCAx += x[i][j]*Ha[i][j]*Ca[i][j]; } } VLOG(2) << "Adding constraints"; //Selection of an admissible area model.add(sum_Cy == sum_HCPx); model.add(sum_Cz == sum_HCAx); model.add(ha_var + hp_var >= 2); for (int i = 0; i < m ; ++i) { for (int j = 0; j < n ; ++j) { //Defining y model.add(y[i][j] <= Bp*x[i][j]); model.add(y[i][j] <= hp_var); model.add(y[i][j] >= hp_var - Bp*(1-x[i][j])); model.add(y[i][j] >= 0); //Defining z model.add(z[i][j] <= Ba*x[i][j]); model.add(z[i][j] <= ha_var); model.add(z[i][j] >= ha_var - Ba*(1-x[i][j])); model.add(z[i][j] >= 0); //Constraint on x if (Cp[i][j] == 0) model.add(x[i][j]==0); } } // Warmstart if (warmstart) { // Setting Warmstart from solution VLOG(2) << "Setting informations for warmstart"; IloNumVarArray startVar(env); IloNumArray startVal(env); for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) { startVar.add(x[i][j]); startVal.add(sol_.x_[i][j]); if (sol_.x_[i][j] == 0){ startVar.add(y[i][j]); startVal.add(sol_.x_[i][j]); startVar.add(z[i][j]); startVal.add(sol_.x_[i][j]); } } cplex.addMIPStart(startVar, startVal); startVal.end(); startVar.end(); } //Solve VLOG(2) << "Resolution..."; cplex.solve(); //Output VLOG(2) << "Solution status = " << cplex.getStatus() << endl; VLOG(2) << "Solution value = " << cplex.getObjValue() << endl; for (int i = 0; i < data_.m; ++i) { for (int j = 0; j < data_.n; ++j) { sol_.x_[i][j] = cplex.getValue(x[i][j]); } } env.end(); return false; }
/** Create a new worker. * The constructor mainly does the following: * - create an IloCplex instance that refers to a remote worker, * - load the model in <code>modelfile</code>, * - setup parameters depending on this worker's index, * - start an asynchronous solve. * If anything fails then an exception will be thrown. * @param env The environment used for instantiating Ilo* objects. * @param i The index of the worker to be created. This also * determines the parameter settings to use in this worker. * @param s A pointer to the global solve state. * @param transport The transport name for the IloCplex constructor. * @param argc The argument count for the IloCplex constructor. * @param argv The array of transport arguments for the IloCplex * constructor. * @param modelfile Name of the model to be loaded into the worker. * @param output The output mode. * @param objdiff The minimal difference between so that two * consecutive objective function values are considered * different. */ Worker(IloEnv env, int i, SolveState *s, char const *transport, int argc, char const **argv, char const *modelfile, OUTPUT output, double objdiff) : idx(i), state(s), model(env), cplex(0), handle(0), primal(IloInfinity), dual(-IloInfinity), obj(env), x(env), rng(env), infoHandler(this), outb(idx), outs(&outb) { try { // Create remote object, setup output and load the model. cplex = IloCplex(model, transport, argc, argv); switch (output) { case OUTPUT_SILENT: // Disable output on the output and warning stream. cplex.setOut(env.getNullStream()); cplex.setWarning(env.getNullStream()); break; case OUTPUT_PREFIXED: // Redirect output to our custom stream. cplex.setOut(outs); cplex.setWarning(outs); break; case OUTPUT_LOG: // Nothing to do here. By default output is enabled. break; } cplex.importModel(model, modelfile, obj, x, rng); if ( obj.getSense() == IloObjective::Minimize ) { primal = -IloInfinity; dual = IloInfinity; } // We set the thread count for each solver to 1 so that we do not // run into problems if multiple solves are performed on the same // machine. cplex.setParam(IloCplex::Param::Threads, 1); // Each worker runs with a different random seed. This way we // get different paths through the tree even if the other // parameter settings are the same. cplex.setParam(IloCplex::Param::RandomSeed, idx); // Apply parameter settings. for (class ParamValue const *vals = settings[idx % NUMSETTINGS].values; vals->isValid(); ++vals) vals->apply(cplex); // Install callback and set objective change. int status = cplex.userfunction (USERACTION_ADDCALLBACK, 0, NULL, 0, 0, NULL); if ( status ) throw status; IloCplex::Serializer s; s.add(objdiff); status = cplex.userfunction (USERACTION_CHANGEOBJDIFF, s.getRawLength(), s.getRawData(), 0, 0, NULL); if ( status ) throw status; // Register the handler that will process info messages sent // from the worker. cplex.setRemoteInfoHandler(&infoHandler); // Everything is setup. Launch the asynchronous solve. handle = cplex.solve(true); } catch (...) { // In case of an exception we need to take some special // cleanup actions. Note that if we get here then the // solve cannot have been started and we don't need to // kill or join the asynchronous solve. if ( cplex.getImpl() ) cplex.end(); rng.end(); x.end(); obj.end(); model.end(); throw; } }