dwavesystems / dwavebinarycsp

Map constraint satisfaction problems with binary variables to binary quadratic models.
https://docs.ocean.dwavesys.com/projects/binarycsp/en/latest
Apache License 2.0
21 stars 28 forks source link

"dwavebinarycsp.stitch" raises AttributeError #105

Open ghost opened 4 years ago

ghost commented 4 years ago

Description When I use dwavebinarycsp in following code as Steps To Reproduce, I faced "AttibuteError: 'NoneType' object has no attibute 'classical_gap'".

I suspect that this should be modified as ImpossibleBQM or anything else.

Steps To Reproduce

import dwavebinarycsp

csp = dwavebinarycsp.ConstraintSatisfactionProblem(dwavebinarycsp.SPIN)

csp.add_constraint(lambda a, b, c, d, e, f, g, h, i: a * b * c * d * e * f * g * h * i == 1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
csp.add_constraint(lambda a, b, c, d, e, f, g, h, i: a + b + c + d + e + f + g + h * i < 2,  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])

bqm = dwavebinarycsp.stitch(csp, max_graph_size=9)

print(bqm)

This example code raises following error message.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-3030c141576b> in <module>
      4 csp.add_constraint(lambda a, b, c, d, e, f, g, h, i: a + b + c + d + e + f + g + h * i < 2,  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
      5 
----> 6 bqm = dwavebinarycsp.stitch(csp, max_graph_size=9)
      7 
      8 print(bqm)

~/.pyenv/versions/3.6.10/lib/python3.6/site-packages/dwavebinarycsp/compilers/stitcher.py in stitch(csp, min_classical_gap, max_graph_size)
    185                 continue
    186 
--> 187             if pmodel.classical_gap >= min_classical_gap:
    188                 break
    189 

AttributeError: 'NoneType' object has no attribute 'classical_gap'

Expected Behavior I think that "ImpossibleBQM: No penalty model can be build for constraint xxx." should be raised as written in dwavebinarycsp/compilers/stitcher.py line 195, though the example above raised AttributeError.

Environment

Additional Context In my understanding, this issue may be related to following files:

Original Implementation: pm.get_penalty_model(spec) on "stitch" line 182 raises pm.ImpossiblePenaltyModel if the calculation is fault. Many such cases are caught by try-except on line 183 and continued, so these are caught by else block on line 193 and line 195 raises ImpossibleBQM Error finally.

Problem: If the max_graph_size is larger than 8 (max_graph_size=9 on my code example), the graph instance G in "stitch" line 173 becomes larger than 8.

It seems that this max_graph_size isn't inputted into max_decision parameter on "generate_bqm" appropriately. And especially if max_graph_size is larger than 8, this unexpected problem occurs.

On "get_penalty_model" line 56-60, no input parameter is assigned for max_decision for "generate_bqm". This lead to the ValueError on "generate_bqm" line 111 because len(decision)=9 is larger than max_decision=8 (8 is default value on generate_bqm).

Question: I wonder whether this behavior is bug or not... I would appreciate it if you would give me some advice.