hone-python 0.11.0__py3-none-any.whl → 0.12.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hone/__init__.py +23 -4
- hone/benchmarks/__init__.py +1 -0
- hone/benchmarks/__main__.py +27 -0
- hone/benchmarks/multi.py +447 -0
- hone/config/__init__.py +1 -0
- hone/config/multi_objective_config.py +86 -0
- hone/objective_function/objective_function.py +10 -0
- hone/optimizers/__init__.py +4 -2
- hone/optimizers/_gp.py +5 -0
- hone/optimizers/base.py +81 -485
- hone/optimizers/bayesian.py +59 -46
- hone/optimizers/choice.py +34 -1
- hone/optimizers/core.py +564 -0
- hone/optimizers/genetic/__init__.py +1 -0
- hone/optimizers/genetic/breeding.py +206 -0
- hone/optimizers/genetic/nsga2.py +115 -0
- hone/optimizers/genetic/optimizer.py +10 -164
- hone/optimizers/multi.py +230 -0
- hone/optimizers/multi_bayesian.py +153 -0
- hone/pareto.py +265 -0
- hone/result.py +12 -5
- {hone_python-0.11.0.dist-info → hone_python-0.12.0.dist-info}/METADATA +69 -2
- hone_python-0.12.0.dist-info/RECORD +45 -0
- hone_python-0.11.0.dist-info/RECORD +0 -37
- {hone_python-0.11.0.dist-info → hone_python-0.12.0.dist-info}/WHEEL +0 -0
- {hone_python-0.11.0.dist-info → hone_python-0.12.0.dist-info}/licenses/LICENSE +0 -0
hone/__init__.py
CHANGED
|
@@ -1,34 +1,55 @@
|
|
|
1
|
-
from hone.config import BayesianConfig, CMAESConfig, GeneticOptimizerConfig, OptimizerConfig, SelectionMethod
|
|
1
|
+
from hone.config import BayesianConfig, CMAESConfig, GeneticOptimizerConfig, MultiObjectiveBayesianConfig, MultiObjectiveConfig, NSGA2Config, OptimizerConfig, SelectionMethod
|
|
2
2
|
from hone.optimizers import (
|
|
3
3
|
CMAES,
|
|
4
4
|
BayesianOptimizer,
|
|
5
5
|
EvaluationFailed,
|
|
6
6
|
GeneticOptimizer,
|
|
7
|
+
MultiObjectiveBayesian,
|
|
8
|
+
MultiObjectiveOptimizer,
|
|
9
|
+
MultiObjectiveRandomSearch,
|
|
10
|
+
NSGA2,
|
|
7
11
|
Optimizer,
|
|
8
12
|
RandomSearch,
|
|
9
13
|
Trial,
|
|
10
14
|
UnsupportedSearchSpace,
|
|
11
15
|
choose_optimizer,
|
|
16
|
+
choose_pareto_optimizer,
|
|
12
17
|
)
|
|
13
18
|
from hone.parameters import ChoiceParameter, Condition, Constraint, FixedParameter, InfeasibleSearchSpace, RangeParameter, SearchSpace
|
|
19
|
+
from hone.pareto import Front, ParetoGenerationStats, ParetoPoint, ParetoProgress, ParetoResult
|
|
14
20
|
from hone.result import GenerationStats, Progress, Result, RunInfo, print_progress
|
|
15
21
|
|
|
16
22
|
__all__ = [
|
|
17
23
|
"BayesianConfig",
|
|
18
24
|
"BayesianOptimizer",
|
|
25
|
+
"ChoiceParameter",
|
|
26
|
+
"choose_optimizer",
|
|
27
|
+
"choose_pareto_optimizer",
|
|
19
28
|
"CMAES",
|
|
20
29
|
"CMAESConfig",
|
|
21
|
-
"ChoiceParameter",
|
|
22
30
|
"Condition",
|
|
23
31
|
"Constraint",
|
|
24
32
|
"EvaluationFailed",
|
|
25
33
|
"FixedParameter",
|
|
34
|
+
"Front",
|
|
26
35
|
"GenerationStats",
|
|
27
36
|
"GeneticOptimizer",
|
|
28
37
|
"GeneticOptimizerConfig",
|
|
29
38
|
"InfeasibleSearchSpace",
|
|
39
|
+
"MultiObjectiveBayesian",
|
|
40
|
+
"MultiObjectiveBayesianConfig",
|
|
41
|
+
"MultiObjectiveConfig",
|
|
42
|
+
"MultiObjectiveOptimizer",
|
|
43
|
+
"MultiObjectiveRandomSearch",
|
|
44
|
+
"NSGA2",
|
|
45
|
+
"NSGA2Config",
|
|
30
46
|
"Optimizer",
|
|
31
47
|
"OptimizerConfig",
|
|
48
|
+
"ParetoGenerationStats",
|
|
49
|
+
"ParetoPoint",
|
|
50
|
+
"ParetoProgress",
|
|
51
|
+
"ParetoResult",
|
|
52
|
+
"print_progress",
|
|
32
53
|
"Progress",
|
|
33
54
|
"RandomSearch",
|
|
34
55
|
"RangeParameter",
|
|
@@ -38,6 +59,4 @@ __all__ = [
|
|
|
38
59
|
"SelectionMethod",
|
|
39
60
|
"Trial",
|
|
40
61
|
"UnsupportedSearchSpace",
|
|
41
|
-
"choose_optimizer",
|
|
42
|
-
"print_progress",
|
|
43
62
|
]
|
hone/benchmarks/__init__.py
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
from .conditional import conditional_problems
|
|
3
3
|
from .constrained import constrained_problems
|
|
4
4
|
from .ml import ml_problems
|
|
5
|
+
from .multi import MultiProblem, multi_problems, run_multi_benchmark, summarize_multi
|
|
5
6
|
from .problems import Problem, noisy_problems, quick_problems, standard_problems
|
|
6
7
|
from .runner import (
|
|
7
8
|
BUDGETS,
|
hone/benchmarks/__main__.py
CHANGED
|
@@ -7,6 +7,7 @@ from importlib.metadata import version
|
|
|
7
7
|
|
|
8
8
|
from . import ml
|
|
9
9
|
from .conditional import conditional_problems
|
|
10
|
+
from .multi import multi_markdown, multi_problems, run_multi_benchmark, summarize_multi
|
|
10
11
|
from .constrained import constrained_problems
|
|
11
12
|
from .problems import noisy_problems, quick_problems, standard_problems
|
|
12
13
|
from .runner import BUDGETS, load_summaries, regressions, run_benchmark, save_summaries, summarize, to_markdown
|
|
@@ -29,6 +30,9 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
29
30
|
suite.add_argument("--ml", action="store_true", help="run the machine learning tuning problems instead (needs scikit-learn)")
|
|
30
31
|
suite.add_argument("--noisy", action="store_true", help="run the problems with noise added to every evaluation instead")
|
|
31
32
|
suite.add_argument("--constrained", action="store_true", help="run the problems with constraints instead")
|
|
33
|
+
suite.add_argument(
|
|
34
|
+
"--multi", action="store_true", help="run the problems with several objectives instead, scored by hypervolume"
|
|
35
|
+
)
|
|
32
36
|
suite.add_argument(
|
|
33
37
|
"--conditional", action="store_true", help="run the problems with conditional parameters, and their flat versions, instead"
|
|
34
38
|
)
|
|
@@ -48,6 +52,8 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
48
52
|
parser.add_argument("--baseline", help="compare against summaries saved with --save; exit 1 on a regression")
|
|
49
53
|
args = parser.parse_args(argv)
|
|
50
54
|
|
|
55
|
+
if args.multi:
|
|
56
|
+
return _multi(args)
|
|
51
57
|
default_budgets: Sequence[int]
|
|
52
58
|
if args.quick:
|
|
53
59
|
problems, default_budgets = quick_problems(), QUICK_BUDGETS
|
|
@@ -90,5 +96,26 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
90
96
|
return 0
|
|
91
97
|
|
|
92
98
|
|
|
99
|
+
def _multi(args: argparse.Namespace) -> int:
|
|
100
|
+
"""The multi-objective suite: its own runs, scored by hypervolume gap."""
|
|
101
|
+
problems = multi_problems()
|
|
102
|
+
seeds = args.seeds or 10
|
|
103
|
+
budgets = sorted(set(args.budgets or BUDGETS))
|
|
104
|
+
start = time.perf_counter()
|
|
105
|
+
summaries = summarize_multi(run_multi_benchmark(problems, seeds=range(seeds), budgets_per_dimension=budgets, jobs=args.jobs))
|
|
106
|
+
elapsed = time.perf_counter() - start
|
|
107
|
+
table = multi_markdown(summaries)
|
|
108
|
+
print(table)
|
|
109
|
+
setup = f"{len(problems)} problems, {seeds} seeds, budgets of {_join(budgets)} evaluations per dimension"
|
|
110
|
+
print(f"{setup}, in {elapsed:.0f}s")
|
|
111
|
+
if args.markdown:
|
|
112
|
+
with open(args.markdown, "w") as f:
|
|
113
|
+
f.write(f"# Multi-objective benchmark results\n\nGenerated by `python -m hone.benchmarks --multi` with hone {version('hone-python')}: {setup}.\n\n{table}")
|
|
114
|
+
if args.save:
|
|
115
|
+
with open(args.save, "w") as f:
|
|
116
|
+
f.write("[\n" + ",\n".join(s.model_dump_json() for s in summaries) + "\n]\n")
|
|
117
|
+
return 0
|
|
118
|
+
|
|
119
|
+
|
|
93
120
|
if __name__ == "__main__":
|
|
94
121
|
sys.exit(main())
|
hone/benchmarks/multi.py
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
"""Multi-objective test problems, and a runner that scores fronts by hypervolume.
|
|
2
|
+
|
|
3
|
+
- zdt1, zdt2, zdt3 and zdt4: two objectives, with fronts that are convex, bend inward,
|
|
4
|
+
come in five disconnected pieces, and (zdt4) sit behind many false fronts. They are shifted
|
|
5
|
+
so the optimal designs lie inside the bounds rather than on them, which clipping and blend
|
|
6
|
+
crossover would otherwise favour, with the distance from the front measured by a smooth
|
|
7
|
+
quadratic. rotated-zdt1 turns the distance variables by a fixed rotation, so they interact.
|
|
8
|
+
- dtlz2: three objectives, whose front is an eighth of a sphere, shifted the same way.
|
|
9
|
+
- welded-beam-2: the welded beam's cost against its deflection, under its stress, buckling
|
|
10
|
+
and geometry constraints, with a front found by long reference runs.
|
|
11
|
+
|
|
12
|
+
Each problem's objectives are scaled from 0 at the true front's ideal to 1 at its nadir, and
|
|
13
|
+
hypervolume is measured in that space up to a reference point of 1.1 in every objective, so
|
|
14
|
+
scores compare across runs and versions. A run's gap is the true front's hypervolume less
|
|
15
|
+
the run's: 0 is the true front itself.
|
|
16
|
+
"""
|
|
17
|
+
import functools
|
|
18
|
+
import math
|
|
19
|
+
import multiprocessing
|
|
20
|
+
import statistics
|
|
21
|
+
import zlib
|
|
22
|
+
from concurrent.futures import ProcessPoolExecutor
|
|
23
|
+
from typing import Any, Callable, Iterable
|
|
24
|
+
|
|
25
|
+
import numpy as np
|
|
26
|
+
from pydantic import BaseModel, ConfigDict
|
|
27
|
+
|
|
28
|
+
from hone.config import BayesianConfig, GeneticOptimizerConfig, MultiObjectiveBayesianConfig, MultiObjectiveConfig, NSGA2Config
|
|
29
|
+
from hone.optimizers import (
|
|
30
|
+
BayesianOptimizer,
|
|
31
|
+
GeneticOptimizer,
|
|
32
|
+
MultiObjectiveBayesian,
|
|
33
|
+
MultiObjectiveOptimizer,
|
|
34
|
+
MultiObjectiveRandomSearch,
|
|
35
|
+
)
|
|
36
|
+
from hone.optimizers.genetic import NSGA2
|
|
37
|
+
from hone.parameters import Constraint, RangeParameter, SearchSpace
|
|
38
|
+
from hone.pareto import Direction, hypervolume, non_dominated
|
|
39
|
+
|
|
40
|
+
from .constrained import E, L, P, _buckling, _shear
|
|
41
|
+
from .runner import BAYESIAN_MAX_EVALUATIONS, BUDGETS, POPULATION_SIZE, SkipBudget
|
|
42
|
+
from .problems import _rotation
|
|
43
|
+
|
|
44
|
+
REFERENCE = 1.1 # the reference point in every scaled objective
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class MultiProblem(BaseModel):
|
|
48
|
+
"""A function of several objectives, its search space, and its true front's scale and hypervolume."""
|
|
49
|
+
model_config = ConfigDict(frozen=True)
|
|
50
|
+
|
|
51
|
+
name: str
|
|
52
|
+
function: Callable[..., dict[str, float]]
|
|
53
|
+
search_space: SearchSpace
|
|
54
|
+
objectives: dict[str, Direction]
|
|
55
|
+
ideal: dict[str, float] # the true front's best value of each objective
|
|
56
|
+
nadir: dict[str, float] # and its worst
|
|
57
|
+
true_hypervolume: float # of the true front, scaled, up to REFERENCE
|
|
58
|
+
tags: frozenset[str] = frozenset()
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def dimensions(self) -> int:
|
|
62
|
+
return len(self.search_space.parameters)
|
|
63
|
+
|
|
64
|
+
def scaled(self, values: dict[str, float]) -> np.ndarray:
|
|
65
|
+
"""values scaled to minimize, from 0 at the ideal to 1 at the nadir."""
|
|
66
|
+
return np.array([(values[n] - self.ideal[n]) / (self.nadir[n] - self.ideal[n]) for n in self.objectives])
|
|
67
|
+
|
|
68
|
+
def hypervolume(self, points: Iterable[dict[str, float]]) -> float:
|
|
69
|
+
vectors = np.array([self.scaled(v) for v in points]).reshape(-1, len(self.objectives))
|
|
70
|
+
return hypervolume(vectors, np.full(len(self.objectives), REFERENCE))
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _centre(name: str, size: int, low: float, high: float) -> np.ndarray:
|
|
74
|
+
return np.random.default_rng(zlib.crc32(f"{name}/centre".encode())).uniform(low, high, size)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _front_hypervolume(f1: np.ndarray, f2: np.ndarray, ideal: tuple[float, float], nadir: tuple[float, float]) -> float:
|
|
78
|
+
"""The scaled hypervolume of a two-objective front sampled densely."""
|
|
79
|
+
points = np.column_stack([(f1 - ideal[0]) / (nadir[0] - ideal[0]), (f2 - ideal[1]) / (nadir[1] - ideal[1])])
|
|
80
|
+
return hypervolume(points, np.full(2, REFERENCE))
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
ZDT_SHAPES: dict[str, Callable[[np.ndarray, np.ndarray], np.ndarray]] = {
|
|
84
|
+
"zdt1": lambda f1, g: 1 - np.sqrt(f1 / g),
|
|
85
|
+
"zdt2": lambda f1, g: 1 - (f1 / g) ** 2,
|
|
86
|
+
"zdt3": lambda f1, g: 1 - np.sqrt(f1 / g) - (f1 / g) * np.sin(10 * np.pi * f1),
|
|
87
|
+
"zdt4": lambda f1, g: 1 - np.sqrt(f1 / g),
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@functools.cache
|
|
92
|
+
def _zdt_front(shape: str) -> tuple[tuple[float, float], tuple[float, float], float]:
|
|
93
|
+
"""The ideal, nadir and scaled hypervolume of a ZDT front, from 200,001 points along it."""
|
|
94
|
+
f1 = np.linspace(0, 1, 200_001)
|
|
95
|
+
f2 = ZDT_SHAPES[shape](f1, np.ones_like(f1))
|
|
96
|
+
vectors = np.column_stack([f1, f2])
|
|
97
|
+
if shape == "zdt3": # the disconnected front: keep only the points nothing dominates
|
|
98
|
+
order = np.argsort(f1)
|
|
99
|
+
best = np.minimum.accumulate(f2[order])
|
|
100
|
+
keep = order[np.concatenate([[True], f2[order][1:] < best[:-1]])]
|
|
101
|
+
vectors = vectors[keep]
|
|
102
|
+
ideal, nadir = (float(vectors[:, 0].min()), float(vectors[:, 1].min())), (float(vectors[:, 0].max()), float(vectors[:, 1].max()))
|
|
103
|
+
return ideal, nadir, _front_hypervolume(vectors[:, 0], vectors[:, 1], ideal, nadir)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def zdt(shape: str, dimensions: int, rotate: bool = False) -> MultiProblem:
|
|
107
|
+
name = f"{'rotated-' if rotate else ''}{shape}-{dimensions}d"
|
|
108
|
+
wide = shape == "zdt4"
|
|
109
|
+
first = RangeParameter("x0", 0, 1)
|
|
110
|
+
rest = [RangeParameter(f"x{i}", -5 if wide else 0, 5 if wide else 1) for i in range(1, dimensions)]
|
|
111
|
+
centre = _centre(name, dimensions - 1, -2 if wide else 0.2, 2 if wide else 0.8)
|
|
112
|
+
width = np.maximum(centre, 1 - centre)
|
|
113
|
+
rotation = _rotation(name, dimensions - 1) if rotate else np.eye(dimensions - 1)
|
|
114
|
+
shape_of = ZDT_SHAPES[shape]
|
|
115
|
+
|
|
116
|
+
def function(**params: float) -> dict[str, float]:
|
|
117
|
+
f1 = float(params["x0"])
|
|
118
|
+
x = np.array([params[p.name] for p in rest], dtype=float)
|
|
119
|
+
if wide:
|
|
120
|
+
z = x - centre
|
|
121
|
+
g = 1 + 10 * len(z) + float(np.sum(z**2 - 10 * np.cos(4 * np.pi * z)))
|
|
122
|
+
else:
|
|
123
|
+
d = rotation @ ((x - centre) / width)
|
|
124
|
+
g = 1 + 9 * float(np.mean(d**2))
|
|
125
|
+
return {"f1": f1, "f2": float(g * shape_of(np.array(f1), np.array(g)))}
|
|
126
|
+
|
|
127
|
+
function.__name__ = name.replace("-", "_")
|
|
128
|
+
ideal, nadir, volume = _zdt_front(shape)
|
|
129
|
+
return MultiProblem(
|
|
130
|
+
name=name,
|
|
131
|
+
function=function,
|
|
132
|
+
search_space=SearchSpace([first, *rest]),
|
|
133
|
+
objectives={"f1": "minimize", "f2": "minimize"},
|
|
134
|
+
ideal={"f1": ideal[0], "f2": ideal[1]},
|
|
135
|
+
nadir={"f1": nadir[0], "f2": nadir[1]},
|
|
136
|
+
true_hypervolume=volume,
|
|
137
|
+
tags=frozenset({"zdt", "rotated"} if rotate else {"zdt"}),
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def dtlz2(dimensions: int = 7) -> MultiProblem:
|
|
142
|
+
"""Three objectives on an eighth of the unit sphere; the first two variables place a point on it."""
|
|
143
|
+
name = f"dtlz2-{dimensions}d"
|
|
144
|
+
parameters = [RangeParameter(f"x{i}", 0, 1) for i in range(dimensions)]
|
|
145
|
+
centre = _centre(name, dimensions - 2, 0.25, 0.75)
|
|
146
|
+
|
|
147
|
+
def function(**params: float) -> dict[str, float]:
|
|
148
|
+
x = np.array([params[p.name] for p in parameters], dtype=float)
|
|
149
|
+
g = float(np.sum((x[2:] - centre) ** 2))
|
|
150
|
+
a, b = x[0] * np.pi / 2, x[1] * np.pi / 2
|
|
151
|
+
return {"f1": (1 + g) * math.cos(a) * math.cos(b), "f2": (1 + g) * math.cos(a) * math.sin(b), "f3": (1 + g) * math.sin(a)}
|
|
152
|
+
|
|
153
|
+
function.__name__ = name.replace("-", "_")
|
|
154
|
+
return MultiProblem(
|
|
155
|
+
name=name,
|
|
156
|
+
function=function,
|
|
157
|
+
search_space=SearchSpace(parameters),
|
|
158
|
+
objectives={"f1": "minimize", "f2": "minimize", "f3": "minimize"},
|
|
159
|
+
ideal={"f1": 0.0, "f2": 0.0, "f3": 0.0},
|
|
160
|
+
nadir={"f1": 1.0, "f2": 1.0, "f3": 1.0},
|
|
161
|
+
true_hypervolume=REFERENCE**3 - math.pi / 6, # the reference cube less an eighth of the unit ball
|
|
162
|
+
tags=frozenset({"dtlz"}),
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
# The welded beam's front isn't known in closed form: its scale and hypervolume are those of
|
|
167
|
+
# the front of WELDED_BEAM_REFERENCE_SEARCH.
|
|
168
|
+
WELDED_BEAM_REFERENCE_SEARCH = "NSGA2 with 50,000 evaluations, seeds 0 to 2, combined"
|
|
169
|
+
WELDED_BEAM_SCALE = (
|
|
170
|
+
{"cost": 1.7279633786270103, "deflection": 0.0004390400449635821},
|
|
171
|
+
{"cost": 35.308391288678266, "deflection": 0.014327892424181738},
|
|
172
|
+
)
|
|
173
|
+
WELDED_BEAM_HYPERVOLUME = 1.1323322942394598 # a front of 2,867 points
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def welded_beam() -> MultiProblem:
|
|
177
|
+
"""The welded beam's cost against its deflection under the end load (Deb's two-objective version)."""
|
|
178
|
+
weld, length = RangeParameter("weld", 0.125, 5.0), RangeParameter("length", 0.1, 10.0)
|
|
179
|
+
thickness, width = RangeParameter("thickness", 0.1, 10.0), RangeParameter("width", 0.125, 5.0)
|
|
180
|
+
|
|
181
|
+
def welded_beam_2(weld: float, length: float, thickness: float, width: float) -> dict[str, float]:
|
|
182
|
+
cost = 1.10471 * weld**2 * length + 0.04811 * thickness * width * (14 + length)
|
|
183
|
+
return {"cost": cost, "deflection": 4 * P * L**3 / (E * thickness**3 * width)}
|
|
184
|
+
|
|
185
|
+
def shear(weld: float, length: float, thickness: float) -> float:
|
|
186
|
+
return _shear(weld, length, thickness) - 13600
|
|
187
|
+
|
|
188
|
+
def buckling(thickness: float, width: float) -> float:
|
|
189
|
+
return P - _buckling(thickness, width)
|
|
190
|
+
|
|
191
|
+
space = SearchSpace(
|
|
192
|
+
[weld, length, thickness, width],
|
|
193
|
+
[Constraint.function(shear), 6 * P * L / (width * thickness**2) <= 30000, weld <= width, Constraint.function(buckling)],
|
|
194
|
+
)
|
|
195
|
+
ideal, nadir = WELDED_BEAM_SCALE
|
|
196
|
+
return MultiProblem(
|
|
197
|
+
name="welded-beam-2",
|
|
198
|
+
function=welded_beam_2,
|
|
199
|
+
search_space=space,
|
|
200
|
+
objectives={"cost": "minimize", "deflection": "minimize"},
|
|
201
|
+
ideal=ideal,
|
|
202
|
+
nadir=nadir,
|
|
203
|
+
true_hypervolume=WELDED_BEAM_HYPERVOLUME,
|
|
204
|
+
tags=frozenset({"constrained", "engineering"}),
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def multi_problems() -> list[MultiProblem]:
|
|
209
|
+
"""The multi-objective suite."""
|
|
210
|
+
return [
|
|
211
|
+
zdt("zdt1", 8),
|
|
212
|
+
zdt("zdt1", 8, rotate=True),
|
|
213
|
+
zdt("zdt2", 8),
|
|
214
|
+
zdt("zdt3", 8),
|
|
215
|
+
zdt("zdt4", 6),
|
|
216
|
+
dtlz2(7),
|
|
217
|
+
welded_beam(),
|
|
218
|
+
]
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
# --- optimizers --------------------------------------------------------------------
|
|
222
|
+
|
|
223
|
+
MultiFactory = Callable[[MultiProblem, int, int], Any]
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def nsga2(problem: MultiProblem, budget: int, seed: int) -> MultiObjectiveOptimizer:
|
|
227
|
+
return NSGA2(problem.search_space, NSGA2Config(objectives=problem.objectives, max_evaluations=budget, seed=seed))
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def _bayesian(method: str) -> MultiFactory:
|
|
231
|
+
def factory(problem: MultiProblem, budget: int, seed: int) -> MultiObjectiveOptimizer:
|
|
232
|
+
if budget > BAYESIAN_MAX_EVALUATIONS:
|
|
233
|
+
raise SkipBudget(f"{budget} evaluations is more than Bayesian optimization is run with")
|
|
234
|
+
config = MultiObjectiveBayesianConfig(objectives=problem.objectives, max_evaluations=budget, seed=seed, method=method)
|
|
235
|
+
return MultiObjectiveBayesian(problem.search_space, config)
|
|
236
|
+
|
|
237
|
+
return factory
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def random_search(problem: MultiProblem, budget: int, seed: int) -> MultiObjectiveOptimizer:
|
|
241
|
+
config = MultiObjectiveConfig(
|
|
242
|
+
objectives=problem.objectives, population_size=POPULATION_SIZE, generations=budget, max_evaluations=budget, seed=seed
|
|
243
|
+
)
|
|
244
|
+
return MultiObjectiveRandomSearch(problem.search_space, config)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def weighted_sums(problem: MultiProblem, budget: int, seed: int) -> "WeightedSums":
|
|
248
|
+
return WeightedSums(problem, budget, seed)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class WeightedSums:
|
|
252
|
+
"""The way to search for trade-offs without multi-objective optimization: several single-objective runs, each of a weighted sum.
|
|
253
|
+
|
|
254
|
+
The budget is shared among runs with weights spread evenly over the scaled objectives (5
|
|
255
|
+
runs for two objectives, 6 for three), each by the optimizer choose_optimizer would pick
|
|
256
|
+
for its share, and the front is that of every point any of them evaluated. The objectives
|
|
257
|
+
are scaled by the true front's ideal and nadir, which a real user wouldn't know: a
|
|
258
|
+
generous baseline.
|
|
259
|
+
"""
|
|
260
|
+
|
|
261
|
+
def __init__(self, problem: MultiProblem, budget: int, seed: int) -> None:
|
|
262
|
+
self.problem, self.budget, self.seed = problem, budget, seed
|
|
263
|
+
m = len(problem.objectives)
|
|
264
|
+
if m == 2:
|
|
265
|
+
self.weights = [np.array([w, 1 - w]) for w in np.linspace(0, 1, 5)]
|
|
266
|
+
else:
|
|
267
|
+
self.weights = [np.array(w, dtype=float) / 2 for w in [(2, 0, 0), (0, 2, 0), (0, 0, 2), (1, 1, 0), (1, 0, 1), (0, 1, 1)]]
|
|
268
|
+
|
|
269
|
+
def run(self) -> tuple[list[dict[str, float]], int]:
|
|
270
|
+
seen: list[dict[str, float]] = []
|
|
271
|
+
evaluations = 0
|
|
272
|
+
for i, weights in enumerate(self.weights):
|
|
273
|
+
share = self.budget // len(self.weights) + (i < self.budget % len(self.weights))
|
|
274
|
+
|
|
275
|
+
def weighted(**params: Any) -> float:
|
|
276
|
+
values = self.problem.function(**params)
|
|
277
|
+
seen.append(values)
|
|
278
|
+
return float(weights @ self.problem.scaled(values))
|
|
279
|
+
|
|
280
|
+
if share <= BAYESIAN_MAX_EVALUATIONS:
|
|
281
|
+
optimizer: Any = BayesianOptimizer(self.problem.search_space, BayesianConfig(max_evaluations=share, seed=self.seed + i))
|
|
282
|
+
else:
|
|
283
|
+
optimizer = GeneticOptimizer(self.problem.search_space, GeneticOptimizerConfig(max_evaluations=share, seed=self.seed + i))
|
|
284
|
+
evaluations += optimizer.optimize(weighted).evaluations
|
|
285
|
+
return seen, evaluations
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
DEFAULT_MULTI_OPTIMIZERS: dict[str, MultiFactory] = {
|
|
289
|
+
"nsga2": nsga2,
|
|
290
|
+
"bayesian-parego": _bayesian("parego"),
|
|
291
|
+
"bayesian-models": _bayesian("models"),
|
|
292
|
+
"random": random_search,
|
|
293
|
+
"weighted-sums": weighted_sums,
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
# --- runs and summaries ------------------------------------------------------------
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
class MultiRun(BaseModel):
|
|
301
|
+
"""One optimizer on one problem with one seed and one budget."""
|
|
302
|
+
model_config = ConfigDict(frozen=True)
|
|
303
|
+
|
|
304
|
+
problem: str
|
|
305
|
+
optimizer: str
|
|
306
|
+
budget_per_dimension: int
|
|
307
|
+
seed: int
|
|
308
|
+
gap: float # the true front's scaled hypervolume less the run's front's; 0 is the true front
|
|
309
|
+
front_size: int
|
|
310
|
+
evaluations: int
|
|
311
|
+
front: list[list[float]] # the run's front, scaled, for attainment surfaces
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
class MultiSummary(BaseModel):
|
|
315
|
+
"""All runs of one optimizer on one problem with one budget."""
|
|
316
|
+
model_config = ConfigDict(frozen=True)
|
|
317
|
+
|
|
318
|
+
problem: str
|
|
319
|
+
optimizer: str
|
|
320
|
+
budget_per_dimension: int
|
|
321
|
+
runs: int
|
|
322
|
+
median_gap: float
|
|
323
|
+
lower_quartile: float
|
|
324
|
+
upper_quartile: float
|
|
325
|
+
attainment: list[list[float]] | None = None # for two objectives, the median attainment surface's corners, scaled
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def run_multi_benchmark(
|
|
329
|
+
problems: Iterable[MultiProblem],
|
|
330
|
+
optimizers: dict[str, MultiFactory] | None = None,
|
|
331
|
+
seeds: Iterable[int] = range(10),
|
|
332
|
+
budgets_per_dimension: Iterable[int] = BUDGETS,
|
|
333
|
+
jobs: int = 1,
|
|
334
|
+
) -> list[MultiRun]:
|
|
335
|
+
"""Run every optimizer on every problem with every seed and budget, as run_benchmark does for one objective."""
|
|
336
|
+
problems = list(problems)
|
|
337
|
+
optimizers = optimizers if optimizers is not None else DEFAULT_MULTI_OPTIMIZERS
|
|
338
|
+
seeds, budgets = list(seeds), list(budgets_per_dimension)
|
|
339
|
+
tasks = []
|
|
340
|
+
for index, problem in enumerate(problems):
|
|
341
|
+
for name, factory in optimizers.items():
|
|
342
|
+
for per_dimension in budgets:
|
|
343
|
+
try:
|
|
344
|
+
if seeds:
|
|
345
|
+
factory(problem, per_dimension * problem.dimensions, seeds[0])
|
|
346
|
+
except SkipBudget:
|
|
347
|
+
continue
|
|
348
|
+
tasks += [(index, name, per_dimension, seed) for seed in seeds]
|
|
349
|
+
global _shared
|
|
350
|
+
_shared = (problems, optimizers)
|
|
351
|
+
if jobs == 1 or "fork" not in multiprocessing.get_all_start_methods():
|
|
352
|
+
return [_task(task) for task in tasks]
|
|
353
|
+
with ProcessPoolExecutor(jobs, mp_context=multiprocessing.get_context("fork")) as pool:
|
|
354
|
+
return list(pool.map(_task, tasks))
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
_shared: tuple[list[MultiProblem], dict[str, MultiFactory]] = ([], {})
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def _task(task: tuple[int, str, int, int]) -> MultiRun:
|
|
361
|
+
index, name, per_dimension, seed = task
|
|
362
|
+
problems, optimizers = _shared
|
|
363
|
+
problem = problems[index]
|
|
364
|
+
optimizer = optimizers[name](problem, per_dimension * problem.dimensions, seed)
|
|
365
|
+
if isinstance(optimizer, WeightedSums):
|
|
366
|
+
seen, evaluations = optimizer.run()
|
|
367
|
+
else:
|
|
368
|
+
result = optimizer.optimize(problem.function)
|
|
369
|
+
seen, evaluations = [p.values for p in result.front], result.evaluations
|
|
370
|
+
vectors = np.array([problem.scaled(v) for v in seen]).reshape(-1, len(problem.objectives))
|
|
371
|
+
front = vectors[non_dominated(vectors)] if len(vectors) else vectors
|
|
372
|
+
volume = hypervolume(front, np.full(len(problem.objectives), REFERENCE))
|
|
373
|
+
return MultiRun(
|
|
374
|
+
problem=problem.name,
|
|
375
|
+
optimizer=name,
|
|
376
|
+
budget_per_dimension=per_dimension,
|
|
377
|
+
seed=seed,
|
|
378
|
+
gap=max(problem.true_hypervolume - volume, 0.0),
|
|
379
|
+
front_size=len(front),
|
|
380
|
+
evaluations=evaluations,
|
|
381
|
+
front=front.tolist(),
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def attainment_surface(fronts: list[np.ndarray], share: float = 0.5) -> list[list[float]]:
|
|
386
|
+
"""The corners of the surface that the given share of two-objective fronts attain, scaled values to minimize.
|
|
387
|
+
|
|
388
|
+
A point is attained by a front when some point of the front is at least as good on both
|
|
389
|
+
objectives. The median surface (share 0.5) is the usual picture of where a method's
|
|
390
|
+
fronts typically lie.
|
|
391
|
+
"""
|
|
392
|
+
xs = sorted({float(x) for front in fronts for x in front[:, 0]} if fronts else set())
|
|
393
|
+
needed = max(1, math.ceil(share * len(fronts)))
|
|
394
|
+
corners: list[list[float]] = []
|
|
395
|
+
for x in xs:
|
|
396
|
+
best = sorted(float(front[front[:, 0] <= x, 1].min()) if np.any(front[:, 0] <= x) else math.inf for front in fronts)
|
|
397
|
+
y = best[needed - 1]
|
|
398
|
+
if math.isfinite(y) and (not corners or y < corners[-1][1]):
|
|
399
|
+
corners.append([x, y])
|
|
400
|
+
return corners
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
def summarize_multi(runs: list[MultiRun]) -> list[MultiSummary]:
|
|
404
|
+
groups: dict[tuple[str, str, int], list[MultiRun]] = {}
|
|
405
|
+
for run in runs:
|
|
406
|
+
groups.setdefault((run.problem, run.optimizer, run.budget_per_dimension), []).append(run)
|
|
407
|
+
summaries = []
|
|
408
|
+
for (problem, optimizer, budget), group in groups.items():
|
|
409
|
+
gaps = sorted(run.gap for run in group)
|
|
410
|
+
quartiles = statistics.quantiles(gaps, n=4, method="inclusive") if len(gaps) > 1 else gaps * 3
|
|
411
|
+
fronts = [np.array(run.front).reshape(-1, len(run.front[0]) if run.front else 2) for run in group]
|
|
412
|
+
two = all(f.shape[1] == 2 for f in fronts)
|
|
413
|
+
summaries.append(
|
|
414
|
+
MultiSummary(
|
|
415
|
+
problem=problem,
|
|
416
|
+
optimizer=optimizer,
|
|
417
|
+
budget_per_dimension=budget,
|
|
418
|
+
runs=len(group),
|
|
419
|
+
median_gap=statistics.median(gaps),
|
|
420
|
+
lower_quartile=quartiles[0],
|
|
421
|
+
upper_quartile=quartiles[2],
|
|
422
|
+
attainment=attainment_surface(fronts) if two else None,
|
|
423
|
+
)
|
|
424
|
+
)
|
|
425
|
+
return summaries
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
def multi_markdown(summaries: list[MultiSummary]) -> str:
|
|
429
|
+
"""Median hypervolume gaps, by budget, with the lowest of each row in bold."""
|
|
430
|
+
optimizers = list(dict.fromkeys(s.optimizer for s in summaries))
|
|
431
|
+
problems = list(dict.fromkeys(s.problem for s in summaries))
|
|
432
|
+
lines = []
|
|
433
|
+
for budget in sorted({s.budget_per_dimension for s in summaries}):
|
|
434
|
+
by = {(s.problem, s.optimizer): s.median_gap for s in summaries if s.budget_per_dimension == budget}
|
|
435
|
+
lines += [f"## {budget} evaluations per dimension", "", "| Problem | " + " | ".join(optimizers) + " |", "| --- |" + " ---: |" * len(optimizers)]
|
|
436
|
+
for problem in problems:
|
|
437
|
+
row = [by.get((problem, o)) for o in optimizers]
|
|
438
|
+
known = [g for g in row if g is not None]
|
|
439
|
+
best = min(known) if known else None
|
|
440
|
+
cells = ["n/a" if g is None else (f"**{g:.3g}**" if g <= (best or 0) * (1 + 1e-9) + 1e-12 else f"{g:.3g}") for g in row]
|
|
441
|
+
lines.append(f"| {problem} | " + " | ".join(cells) + " |")
|
|
442
|
+
lines.append("")
|
|
443
|
+
lines.append(
|
|
444
|
+
f"Median hypervolume gap over the runs: the true front's hypervolume less the run's, with objectives scaled from 0 at "
|
|
445
|
+
f"the true front's ideal to 1 at its nadir and a reference point of {REFERENCE}. 0 is the true front; lower is better."
|
|
446
|
+
)
|
|
447
|
+
return "\n".join(lines) + "\n"
|
hone/config/__init__.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from .bayesian_config import BayesianConfig
|
|
2
2
|
from .cmaes_config import CMAESConfig
|
|
3
3
|
from .genetic_optimizer_config import GeneticOptimizerConfig, SelectionMethod
|
|
4
|
+
from .multi_objective_config import MultiObjectiveBayesianConfig, MultiObjectiveConfig, NSGA2Config
|
|
4
5
|
from .optimizer_config import OptimizerConfig
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
from typing import Any, Literal
|
|
2
|
+
|
|
3
|
+
from pydantic import Field, model_validator
|
|
4
|
+
|
|
5
|
+
from .genetic_optimizer_config import GENERATIONS_PER_BUDGET, MAX_POPULATION, MIN_POPULATION
|
|
6
|
+
|
|
7
|
+
from .optimizer_config import OptimizerConfig
|
|
8
|
+
|
|
9
|
+
type Direction = Literal["minimize", "maximize"]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class MultiObjectiveConfig(OptimizerConfig):
|
|
13
|
+
"""Settings for optimizing several objectives together.
|
|
14
|
+
|
|
15
|
+
objectives names each objective and whether to minimize or maximize it, such as
|
|
16
|
+
{"accuracy": "maximize", "cost": "minimize"}; the objective function returns a dict with
|
|
17
|
+
exactly those keys. It takes the place of maximize, and threshold and final_evaluations
|
|
18
|
+
don't apply.
|
|
19
|
+
"""
|
|
20
|
+
objectives: dict[str, Direction] = Field(min_length=2)
|
|
21
|
+
# A point every objective must be better than to count toward hypervolume, by objective. None:
|
|
22
|
+
# the worst value of each objective in the first generation, a tenth of its range further out.
|
|
23
|
+
reference: dict[str, float] | None = None
|
|
24
|
+
|
|
25
|
+
@model_validator(mode="after")
|
|
26
|
+
def _check_objectives(self):
|
|
27
|
+
if self.maximize:
|
|
28
|
+
raise ValueError("objectives says whether to minimize or maximize each objective, so maximize doesn't apply")
|
|
29
|
+
if self.threshold is not None:
|
|
30
|
+
raise ValueError("threshold doesn't apply to several objectives; stop a run with a callback instead")
|
|
31
|
+
if self.final_evaluations:
|
|
32
|
+
raise ValueError("final_evaluations doesn't apply to several objectives yet")
|
|
33
|
+
if self.reference is not None and set(self.reference) != set(self.objectives):
|
|
34
|
+
raise ValueError(f"reference needs a value for each objective: {', '.join(self.objectives)}")
|
|
35
|
+
return self
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class NSGA2Config(MultiObjectiveConfig):
|
|
39
|
+
"""Configuration for NSGA2, the multi-objective genetic optimizer.
|
|
40
|
+
|
|
41
|
+
Breeding works as in the genetic optimizer, with the same settings; selection and elitism
|
|
42
|
+
are NSGA-II's own. With max_evaluations set, population_size and generations default as
|
|
43
|
+
for the genetic optimizer.
|
|
44
|
+
"""
|
|
45
|
+
mutation_rate: float | None = Field(default=None, ge=0, le=1) # as in GeneticOptimizerConfig
|
|
46
|
+
mutation_scale: float = Field(default=0.1, gt=0)
|
|
47
|
+
adaptive_mutation: bool | None = None
|
|
48
|
+
crossover_rate: float = Field(default=0.8, ge=0, le=1)
|
|
49
|
+
blend_crossover: bool = True
|
|
50
|
+
blend_alpha: float = Field(default=0.1, ge=0)
|
|
51
|
+
niche_share: float | None = Field(default=None, ge=0, lt=1)
|
|
52
|
+
|
|
53
|
+
@model_validator(mode="before")
|
|
54
|
+
@classmethod
|
|
55
|
+
def _defaults_from_budget(cls, data: Any) -> Any:
|
|
56
|
+
if not isinstance(data, dict) or data.get("max_evaluations") is None:
|
|
57
|
+
return data
|
|
58
|
+
budget = data["max_evaluations"]
|
|
59
|
+
data = dict(data)
|
|
60
|
+
if data.get("population_size") is None:
|
|
61
|
+
data["population_size"] = min(max(budget // GENERATIONS_PER_BUDGET, MIN_POPULATION), MAX_POPULATION)
|
|
62
|
+
if data.get("generations") is None:
|
|
63
|
+
data["generations"] = budget
|
|
64
|
+
return data
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class MultiObjectiveBayesianConfig(MultiObjectiveConfig):
|
|
68
|
+
"""Configuration for multi-objective Bayesian optimization. Usually only the budget, max_evaluations, needs setting.
|
|
69
|
+
|
|
70
|
+
As with BayesianConfig, each generation is a batch of candidates chosen together, and with
|
|
71
|
+
max_evaluations set, generations defaults to the budget.
|
|
72
|
+
"""
|
|
73
|
+
population_size: int | None = Field(default=None, ge=1) # type: ignore[assignment] # candidates per batch; None: 1, or the number of workers
|
|
74
|
+
generations: int = Field(default=100, ge=1)
|
|
75
|
+
initial_samples: int | None = Field(default=None, ge=1) # spread-out candidates before the models are used; None: 2 × (variables + 1)
|
|
76
|
+
# How candidates are chosen. "parego": a random weighting of the scaled objectives per
|
|
77
|
+
# candidate, scored by one Gaussian process. "models": one Gaussian process per objective,
|
|
78
|
+
# with the weighting applied to their predictions.
|
|
79
|
+
method: Literal["parego", "models"] = "parego"
|
|
80
|
+
|
|
81
|
+
@model_validator(mode="before")
|
|
82
|
+
@classmethod
|
|
83
|
+
def _generations_from_budget(cls, data: Any) -> Any:
|
|
84
|
+
if isinstance(data, dict) and data.get("max_evaluations") is not None and data.get("generations") is None:
|
|
85
|
+
data = {**data, "generations": data["max_evaluations"]}
|
|
86
|
+
return data
|
|
@@ -60,3 +60,13 @@ class ObjectiveFunction(BaseModel):
|
|
|
60
60
|
)
|
|
61
61
|
if missing:
|
|
62
62
|
raise ValueError(f"{self.name}() requires parameters missing from the search space: {', '.join(missing)}")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class MultiObjectiveFunction(ObjectiveFunction):
|
|
66
|
+
"""An objective function returning a dict of values, one per objective, checked against the search space.
|
|
67
|
+
|
|
68
|
+
The dict itself is checked when it is told to the optimizer, in the main process.
|
|
69
|
+
"""
|
|
70
|
+
|
|
71
|
+
def __call__(self, params: dict[str, Any]) -> Any:
|
|
72
|
+
return self.function(**params)
|