Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Compiler API #51

Open
sirwhinesalot opened this issue Aug 22, 2024 · 3 comments
Open

Feature Request: Compiler API #51

sirwhinesalot opened this issue Aug 22, 2024 · 3 comments

Comments

@sirwhinesalot
Copy link

Right now when working with PyCSP3, you create one model in a script, which is then compiled to XCSP3 when executed.

I would much rather have the option to script the actual compilation process, so I can create multiple different models through the PyCSP3 API, compile them through the API, and then solve them with whatever XCSP3 capable solver I choose.

Consider for example a single Jupyter notebook that models different constraint problems and wants to solve them separately.

The alternative is to generate the XML directly myself but that's not nearly as convenient as using the API, or to generate a python string in memory and then "exec" it to do the compilation.

@xcsp3team
Copy link
Owner

Currently, you can compile and solve instances in sequence (by calling clear() between each problem).
I am not sure that it is what you are looking.
Please, tell us.

An example below xith cells from a notebook:

from pycsp3 import *
x = VarArray(size=3, dom=range(3))

satisfy(
    AllDifferent(x)
)
allDifferent(list:[x[0], x[1], x[2]])
instance1 = compile()
print(instance1)
('ipykernel_launcher.xml', False)
ace = solver(ACE)
result = ace.solve(instance1)
print(result, values(x))
SAT [0, 1, 2]
result = ace.solve(instance1)
print(result, values(x))
SAT [0, 1, 2]
clear()
result = ace.solve(instance1)
 The instance has no variable, so the solver is not run.
Did you forget to indicate the variant of the model?
y = VarArray(size=3, dom=range(3))

satisfy(
    AllEqual(y)
)
allEqual(list:[y[0], y[1], y[2]])
instance2 = compile()
print(instance1 == instance2)
result = ace.solve(instance2)
print(result, values(y))
True
SAT [0, 0, 0]

@sirwhinesalot
Copy link
Author

I didn't realize the compile function existed! I didn't see it in the docs...

But what I'd really like is for the "state" of PyCSP3 to not be global, meaning I can add new constraints to different instances, in the same execution, without "clearing".

csp1 = CSP()
x = csp1.VarArray(size=3, dom=range(3))

csp2 = CSP()
y = csp2.VarArray(size=5, dom=range(5))

csp1.satisfy(AllDifferent(x))
csp2.satisfy(...)

instance1 = csp1.compile()
instance2 = csp2.compile()

ace = solver(ACE)
ace.solve(instance1)
ace.solve(instance2)

# add more stuff to csp1
z = csp1.VarArray(...)
csp1.satisfy(...)
# etc.

@xcsp3team
Copy link
Owner

I will think about it soon and let you informed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants