chocoteam / choco-solver

An open-source Java library for Constraint Programming
http://choco-solver.org/
BSD 4-Clause "Original" or "Old" License
684 stars 137 forks source link

"regular" constraint doesn't accept empty array #335

Open aengelberg opened 8 years ago

aengelberg commented 8 years ago

ICF.regular() throws when I pass it an empty IntVar array:

ArrayIndexOutOfBoundsException 0  org.chocosolver.solver.constraints.Propagator.<init> (Propagator.java:140)

I assumed that if I passed in an empty array, the constraint would succeed if and only if the automaton accepts the empty string. Is that not a desired feature in Choco or is this a bug?

cprudhom commented 8 years ago

First, posting a constraint without variable is either always true (and does not help the resolution) or false (and there is trivially no solution). So, we consider that posting a constraint without variable is not an expected behavior since the goal is to find a solution, that is, a state wherein all variables are instantiated and all constraints are satisfied.

Technically, the main reason is that every propagator needs to know the Solver and most of the time, it is reached through on of its variables. There is a check in the Propagator's constructor , based on assertion:

assert vars != null && vars.length > 0 && vars[0] != null : "wrong variable set in propagator constructor";

Note that TRUE and FALSE also require a variable (solver.ONE or solver.ZERO. So, all APIs in IntConstraintFactory always require at least one variable.

I can consider either promoting the assertion to SolverException (which would be the easy solution) or creating a kind of boolean constraint which would be lazily created on posting (a priori, a better solution, but instrumenting the library could be a tough task to do).