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

Incremental solving with an objective function #32

Open
IgnaceBleukx opened this issue Oct 8, 2024 · 1 comment
Open

Incremental solving with an objective function #32

IgnaceBleukx opened this issue Oct 8, 2024 · 1 comment

Comments

@IgnaceBleukx
Copy link
Contributor

IgnaceBleukx commented Oct 8, 2024

Hi,

I am using the Python interface of Choco to do incremental solving.
My situation is the following.
I have a model with a given objective to solve. After getting the optimal solution, I want to add a constraint to the model and re-solve to optimality again.

However, because of the consrtraints added to the solver during optimization, the status seems to be invalid.
Is there a way around this? Below is a minimal working example.

import pychoco as pc

model = pc.Model()

a = model.boolvar(name="a")
b = model.boolvar(name="b")
c = model.boolvar(name="c")
sum_of = model.intvar(0,3,name="thesum")

model.or_([a,b,c]).post()
model.sum([a,b,c], "=", sum_of).post()

solver = model.get_solver()
sol = solver.find_optimal_solution(maximize=False, objective=sum_of)

for var in [a,b,c]:
    print(var, sol.get_int_val(var))

model.arithm(sum_of, ">=", 2)
solver = model.get_solver()
sol = solver.find_optimal_solution(maximize=False, objective=sum_of)
print("Solution:", sol)
for var in [a,b,c]:
    print(var, sol.get_int_val(var))

Which produces the following output:

BoolVar 'a' = [0, 1] 0
BoolVar 'b' = [0, 1] 0
BoolVar 'c' = [0, 1] 1
Solution: Choco Solution
Unhandled exception: org.chocosolver.solver.exception.SolverException: Cannot access value of a = [0,1]: No solution has been recorded yet (empty solution). Make sure this.record() has been called.
    at org.chocosolver.solver.Solution.getIntVal(Solution.java:221)
    at org.chocosolver.capi.SolutionApi.getIntVal(SolutionApi.java:28)

Kind regards,
Ignace

@dimitri-justeau
Copy link
Collaborator

Hi,

I need to include reset()and hardReset() in the API. It should be quick to do, I'll do it ASAP. Meanwhile, you can also encapsulate the creation of your base model (with the constraints of the first solving) in a function (e.g. make_model()), or a small encapsulating class if you want facilitated access to variables. That way, after solving the first problem, you can call make_model() again, post additional constraints, and solve again.

I will close this issue and let you know as soon as reset()and hardReset() are added to the API.

Best,

Dimitri.

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