hone-python 0.14.0__py3-none-any.whl → 0.15.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.
@@ -1,8 +1,11 @@
1
1
  """Benchmark problems and a runner for comparing optimizers. Run `python -m hone.benchmarks --help`."""
2
2
  from .conditional import conditional_problems
3
3
  from .constrained import constrained_problems
4
+ from .engineering import engineering_problems, engineering_table
4
5
  from .ml import ml_problems
5
6
  from .multi import MultiProblem, multi_problems, run_multi_benchmark, summarize_multi
7
+ from .multi_outputs import cantilever_outputs, noisy_multi_output_problems, osy, tnk
8
+ from .outputs import noisy_output_problems, output_problems
6
9
  from .problems import Problem, noisy_problems, quick_problems, standard_problems
7
10
  from .runner import (
8
11
  BUDGETS,
@@ -10,6 +13,7 @@ from .runner import (
10
13
  Run,
11
14
  SkipBudget,
12
15
  Summary,
16
+ batch_optimizers,
13
17
  load_summaries,
14
18
  regressions,
15
19
  run_benchmark,
@@ -4,22 +4,39 @@ import sys
4
4
  import time
5
5
  from collections.abc import Sequence
6
6
  from importlib.metadata import version
7
+ from pathlib import Path
7
8
 
8
- from . import ml
9
+ from . import cost, engineering, ml
9
10
  from .conditional import conditional_problems
10
11
  from .multi import multi_markdown, multi_problems, run_multi_benchmark, summarize_multi
11
12
  from .constrained import constrained_problems
13
+ from .outputs import noisy_output_problems, output_problems
12
14
  from .problems import noisy_problems, quick_problems, standard_problems
13
- from .runner import BUDGETS, load_summaries, regressions, run_benchmark, save_summaries, summarize, to_markdown
15
+ from .runner import (
16
+ BUDGETS,
17
+ DEFAULT_OPTIMIZERS,
18
+ batch_optimizers,
19
+ load_summaries,
20
+ regressions,
21
+ run_benchmark,
22
+ save_summaries,
23
+ summarize,
24
+ to_markdown,
25
+ )
14
26
 
15
27
 
16
28
  QUICK_BUDGETS = (10, 50, 200)
17
29
  NOISY_BUDGETS = (10, 40, 200)
30
+ OUTPUT_BATCH_SIZES = (4, 8) # Bayesian optimization's batch sizes run on the problems with constraints on outputs, besides 1
18
31
 
19
32
 
20
33
  def _join(numbers: list[int]) -> str:
21
34
  """[10, 20, 50] -> "10, 20 and 50"."""
22
- words = [f"{n:,}" for n in numbers]
35
+ return _join_words([f"{n:,}" for n in numbers])
36
+
37
+
38
+ def _join_words(words: list[str]) -> str:
39
+ """["a", "b", "c"] -> "a, b and c"."""
23
40
  return words[0] if len(words) == 1 else ", ".join(words[:-1]) + " and " + words[-1]
24
41
 
25
42
 
@@ -30,12 +47,34 @@ def main(argv: list[str] | None = None) -> int:
30
47
  suite.add_argument("--ml", action="store_true", help="run the machine learning tuning problems instead (needs scikit-learn)")
31
48
  suite.add_argument("--noisy", action="store_true", help="run the problems with noise added to every evaluation instead")
32
49
  suite.add_argument("--constrained", action="store_true", help="run the problems with constraints instead")
50
+ suite.add_argument(
51
+ "--outputs", action="store_true", help="run the problems with constraints on outputs instead, the noisy engineering designs included, with Bayesian optimization at batch sizes 1, 4 and 8"
52
+ )
33
53
  suite.add_argument(
34
54
  "--multi", action="store_true", help="run the problems with several objectives instead, scored by hypervolume"
35
55
  )
56
+ suite.add_argument(
57
+ "--cost",
58
+ action="store_true",
59
+ help="time Bayesian optimization's candidates against evaluations and limits on outputs instead, with one profiled run; "
60
+ "--baseline then checks the ceiling at 500 evaluations",
61
+ )
62
+ suite.add_argument(
63
+ "--cost-multi",
64
+ action="store_true",
65
+ help="time MultiObjectiveBayesian's candidates (parego and models, 0 and 8 limits) as --cost does, without the "
66
+ "single-objective table or the profile; --noise times the noisy problem; --baseline checks the ceiling at 500 evaluations",
67
+ )
36
68
  suite.add_argument(
37
69
  "--conditional", action="store_true", help="run the problems with conditional parameters, and their flat versions, instead"
38
70
  )
71
+ suite.add_argument(
72
+ "--engineering",
73
+ action="store_true",
74
+ help="run nothing: tabulate the engineering designs and g06, g07 and g10 against their published optima, "
75
+ "from saved summaries (see --summaries)",
76
+ )
77
+ parser.add_argument("--noise", action="store_true", help="with --cost-multi, also time the noisy problem (noise=True, which needs noise with limits), after the noise-free cells")
39
78
  parser.add_argument("--seeds", type=int, help="number of seeds per problem (default 10, or 5 with --quick)")
40
79
  parser.add_argument(
41
80
  "--budgets",
@@ -45,8 +84,23 @@ def main(argv: list[str] | None = None) -> int:
45
84
  f"{' '.join(map(str, QUICK_BUDGETS))} with --quick, {' '.join(map(str, ml.BUDGETS))} with --ml, "
46
85
  f"or {' '.join(map(str, NOISY_BUDGETS))} with --noisy)",
47
86
  )
87
+ parser.add_argument(
88
+ "--batch-sizes",
89
+ type=int,
90
+ nargs="+",
91
+ metavar="SIZE",
92
+ help="also run Bayesian optimization choosing this many candidates per batch, as bayesian-SIZE (default 4 8 with --outputs)",
93
+ )
48
94
  parser.add_argument("--jobs", type=int, default=1, help="share the runs among this many processes (Linux only)")
49
95
  parser.add_argument("--tolerance", type=float, default=1e-4, help="error at or below which a run counts as solved")
96
+ parser.add_argument(
97
+ "--summaries",
98
+ nargs="+",
99
+ metavar="FILE",
100
+ default=list(engineering.SOURCES),
101
+ help="with --engineering, the saved summaries to read, a later file's results replacing an earlier one's "
102
+ f"(default {' '.join(engineering.SOURCES)})",
103
+ )
50
104
  parser.add_argument("--markdown", help="write the results table to this file")
51
105
  parser.add_argument("--save", help="write the summaries as JSON to this file, for use as a baseline")
52
106
  parser.add_argument("--baseline", help="compare against summaries saved with --save; exit 1 on a regression")
@@ -54,6 +108,12 @@ def main(argv: list[str] | None = None) -> int:
54
108
 
55
109
  if args.multi:
56
110
  return _multi(args)
111
+ if args.cost:
112
+ return _cost(args)
113
+ if args.cost_multi:
114
+ return _cost_multi(args)
115
+ if args.engineering:
116
+ return _engineering(args)
57
117
  default_budgets: Sequence[int]
58
118
  if args.quick:
59
119
  problems, default_budgets = quick_problems(), QUICK_BUDGETS
@@ -63,15 +123,20 @@ def main(argv: list[str] | None = None) -> int:
63
123
  problems, default_budgets = noisy_problems(), NOISY_BUDGETS
64
124
  elif args.constrained:
65
125
  problems, default_budgets = constrained_problems(), BUDGETS
126
+ elif args.outputs:
127
+ problems, default_budgets = output_problems() + noisy_output_problems(), BUDGETS
66
128
  elif args.conditional:
67
129
  problems, default_budgets = conditional_problems(), BUDGETS
68
130
  else:
69
131
  problems, default_budgets = standard_problems(), BUDGETS
70
132
  seeds = args.seeds or (5 if args.quick else 10)
71
133
  budgets = sorted(set(args.budgets or default_budgets))
134
+ batch_sizes = args.batch_sizes if args.batch_sizes is not None else OUTPUT_BATCH_SIZES if args.outputs else ()
135
+ optimizers = {**DEFAULT_OPTIMIZERS, **batch_optimizers(batch_sizes)}
72
136
 
73
137
  start = time.perf_counter()
74
- summaries = summarize(run_benchmark(problems, seeds=range(seeds), budgets_per_dimension=budgets, jobs=args.jobs), args.tolerance)
138
+ runs = run_benchmark(problems, optimizers, seeds=range(seeds), budgets_per_dimension=budgets, jobs=args.jobs)
139
+ summaries = summarize(runs, args.tolerance)
75
140
  elapsed = time.perf_counter() - start
76
141
 
77
142
  table = to_markdown(summaries, args.tolerance)
@@ -117,5 +182,74 @@ def _multi(args: argparse.Namespace) -> int:
117
182
  return 0
118
183
 
119
184
 
185
+ def _engineering(args: argparse.Namespace) -> int:
186
+ """The engineering designs against their published optima, from summaries already saved."""
187
+ summaries = [s for path in args.summaries for s in load_summaries(path)]
188
+ table = engineering.engineering_table(summaries)
189
+ print(table)
190
+ if args.markdown:
191
+ sources = _join_words([f"`{Path(path).name}`" for path in args.summaries])
192
+ with open(args.markdown, "w") as f:
193
+ f.write(
194
+ "# Engineering designs against their published optima\n\n"
195
+ f"Generated by `python -m hone.benchmarks --engineering` from {sources}, which the constrained and "
196
+ f"outputs suites save; nothing is run.\n\n{table}"
197
+ )
198
+ return 0
199
+
200
+
201
+ def _cost(args: argparse.Namespace) -> int:
202
+ """The cost of a Bayesian candidate: the table, one profiled run, and the ceiling against a saved table."""
203
+ print("Timing Bayesian candidates, one cell at a time:")
204
+ cells = cost.measure_cost(progress=True)
205
+ print("Profiling one run on g07:")
206
+ table = cost.CostTable(
207
+ hone=version("hone-python"),
208
+ machine=cost.machine(),
209
+ dimensions=cost.DIMENSIONS,
210
+ candidates=cost.CANDIDATES,
211
+ seeds=cost.SEEDS,
212
+ cells=cells,
213
+ profile=cost.profile_run(),
214
+ )
215
+ text = cost.cost_markdown(table)
216
+ print(text)
217
+ if args.markdown:
218
+ with open(args.markdown, "w") as f:
219
+ f.write(f"# The cost of a Bayesian candidate\n\nGenerated by `python -m hone.benchmarks --cost`.\n\n{text}")
220
+ if args.save:
221
+ cost.save_cost(table, args.save)
222
+ if args.baseline:
223
+ found = cost.ceiling_breaches(cells, cost.load_cost(args.baseline).cells)
224
+ if found:
225
+ print(f"\nOver the ceiling of ×{cost.CEILING} at {cost.CEILING_AT} evaluations:", *found, sep="\n ")
226
+ return 1
227
+ print(f"\nWithin the ceiling of ×{cost.CEILING} at {cost.CEILING_AT} evaluations.")
228
+ return 0
229
+
230
+
231
+ def _cost_multi(args: argparse.Namespace) -> int:
232
+ """The cost of a MultiObjectiveBayesian candidate: the table, and the ceiling against a saved table."""
233
+ print("Timing multi-objective Bayesian candidates, one cell at a time:")
234
+ table = cost.CostTable(
235
+ hone=version("hone-python"), machine=cost.machine(), dimensions=cost.DIMENSIONS, candidates=cost.CANDIDATES, seeds=cost.SEEDS,
236
+ cells=[], multi=cost.measure_multi_cost(progress=True) + (cost.measure_multi_cost(noise=True, progress=True) if args.noise else []),
237
+ )
238
+ text = cost.cost_markdown(table)
239
+ print(text)
240
+ if args.markdown:
241
+ with open(args.markdown, "w") as f:
242
+ f.write(f"# The cost of a multi-objective Bayesian candidate\n\nGenerated by `python -m hone.benchmarks --cost-multi`.\n\n{text}")
243
+ if args.save:
244
+ cost.save_cost(table, args.save)
245
+ if args.baseline:
246
+ found = cost.multi_ceiling_breaches(table.multi, cost.load_cost(args.baseline).multi)
247
+ if found:
248
+ print(f"\nOver the ceiling of ×{cost.CEILING} at {cost.CEILING_AT} evaluations:", *found, sep="\n ")
249
+ return 1
250
+ print(f"\nWithin the ceiling of ×{cost.CEILING} at {cost.CEILING_AT} evaluations.")
251
+ return 0
252
+
253
+
120
254
  if __name__ == "__main__":
121
255
  sys.exit(main())
@@ -6,8 +6,11 @@
6
6
  - rosenbrock-disk: Rosenbrock's function inside the disk x² + y² ≤ 2, whose optimum (1, 1) sits
7
7
  on the disk's edge.
8
8
  - pressure-vessel, tension-spring and welded-beam: three standard engineering design problems,
9
- scored against the best designs published for them. The welded beam's stress formulas need
10
- square roots, so its constraints are functions; the others are expressions.
9
+ scored against the best designs published for them, which Cagnina, Esquivel and Coello Coello
10
+ (2008), "Solving engineering optimization problems with the simple constrained particle swarm
11
+ optimizer", Informatica 32(3):319-326, list as problems E02, E04 and E01. The welded beam's
12
+ stress formulas need square roots, so its constraints are functions; the others are
13
+ expressions.
11
14
 
12
15
  Each engineering problem also comes with its limits on outputs, as the -outputs version: the
13
16
  objective reports the volume, stresses and so on that the limits apply to, as a simulation
@@ -81,7 +84,8 @@ def pressure_vessel() -> Problem:
81
84
  """A cylindrical vessel with hemispherical heads, at least 1,296,000 cubic inches, of least cost.
82
85
 
83
86
  Shell and head thicknesses come in sixteenths of an inch, so they are integers here. The best
84
- published design costs 6059.714 (Ts = 13/16, Th = 7/16, R = 42.098, L = 176.637).
87
+ published design costs 6059.714 (Ts = 13/16, Th = 7/16, R = 42.098, L = 176.637)
88
+ (Cagnina et al., 2008, problem E02).
85
89
  """
86
90
  shell, head = RangeParameter("shell", 1, 99, integer=True), RangeParameter("head", 1, 99, integer=True)
87
91
  radius, length = RangeParameter("radius", 10, 200), RangeParameter("length", 10, 200)
@@ -104,7 +108,14 @@ def pressure_vessel() -> Problem:
104
108
  def tension_spring() -> Problem:
105
109
  """A coil spring of least weight under limits on deflection, shear stress, surge frequency and diameter.
106
110
 
107
- The best published design weighs 0.0126652 (d = 0.051690, D = 0.356750, N = 11.287126).
111
+ The best published design weighs 0.0126652 (d = 0.051689, D = 0.356720, N = 11.288832).
112
+ Cagnina et al., 2008, problem E04, give the design (0.051690, 0.356750, 11.287126) with a
113
+ weight of 0.012665. Its rounded values break the shear stress limit by about 2e-5 (the stress
114
+ is 1.00002 times the limit), which is why they weigh 0.0126651, a little less than the optimum.
115
+ Celik and Kutucu, "Solving the tension/compression spring design problem by an improved
116
+ firefly algorithm", CEUR Workshop Proceedings 2255, paper 2, give the optimum as 0.0126652327883
117
+ at (0.051689156131, 0.356720026419, 11.288831695483), which satisfies every limit here. They
118
+ cite no source for it.
108
119
  """
109
120
  d, coil, n = RangeParameter("wire", 0.05, 2.0), RangeParameter("coil", 0.25, 1.3), RangeParameter("turns", 2.0, 15.0)
110
121
 
@@ -144,6 +155,7 @@ def welded_beam() -> Problem:
144
155
  """A beam welded to a support, of least cost under limits on shear, bending, buckling and deflection.
145
156
 
146
157
  The best published design costs 1.724852 (h = 0.205730, l = 3.470489, t = 9.036624, b = 0.205730).
158
+ Cagnina et al., 2008, problem E01, give the same cost and design with b = 0.205729.
147
159
  """
148
160
  weld, length = RangeParameter("weld", 0.1, 2.0), RangeParameter("length", 0.1, 10.0)
149
161
  thickness, width = RangeParameter("thickness", 0.1, 10.0), RangeParameter("width", 0.1, 2.0)
@@ -265,8 +277,3 @@ def constrained_problems(dimensions: tuple[int, ...] = (2, 5, 10)) -> list[Probl
265
277
  tension_spring_outputs(),
266
278
  welded_beam_outputs(),
267
279
  ]
268
-
269
-
270
- def output_problems() -> list[Problem]:
271
- """The engineering designs with their limits on outputs only, for a quick comparison with the parameter versions."""
272
- return [pressure_vessel_outputs(), tension_spring_outputs(), welded_beam_outputs()]