potassco / clingo

🤔 A grounder and solver for logic programs.
https://potassco.org/clingo
MIT License
601 stars 79 forks source link

Python RuntimeError when using --models > 0 #365

Closed drwadu closed 2 years ago

drwadu commented 2 years ago

I'm trying to enumerate n models as described in the following minimal example snippet:

n = 0
ctl = Control(str(n))

# grounding, collecting assumptions etc.

with ctl.solve(assumptions=assumptions, yield_=True) as handle:
    models = [f"{model}".split(" ") for model in handle]

However, whenever I choose n > 0, I run into: RuntimeError: In context '': multiple occurrences: 'models'

How can I potentially resolve the problem?

rkaminsk commented 2 years ago

The initializer of the Control class expects a sequence of strings. Since Python has no character type, a string is also sequence of strings. Even a type checker will not find this bug. You should pass [str(n)] instead. Alternatively, you can also use the Configuration class to set the number of models to enumerate: ctl.configuration.solve.models = n.

drwadu commented 2 years ago

It works, thank you.