SolutionILP solveILP(const ILPModel& m, bool verbose, double gap, double timeLimit, int numberOfThreads, int)	{

    IloEnv env;
    SolutionILP solution;

    try {
        IloModel model(env);
        IloObjective obj(env);
        IloNumVarArray var(env);
        IloRangeArray con(env);
        generateProblem(m, model, var, con);

        IloCplex cplex(model);
        cplex.setParam(IloCplex::Threads, numberOfThreads);
        if (!verbose)
            cplex.setOut(env.getNullStream());
        if (gap != 0.0)
            cplex.setParam(IloCplex::EpGap, gap);
        if (timeLimit != 0.0)
            cplex.setParam(IloCplex::TiLim, timeLimit);

        cplex.solve();
        switch (cplex.getStatus())	{
        case  IloAlgorithm::Optimal:
            solution.status = ILP_OPTIMAL;
            break;
        case IloAlgorithm::Feasible:
            solution.status = ILP_FEASIBLE;
            break;
        case IloAlgorithm::Infeasible:
            solution.status = ILP_INFEASIBLE;
            break;
        case IloAlgorithm::Unbounded:
            solution.status = ILP_UNBOUNDED;
            break;
        default:
            solution.status = ILP_UNKNOWN;
        }

        if (solution.status == ILP_OPTIMAL || solution.status == ILP_FEASIBLE)	{
            IloNumArray sol(env);
            cplex.getValues(sol, var);
            solution.criterion = cplex.getObjValue();
            for (long v = 0; v < sol.getSize(); ++v)
                solution.solution.push_back(sol[v]);
        }

        solution.bound = cplex.getBestObjValue();	// Can throw IloException!
        env.end();

    } catch (IloException& e)	{
        env.end();
        throw ILPSolverException(caller(), e.getMessage());
    } catch (...) {
        env.end();
        throw_with_nested(ILPSolverException(caller(), "Error during solving the ILP problem!"));
    }

    return solution;
}
Example #2
0
int main(int , const char * []){
  IloEnv env;
  try {
    IloModel model(env);
    IloIntVar Belgium(env, 0, 3), Denmark(env, 0, 3),
      France(env, 0, 3), Germany(env, 0, 3),
      Luxembourg(env, 0, 3), Netherlands(env, 0, 3);
    model.add(Belgium != France);
    model.add(Belgium != Germany);
    model.add(Belgium != Netherlands);
    model.add(Belgium != Luxembourg);
    model.add(Denmark == Germany);
    model.add(France != Germany);
    model.add(France != Luxembourg);
    model.add(Germany != Luxembourg);
    model.add(Germany != Netherlands);
    IloCP cp(model);
    cp.setParameter(IloCP::LogVerbosity, IloCP::Quiet);
    if (cp.solve()){
      cp.out() << std::endl << cp.getStatus() << " Solution" << std::endl;
      cp.out() << "Belgium:     " << Names[cp.getValue(Belgium)] << std::endl;
      cp.out() << "Denmark:     " << Names[cp.getValue(Denmark)] << std::endl;
      cp.out() << "France:      " << Names[cp.getValue(France)] << std::endl;
      cp.out() << "Germany:     " << Names[cp.getValue(Germany)] << std::endl;
      cp.out() << "Luxembourg:  " << Names[cp.getValue(Luxembourg)]  << std::endl;
      cp.out() << "Netherlands: " << Names[cp.getValue(Netherlands)] << std::endl;
    }
  }
  catch (IloException& ex) {
    env.out() << "Error: " << ex << std::endl;
  }
  env.end();
  return 0;
}
Example #3
0
int
main (void) {
   IloEnv env;
   try {
      IloModel model(env);

      IloNumVarArray var(env);
      IloRangeArray con(env);
      populatebyrow (model, var, con);

      IloCplex cplex(model);
      cplex.solve();

      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, con);
      env.out() << "Slacks        = " << vals << endl;

      cplex.exportModel("mipex1.lp");
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;

}  // END main
Example #4
0
ILOSTLBEGIN

