Пример #1
0
void profit_create_vars(graph g, IloModel model, IloNumVarArray x, IloNumVarArray p, IloNumVarArray z, int **columns) {
  IloEnv env = model.getEnv();
  string s;
  assignment_vars(g, model, x, columns);  
  for(int j = 0; j < g->items; j++) {
    s = "p_" + itos(j);
    p.add(IloNumVar(env, 0.0, boundp(j), ILOFLOAT, s.c_str()));
    // p.add(IloNumVar(env, lowerp(j), boundp(j), ILOFLOAT, s.c_str()));
  }
  for(int i = 0; i < g->bidders; i++) {
    s = "z_" + itos(i);
    z.add(IloNumVar(env, 0.0, boundu(i), ILOFLOAT, s.c_str()));
  }
}
Пример #2
0
void addInitColumn(IloNumVarArray lambda, IloObjective rmpObj, 
	IloRangeArray maintConEng, IloRangeArray removeMod, IloRangeArray convex, 
	IloNumArray2 addXCol, IloNumArray addZCol, 
	const IloNumArray compCosts, const IloNumArray convexityCoef) {

	// loop counter
	IloInt t;		

	// counter for objective function coefficient for lambda
	// variable to be added.
	IloNum lambdaObjCoef = 0;

	// function assumes addXCol and addZCol contains proper values.

	// calculate objective function coefficient lambdaObjCoef
	for (t = 0; t < TIME_SPAN; t++) {

		// for each fixed t: scalar product of x[m]* vector and
		// component costs vector compCosts[m]:
		lambdaObjCoef += IloScalProd(addXCol[t],compCosts);

		// also clear the addXCol subarrays as soon as they
		// have been used
		addXCol[t].clear();
	}
			
	// now add this column and it's associated lambda variable to the RMP.
	lambda.add(IloNumVar(rmpObj(lambdaObjCoef) + maintConEng(addZCol) + 
		removeMod(addZCol) + convex(convexityCoef), 0.0, 1.0));

	// clear addZCol num array.
	addZCol.clear();

} // END of addColumn
Пример #3
0
static void
populatebycolumn (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   IloObjective obj = IloMaximize(env);
   c.add(IloRange(env, -IloInfinity, 20.0));
   c.add(IloRange(env, -IloInfinity, 30.0));

   x.add(IloNumVar(obj(1.0) + c[0](-1.0) + c[1]( 1.0), 0.0, 40.0));
   x.add(obj(2.0) + c[0]( 1.0) + c[1](-3.0));
   x.add(obj(3.0) + c[0]( 1.0) + c[1]( 1.0));

   model.add(obj);
   model.add(c);
}  // END populatebycolumn
Пример #4
0
void addColumn(IloCplex subSolver, IloNumVarArray2 x, IloNumVarArray z, IloNumVarArray lambda,
	IloObjective rmpObj, IloRangeArray maintConEng, IloRangeArray removeMod,
	IloRangeArray convex, IloNumArray2 addXCol, IloNumArray addZCol, 
	const IloNumArray compCosts, const IloNumArray convexityCoef) {

	// loop counter
	IloInt t;		

	// counter for objective function coefficient for lambda
	// variable to be added.
	IloNum lambdaObjCoef = 0;

	// extract subproblem-optimal solution values
	// (into IloNumArrays addXCol (2d) and addZCol (1d)).

	// z values:
	subSolver.getValues(addZCol,z);

	//cout << endl << endl << "z = " << endl << addZCol << endl;
	//cin.get();

	// !!! OBS !!!
	// here we might want to save these z values some column pool's custom-nitted class
	// array. Or to be specific, we want to add the indexes for NON-ZERO-ENTRIES in addZCol
	// to our class that keep place of columns.
	// E.g., given variable lambda(m)_(q_m), we want to know in our own class object,
	// given (m)(q_m), the indexes of non-zeros in that Z column.
	


	// and for each t...
	for (t = 0; t < TIME_SPAN; t++) {

		// x values:
		subSolver.getValues(addXCol[t],x[t]);
		//cout << endl << endl << "x[t=" << t << "] =" << endl << addXCol[t] << endl;
	}
	//cin.get();

	// calculate objective function coefficient lambdaObjCoef
	for (t = 0; t < TIME_SPAN; t++) {

		// for each fixed t: scalar product of x[m]* vector and
		// component costs vector compCosts[m]:
		lambdaObjCoef += IloScalProd(addXCol[t],compCosts);

		// also clear the addXCol subarrays as soon as they
		// have been used
		addXCol[t].clear();
	}
			
	// now add this column and it's associated lambda variable to the RMP.
	lambda.add(IloNumVar(rmpObj(lambdaObjCoef) + maintConEng(addZCol) + 
		removeMod(addZCol) + convex(convexityCoef), 0.0, 1.0));

	// clear addZCol num array.
	addZCol.clear();

} // END of addColumn
Пример #5
0
static void
populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   x.add(IloNumVar(env, 0.0, 40.0));
   x.add(IloNumVar(env));
   x.add(IloNumVar(env));
   x.add(IloNumVar(env, 2.0, 3.0, ILOINT));
   model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2] + x[3]));

   c.add( - x[0] +     x[1] + x[2] + 10 * x[3] <= 20);
   c.add(   x[0] - 3 * x[1] + x[2]             <= 30);
   c.add(              x[1]        - 3.5* x[3] == 0);
   model.add(c);

}  // END populatebyrow
Пример #6
0
static void
populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   x.add(IloNumVar(env, 0.0, 40.0));
   x.add(IloNumVar(env));
   x.add(IloNumVar(env));
   model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2]
                            - 0.5 * (33*x[0]*x[0] + 22*x[1]*x[1] +
                                     11*x[2]*x[2] - 12*x[0]*x[1] -
                                     23*x[1]*x[2]                 ) ));

   c.add( - x[0] +     x[1] + x[2] <= 20);
   c.add(   x[0] - 3 * x[1] + x[2] <= 30);
   model.add(c);
}  // END populatebyrow
Пример #7
0
void PopulateFromGraph(IloModel model, IloNumVarArray s, IloRangeArray c){
  IloEnv env = model.getEnv();

  // Used n+1 for accomodating an extra variable
  // For being able to write the objective function
  for(int i = 0; i <= n; i++)
    s.add(IloNumVar(env));
}
void generateProblem(const ILPModel& m, IloModel& model, IloNumVarArray& x, IloRangeArray& con)	{
    IloEnv env = model.getEnv();
    IloObjective obj = (m.obj == MINIMIZE ? IloMinimize(env) : IloMaximize(env));
    for (unsigned long v = 0; v < m.numberOfVariables(); ++v)	{
        switch (m.x[v].type)	{
        case FLT:
            x.add(IloNumVar(env, m.x[v].lowerBound, m.x[v].upperBound, IloNumVar::Float));
            break;
        case BIN:
            x.add(IloNumVar(env, m.x[v].lowerBound, m.x[v].upperBound, IloNumVar::Bool));
            break;
        default:
            x.add(IloNumVar(env, m.x[v].lowerBound, m.x[v].upperBound, IloNumVar::Int));
        }

        obj.setLinearCoef(x[v], m.c[v]);
        x[v].setName(m.varDesc[v].c_str());
    }

    for (unsigned long c = 0; c < m.numberOfConstraints(); ++c)	{
        switch (m.ops[c])	{
        case LESS_EQUAL:
            con.add(IloRange(env, -IloInfinity, m.b[c]));
            break;
        case EQUAL:
            con.add(IloRange(env, m.b[c], m.b[c]));
            break;
        case GREATER_EQUAL:
            con.add(IloRange(env, m.b[c], IloInfinity));
        }

        for (const pair<uint32_t, double>& p : m.A[c])
            con[c].setLinearCoef(x[p.first], p.second);

        con[c].setName(m.conDesc[c].c_str());
    }

    model.add(obj);
    model.add(con);
}
Пример #9
0
static void
populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   x.add(IloNumVar(env, -1.0, 1.0));
   x.add(IloNumVar(env,  0.0, 1.0));
   model.add(IloMinimize(env, 0.5 * (-3*x[0]*x[0] - 3*x[1]*x[1] +
                                       - 1*x[0]*x[1]               ) ));

   c.add( - x[0] + x[1] >= 0);
   c.add(   x[0] + x[1] >= 0);
   model.add(c);
}  // END populatebyrow
Пример #10
0
static void
populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   x.add(IloNumVar(env, 0.0, 40.0));
   x.add(IloNumVar(env, 0.0, IloInfinity, ILOINT));
   x.add(IloNumVar(env, 0.0, IloInfinity, ILOINT));
   x.add(IloNumVar(env, 2.0, 3.0, ILOINT));
   model.add(IloMaximize(env, x[0] + 2 * x[1] + 3 * x[2] + x[3]));

   c.add( - x[0] +     x[1] + x[2] + 10 * x[3] <= 20);
   c.add(   x[0] - 3 * x[1] + x[2]             <= 30);
   c.add(              x[1]        - 3.5* x[3] == 0);
   model.add(c);

   IloNumVarArray sosvar(env, 2);
   IloNumArray    sosval(env, 2);
   sosvar[0] = x[2]; sosvar[1] = x[3];
   sosval[0] = 25.0; sosval[1] = 18.0;

   model.add(IloSOS1(model.getEnv(), sosvar, sosval));

}  // END populatebyrow
Пример #11
0
// This function creates the following model:
//   Minimize
//    obj: x1 + x2 + x3 + x4 + x5 + x6
//   Subject To
//    c1: x1 + x2      + x5      = 8
//    c2:           x3 + x5 + x6 = 10
//    q1: [ -x1^2 + x2^2 + x3^2 ] <= 0
//    q2: [ -x4^2 + x5^2 ] <= 0
//   Bounds
//    x2 Free
//    x3 Free
//    x5 Free
//   End
// which is a second order cone program in standard form.
// The function returns objective, variables and constraints in the
// values obj, vars and rngs.
// The function also sets up cone so that for a column j we have
// cone[j] >= 0               Column j is in a cone constraint and is the
//                            cone's head variable.
// cone[j] == NOT_CONE_HEAD   Column j is in a cone constraint but is
//                            not the cone's head variable..
// cone[j] == NOT_IN_CONE     Column j is not contained in any cone constraint.
static void
createmodel (IloModel& model, IloObjective &obj, IloNumVarArray &vars,
             IloRangeArray &rngs, IloIntArray& cone)
{
   // The indices we assign as user objects to the modeling objects.
   // We define them as static data so that we don't have to worry about
   // dynamic memory allocation/leakage.
   static int indices[] = { 0, 1, 2, 3, 4, 5, 6 };

   IloEnv env = model.getEnv();

   // Create variables.
   IloNumVar x1(env,            0, IloInfinity, "x1");
   IloNumVar x2(env, -IloInfinity, IloInfinity, "x2");
   IloNumVar x3(env, -IloInfinity, IloInfinity, "x3");
   IloNumVar x4(env,            0, IloInfinity, "x4");
   IloNumVar x5(env, -IloInfinity, IloInfinity, "x5");
   IloNumVar x6(env,            0, IloInfinity, "x6");

   // Create objective function and immediately store it in return value.
   obj = IloMinimize(env, x1 + x2 + x3 + x4 + x5 + x6);

   // Create constraints.
   IloRange c1(env, 8,  x1 + x2      + x5,       8, "c1");
   IloRange c2(env, 10,           x3 + x5 + x6, 10, "c2");
   IloRange q1(env, -IloInfinity, -x1*x1 + x2*x2 + x3*x3, 0, "q1");
   cone.add(2);             // x1, cone head of constraint at index 2
   cone.add(NOT_CONE_HEAD); // x2
   cone.add(NOT_CONE_HEAD); // x3
   IloRange q2(env, -IloInfinity, -x4*x4 + x5*x5, 0, "q2");
   cone.add(3);             // x4, cone head of constraint at index 3
   cone.add(NOT_CONE_HEAD); // x5

   cone.add(NOT_IN_CONE);   // x6

   // Setup model.
   model.add(obj);
   model.add(obj);
   model.add(c1);
   model.add(c2);
   model.add(q1);
   model.add(q2);

   // Setup return values.
   vars.add(x1);
   vars.add(x2);
   vars.add(x3);
   vars.add(x4);
   vars.add(x5);
   vars.add(x6);

   rngs.add(c1);
   rngs.add(c2);
   rngs.add(q1);
   rngs.add(q2);

   // We set the user object for each modeling object to its index in the
   // respective array. This makes the code in checkkkt a little simpler.
   for (IloInt i = 0; i < vars.getSize(); ++i)
      vars[i].setObject(&indices[i]);
   for (IloInt i = 0; i < rngs.getSize(); ++i)
      rngs[i].setObject(&indices[i]);
}
Пример #12
0
// This routine set up the IloCplex algorithm to solve the worker LP, and
// creates the worker LP (i.e., the dual of flow constraints and
// capacity constraints of the flow MILP)
//
// Modeling variables:
// forall k in V0, i in V:
//    u(k,i) = dual variable associated with flow constraint (k,i)
//
// forall k in V0, forall (i,j) in A:
//    v(k,i,j) = dual variable associated with capacity constraint (k,i,j)
//
// Objective:
// 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)
//
// Constraints:
// forall k in V0, forall (i,j) in A: u(k,i) - u(k,j) <= v(k,i,j)
//
// Nonnegativity on variables v(k,i,j)
// forall k in V0, forall (i,j) in A: v(k,i,j) >= 0
//
void
createWorkerLP(IloCplex cplex, IloNumVarArray v, IloNumVarArray u, 
               IloObjective obj, IloInt numNodes)
{

   IloInt i, j, k;
   IloEnv env = cplex.getEnv();
   IloModel mod(env, "atsp_worker"); 

   // Set up IloCplex algorithm to solve the worker LP

   cplex.extract(mod);
   cplex.setOut(env.getNullStream());
      
   // Turn off the presolve reductions and set the CPLEX optimizer
   // to solve the worker LP with primal simplex method.

   cplex.setParam(IloCplex::Reduce, 0);
   cplex.setParam(IloCplex::RootAlg, IloCplex::Primal); 
   
   // Create variables v(k,i,j) forall k in V0, (i,j) in A
   // For simplicity, also dummy variables v(k,i,i) are created.
   // Those variables are fixed to 0 and do not partecipate to 
   // the constraints.

   IloInt numArcs  = numNodes * numNodes;
   IloInt vNumVars = (numNodes-1) * numArcs;
   IloNumVarArray vTemp(env, vNumVars, 0, IloInfinity);
   for (k = 1; k < numNodes; ++k) {
      for (i = 0; i < numNodes; ++i) {
         vTemp[(k-1)*numArcs + i *numNodes + i].setBounds(0, 0);
      }
   }
   v.clear();
   v.add(vTemp);
   vTemp.end();
   mod.add(v);

   // Set names for variables v(k,i,j) 

   for (k = 1; k < numNodes; ++k) {
      for(i = 0; i < numNodes; ++i) {
         for(j = 0; j < numNodes; ++j) {
            char varName[100];
            sprintf(varName, "v.%d.%d.%d", (int) k, (int) i, (int) j); 
            v[(k-1)*numArcs + i*numNodes + j].setName(varName);
         }
      }
   }
   
   // Associate indices to variables v(k,i,j)

   IloIntArray vIndex(env, vNumVars);
   for (j = 0; j < vNumVars; ++j)
   {
      vIndex[j] = j;
      v[j].setObject(&vIndex[j]);
   }

   // Create variables u(k,i) forall k in V0, i in V

   IloInt uNumVars = (numNodes-1) * numNodes;
   IloNumVarArray uTemp(env, uNumVars, -IloInfinity, IloInfinity);
   u.clear();
   u.add(uTemp);
   uTemp.end();
   mod.add(u);

   // Set names for variables u(k,i) 

   for (k = 1; k < numNodes; ++k) {
      for(i = 0; i < numNodes; ++i) {
         char varName[100];
         sprintf(varName, "u.%d.%d", (int) k, (int) i); 
         u[(k-1)*numNodes + i].setName(varName);
      }
   }

   // Associate indices to variables u(k,i)

   IloIntArray uIndex(env, uNumVars);
   for (j = 0; j < uNumVars; ++j)
   {
      uIndex[j] = vNumVars + j;
      u[j].setObject(&uIndex[j]);
   }

   // Initial objective function is empty

   obj.setSense(IloObjective::Minimize);
   mod.add(obj);

   // Add constraints:
   // forall k in V0, forall (i,j) in A: u(k,i) - u(k,j) <= v(k,i,j)

   for (k = 1; k < numNodes; ++k) {
      for(i = 0; i < numNodes; ++i) {
         for(j = 0; j < numNodes; ++j) {
            if ( i != j ) {
               IloExpr expr(env);
               expr -= v[(k-1)*numArcs + i*(numNodes) + j];
               expr += u[(k-1)*numNodes + i];
               expr -= u[(k-1)*numNodes + j];
               mod.add(expr <= 0);
               expr.end();
            }
         }
      }
   }

}// END createWorkerLP
Пример #13
0
   Example(IloEnv env)
      : nblocks(0), model(env), vars(env), ranges(env)
   {
      // Model data.
      // fixed[] is the fixed cost for opening a facility,
      // cost[i,j] is the cost for serving customer i from facility j.
      static double const fixed[] = { 2.0, 3.0, 3.0 };
      static double const cost[] = { 2.0, 3.0, 4.0, 5.0, 7.0,
                                     4.0, 3.0, 1.0, 2.0, 6.0,
                                     5.0, 4.0, 2.0, 1.0, 3.0 };
#define NFACTORY ((CPXDIM)(sizeof(fixed) / sizeof(fixed[0])))
#define NCUSTOMER ((CPXDIM)((sizeof(cost) / sizeof(cost[0])) / NFACTORY))
      nblocks = NCUSTOMER;

      IloExpr obj(env);
      // Create integer y  variables.
      IloNumVarArray y(env);
      for (IloInt f = 0; f < NFACTORY; ++f) {
         std::stringstream s;
         s << "y" << f;
         IloIntVar v(env, 0, 1, s.str().c_str());
         obj += fixed[f] * v;
         objMap[v] = fixed[f];
         y.add(v);
         blockMap.insert(BlockMap::value_type(v, -1));
         intersectMap.insert(IntersectMap::value_type(v, RowSet()));
      }

      // Create continuous x variables.
      IloNumVarArray x(env);
      for (IloInt f = 0; f < NFACTORY; ++f) {
         for (IloInt c = 0; c < NCUSTOMER; ++c) {
            std::stringstream s;
            s << "x" << f << "#" << c;
            IloNumVar v(env, 0.0, IloInfinity, s.str().c_str());
            obj += v * cost[f * NCUSTOMER + c];
            objMap[v] = cost[f * NCUSTOMER + c];
            x.add(v);
            blockMap.insert(BlockMap::value_type(v, c));
            intersectMap.insert(IntersectMap::value_type(v, RowSet()));
         }
      }
      vars.add(y);
      vars.add(x);
      model.add(vars);

      // Add objective function.
      model.add(IloMinimize(env, obj, "obj"));
      objSense = IloObjective::Minimize;
      obj.end();

      // Satisfy each customer's demand.
      for (IloInt c = 0; c < NCUSTOMER; ++c) {
         std::stringstream s;
         s << "c1_" << c;
         IloRange r(env, 1.0, IloInfinity, s.str().c_str());
         IloExpr lhs(env);
         for (IloInt f = 0; f < NFACTORY; ++f) {
            lhs += x[f * NCUSTOMER + c];
            intersectMap[x[f * NCUSTOMER + c]].insert(r);
         }
         r.setExpr(lhs);
         ranges.add(r);
         lhs.end();
      }

      // A factory must be open if we service from it.
      for (IloInt c = 0; c < NCUSTOMER; ++c) {
         for (IloInt f = 0; f < NFACTORY; ++f) {
            std::stringstream s;
            s << "c2_" << c << "#" << f;
            IloRange r(env, 0.0, IloInfinity, s.str().c_str());
            intersectMap[x[f * NCUSTOMER + c]].insert(r);
            intersectMap[y[f]].insert(r);
            r.setExpr(-x[f * NCUSTOMER + c] + y[f]);
            ranges.add(r);
         }
      }

      // Capacity constraint.
      IloRange r(env, -IloInfinity, NFACTORY - 1, "c3");
      IloExpr lhs(env);
      for (IloInt f = 0; f < NFACTORY; ++f) {
         lhs += y[f];
         intersectMap[y[f]].insert(r);
      }
      r.setExpr(lhs);
      ranges.add(r);
      lhs.end();

      model.add(ranges);

#undef NFACTORY
#undef NCUSTOMER
   }
Пример #14
0
void LpSolver::populatebyrow (CplexConverter& cplexConverter, 
	IloModel model, IloNumVarArray x, IloRangeArray c)
{
	IloEnv env = model.getEnv();
	// CAPITAL LETTERS MEAN I NEED YOUR HELP, here is help 

	// IloExpr cost(env);
	
	// Create Variables
	// cout << "size of var: " << cplexConverter.variables.size() << endl;
	for (int i = 0; i < cplexConverter.variables.size(); ++i){
		IloNumVar iloVar(env, 0.0, cplexConverter.capacities[i], IloNumVar::Int);
		// cout << iloVar << endl;
		x.add(iloVar);
	}

	//Capacity Constraints
	for (auto &it : cplexConverter.atomicIdToVarIdDict){
		IloExpr t(env);
		// cout << "adding constraint ";
		for (int j = 0; j < it.second.size(); j++){
			// cout << "x[" << it.second[j] << "] + ";
			t += x[it.second[j]];
		}
		// cout << endl;
		c.add(t <= cplexConverter.graph->atomicEdges[it.first]->capacity);
		// cout << c << endl;
		t.end();
	}

	// other constraints
	for (auto nodePair : cplexConverter.graph->nodes){

		// For all nodes
		Node* n = nodePair.second;

		if(n == cplexConverter.src){

			// source constraints
			// IloExpr inFlow(env);
			IloExpr outFlow(env);	
			for(auto &atoIn : n->atomicEdge_in){
				int aeId = atoIn.second->atomicEdgeId;
				for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){
					// var Id
					int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
					outFlow += x[vId];
					// cost += cplexConverter.graph->atomicEdges[cplexConverter.variables[vId].atomicEdgeId]->interest_rate * x[vId];
				}
			}
			for (auto &atoOut : n->atomicEdge_out){
				int aeId = atoOut.second->atomicEdgeId;
				for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){
					// var Id
					int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
					// inFlow += x[vId];
					c.add(x[vId] == 0);
					// cost -= cplexConverter.graph->atomicEdges[cplexConverter.variables[vId].atomicEdgeId]->interest_rate * x[vId];
				}
			}

			c.add(outFlow == cplexConverter.request);
			// inFlow.end();
			outFlow.end();

		} else if(n == cplexConverter.dest){

			// destination constraints
			IloExpr inFlow(env);
			// IloExpr outFlow(env);
			for(auto &atoIn : n->atomicEdge_in){
				int aeId = atoIn.second->atomicEdgeId;
				for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){
					// var Id
					int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
					// outFlow += x[vId];
					c.add(x[vId] == 0);
				}
			}
			for (auto &atoOut : n->atomicEdge_out){
				int aeId = atoOut.second->atomicEdgeId;
				for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){
					// var Id
					int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
					inFlow += x[vId];
				}
			}

			c.add(inFlow == cplexConverter.request);
			inFlow.end();
			// outFlow.end();

		} else {

			// Monotonicity Constraints
			for (int i = 0; i < credNetConstants.totalIrs.size(); ++i){
				IloExpr tempin(env);
				IloExpr tempout(env);

				for (auto &atoIn : n->atomicEdge_in){
					int aeId = atoIn.second->atomicEdgeId;
					for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){

						// var Id
						int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
						if (cplexConverter.variables[vId].interest_rate <= credNetConstants.totalIrs[i]){
							tempout += x[vId];
						}
					}
				}
				for (auto &atoOut : n->atomicEdge_out){
					int aeId = atoOut.second->atomicEdgeId;
					for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){

						// var Id
						int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
						if (cplexConverter.variables[vId].interest_rate <= credNetConstants.totalIrs[i]){
							tempin += x[vId];
						}
					}
				}

				c.add(tempout - tempin >= 0);
				tempout.end();
				tempin.end();
			}

			//Flow Constraints
			IloExpr inFlow(env);
			IloExpr outFlow(env);	
			for(auto &atoIn : n->atomicEdge_in){
				int aeId = atoIn.second->atomicEdgeId;
				for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){
					// var Id
					int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
					outFlow += x[vId];
				}
			}
			for (auto &atoOut : n->atomicEdge_out){
				int aeId = atoOut.second->atomicEdgeId;
				for (int j = 0; j < cplexConverter.atomicIdToVarIdDict[aeId].size(); j++){
					// var Id
					int vId = cplexConverter.atomicIdToVarIdDict[aeId][j];
					inFlow += x[vId];
				}
			}

			c.add(inFlow - outFlow == 0);
			inFlow.end();
			outFlow.end();

		}

	}


	model.add(c);
	// model.add(IloMinimize(env, cost));
	// model.add(IloMaximize(env,cost));  //option to minimize cost
	// cost.end();

}  // END populatebyrow
Пример #15
0
static void populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c) {
  
  IloEnv env = model.getEnv();
  IloNumArray costs(env);
  IloNumArray time(env);
IloNumArray product(env);

  int costs_array[]  = {1,1,1,10,1,12,2,2,5,10};
  int time_array[]  = {10,1,7,3,2,3,2,3,7,1};
  int product_array[]  = {0,3,1,2,-2,0,0,0,0,0};
  

  for(int i=0;i<10;i++)
    costs.add(costs_array[i]);

  for(int i=0;i<10;i++)
    time.add(time_array[i]);

  for(int i=0;i<10;i++)
    product.add(product_array[i]);



  x.add(IloBoolVar(env,"x12")); //0
  x.add(IloBoolVar(env,"x24")); //1
  x.add(IloBoolVar(env,"x46")); //2
  x.add(IloBoolVar(env,"x13")); //3
  x.add(IloBoolVar(env,"x32")); //4
  x.add(IloBoolVar(env,"x35")); //5
  x.add(IloBoolVar(env,"x56")); //6
  x.add(IloBoolVar(env,"x25")); //7
  x.add(IloBoolVar(env,"x34")); //8
  x.add(IloBoolVar(env,"x45")); //9


x.add(IloNumVar(env,0,IloInfinity,ILOINT,"s2")); //10
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"s3")); //11
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"s4")); //12
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"s5")); //13
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"s1")); //14
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"s6")); //15

