static void populatebyrow (IloModel model, IloIntVarArray x, IloRangeArray c) { IloEnv env = model.getEnv(); x.add(IloIntVar(env, 0.0)); x.add(IloIntVar(env, 0.0)); x.add(IloIntVar(env, 0.0)); x.add(IloIntVar(env, 2.0)); 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
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
static void populatebycolumn (IloModel model, IloIntVarArray x, IloRangeArray c) { IloEnv env = model.getEnv(); IloObjective obj = IloMaximize(env); c.add(IloRange(env, -IloInfinity, 20.0, "c1")); c.add(IloRange(env, -IloInfinity, 30.0, "c2")); x.add(IloIntVar(obj(1.0) + c[0](-1.0) + c[1]( 1.0), 0.0, 40.0)); x.add(IloIntVar(obj(2.0) + c[0]( 1.0) + c[1](-3.0))); x.add(IloIntVar(obj(3.0) + c[0]( 1.0) + c[1]( 1.0))); x[0].setName("x1"); x[1].setName("x2"); x[2].setName("x3"); model.add(obj); model.add(c); } // END populatebycolumn
IloInt choose(IloCP cp, IloIntVarArray vars) { IloNum best = IloInfinity; IloInt bestIndex = -1; IloInt n = vars.getSize(); for (IloInt i = 0; i < n; i++) { if (!cp.isFixed(vars[i])) { IloNum c = CalcCentroid(cp, vars[i]); if (c < best) { best = c; bestIndex = i; } } } return bestIndex; }
void wqueens(IloEnv& env, IloModel& model, IloIntVarArray& vars) { cout << "Add 3 hard AllDiff constraints for the Queens problem." << endl; model.add(vars); // ensure vars are the main decision variables int nqueen = vars.getSize(); IloIntVarArray vars1(env, nqueen, -2 * nqueen, 2 * nqueen); IloIntVarArray vars2(env, nqueen, -2 * nqueen, 2 * nqueen); for (IloInt i = 0; i < nqueen; i++) { model.add(vars1[i] == vars[i] + i); model.add(vars2[i] == vars[i] - i); } model.add(IloAllDiff(env, vars)); model.add(IloAllDiff(env, vars1)); model.add(IloAllDiff(env, vars2)); }
void quasi(IloEnv& env, IloModel& model, IloIntVarArray& vars) { int n = sqrt((double)vars.getSize()); cout << "Add " << n * 2 << " hard AllDiff constraints for the \"homogeneous\" QuasiGroup problem." << endl; for (int i = 0; i < n; i++) { int pos = i * n; IloIntVarArray vars1(env, n); for (int j = 0; j < n; j++) { vars1[j] = vars[pos]; pos++; } model.add(IloAllDiff(env, vars1)); } for (int j = 0; j < n; j++) { int pos = j; IloIntVarArray vars1(env, n); for (int i = 0; i < n; i++) { vars1[i] = vars[pos]; pos += n; } model.add(IloAllDiff(env, vars1)); } }