Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element constraint sets incorrect index value. #28

Closed
xgillard opened this issue Feb 11, 2019 · 1 comment
Closed

Element constraint sets incorrect index value. #28

xgillard opened this issue Feb 11, 2019 · 1 comment

Comments

@xgillard
Copy link

Using JaCop 4.6.0 on Java.

The following code snippet showcases an example where jacop's element constraint yields an incorrect result. This issue might be related to #27, but I don't believe so. This is why I created this second issue for you to track.

Store cp = new Store();

IntVar   index = new IntVar(cp, -3, 3);
IntVar[]  vars = new IntVar[]{new IntVar(cp, 0, 0)};
IntVar   value = new IntVar(cp, -4, 2);

cp.setLevel(cp.level + 1);
cp.impose(Element.choose(index, vars, value));
if ( !cp.consistency() ) {
    System.err.println("Ahem, this is wrong !");
}

// This is what gets printed:
// index = _1 = 1
// vars  = [_2 = 0]
// value = _3 = 0
System.out.println("index = "+ index);
System.out.println("vars  = "+ Arrays.toString(vars));
System.out.println("value = "+ value);

// 1st assertion fails because index = {1}. Still vars[1] is not feasible : it is out of bounds
assert index.value()   == 0;
assert vars[0].value() == 0;
assert value.value()   == 0;
@radsz
Copy link
Owner

radsz commented Feb 11, 2019

Thank you for making an effort to report a problem.

For element the first element in the list has index 1 not 0. ;). Therefore, it is correct.
If you prefer to have things indexed from zero then you need to add extra parameter to Element.
cp.impose(Element.choose(index, vars, value, -1));
This -1 changes starting index from 1 to 0. If I do then your test does not fail.

Again, thank you for making an effort to report a problem. JaCoP participates in solver competition and solves thousands of different problems so usually we have no simple issues like that. Therefore, if you find those type of simple problems most of the time is use of API in the wrong place or misunderstanding of the constraint contract.

I hope you will pass those initial difficulty in using CP/JaCoP and enjoying using our library. I recommend looking at many different examples we provide. It helps a lot at the beginning.

@radsz radsz closed this as completed Feb 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants