I am new to Google OR-Tools and trying to start by running a basic example from OR-Tools example library found here
https://github.com/google/or-tools/blob/stable/ortools/sat/samples/cp_sat_example.py.
Every code example gets hung up on the same part after creating the solver.
When I run the code I am not getting an output from any of the examples, each time it gets hung up after creating the solver, then the script just seems to terminate. I have modified this CP-Stat model code example with some print statements for debugging but I still can't figure out why every example gets hung up on the same part.
#!/usr/bin/env python3
# [START program]
"""Simple solve."""
# [START import]
from ortools.sat.python import cp_model
# [END import]
def main() -> None:
"""Minimal CP-SAT example to showcase calling the solver."""
print("Starting the solver...") # Debug print statement
try:
# Creates the model.
# [START model]
print("Creating model...")
model = cp_model.CpModel()
print("Model created.")
# [END model]
# Creates the variables.
# [START variables]
print("Creating variables...")
var_upper_bound = max(50, 45, 37)
x = model.NewIntVar(0, var_upper_bound, "x")
y = model.NewIntVar(0, var_upper_bound, "y")
z = model.NewIntVar(0, var_upper_bound, "z")
print(f"Variables created: x in [0, {var_upper_bound}], y in [0, {var_upper_bound}], z in [0, {var_upper_bound}]")
# [END variables]
# Creates the constraints.
# [START constraints]
print("Adding constraints...")
model.Add(2 * x + 7 * y + 3 * z <= 50)
model.Add(3 * x - 5 * y + 7 * z <= 45)
model.Add(5 * x + 2 * y - 6 * z <= 37)
print("Constraints added.")
# [END constraints]
# [START objective]
print("Setting objective...")
model.Maximize(2 * x + 2 * y + 3 * z)
print("Objective set.")
# [END objective]
# Creates a solver and solves the model.
# [START solve]
print("Creating solver...")
solver = cp_model.CpSolver()
print("Solver created.")
print("Solving model...")
status = solver.Solve(model)
print(f"Model solved with status: {status}")
# [END solve]
# [START print_solution]
print("Checking solution...")
if status == cp_model.OPTIMAL or status == cp_model.FEASIBLE:
print(f"Maximum of objective function: {solver.ObjectiveValue()}\n")
print(f"x = {solver.Value(x)}")
print(f"y = {solver.Value(y)}")
print(f"z = {solver.Value(z)}")
else:
print("No solution found.")
# [END print_solution]
# Statistics.
# [START statistics]
print("Printing statistics...")
print("\nStatistics")
print(f" status : {solver.StatusName(status)}")
print(f" conflicts: {solver.NumConflicts()}")
print(f" branches : {solver.NumBranches()}")
print(f" wall time: {solver.WallTime()} s")
print("Statistics printed.")
# [END statistics]
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()
print("Solver finished.") # Debug print statement
# [END program]
I am using python version 3.12.1 with VS Code. I have uninstalled and reinstalled OR-Tools and all dependent libraries multiple times. The output in the terminal appears as follows:
Starting the solver...
Creating model...
Model created.
Creating variables...
Variables created: x in [0, 50], y in [0, 50], z in [0, 50]
Adding constraints...
Constraints added.
Setting objective...
Objective set.
Creating solver...
Solver created.
Solving model...
The optimized solution should look something like:
Model solved with status: 4 # This status code should indicate OPTIMAL or FEASIBLE
Checking solution...
Maximum of objective function: 32.0 # Example value
x = 5.0 # Example value
y = 0.0 # Example value
z = 0.0 # Example value
Printing statistics...
Statistics
status : OPTIMAL # Example status
conflicts: 0
branches : 0
wall time: 0.001 s
Statistics printed.
Solver finished.
I believe there is an issue with solver itself but I'm not sure how to diagnose what it is, it may be something simple that I am overlooking. Any help would be greatly appreciated.
Are you using or-tools 9.10 on windows ?
If yes, check the known issues in the release notes: https://github.com/google/or-tools/releases
You may need to update the visual studio redistributable libraries