-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v0.2.0 now with CLI and better output.
- Loading branch information
Showing
7 changed files
with
281 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ where = ["src"] | |
|
||
[project] | ||
name = "cpsat-autotune" | ||
version = "0.1.1" | ||
version = "0.2.0" | ||
authors = [ | ||
{ name = "Dominik Krupke", email = "[email protected]" }, | ||
] | ||
|
@@ -20,5 +20,8 @@ classifiers = [ | |
"Development Status :: 3 - Alpha", | ||
] | ||
dependencies = [ | ||
"optuna", "ortools", "numpy", "scipy", "rich" | ||
"optuna", "ortools", "numpy", "scipy", "rich", "click" | ||
] | ||
|
||
[project.scripts] | ||
cpsat-autotune = "cpsat_autotune.cli:cli" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .cli import cli | ||
|
||
if __name__ == "__main__": | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
import click | ||
from .model_loading import import_model | ||
from .tune import tune_time_to_optimal, tune_for_quality_within_timelimit | ||
|
||
import logging | ||
|
||
# Configure logging | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(asctime)s - %(levelname)s - %(message)s", | ||
handlers=[logging.StreamHandler()], | ||
) | ||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@click.group() | ||
def cli(): | ||
"""CLI for CP-SAT hyperparameter tuning.""" | ||
pass | ||
|
||
|
||
def _estimate_time(max_time, n_trials, n_samples): | ||
expected_time = max_time * n_samples * n_trials | ||
# convert to hours and minutes | ||
hours = int(expected_time // 3600) | ||
minutes = int((expected_time % 3600) // 60) | ||
if hours > 0: | ||
logging.info( | ||
f"The expected time for the tuning process is {hours} hours and {minutes} minutes." | ||
) | ||
else: | ||
logging.info(f"The expected time for the tuning process is {minutes} minutes.") | ||
logging.info( | ||
"The tuning algorithm will try to take shortcuts whenever possible, potentially reducing the time drastically." | ||
) | ||
logging.info( | ||
"To reduce the expected time, you can try to reduce the number of trials or samples per trial, as well as the maximum time allowed for each solve operation. However, this may affect the reliability of the tuning process." | ||
) | ||
|
||
@click.command( | ||
help=""" | ||
Tune CP-SAT hyperparameters to minimize the time required to find an optimal solution. | ||
This command tunes the hyperparameters of a CP-SAT model to minimize the time required to find an optimal solution. | ||
You need to provide the path to the model file and specify the maximum time allowed for each solve operation, | ||
the relative optimality gap, and the number of trials and samples for the tuning process. | ||
""" | ||
) | ||
@click.argument("model_path", type=click.Path(exists=True)) | ||
@click.option( | ||
"--max-time", | ||
type=float, | ||
required=True, | ||
help="Maximum time allowed for each solve operation in seconds." | ||
) | ||
@click.option( | ||
"--relative-gap", | ||
type=float, | ||
default=0.0, | ||
help="Relative optimality gap for considering a solution as optimal." | ||
) | ||
@click.option( | ||
"--n-trials", | ||
type=int, | ||
default=100, | ||
help="Number of trials to execute in the tuning process." | ||
) | ||
@click.option( | ||
"--n-samples-trial", | ||
type=int, | ||
default=10, | ||
help="Number of samples to take in each trial." | ||
) | ||
@click.option( | ||
"--n-samples-verification", | ||
type=int, | ||
default=30, | ||
help="Number of samples for verifying parameters." | ||
) | ||
def time( | ||
model_path, | ||
max_time, | ||
relative_gap, | ||
n_trials, | ||
n_samples_trial, | ||
n_samples_verification, | ||
): | ||
"""Tune CP-SAT hyperparameters to minimize the time required to find an optimal solution.""" | ||
_estimate_time(max_time, n_trials, n_samples_trial) | ||
model = import_model(model_path) | ||
best_params = tune_time_to_optimal( | ||
model=model, | ||
max_time_in_seconds=max_time, | ||
relative_gap_limit=relative_gap, | ||
n_samples_for_trial=n_samples_trial, | ||
n_samples_for_verification=n_samples_verification, | ||
n_trials=n_trials, | ||
) | ||
click.echo(f"Best parameters: {best_params}") | ||
|
||
|
||
@click.command( | ||
help=""" | ||
Tune CP-SAT hyperparameters to maximize or minimize solution quality within a given time limit. | ||
This command tunes the hyperparameters of a CP-SAT model to optimize the solution quality within a specified time limit. | ||
You need to provide the path to the model file, the maximum time allowed for each solve operation, the objective value | ||
to return if the solver times out, and the direction to optimize the objective value. Additionally, you can specify | ||
the number of trials and samples for the tuning process. | ||
""" | ||
) | ||
@click.argument("model_path", type=click.Path(exists=True)) | ||
@click.option( | ||
"--max-time", | ||
type=float, | ||
required=True, | ||
help="Time limit for each solve operation in seconds." | ||
) | ||
@click.option( | ||
"--obj-for-timeout", | ||
type=int, | ||
required=True, | ||
help="Objective value to return if the solver times out." | ||
) | ||
@click.option( | ||
"--direction", | ||
type=click.Choice(["maximize", "minimize"]), | ||
required=True, | ||
help="Direction to optimize the objective value." | ||
) | ||
@click.option( | ||
"--n-trials", | ||
type=int, | ||
default=100, | ||
help="Number of trials to execute in the tuning process." | ||
) | ||
@click.option( | ||
"--n-samples-trial", | ||
type=int, | ||
default=10, | ||
help="Number of samples to take in each trial." | ||
) | ||
@click.option( | ||
"--n-samples-verification", | ||
type=int, | ||
default=30, | ||
help="Number of samples for verifying parameters." | ||
) | ||
def quality( | ||
model_path, | ||
max_time, | ||
obj_for_timeout, | ||
direction, | ||
n_trials, | ||
n_samples_trial, | ||
n_samples_verification, | ||
): | ||
"""Tune CP-SAT hyperparameters to maximize or minimize solution quality within a given time limit.""" | ||
_estimate_time(max_time, n_trials, n_samples_trial) | ||
model = import_model(model_path) | ||
best_params = tune_for_quality_within_timelimit( | ||
model=model, | ||
max_time_in_seconds=max_time, | ||
obj_for_timeout=obj_for_timeout, | ||
direction=direction, | ||
n_samples_for_trial=n_samples_trial, | ||
n_samples_for_verification=n_samples_verification, | ||
n_trials=n_trials, | ||
) | ||
click.echo(f"Best parameters: {best_params}") | ||
|
||
|
||
cli.add_command(time) | ||
cli.add_command(quality) | ||
|
||
if __name__ == "__main__": | ||
cli() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters