Coverage for moptipyapps / binpacking2d / instgen / experiment.py: 65%
26 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-11 04:40 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-11 04:40 +0000
1"""An example experiment for generating bin packing instances."""
3import argparse
4from typing import Callable, Final, cast
6from moptipy.algorithms.so.vector.cmaes_lib import BiPopCMAES
7from moptipy.api.execution import Execution
8from moptipy.api.experiment import run_experiment
9from pycommons.io.path import Path
11from moptipyapps.binpacking2d.instance import Instance
12from moptipyapps.binpacking2d.instgen.errors_and_hardness import (
13 ErrorsAndHardness,
14)
15from moptipyapps.binpacking2d.instgen.problem import Problem
16from moptipyapps.utils.shared import moptipyapps_argparser
18#: the maximum number of FEs
19MAX_FES: Final[int] = 10_000
21#: the maximum number of FEs in for the algorithm runs inside the objective
22INNER_MAX_FES: Final[int] = 225_000
24#: the number of runs of the algorithms inside the objective
25INNER_RUNS: Final[int] = 2
28def cmaes(problem: Problem) -> Execution:
29 """
30 Create the basic BiPop-CMA-ES setup.
32 :param problem: the problem to solve
33 :return: the execution
34 """
35 return (Execution()
36 .set_algorithm(BiPopCMAES(problem.search_space))
37 .set_search_space(problem.search_space)
38 .set_solution_space(problem.solution_space)
39 .set_encoding(problem.encoding)
40 .set_objective(ErrorsAndHardness(
41 problem.solution_space, INNER_MAX_FES, INNER_RUNS))
42 .set_max_fes(MAX_FES)
43 .set_log_improvements(True))
46#: the instances to use as blueprints
47__USE_INSTANCES: Final[tuple[str, ...]] = (
48 "beng01", "beng02", "beng03", "beng04", "beng05", "beng06", "beng07",
49 "beng08", "beng09", "beng10", "cl01_020_01", "cl01_040_01", "cl01_060_01",
50 "cl01_080_01", "cl01_100_01", "cl02_020_01", "cl02_040_01", "cl02_060_01",
51 "cl02_080_01", "cl02_100_01", "cl03_020_01", "cl03_040_01", "cl03_060_01",
52 "cl03_080_01", "cl03_100_01", "cl04_020_01", "cl04_040_01", "cl04_060_01",
53 "cl04_080_01", "cl04_100_01", "cl05_020_01", "cl05_040_01", "cl05_060_01",
54 "cl05_080_01", "cl05_100_01", "cl06_020_01", "cl06_040_01", "cl06_060_01",
55 "cl06_080_01", "cl06_100_01", "cl07_020_01", "cl07_040_01", "cl07_060_01",
56 "cl07_080_01", "cl07_100_01", "cl08_020_01", "cl08_040_01", "cl08_060_01",
57 "cl08_080_01", "cl08_100_01", "cl09_020_01", "cl09_040_01", "cl09_060_01",
58 "cl09_080_01", "cl09_100_01", "cl10_020_01", "cl10_040_01", "cl10_060_01",
59 "cl10_080_01", "cl10_100_01", "cl10_100_10")
62def run(base_dir: str) -> None:
63 """
64 Run the experiment.
66 :param base_dir: the base directory
67 """
68 use_dir: Final[Path] = Path(base_dir)
69 use_dir.ensure_dir_exists()
71 inst_creators: list[Callable[[], Instance]] = [cast(
72 "Callable[[], Instance]", lambda __s=sss, __t=ttt: Problem(__s, __t))
73 for sss in __USE_INSTANCES for ttt in (0.25, 0.125)]
75 run_experiment(
76 base_dir=use_dir,
77 instances=inst_creators,
78 setups=[cmaes],
79 n_runs=[1, 2, 3, 5, 7, 11, 13, 17, 20, 23, 29, 30, 31],
80 perform_warmup=False,
81 perform_pre_warmup=False)
84# Run the experiment from the command line
85if __name__ == "__main__":
86 parser: Final[argparse.ArgumentParser] = moptipyapps_argparser(
87 __file__, "2D Bin Packing Instance Generator",
88 "Run the 2D Bin Packing Instance Generator experiment.")
89 parser.add_argument(
90 "dest", help="the directory to store the experimental results under",
91 type=Path, nargs="?", default="./results/")
92 args: Final[argparse.Namespace] = parser.parse_args()
93 run(args.dest)