x.add(IloNumVar(env,0,IloInfinity,ILOINT,"q2")); //16
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"q3")); //17
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"q4")); //18
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"q5")); //19
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"q1")); //20
x.add(IloNumVar(env,0,IloInfinity,ILOINT,"q6")); //21



  
  model.add(IloMinimize(env, costs[0]*x[0] + costs[1]*x[1] + costs[2]*x[2] + costs[3]*x[3] + costs[4]*x[4] + costs[5]*x[5] + costs[6]*x[6] + costs[7]*x[7] + costs[8]*x[8] + costs[9]*x[9]));
  c.add(x[0]+ x[3] == 1); // arcs sortant du noeud de depart
  c.add(x[2]+ x[6] == 1); // arcs entrant au noeud d arrivee
  c.add(x[1]+ x[7] - x[0] - x[4] == 0);
  c.add(x[8]+ x[5] + x[4] - x[3] == 0);
  c.add(x[9]+ x[2] - x[1] - x[8] == 0);
  c.add(x[6]- x[7] - x[5] - x[9] == 0);
  c.add(time[0]*x[0] + time[1]*x[1] + time[2]*x[2] + time[3]*x[3] + time[4]*x[4] + time[5]*x[5] + time[6]*x[6] + time[7]*x[7] + time[8]*x[8] + time[9]*x[9] <= 14);
