Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions examples/advanced/hyperparameter_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

Generally applicable, and demonstrated on n-queens with ortools
"""
import sys
from cpmpy import *
from cpmpy.solvers import CPM_ortools, param_combinations
import cpmpy as cp
from cpmpy.solvers import param_combinations
from cpmpy.transformations.flatten_model import flatten_model

def main():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

give model as arg?
Also function name could be more informative. Like "tune_parameters" or something.

model = nqueens(n=25)
model = nqueens(n=50)
# flatten once upfront, reduces overhead of multiple solves
model = flatten_model(model)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from when flatten was the only transformation for ortools...
I would actually just remove this. The overhead should be negligable, and we don't count it in the runtime anyway (only solve time there)


Expand All @@ -22,7 +21,7 @@ def main():
configs = [] # (runtime, param)
for params in param_combinations(all_params):
print("Running with", params)
s = CPM_ortools(model)
s = cp.SolverLookup.get("ortools", model)
s.solve(**params)
print(s.status())

Expand All @@ -36,24 +35,25 @@ def main():
print("Fastest in", round(best[0],2), "seconds, config:", best[1])
print("Comparing best -- worst:", round(configs[0][0],2), "--", round(configs[-1][0],2))

s = CPM_ortools(model); s.solve()
s = cp.SolverLookup.get("ortools", model)
s.solve()
print("With default parameters:", round(s.status().runtime,2))

# Outputs:
# Fastest in 0.02 seconds, config: {'cp_model_probing_level': 0, 'linearization_level': 0, 'symmetry_level': 1}
# Comparing best -- worst: 0.02 -- 0.2
# With default parameters: 0.13
# Fastest in 0.01 seconds, config: {'cp_model_probing_level': 0, 'linearization_level': 2, 'symmetry_level': 0}
# Comparing best -- worst: 0.05 -- 0.24
# With default parameters: 0.16



def nqueens(n=8):
""" N-queens problem
"""
queens = intvar(1,n, shape=n)
return Model(
AllDifferent(queens),
AllDifferent([queens[i] + i for i in range(n)]),
AllDifferent([queens[i] - i for i in range(n)]),
queens = cp.intvar(1,n, shape=n)
return cp.Model(
cp.AllDifferent(queens),
cp.AllDifferent([queens[i] + i for i in range(n)]),
cp.AllDifferent([queens[i] - i for i in range(n)]),
)


Expand Down
Loading