I have many variable-sized lists containing instances of the same class with attribute foo, and for every list I must apply rules like:
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With