예제 #1
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;
}
예제 #2
0
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));
}
예제 #3
0
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));
    }
}
예제 #4
0
void zebra(IloEnv& env, IloModel& model, IloIntVarArray& vars)
{
    cout << "Add 5 hard AllDiff constraints for the Zebra problem." << endl;
    for (int i = 0; i < 5; i++) {
        int pos = i * 5;
        IloIntVarArray vars1(env, 5);
        vars1[0] = vars[0 + pos];
        vars1[1] = vars[1 + pos];
        vars1[2] = vars[2 + pos];
        vars1[3] = vars[3 + pos];
        vars1[4] = vars[4 + pos];
        model.add(IloAllDiff(env, vars1));
    }
}
예제 #5
0
void alldiff(IloEnv& env, IloModel& model, IloIntVarArray& vars)
{
    cout << "Add 1 hard AllDiff constraint on all the (permutation) problem variables." << endl;
    model.add(IloAllDiff(env, vars));
}
예제 #6
0
int main(int argc, const char * argv[]) {

    IloEnv env;
    try {
        IloModel model(env);
        int n = 9;
        IloArray<IloIntVarArray> matrix(env,n);
        //All elements in a  row are different
        for(int i=0;i<n;i++)
        {
            matrix[i] = IloIntVarArray(env,n,1,9);
            for(int j=0;j<n;j++)
            {
                model.add(IloAllDiff(env,matrix[i]));
            }
        }
        //All elements in a coloum are different
        for(int i=0;i<n;i++)
        {
            IloIntVarArray window(env);
            for(int j=0;j<n;j++)
            {
                window.add(matrix[j][i]);
            }
            model.add(IloAllDiff(env,window));
        }
        //All elements in each of the 3x3sub-squares are different
        for(int i=0;i<9;i+=3)
        {
            for(int j=0;j<9;j+=3)
            {
                IloIntVarArray window(env);
                for(int k=i;k<i+3;k++)
                {
                    for(int l=j;l<j+3;l++)
                    {
                        window.add(matrix[k][l]);
                    }
                }
                model.add(IloAllDiff(env,window));
            }
        }
        //Already provided numbers
        int PreSolve[][9] = {9,5,0,3,7,0,0,0,0,
                            0,0,0,0,0,0,0,0,0,
                            0,0,4,0,8,0,5,7,0,
                            3,9,0,0,0,0,0,0,4,
                            0,0,5,0,4,0,8,0,0,
                            8,0,0,0,0,0,0,1,7,
                            0,6,8,0,3,0,7,0,0,
                            0,0,0,0,0,0,0,0,0,
                            0,0,0,0,1,8,0,2,9};
        //Add presolved amtrix
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(PreSolve[i][j] != 0)
                model.add(matrix[i][j] == PreSolve[i][j]);
            }
        }
        //Solve
        IloCP cp(model);
        if(cp.solve())
        {
            for(IloInt i = 0;i<n;i++)
            {
                for(int j=0;j<n;j++)
                {
                    cp.out() << cp.getValue(matrix[i][j]) << "\t";
                }
                cp.out()<<std::endl;
            }
        }
        
    }
    catch (IloException & ex) {
        env.out() << "Exception Caught : " << ex << std::endl;
    }
}