Why does cplex return a solution which doesn't meet the constraints?

47 Views Asked by At
int n=6;
int a[1..n] = [12,0,10,4,10,8];

dvar int x in 0..1;

maximize x;
subject to
{
    forall(i,j in 1..n)
        (i!=j && a[i]==a[j]) => x==0;
}

Since there is a duplicate inside the array a, x should be 0 but cplex returns 1, why?

2

There are 2 best solutions below

0
Olivier Lhomme On BEST ANSWER

you have found a bug, involving indicator constraints and conjunctions. A workaround is to replace the conjunction by the keyword "ordered" in the forall:

    forall(ordered i,j in 1..n) {
        (a[i]==a[j]) => x==0;
    }

Another workaround is to use "if" instead of "=>" :

    forall(i,j in 1..n) {
        if (i!=j && a[i]==a[j]) x==0;
    }

We will get in touch with the OPL dev team who will investigate and improve in a next release.

0
Alex Fleischer On

I would use slicing

int n=6;
int a[1..n] = [12,0,10,4,10,8];



dvar int x in 0..1;

maximize x;
subject to
{
    forall(i,j in 1..n:(i!=j) && (a[i]==a[j]))
        
        (x==0);
}

and that gives 0