Exemplo n.º 1
0
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
Exemplo n.º 2
0
// 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;
}
Exemplo n.º 4
0
 /** Get the solution status for this worker.
  * Calling this function before calling join() is an error.
  */
 IloAlgorithm::Status getStatus() const { return cplex.getStatus(); }
std::string CPLEX_LP_MSTSolverIF::getCPLEXStatus(IloCplex& cplex) {
	std::ostringstream oss { };
	oss << cplex.getStatus() << std::flush;
	return oss.str();
}