//c.add(product[0]*x[0] + product[1]*x[1] + product[2]*x[2] + product[3]*x[3] + product[4]*x[4] + product[5]*x[5] + product[6]*x[6] + product[7]*x[7] + product[8]*x[8] + product[9]*x[9] <= 4);

c.add(x[14]+time[0]-1000*(1-x[0]) - x[10]<= 0);
c.add(x[20]+product[0]-1000*(1-x[0]) - x[16]<= 0);


c.add(x[10]+time[1]-1000*(1-x[1]) - x[12]<= 0);
c.add(x[16]+product[1]-1000*(1-x[1]) - x[18]<= 0);
c.add(x[18]-product[1]-1000*(1-x[1]) - x[16]<= 0);


c.add(x[12]+time[2]-1000*(1-x[2]) - x[15]<= 0);
c.add(x[18]+product[2]-1000*(1-x[2]) - x[21]<= 0);
c.add(x[21]-product[2]-1000*(1-x[2]) - x[18]<= 0);


c.add(x[14]+time[3]-1000*(1-x[3]) - x[11]<= 0);
c.add(x[20]+product[3]-1000*(1-x[3]) - x[17]<= 0);
c.add(x[17]-product[3]-1000*(1-x[3]) - x[20]<= 0);

c.add(x[13]+time[6]-1000*(1-x[6]) - x[15]<= 0);
c.add(x[19]+product[6]-1000*(1-x[6]) - x[21]<= 0);

