Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check constraints between elements in a list / is this Constraint Programming?

I have many variable-sized lists containing instances of the same class with attribute foo, and for every list I must apply rules like:

  • if there's an element foo=A there cannot be elements with foo in [B,C,D]
  • if there's an element foo=X there must by at least one with foo in [Y,Z]
  • there can be between MIN and MAX elements foo=BAR

combining the above three rules is probably enough to express any similar constraint I'll ever need. It's something like dependency checking in software packages but I have quantities and lack versions :)

a naïve approach would be:

R_CONFLICT={ A: [B,C,D] }
R_DEPENDS ={ X: [ [Y,Z], W, .. } # means: A depends on either Y or Z, and W
R_MIN     ={BAR: n, BAZ: m}
R_MAX     ={BAR: o, BAZ: p}
# now just loop over lists to check them..

Is this a problem of Constraint programming? I don't actually need to solve something to get a result, I need to validate my list against some constraints and check if they are satisfied or not. How would you classify this problem and how would you solve it?

For what it's worth, I'm coding in Python, but I welcome a generic programming answer :) If it turns out I must delve in constraint programming I'll probably start by trying python-constraint.

like image 569
Luke404 Avatar asked Sep 08 '10 09:09

Luke404


People also ask

How does constraint programming work?

Constraint programming (CP) is a paradigm for solving combinatorial problems that draws on a wide range of techniques from artificial intelligence, computer science, and operations research. In constraint programming, users declaratively state the constraints on the feasible solutions for a set of decision variables.

What is a check constraint in SQL?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.


1 Answers

Short answer - yes this could be checked using constraint programming, in effect you are supplying a solution and checking it against constraints rather than having the solver search through domains of potentials for a matching solution. Which kind of makes constraint programming overkill, especially if you are using Python which can quite easily check those kind of conditions.

I don't have Python on this machine so there may be a typo/error in this code but it shows what you're after without needing to get involved with constraint programming.

conflict = set([B, C , D])
foos = set([x.foo for x in list])
if A in foos:
    if len(foos & conflict): #Set intersection
         return false

len([x for x in list where x.foo == BAR]) #Gives you number of occurances of BAR

Basically I'd say unless the constraints are going to get much more complex or you're going to want to find solutions rather than just test I'd stick with code rather than constraint programming.

like image 171
Chris Avatar answered Oct 14 '22 15:10

Chris