int
main()
{
   IloEnv   env;
   try {
      IloModel model(env, "chvatal");

      IloNumVarArray x(env, 12, 0.0, 50.0);
      model.add(IloMinimize(env, x[0] + x[1] + x[2] + x[3] + x[4] +
                                 x[5] + x[6] + 2*x[10] + 2*x[11] ));

      model.add(                                   -x[7]-x[8]-x[9]          
         == -1);
      model.add( x[0]                    +x[5]     +x[7]
         ==  4);
      model.add(      x[1]     +x[3]          +x[6]     +x[8]                 
         ==  1);
      model.add(           x[2]     +x[4]                    +x[9]            
         ==  1);
      model.add(                         -x[5]-x[6]               -x[10]+x[11]
         == -2);
      model.add(               -x[3]-x[4]                         +x[10]      
         == -2);
      model.add(-x[0]-x[1]-x[2]                                         -x[11]
         == -1);

      IloCplex cplex(model);
      cplex.setParam(IloCplex::Param::Simplex::Display, 2);
      cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Network);
      cplex.solve();
      cplex.out() << "After network optimization, objective is "
                  << cplex.getObjValue() << endl;

      model.add(2*x[10] + 5*x[11] == 2);
      model.add(  x[0] + x[2] + x[5] == 3);

      cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Dual);
      cplex.solve();

      IloNumArray vals(env);
      cplex.getValues(vals, x);
      cplex.out() << "Solution status " << cplex.getStatus() << endl;
      cplex.out() << "Objective value " << cplex.getObjValue() << endl;
      cplex.out() << "Solution is: " << vals << endl;

      cplex.exportModel("lpex3.lp");
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;
} // END main
Example #5
0
int main(int, const char * []) {
  IloEnv env;
  try {
    IloIntVarArray x(env);
    for (IloInt i = 0; i < 10; i++) {
      char name[6];
      sprintf(name, "X%ld", i);
      x.add(IloIntVar(env, 0, 100 - 2*(i / 2), name));
    }
    IloModel mdl(env);
    mdl.add(IloAllDiff(env, x));
    mdl.add(x);

    IloIntVarChooser varChooser   = ChooseSmallestCentroid(env);
    IloIntValueChooser valChooser = ChooseSmallestDistanceFromCentroid(env);
    IloSearchPhase sp1(env, x, varChooser, valChooser);

    IloIntVarEval   varEval       = Centroid(env);
    IloIntValueEval valEval       = DistanceFromCentroid(env);
    IloSearchPhase sp2(env, x, IloSelectSmallest(varEval),
                               IloSelectSmallest(valEval));

    // sp2 can have ties as two variable or values could evaluate
    // to the same values.  sp3 shows how to break these ties
    // choosing, for equivalent centroid and distance-to-centroid
    // evaluations, the lowest indexed variable in x and the
    // lowest value.
    IloVarSelectorArray selVar(env);
    selVar.add(IloSelectSmallest(varEval));
    selVar.add(IloSelectSmallest(IloVarIndex(env, x))); // break ties on index

    IloValueSelectorArray selValue(env);
    selValue.add(IloSelectSmallest(valEval));
    selValue.add(IloSelectSmallest(IloValue(env))); // break ties on smallest

    IloSearchPhase sp3(env, x, selVar, selValue);

    IloCP cp(mdl);
    cp.setParameter(IloCP::Workers, 1);
    cp.setParameter(IloCP::SearchType, IloCP::DepthFirst);
    cp.setParameter(IloCP::LogPeriod, 1);
    cp.out() << "Choosers" << std::endl;
    cp.solve(sp1);
    cp.out() << cp.domain(x) << std::endl;

    cp.out() << "Evaluators" << std::endl;
    cp.solve(sp2);
    cp.out() << cp.domain(x) << std::endl;
    cp.out() << "Evaluators (with tie-break)" << std::endl;
    cp.solve(sp3);
    cp.out() << cp.domain(x) << std::endl;

    cp.end();
  } catch (IloException & ex) {
    env.out() << "Caught: " << ex << std::endl;
  }
  env.end();
  return 0;
}
string solverIdentification()	{
    IloEnv env;
    IloCplex cplex(env);
    string identification = "Cplex "+string(cplex.getVersion());
    env.end();

    return identification;
}
int main(int argc, const char* argv[]){
  IloEnv env;
  try {
    const char* filename = "../../../examples/data/sched_conflict.data";
    IloInt failLimit = 10000;
    if (argc > 1)
      filename = argv[1];
    if (argc > 2)
      failLimit = atoi(argv[2]);
    IloConstraintArray allCts(env);
    IloConstraintArray capacityCts(env);
    IloConstraintArray precedenceCts(env);
    IloModel model = ReadModel(env, filename, capacityCts, precedenceCts);
    allCts.add(capacityCts);
    allCts.add(precedenceCts);
    IloCP cp(model);
    cp.setParameter(IloCP::FailLimit, failLimit);
    cp.setParameter(IloCP::CumulFunctionInferenceLevel, IloCP::Extended);
    cp.setParameter(IloCP::ConflictRefinerOnVariables, IloCP::On);
    cp.out() << "Instance \t: " << filename << std::endl;
    if (cp.solve()) {
      // A solution was found
      cp.out() << "Solution found with makespan : " << cp.getObjValue() << std::endl;
    } else {
      IloInt status = cp.getInfo(IloCP::FailStatus);
      if (status != IloCP::SearchHasFailedNormally) {
        // No solution found but problem was not proved infeasible
        cp.out() << "No solution found but problem was not proved infeasible." << std::endl;
      } else {
        // Run conflict refiner only if problem was proved infeasible
        cp.out() << "Infeasible problem, running conflict refiner ..." << std::endl;
        cp.out() << std::endl;
        cp.out() << "SCENARIO 1: Basic conflict refiner:" << std::endl;
        cp.out() << std::endl;
        runBasicConflictRefiner(cp);
        cp.setParameter(IloCP::LogVerbosity, IloCP::Quiet);
        cp.out() << "SCENARIO 2: Conflict refiner with preference on resource capacity constraints:" << std::endl;
        cp.out() << std::endl;
        runConflictRefinerWithPreferences(cp, capacityCts, precedenceCts);
        cp.out() << "SCENARIO 3: Conflict refiner with preference on precedence constraints:" << std::endl;
        cp.out() << std::endl;
        runConflictRefinerWithPreferences(cp, precedenceCts, capacityCts);
        cp.out() << "SCENARIO 4: Conflict partition:" << std::endl; 
        cp.out() << std::endl;
        runConflictRefinerPartition(cp, allCts);
        cp.out() << "SCENARIO 5: All conflicts:" << std::endl;
        cp.out() << std::endl;
        runConflictRefinerAllConflicts(cp, allCts);
      }
    }
  } catch (IloException& ex) {
    env.out() << "Caught: " << ex << std::endl;
  }
  env.end();
  return 0;
}
Example #8
0
 void Instance::ComputeCPLEXRevenue() {
     IloEnv env;
     IloInt i, j;
     IloModel model(env);
     IloInt nbImpressions = num_impressions_;
     IloInt nbAdvertisers = num_advertisers_;
     
     NumVarMatrix x(env, nbImpressions);
     
     for(i = 0; i < nbImpressions; i++) {
         x[i] = IloNumVarArray(env, nbAdvertisers, 0.0, 1.0, ILOFLOAT);
     }
     
     // Add impression constraints.
     for(i = 0; i < nbImpressions; i++) {
         model.add(IloSum(x[i]) <= 1.0);
     }
     
     // Add weighted contraint.
     for(j = 0; j < nbAdvertisers; j++) {
         IloExpr curr_adv_constraint(env);
         for (__gnu_cxx::hash_map<int, long double>::iterator iter = bids_matrix_[j].begin();
              iter != bids_matrix_[j].end();
              ++iter) {
             curr_adv_constraint += ((double) iter->second) * ((double) weights_[j]) * x[iter->first][j];
         }
         model.add(curr_adv_constraint <= ((double) budgets_[j]));
     }
     
     IloExpr obj_exp(env);
     for(i = 0; i < nbImpressions; i++) {
         for(j = 0; j < nbAdvertisers; j++) {
             obj_exp += ((double) transpose_bids_matrix_[i][j]) * x[i][j];
         }
     }
     
     model.add(IloMaximize(env, obj_exp));
     obj_exp.end();
     
     IloCplex cplex(env);
     cplex.setOut(env.getNullStream());
     cplex.extract(model);
     
     // Optimize the problem and obtain solution.
     if ( !cplex.solve() ) {
         env.error() << "Failed to optimize LP" << endl;
         throw(-1);
     }
     
     cplex.exportModel("/Users/ciocan/Documents/Google/data/example.lp");
     
     cout << "CPLEX opt is " << cplex.getObjValue() << "\n";
     
     env.end();    
 }
Example #9
0
ILOSTLBEGIN

//typedef IloArray<IloNumArray> TwoDMatrix;

int main(int argc, char **argv) 
{
	IloEnv env;
	try 
	{
		IloNumVar X(env, 0, IloInfinity, ILOFLOAT);
		IloNum X_val;
		IloNumVar Y(env, 0, 10, ILOINT);
		IloNum Y_val;
		IloModel model(env);
		IloExpr Obj(env);
		Obj = 5*X - 3*Y;
		model.add(IloMinimize(env,Obj)); // IloMinimize is used for minimization problems
		//Alternatively, model.add(IloMinimize(env,Obj)); can be replaced by the following three lines.
		//This is useful while iterating in Decomposition Techniques where the objective function is redefined at each iteration
		//IloObjective Objective = IloMinimize(env); 
		//model.add(Objective); 
		//Objective.setExpr(Obj);

		Obj.end();
		model.add(X + 2*Y >= 10);
		model.add(2*X - Y >= 0);
		model.add(X - 3*Y >= -13);
		// Optimize
		IloCplex cplex(model);
		//cplex.setOut(env.getNullStream()); // This is to supress the output of Branch & Bound Tree on screen
		//cplex.setWarning(env.getNullStream()); //This is to supress warning messages on screen
		cplex.solve();//solving the MODEL
		if (cplex.getStatus() == IloAlgorithm::Infeasible) // if the problem is infeasible
		{
			env.out() << "Problem Infeasible" << endl; 
		}
		X_val = cplex.getValue(X);
		Y_val = cplex.getValue(Y);
		// Print results
		cout<< "Objective Value = " << cplex.getObjValue() << endl;
		cout<<"X = "<<X_val<<endl;
		cout<<"Y = "<<Y_val<<endl;
	}
	catch(IloException &e) 
	{
		env.out() << "ERROR: " << e << endl;
	}
	catch(...)
	{
		env.out() << "Unknown exception" << endl;
	}
	env.end();
	return 0;
}
Example #10
0
int main(int, const char * []) {
    IloEnv env;
    try {
        IloInt i,j;
        IloModel model(env);

        IloNumExpr cost(env);
        IloIntervalVarArray allTasks(env);
        IloIntervalVarArray joeTasks(env);
        IloIntervalVarArray jimTasks(env);
        IloIntArray joeLocations(env);
        IloIntArray jimLocations(env);

        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 0, 0,   120, 100.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 1, 0,   212, 100.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 2, 151, 304, 100.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 3, 59,  181, 200.0);
        MakeHouse(model, cost, allTasks, joeTasks, jimTasks, joeLocations,
                  jimLocations, 4, 243, 425, 100.0);

        IloTransitionDistance tt(env, 5);
        for (i=0; i<5; ++i)
            for (j=0; j<5; ++j)
                tt.setValue(i, j, IloAbs(i-j));

        IloIntervalSequenceVar joe(env, joeTasks, joeLocations, "Joe");
        IloIntervalSequenceVar jim(env, jimTasks, jimLocations, "Jim");

        model.add(IloNoOverlap(env, joe, tt));
        model.add(IloNoOverlap(env, jim, tt));

        model.add(IloMinimize(env, cost));

        /* EXTRACTING THE MODEL AND SOLVING. */
        IloCP cp(model);
        if (cp.solve()) {
            cp.out() << "Solution with objective " << cp.getObjValue() << ":" << std::endl;
            for (i=0; i<allTasks.getSize(); ++i) {
                cp.out() << cp.domain(allTasks[i]) << std::endl;
            }
        } else {
            cp.out() << "No solution found. " << std::endl;
        }
    } catch (IloException& ex) {
        env.out() << "Error: " << ex << std::endl;
    }
    env.end();
    return 0;
}
Example #11
0
int
main (int argc, char **argv)
{
   char const *vmconfig = NULL;

   // Check command line length (exactly two arguments are required).
   if ( argc != 3 ) {
      usage (argv[0]);
      return -1;
   }

   // Pick up VMC from command line.
   vmconfig = argv[1];

   // Solve the model.
   int exitcode = 0;
   IloEnv env;
   try {
      // Create and read the model.
      IloModel model(env);
      IloCplex cplex(model);
      cplex.importModel(model, argv[2]);

      // Load the virtual machine configuration.
      // This will force solve() to use parallel distributed MIP.
      cplex.readVMConfig(vmconfig);

      // Solve the problem and display some results.
      if ( cplex.solve() )
         env.out() << "Solution value  = " << cplex.getObjValue() << endl;
      else
         env.out() << "No solution" << endl;
      env.out() << "Solution status = " << cplex.getStatus() << endl;

      // Cleanup.
      cplex.end();
      model.end();
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
      exitcode = -1;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
      exitcode = -1;
   }

   env.end();

   return exitcode;

}  // END main
Example #12
0
int main(int argc, const char *argv[]){
  IloEnv env;
  try {
    IloModel model(env);
    IloInt nbTransmitters = GetTransmitterIndex(nbCell, 0);
    IloIntVarArray freq(env, nbTransmitters, 0, nbAvailFreq - 1);
    freq.setNames("freq");
    for (IloInt cell = 0; cell < nbCell; cell++)
      for (IloInt channel1 = 0; channel1 < nbChannel[cell]; channel1++)
        for (IloInt channel2= channel1+1; channel2 < nbChannel[cell]; channel2++)
          model.add(IloAbs(  freq[GetTransmitterIndex(cell, channel1)]
                             - freq[GetTransmitterIndex(cell, channel2)] )
                    >= 16);
    for (IloInt cell1 = 0; cell1 < nbCell; cell1++)
      for (IloInt cell2 = cell1+1; cell2 < nbCell; cell2++)
        if (dist[cell1][cell2] > 0)
          for (IloInt channel1 = 0; channel1 < nbChannel[cell1]; channel1++)
            for (IloInt channel2 = 0; channel2 < nbChannel[cell2]; channel2++)
              model.add(IloAbs(  freq[GetTransmitterIndex(cell1, channel1)]
                                 - freq[GetTransmitterIndex(cell2, channel2)] )
                        >= dist[cell1][cell2]);
    
    // Minimizing the total number of frequencies
    IloIntExpr nbFreq = IloCountDifferent(freq); 
    model.add(IloMinimize(env, nbFreq));
    
    IloCP cp(model);
    cp.setParameter(IloCP::CountDifferentInferenceLevel, IloCP::Extended);
    cp.setParameter(IloCP::FailLimit, 40000);
    cp.setParameter(IloCP::LogPeriod, 100000);

    if (cp.solve()) {
      for (IloInt cell = 0; cell < nbCell; cell++) {
        for (IloInt channel = 0; channel < nbChannel[cell]; channel++)
          cp.out() << cp.getValue(freq[GetTransmitterIndex(cell, channel)])
                   << "  " ;
        cp.out() << std::endl;
      }
      cp.out() << "Total # of sites       " << nbTransmitters << std::endl;
      cp.out() << "Total # of frequencies " << cp.getValue(nbFreq) << std::endl;
    } else
      cp.out() << "No solution found."  << std::endl;
    cp.end();
  } catch (IloException & ex) {
    env.out() << "Caught: " << ex << std::endl;
  }
  env.end();
  return 0;
}
int
main()
{
   IloEnv env;
   try {
      IloModel model(env);
    
      setData(env);
      IloNumVarArray  inside(env, nbProds);
      IloNumVarArray outside(env, nbProds);
   
      IloObjective obj = IloAdd(model, IloMinimize(env));
    
      // Must meet demand for each product
    
      for(IloInt p = 0; p < nbProds; p++) {
         IloRange demRange = IloAdd(model,
                                    IloRange (env, demand[p], demand[p]));
         inside[p]  = IloNumVar(obj(insideCost[p])  + demRange(1));
         outside[p] = IloNumVar(obj(outsideCost[p]) + demRange(1));
      }
    
      // Must respect capacity constraint for each resource
    
      for(IloInt r = 0; r < nbResources; r++)
         model.add(IloScalProd(consumption[r], inside) <= capacity[r]);
    
      IloCplex cplex(env);
      cplex.extract(model);
    
      cplex.solve();
    
      if (cplex.getStatus() != IloAlgorithm::Optimal)
         cout << "No optimal solution" << endl;
    
      cout << "Solution status: " << cplex.getStatus() << endl;
      displayResults(cplex, inside, outside);
      cout << "----------------------------------------" << endl;
   }
   catch (IloException& ex) {
      cerr << "Error: " << ex << endl;
   }
   catch (...) {
      cerr << "Error" << endl;
   }
   env.end();
   return 0;
}
Example #14
0
int
main (int argc, char **argv)
{
   IloEnv   env;
   try {
      IloModel model(env);
      IloNumVarArray var(env);
      IloRangeArray con(env);
      IloRange add_con(env, 0.0, IloInfinity);

      populatebyrow (model, var, con);

      IloCplex cplex(model);

      // When a non-convex objective function is present, CPLEX will
      // raise an exception unless the parameter
      // IloCplex::Param::OptimalityTarget is set to accept first-order optimal
      // solutions
      cplex.setParam(IloCplex::Param::OptimalityTarget,
                     IloCplex::SolutionFirstOrder);

      // CPLEX may converge to either local optimum 
      solveanddisplay(env, cplex, var, con);

      // Add a constraint that cuts off the solution at (-1, 1)
      add_con.setExpr(var[0]);
      model.add(add_con);
      solveanddisplay(env, cplex, var, con);

      // Change the bounds of the newly added constraint to cut off
      // the solution at (1, 1)
      add_con.setBounds(-IloInfinity, 0.0);
      solveanddisplay(env, cplex, var, con);

      cplex.exportModel("indefqpex1.lp");
      
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();

   return 0;
}  // END main
Example #15
0
int
main (int argc, char **argv)
{
   IloEnv env;
   try {
      IloModel model(env);
      IloCplex cplex(env);

      if ( argc != 2 ) {
         usage (argv[0]);
         throw(-1);
      }

      IloObjective   obj;
      IloNumVarArray var(env);
      IloRangeArray  rng(env);
      IloSOS1Array   sos1(env);
      IloSOS2Array   sos2(env);
      IloRangeArray  lazy(env);
      IloRangeArray  cuts(env);

      cplex.importModel(model, argv[1], obj, var, rng, sos1, sos2, lazy, cuts);

      cplex.extract(model);

      if ( lazy.getSize() > 0 )  cplex.addLazyConstraints (lazy);
      if ( cuts.getSize() > 0 )  cplex.addUserCuts (cuts);

      cplex.solve();

      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;
}  // END main
Example #16
0
int LPsolveSE(const Problem<double>& P,const std::vector<int>& config,int time_limite) {  
  try{
    IloNum start,time_exec;
    const int nbTask = P.nbTask;
    const int E=2*nbTask;
    
    IloEnv env;
    IloModel model(env);
    IloNumVarMatrix x(env,nbTask),y(env,nbTask);
    IloNumVarMatrix b(env,nbTask), w(env,nbTask);
    IloNumVarArray t(env, E, 0, P.D, ILOFLOAT);
    
    // create cplex model
    createModel(P,env,model,t,x,y,b,w,config);
    for (int i=0;i<nbTask;++i){
      for (int e=0;e<E;++e){
	model.add(IloConversion(env,x[i][e],ILOFLOAT));
	model.add(IloConversion(env,y[i][e],ILOFLOAT));
      }
    }
    IloCplex cplex(model);
    setCplexParam(cplex,env,time_limite);
    start= cplex.getCplexTime();
      
    // solve !
    if (cplex.solve()) {	 
      time_exec=cplex.getCplexTime()-start;
      std::cout << "Final status: \t"<< cplex.getStatus() << std::endl;
      std::cout << "Final time: \t"<< time_exec << std::endl;
      std:: cout << "Final objective: " << cplex.getObjValue() <<"\nFinal gap: \t" 
		 << cplex.getMIPRelativeGap()<< std::endl;   
            env.end();
      return 0;
    }
    else if (cplex.getStatus()==IloAlgorithm::Infeasible){
      time_exec=cplex.getCplexTime()-start;
      std::cout << "status: "<< cplex.getStatus() << " en " << time_exec << std::endl;
    }
    env.end();
    return 1;
  }
  catch (IloException &e){
    std::cout << "Iloexception in solve" << e << std::endl;
    e.end();
    return  1;
  }
}
Example #17
0
int solveSE(const Problem<double>& P,Solution<double,double> &s,const std::vector<int>& config) {  
  try  {
    IloNum start,time_exec;
    const int n = P.nbTask;
    
    // create cplex model
    IloEnv env;
    IloModel model(env);
    IloNumVarMatrix x(env,n),y(env,n),b(env,n), w(env,n);
    IloNumVarArray t(env, 2*n, 0, P.D, ILOFLOAT);
    createModel(P,env,model,t,x,y,b,w,config);
    IloCplex cplex(model);
    setCplexParam(cplex,env,time_limit);

    start = cplex.getCplexTime();
    IloInt cpt=0;
    cplex.use(getFirstSolInfo(env,cpt,start));
    // solve !
    if (cplex.solve()) {	 
      time_exec=cplex.getCplexTime()-start;
      std::cout << "Final status: \t"<< cplex.getStatus() << " en " 
		<< time_exec << std::endl;
      std:: cout << "Final objective: " << cplex.getObjValue() 
		 <<"\nFinal gap: \t" << cplex.getMIPRelativeGap()
		 << std::endl;   
      modelToSol(P,s,cplex,t,x,y,b);
      env.end();
      return 0;
    }
    else if (cplex.getStatus()==IloAlgorithm::Infeasible){
      time_exec=cplex.getCplexTime()-start;
      std::cout << "status: "<< cplex.getStatus() << " en " 
		<< time_exec << std::endl;
    }
    env.end();
    return 1;
  }
  catch (IloException &e) {
    std::cout << "Iloexception in solve" << e << std::endl;
    e.end();
    return 1;
  } 
  catch (...){
    std::cout << "Error unknown\n";
    return 1;
  }
}
Example #18
0
int
main (int argc, char **argv)
{
   IloEnv   env;
   try {
      IloModel model(env);
      IloCplex cplex(env);

      if ( argc != 2 ) {
         usage (argv[0]);
         throw(-1);
      }

      IloObjective   obj;
      IloNumVarArray var(env);
      IloRangeArray  rng(env);
      cplex.importModel(model, argv[1], obj, var, rng);

      cplex.use(Rounddown(env, var));

      cplex.extract(model);
      // Check model is all binary except for objective constant variable
      if ( cplex.getNbinVars() < cplex.getNcols() - 1 ) {
         cerr << "Problem contains non-binary variables, exiting." << endl;
         throw (-1);
      }
      cplex.setParam(IloCplex::Param::MIP::Strategy::Search,
                     IloCplex::Traditional);
      cplex.solve();

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;
      env.out() << "Values          = " << vals << endl;
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;
}  // END main
Example #19
0
int setCplexParam(IloCplex& cplex, IloEnv& env, int time_limit){
  //Set cplex parameter
  cplex.setParam(IloCplex::TiLim, time_limit);
  cplex.setParam(IloCplex::Threads, 2);
  cplex.setOut(env.getNullStream());
  cplex.setParam(IloCplex::PreLinear, 0);

  return 0;
}
Example #20
0
int
main (int argc, char **argv)
{
   IloEnv env;
   try {
      IloModel model(env);
      IloCplex cplex(env);
    
      if ( argc != 2 ) {
         usage (argv[0]);
         throw(-1);
      }
    
      IloObjective   obj;
      IloNumVarArray var(env);
      IloRangeArray  rng(env);
      cplex.importModel(model, argv[1], obj, var, rng);
    
      cplex.extract(model); 
      cplex.setParam(IloCplex::Param::MIP::Strategy::Search,
                     IloCplex::Traditional);
    
      IloCplex::Goal iiSumGoal = IloCplex::Apply(cplex, 
                                                 MyBranchGoal(env, var), 
                                                 IISumEvaluator());
      IloCplex::Goal depthGoal = IloCplex::Apply(cplex,
                                                 iiSumGoal,
                                                 DepthEvaluator());
      cplex.solve(depthGoal);
    
      IloNumArray vals(env);
      cplex.getValues(vals, var);
      cout << "Solution status = " << cplex.getStatus() << endl;
      cout << "Solution value  = " << cplex.getObjValue() << endl;
      cout << "Values          = " << vals << endl;
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }

   env.end();

   return 0;
}
Example #21
0
int main(int argc, const char * argv[]) {
  IloEnv env;
  try {
    IloModel model(env);

    IloInt n = 10;
    if (argc > 1)
      n = atoi(argv[1]);
    if ((n % 2) == 1)
      n++;
    env.out() << "Finding schedule for " << n << " teams" << std::endl;

    //Calculate the data
    //Declare the game, home team and away team variables
    //Calculate the allowed tuples
    //Add the constraint on allowed combinations

    //Add the alldiff constraint
    //Add the inverse constraint
    //Add the week of game constraint

    //Create the different halves constraint
    //Create the distance constraint
    //Add max break length constraints
    //Add the constraint on first and last weeks

    //Add the objective

    //Add surrogate constraints
    //Add more surrogate constraints
    //Add constraints to fix first week
    //Add the slot order constraint

    //Create an instance of IloCP
    //Add the time limit
    //Create the search selectors
    //Create the tuning object
    //Search for a solution

      cp.out() << "Solution at " << cp.getValue(breaks) << std::endl;
    }
    cp.endSearch();
    cp.end();
  } catch (IloException & ex) {
Example #22
0
int
main (void) {
   IloEnv env;
   try {
      IloModel model(env);

      IloNumVarArray var(env);
      IloRangeArray con(env);
      populatebyrow (model, var, con);

      IloCplex cplex(model);
      IloNumVarArray ordvar(env, 2);
      IloNumArray    ordpri(env, 2);
      ordvar[0] = var[1]; ordvar[1] = var[3];
      ordpri[0] = 8.0;    ordpri[1] = 7.0;
      cplex.setPriorities (ordvar, ordpri);
      cplex.setDirection(var[1], IloCplex::BranchUp);
      cplex.setDirection(var[3], IloCplex::BranchDown);
      cplex.solve();

      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, con);
      env.out() << "Slacks        = " << vals << endl;

      cplex.exportModel("mipex3.lp");
      cplex.writeOrder("mipex3.ord");
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;

}  // END main
Example #23
0
int
main (int argc, char **argv)
{
    IloEnv   env;
    try {
        IloModel model(env);
        IloCplex cplex(env);

        if ( argc != 2 ) {
            usage (argv[0]);
            throw(-1);
        }

        IloObjective   obj;
        IloNumVarArray var(env);
        IloRangeArray  rng(env);
        IloSOS1Array   sos1(env);
        IloSOS2Array   sos2(env);
        cplex.importModel(model, argv[1], obj, var, rng, sos1, sos2);

        cplex.use(SOSbranch(env, sos1));
        cplex.setParam(IloCplex::Param::MIP::Strategy::Search,
                       IloCplex::Traditional);

        cplex.extract(model);
        cplex.solve();

        IloNumArray vals(env);
        cplex.getValues(vals, var);
        env.out() << "Solution status = " << cplex.getStatus() << endl;
        env.out() << "Solution value  = " << cplex.getObjValue() << endl;
        env.out() << "Values          = " << vals << endl;
    }
    catch (IloException& e) {
        cerr << "Concert exception caught: " << e << endl;
    }
    catch (...) {
        cerr << "Unknown exception caught" << endl;
    }

    env.end();
    return 0;
}  // END main
Example #24
0
int
main (void)
{
   IloEnv env;
   int retval = -1;

   try {
      // Create the model.
      IloModel model(env);
      IloCplex cplex(env);
      IloObjective obj(env);
      IloNumVarArray vars(env);
      IloRangeArray rngs(env);
      IloIntArray cone(env);
      createmodel(model, obj, vars, rngs, cone);

      // Extract model.
      cplex.extract(model);

      // Solve the problem. If we cannot find an _optimal_ solution then
      // there is no point in checking the KKT conditions and we throw an
      // exception.
      cplex.setParam(IloCplex::Param::Barrier::QCPConvergeTol, CONVTOL);
      if ( !cplex.solve() || cplex.getStatus() != IloAlgorithm::Optimal )
         throw string("Failed to solve problem to optimality");

      // Test the KKT conditions on the solution.
      if ( !checkkkt (cplex, obj, vars, rngs, cone, TESTTOL) ) {
         env.error() << "Testing of KKT conditions failed." << endl;
      }
      else {
         env.out() << "Solution status: " << cplex.getStatus() << endl;
         env.out() << "KKT conditions are satisfied." << endl;
         retval = 0;
      }

      env.end();
   } catch (IloException &e) {
      cerr << "IloException: " << e << endl;
      if (env.getImpl())
         env.end();
      ::abort();
   } catch (string& e) {
      cerr << e << endl;
      if (env.getImpl())
         env.end();
      ::abort();
   }
   return retval;
}
Example #25
0
void profit_solve(graph g, vector<int>& allocation, vector<double>& pricing, bool integer) {
  int **columns = (int **)malloc(g->bidders * sizeof(int *));
  for(int i = 0; i < g->bidders; i++)
    columns[i] = (int *)calloc(g->items, sizeof(int));
  IloEnv env;
  try {
    if(getVerbosity() != CplexOutput)
      env.setOut(env.getNullStream());
    IloModel model(env);
    IloNumVarArray x(env);
    IloNumVarArray p(env);
    IloNumVarArray z(env);
    IloCplex cplex(model);
    profit_create_vars(g, model, x, p, z, columns);
    profit_create_objective(g, model, x, z, columns);
    model.add(profit_constraints(g, model, x, p, z, columns));
    config_cplex(cplex); 
    if(!integer) {
      model.add(IloConversion(env, x, ILOFLOAT));
    } else {
      profit_load(g, cplex, model, x, p, z, columns, allocation, pricing);
    }
    clock_start();
    if (!cplex.solve()) {
      failed_print(g);
    } else {
      if(integer)
        solution_print(cplex, env, g);  
      else
        relax_print(cplex, env);
    }
  }
  catch (IloException& e) {
    cerr << "Concert exception caught: " << e << endl;
  }
  catch (...) {
    cerr << "Unknown exception caught" << endl;
  }
}
Example #26
0
int
main (int argc, char **argv)
{
   IloEnv   env;
   try {
      IloModel model(env);
      IloNumVarArray var(env);
      IloRangeArray con(env);

      populatebyrow (model, var, con);

      IloCplex cplex(model);

      // 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;

      cplex.exportModel("qpex1.lp");
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();

   return 0;
}  // END main
Example #27
0
int
main (int argc, char **argv)
{
   IloEnv env;
   try {
      IloModel model(env, "example");

      IloNumVarArray var(env);
      IloRangeArray  rng(env);
      populatebycolumn (model, var, rng);

      IloCplex cplex(model);
      cplex.setOut(env.getNullStream());
      cplex.setParam(IloCplex::Param::RootAlgorithm, IloCplex::Primal);
      cplex.use(MyCallback(env));
      cplex.solve();

      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, rng);
      env.out() << "Slacks        = " << vals << endl;
      cplex.getDuals(vals, rng);
      env.out() << "Duals         = " << vals << endl;
      cplex.getReducedCosts(vals, var);
      env.out() << "Reduced Costs = " << vals << endl;

      cplex.exportModel("lpex4.lp");
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;
}  // END main
Example #28
0
int
main(int argc, char** argv)
{
   IloEnv env;
   try {
      IloModel m(env);
      IloCplex cplex(env);
    
      IloObjective   obj;
      IloNumVarArray var(env);
      IloRangeArray  con(env);

      const char* datadir = (argc >= 2) ? argv[1] : "../../../examples/data";
      char *fname = new char[strlen(datadir) + 1 + strlen("noswot.mps") + 1];
      sprintf(fname, "%s/noswot.mps", datadir);
      env.out() << "reading " << fname << endl;
      cplex.importModel(m, fname, obj, var, con);
      delete[] fname;
    
      env.out() << "constructing cut callback ..." << endl;
      
      IloExprArray lhs(env);
      IloNumArray  rhs(env);
      makeCuts(var, lhs, rhs);
      cplex.use(CtCallback(env, lhs, rhs, cplex.getParam(
         IloCplex::Param::Simplex::Tolerances::Feasibility)));
    
      env.out() << "extracting model ..." << endl;
      cplex.extract(m);
    
      cplex.setParam(IloCplex::Param::MIP::Interval, 1000);
      cplex.setParam(IloCplex::Param::MIP::Strategy::Search,
                     IloCplex::Traditional);
      env.out() << "solving model ...\n";
      cplex.solve();
      env.out() << "solution status is " << cplex.getStatus() << endl;
      env.out() << "solution value  is " << cplex.getObjValue() << endl;
   }
   catch (IloException& ex) {
      cerr << "Error: " << ex << endl;
   }
   env.end();
   return 0;
}
Example #29
0
int
main (int argc, char **argv)
{
   IloEnv   env;
   try {
      IloModel model(env, "example");

      IloNumVarArray var(env);
      IloRangeArray  rng(env);
      populatebycolumn (model, var, rng);

      IloCplex cplex(model);

      IloCplex::BasisStatusArray cstat(env), rstat(env);
      cstat.add(IloCplex::AtUpper);
      cstat.add(IloCplex::Basic);
      cstat.add(IloCplex::Basic);
      rstat.add(IloCplex::AtLower);
      rstat.add(IloCplex::AtLower);
      cplex.setBasisStatuses(cstat, var, rstat, rng);
      cplex.solve();

      cplex.out() << "Solution status = " << cplex.getStatus() << endl;
      cplex.out() << "Solution value  = " << cplex.getObjValue() << endl;
      cplex.out() << "Iteration count = " << cplex.getNiterations() << endl;

      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values        = " << vals << endl;
      cplex.getSlacks(vals, rng);
      env.out() << "Slacks        = " << vals << endl;
      cplex.getDuals(vals, rng);
      env.out() << "Duals         = " << vals << endl;
      cplex.getReducedCosts(vals, var);
      env.out() << "Reduced Costs = " << vals << endl;

      cplex.exportModel("lpex6.lp");
   }
   catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
   }
   catch (...) {
      cerr << "Unknown exception caught" << endl;
   }

   env.end();
   return 0;
}  // END main
int main (void) {
  IloEnv env;
  try {

      IloModel model(env); // declaration du nodel
      IloNumVarArray var(env); // liste des variables de decision
      IloRangeArray con(env); // liste des contraintes a ajouter a notre probleme
      IloNumVarArray duals(env);

      populatebyrow (model, var, con); // remplir les variables avec les valeurs adequat
      IloCplex cplex(model);
      cplex.solve();

      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;
      IloNumArray vals(env);
      cplex.getValues(vals, var);
      env.out() << "Values = " << vals << endl;
      cplex.getSlacks(vals, con);
      env.out() << "Slacks = " << vals << endl;
      


      model.add(var[0] == 0);
      cplex.solve();
      cplex.getValues(vals, var);
      env.out() << "Solution status = " << cplex.getStatus() << endl;
      env.out() << "Solution value  = " << cplex.getObjValue() << endl;
      env.out() << "Values = " << vals << endl;

      //cplex.exportModel("pl.lp");
    }
    catch (IloException& e) {
      cerr << "Concert exception caught: " << e << endl;
    }
    catch (...) {
      cerr << "Unknown exception caught" << endl;
    }
  env.end();
  return 0;
}