当前位置: 首页>>代码示例>>C++>>正文


C++ IloRangeArray类代码示例

本文整理汇总了C++中IloRangeArray的典型用法代码示例。如果您正苦于以下问题:C++ IloRangeArray类的具体用法?C++ IloRangeArray怎么用?C++ IloRangeArray使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了IloRangeArray类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: CreateStorageBindingConstraint

void CreateStorageBindingConstraint(IloModel model, BoolVar3DMatrix L, BoolVarMatrix M, 
			                              BoolVarMatrix X, BoolVar3DMatrix R, BoolVarMatrix Y, 
                                    IloRangeArray c){

  IloEnv env = model.getEnv();

  //The nested for-loops generate L[p][i][t]
  //that is required to linearize equation 16
  for(int p = 0; p < L.getSize(); p++){
    for(int i = 0; i < n; i++){
      for(int t = 0; t < T_MAX; t++){
	     L[p][i].add(IloBoolVar(env));
	     c.add(L[p][i][t] - M[p][i] - X[i][t] >= -1);
	     c.add(L[p][i][t] - M[p][i] - X[i][t] <=  0);
	     c.add(L[p][i][t] + M[p][i] - X[i][t] <=  1);
	     c.add(L[p][i][t] - M[p][i] + X[i][t] <=  1);
      }
    }
  }

  //Contructing the array R[p][e][t]
  for(int p = 0; p < R.getSize(); p++){
    for(int e = 0; e < E; e++){
      for(int t = 0; t < T_MAX; t++){
	     R[p][e].add(IloBoolVar(env));
      }
    }
  }

  //Encoding the constraint eqn-15
  for(int t = 0; t < T_MAX; t++){
    IloExprArray sum(env);
    for(int e = 0; e < E; e++){
      sum.add(IloExpr(env));
      for(int p = 0; p < n_m; p++){
        sum[e] += R[p][e][t];
      }
      c.add(sum[e] - Y[e][t] == 0);
    }
  }

  //Encoding the constraint eqn-21
  for(int t = 0; t < T_MAX; t++){
    IloExprArray sum1(env);
    IloExprArray sum2(env);
    for(int p = 0; p < n_m; p++){
      sum1.add(IloExpr(env));
      sum2.add(IloExpr(env));
      for(int e = 0; e < E; e++)
        sum1[p] += R[p][e][t];
      for(int i = 0; i < n; i++)
        sum2[p] += L[p][i][t];
      c.add(sum1[p] - n_r*sum2[p] <= 0);
    }
  }

  return;
}
开发者ID:trungda,项目名称:BioScheduling,代码行数:58,代码来源:Functions.cpp

示例2: populatebyrow

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
开发者ID:andreasmattas,项目名称:testcode,代码行数:17,代码来源:ilomipex1.cpp

示例3: populatebynonzero

static void
populatebynonzero (IloModel model, IloIntVarArray x, IloRangeArray c)
{
   IloEnv env = model.getEnv();

   IloObjective obj = IloMaximize(env);
	int n, a;
	scanf("%d", &n);

		
	scanf("%d", &a);
	//restrição
   c.add(IloRange(env, -IloInfinity, a));

	//variaveis
	for(int i=0 ; i<n; i++){
		x.add(IloIntVar(env, 0, 1));
	}

   /*x.add(IloIntVar(env, 0.0, 40.0));
   x.add(IloIntVar(env));
   x.add(IloIntVar(env));*/

	
   /*obj.setLinearCoef(x[0], 1.0);
   obj.setLinearCoef(x[1], 2.0);
   obj.setLinearCoef(x[2], 3.0);*/

	/*restricoes*/
	for(int i=0 ; i<n; i++){
		scanf("%d", &a);
		c[0].setLinearCoef(x[i], a);
	}

	//objetivo	
	for(int i=0 ; i<n; i++){
		scanf("%d", &a);
		obj.setLinearCoef(x[i], a);
	}
   
   /*c[0].setLinearCoef(x[1],  1.0);
   c[0].setLinearCoef(x[2],  1.0);
   c[1].setLinearCoef(x[0],  1.0);
   c[1].setLinearCoef(x[1], -3.0);
   c[1].setLinearCoef(x[2],  1.0);*/

   c[0].setName("c1");
	for(int i=0; i<n; i++){
		char tmp[10];
		printf("x%d", i+1);
		x[i].setName(tmp);	
	}

   model.add(obj);
   model.add(c);
}  // END populatebynonzero
开发者ID:drhachmann,项目名称:PLI_TP2,代码行数:56,代码来源:main.cpp

示例4: generateProblem

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);
}
开发者ID:CTU-IIG,项目名称:EnergyOptimizatorOfRoboticCells,代码行数:40,代码来源:CplexSolver.cpp

示例5: CreateMixingBindingConstraint

void CreateMixingBindingConstraint(IloModel model, BoolVarMatrix M, BoolVarMatrix Y, 
                                    BoolVarMatrix X, IloNumVarArray s, IloRangeArray c){

  IloEnv env = model.getEnv();

  //sum[i] holds summation from
  //eqn-13 for operation i
  IloExprArray sum1(env);

  //Creating array M[p][i]
  //i is for ith operation
  for(int p = 0; p < M.getSize(); p++){
    for(int i = 0; i < n; i++)
      M[p].add(IloBoolVar(env));
  }

  //Ensuring operation remains bound to the same module
  for(int i = 0; i < n; i++){
    sum1.add(IloExpr(env));
    for(int p = 0; p < n_m; p++){
      sum1[i] += M[p][i];
    }
    c.add(sum1[i] == 1);
  }

  //Two operations running simulateneously  
  //can not be bound to the same module
  for(int p = 0; p < n_m; p++){
    for(int t = 0; t < T_MAX; t++){
      for(int i = 0; i < n; i++){
       for(int j = i+1; j < n; j++){
         c.add(X[i][t] + X[j][t] + M[p][i] + M[p][j] <=  3);
       }
      }
    }
  }

  return;
}
开发者ID:trungda,项目名称:BioScheduling,代码行数:39,代码来源:Functions.cpp

示例6: CreateSchedulingConstraint

void CreateSchedulingConstraint(IloModel model, BoolVarMatrix X, BoolVarMatrix Y, 
				                        IloNumVarArray s, IloRangeArray c){
  IloEnv env = model.getEnv();

  //sum[i] holds the summation 
  //from eqn-8 for operation i
  IloExprArray sum(env);

  //The for-loop encodes all
  //the execution constraints
  for(int i = 0; i < X.getSize(); i++){
    sum.add(IloExpr(env));
    for(int t = 0; t < T_MAX; t++){
      X[i].add(IloBoolVar(env)); 
      sum[i] +=  X[i][t];
      c.add( t - s[i+1] - T_MAX*(X[i][t]-1)     >= 0);
      c.add(-t + s[i+1] - T_MAX*(X[i][t]-1) + T >= 1);
    }
    c.add(sum[i] == T);
  }

  //Resources Constraints
  IloExprArray summation1(env);
  IloExprArray summation2(env);
  for(int t = 0; t < T_MAX; t++){
    summation1.add(IloExpr(env));
    summation2.add(IloExpr(env));
    for(int i = 0; i < X.getSize(); i++){
      summation1[t] += X[i][t];
    }
    for(int e = 0; e < Y.getSize(); e++){
      summation2[t] += Y[e][t];
    }
    c.add(n_r*summation1[t] + summation2[t] <= n_m*n_r);
  } 
  
  return;
}
开发者ID:trungda,项目名称:BioScheduling,代码行数:38,代码来源:Functions.cpp

示例7: populatebyrow

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
开发者ID:andreasmattas,项目名称:testcode,代码行数:14,代码来源:iloindefqpex1.cpp

示例8: populatebyrow

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
开发者ID:andreasmattas,项目名称:testcode,代码行数:24,代码来源:ilomipex3.cpp

示例9: populatebycolumn

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
开发者ID:annaPolytech,项目名称:PRD,代码行数:16,代码来源:ilolpex4.cpp

示例10: populatebyrow

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
开发者ID:andreasmattas,项目名称:testcode,代码行数:17,代码来源:iloqpex1.cpp

示例11: createmodel