c.add(x[10]+time[7]-1000*(1-x[7]) - x[13]<= 0);
c.add(x[16]+product[7]-1000*(1-x[7]) - x[19]<= 0);

c.add(x[12]+time[9]-1000*(1-x[9]) - x[13]<= 0);
c.add(x[18]+product[9]-1000*(1-x[9]) - x[19]<= 0);

c.add(x[11]+time[4]-1000*(1-x[4]) - x[10] <=0);
c.add(x[17]+product[4]-1000*(1-x[4]) - x[16]<= 0);
c.add(x[16]-product[4]-1000*(1-x[4]) - x[17]<= 0);


c.add(x[11]+time[8]-1000*(1-x[8]) - x[12]<= 0);
c.add(x[17]+product[8]-1000*(1-x[8]) - x[18]<= 0);

c.add(x[11]+time[5]-1000*(1-x[5]) - x[13]<= 0);
c.add(x[17]+product[5]-1000*(1-x[5]) - x[19]<= 0);



c.add(5 <= x[10] <= 7);
c.add(2 <= x[11] <= 5);
c.add(5 <= x[12] <= 9);
c.add(0 <= x[13] <= 20);
c.add(0 <= x[14] <= 0);
c.add(0 <= x[15] <= 14);

/*
c.add(2 <= x[17] <= 4);
c.add(0 <= x[16] <= 2);
c.add(3 <= x[18] <= 4);

c.add(0 <= x[19] <= 1000);
//c.add(0 <= x[20] <= 0);*/
c.add(3 <= x[21] <= 4);

c.add( x[20] == 1);









  model.add(c);
}