// 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]);
}
开发者ID:renvieir,项目名称:ioc,代码行数:85,代码来源:ilosocpex1.cpp

示例12: checkkkt

// Test KKT conditions on the solution.
// The function returns true if the tested KKT conditions are satisfied
// and false otherwise.
// The function assumes that the model currently extracted to CPLEX is fully
// described by obj, vars and rngs.
static bool
checkkkt (IloCplex& cplex, IloObjective const& obj, IloNumVarArray const& vars,
          IloRangeArray const& rngs, IloIntArray const& cone, double tol)
{
   IloEnv env = cplex.getEnv();
   IloModel model = cplex.getModel();
   IloNumArray x(env), dslack(env);
   IloNumArray pi(env, rngs.getSize()), slack(env);

   // Read primal and dual solution information.
   cplex.getValues(x, vars);
   cplex.getSlacks(slack, rngs);

   // pi for second order cone constraints.
   getsocpconstrmultipliers(cplex, vars, rngs, pi, dslack);

   // pi for linear constraints.
   for (IloInt i = 0; i < rngs.getSize(); ++i) {
      IloRange r = rngs[i];
      if ( !r.getQuadIterator().ok() )
         pi[idx(r)] = cplex.getDual(r);
   }

   // Print out the data we just fetched.
   streamsize oprec = env.out().precision(3);
   ios_base::fmtflags oflags = env.out().setf(ios::fixed | ios::showpos);
   env.out() << "x      = [";
   for (IloInt i = 0; i < x.getSize(); ++i)
      env.out() << " " << x[i];
   env.out() << " ]" << endl;
   env.out() << "dslack = [";
   for (IloInt i = 0; i < dslack.getSize(); ++i)
      env.out() << " " << dslack[i];
   env.out() << " ]" << endl;
   env.out() << "pi     = [";
   for (IloInt i = 0; i < rngs.getSize(); ++i)
      env.out() << " " << pi[i];
   env.out() << " ]" << endl;
   env.out() << "slack  = [";
   for (IloInt i = 0; i < rngs.getSize(); ++i)
      env.out() << " " << slack[i];
   env.out() << " ]" << endl;
   env.out().precision(oprec);
   env.out().flags(oflags);

   // Test primal feasibility.
   // This example illustrates the use of dual vectors returned by CPLEX
   // to verify dual feasibility, so we do not test primal feasibility
   // here.

   // Test dual feasibility.
   // We must have
   // - for all <= constraints the respective pi value is non-negative,
   // - for all >= constraints the respective pi value is non-positive,
   // - the dslack value for all non-cone variables must be non-negative.
   // Note that we do not support ranged constraints here.
   for (IloInt i = 0; i < vars.getSize(); ++i) {
      IloNumVar v = vars[i];
      if ( cone[i] == NOT_IN_CONE && dslack[i] < -tol ) {
         env.error() << "Dual multiplier for " << v << " is not feasible: "
                     << dslack[i] << endl;
         return false;
      }
   }
   for (IloInt i = 0; i < rngs.getSize(); ++i) {
      IloRange r = rngs[i];
      if ( fabs (r.getLB() - r.getUB()) <= tol ) {
         // Nothing to check for equality constraints.
      }
      else if ( r.getLB() > -IloInfinity && pi[i] > tol ) {
         env.error() << "Dual multiplier " << pi[i] << " for >= constraint"
                     << endl << r << endl
                     << "not feasible"
                     << endl;
         return false;
      }
      else if ( r.getUB() < IloInfinity && pi[i] < -tol ) {
         env.error() << "Dual multiplier " << pi[i] << " for <= constraint"
                     << endl << r << endl
                     << "not feasible"
                     << endl;
         return false;
      }
   }

   // Test complementary slackness.
   // For each constraint either the constraint must have zero slack or
   // the dual multiplier for the constraint must be 0. We must also
   // consider the special case in which a variable is not explicitly
   // contained in a second order cone constraint.
   for (IloInt i = 0; i < vars.getSize(); ++i) {
      if ( cone[i] == NOT_IN_CONE ) {
         if ( fabs(x[i]) > tol && dslack[i] > tol ) {
            env.error() << "Invalid complementary slackness for " << vars[i]
                        << ":" << endl
//.........这里部分代码省略.........
开发者ID:renvieir,项目名称:ioc,代码行数:101,代码来源:ilosocpex1.cpp

示例13: obj

/** Create the dual of a linear program.
 * The function can only dualize programs of the form
 * <code>Ax <= b, x >= 0</code>. The data in <code>primalVars</code> and
 * <code>dualRows</code> as well as in <code>primalRows</code> and
 * <code>dualVars</code> is in 1-to-1-correspondence.
 * @param primalObj  Objective function of primal problem.
 * @param primalVars Variables in primal problem.
 * @param primalRows Rows in primal problem.
 * @param dualObj    Objective function of dual will be stored here.
 * @param dualVars   All dual variables will be stored here.
 * @param dualRows   All dual rows will be stored here.
 */
void BendersOpt::makeDual(IloObjective const &primalObj,
                          IloNumVarArray const &primalVars,
                          IloRangeArray const &primalRows,
                          IloObjective *dualObj,
                          IloNumVarArray *dualVars,
                          IloRangeArray *dualRows)
{
   // To keep the code simple we only support problems
   // of the form Ax <= b, b >= 0 here. We leave it as a reader's
   // exercise to extend the function to something that can handle
   // any kind of linear model.
   for (IloInt j = 0; j < primalVars.getSize(); ++j)
      if ( primalVars[j].getLB() != 0 ||
           primalVars[j].getUB() < IloInfinity )
      {
         std::stringstream s;
         s << "Cannot dualize variable " << primalVars[j];
         throw s.str();
      }
   for (IloInt i = 0; i < primalRows.getSize(); ++i)
      if ( primalRows[i].getLB() > -IloInfinity ||
           primalRows[i].getUB() >= IloInfinity )
      {
         std::stringstream s;
         s << "Cannot dualize constraint " << primalRows[i];
         std::cerr << s.str() << std::endl;
         throw s.str();
      }

   // The dual of
   //   min/max c^T x
   //       Ax <= b
   //        x >= 0
   // is
   //   max/min y^T b
   //       y^T A <= c
   //           y <= 0
   // We scale y by -1 to get >= 0

   IloEnv env = primalVars.getEnv();
   IloObjective obj(env, 0.0,
                    primalObj.getSense() == IloObjective::Minimize ?
                    IloObjective::Maximize : IloObjective::Minimize);
   IloRangeArray rows(env);
   IloNumVarArray y(env);
   std::map<IloNumVar,IloInt,ExtractableLess<IloNumVar> > v2i;
   for (IloInt j = 0; j < primalVars.getSize(); ++j) {
      IloNumVar x = primalVars[j];
      v2i.insert(std::map<IloNumVar,IloInt,ExtractableLess<IloNumVar> >::value_type(x, j));
      rows.add(IloRange(env, -IloInfinity, 0, x.getName()));
   }
   for (IloExpr::LinearIterator it = primalObj.getLinearIterator(); it.ok(); ++it)
      rows[v2i[it.getVar()]].setUB(it.getCoef());

   for (IloInt i = 0; i < primalRows.getSize(); ++i) {
      IloRange r = primalRows[i];
      IloNumColumn col(env);
      col += obj(-r.getUB());
      for (IloExpr::LinearIterator it = r.getLinearIterator(); it.ok(); ++it)
         col += rows[v2i[it.getVar()]](-it.getCoef());
      y.add(IloNumVar(col, 0, IloInfinity, IloNumVar::Float, r.getName()));
   }

   *dualObj = obj;
   *dualVars = y;
   *dualRows = rows;
}
开发者ID:annaPolytech,项目名称:PRD,代码行数:79,代码来源:iloparbenders.cpp

示例14: env

/** Extract sub block number <code>n</code> from <code>problem</code>.
 * The constructor creates a representation of block number <code>n</code>
 * as described in <code>problem</code>.
 * The constructor will also connect the newly created block to a remote
 * object solver instance.
 * @param problem  The problem from which the block is to be extracted.
 * @param n        Index of the block to be extracted.
 * @param argc     Argument for IloCplex constructor.
 * @param argv     Argument for IloCplex constructor.
 * @param machines List of machines to which to connect. If the code is
 *                 compiled for the TCP/IP transport then the block will
 *                 be connected to <code>machines[n]</code>.
 */
BendersOpt::Block::Block(Problem const *problem, IloInt n, int argc, char const *const *argv,
                         std::vector<char const *> const &machines)
   : env(), number(n), vars(0), rows(0), cplex(0), cb(0)
{
   IloNumVarArray problemVars = problem->getVariables();
   IloRangeArray problemRanges = problem->getRows();

   // Create a map that maps variables in the original model to their
   // respective index in problemVars.
   std::map<IloNumVar,IloInt,ExtractableLess<IloNumVar> > origIdxMap;
   for (IloInt j = 0; j < problemVars.getSize(); ++j)
      origIdxMap.insert(std::map<IloNumVar,IloInt,ExtractableLess<IloNumVar> >::value_type(problemVars[j], j));

   // Copy non-fixed variables from original problem into primal problem.
   IloExpr primalObj(env);
   IloNumVarArray primalVars(env);
   IloRangeArray primalRows(env);
   IdxMap idxMap; // Index of original variable in block's primal model
   RowSet rowSet;
   for (IloInt j = 0; j < problemVars.getSize(); ++j) {
      IloNumVar x = problemVars[j];
      if ( problem->getBlock(x) == number ) {
         // Create column in block LP with exactly the same data.
         if ( x.getType() != IloNumVar::Float ) {
            std::stringstream s;
            s << "Cannot create non-continuous block variable " << x;
            std::cerr << s.str() << std::endl;
            throw s.str();
         }
         IloNumVar v(env, x.getLB(), x.getUB(), x.getType(), x.getName());
         // Normalize objective function to 'minimize'
         double coef = problem->getObjCoef(x);
         if ( problem->getObjSense() != IloObjective::Minimize )
            coef *= -1.0;
         primalObj += coef * v;
            
         // Record the index that the copied variable has in the
         // block model.
         idxMap.insert(IdxMap::value_type(x, primalVars.getSize()));
         primalVars.add(v);
            
         // Mark the rows that are intersected by this column
         // so that we can collect them later.
         RowSet const &intersected = problem->getIntersectedRows(x);
         for (RowSet::const_iterator it = intersected.begin();
              it != intersected.end(); ++it)
            rowSet.insert(*it);
      }
      else
         idxMap.insert(IdxMap::value_type(x, -1));
   }

   // Now copy all rows that intersect block variables.
   for (IloInt i = 0; i < problemRanges.getSize(); ++i) {
      IloRange r = problemRanges[i];
      if ( rowSet.find(r) == rowSet.end() )
         continue;

      // Create a copy of the row, normalizing it to '<='
      double factor = 1.0;
      if ( r.getLB() > -IloInfinity )
         factor = -1.0;
      IloRange primalR(env,
                       factor < 0 ? -r.getUB() : r.getLB(),
                       factor < 0 ? -r.getLB() : r.getUB(), r.getName());
      IloExpr lhs(env);
      for (IloExpr::LinearIterator it = r.getLinearIterator(); it.ok(); ++it)
      {
         IloNumVar v = it.getVar();
         double const val = factor * it.getCoef();
         if ( problem->getBlock(v) != number ) {
            // This column is not explicitly in this block. This means
            // that it is a column that will be fixed by the master.
            // We collect all such columns so that we can adjust the
            // dual objective function according to concrete fixings.
            // Store information about variables in this block that
            // will be fixed by master solves.
            fixed.push_back(FixData(primalRows.getSize(), origIdxMap[v], -val));
         }
         else {
            // The column is an ordinary in this block. Just copy it.
            lhs += primalVars[idxMap[v]] * val;
         }
      }
      primalR.setExpr(lhs);
      primalRows.add(primalR);
      lhs.end();
//.........这里部分代码省略.........
开发者ID:annaPolytech,项目名称:PRD,代码行数:101,代码来源:iloparbenders.cpp

示例15: populatebyrow

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){
//.........这里部分代码省略.........
开发者ID:CED-9,项目名称:CreditNetwork,代码行数:101,代码来源:CN_Solver.cpp


注:本文中的IloRangeArray类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。