-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolar_python_tools.py
More file actions
538 lines (445 loc) · 16.6 KB
/
Copy pathsolar_python_tools.py
File metadata and controls
538 lines (445 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from __future__ import annotations
from contextlib import contextmanager
import csv
import inspect
import json
import math
import os
from pathlib import Path
import subprocess
import tempfile
import time
import numpy as np
from optiprofiler.opclasses import Problem
CURRENT_DIR = Path(__file__).resolve().parent
RUNTIME_DIR = CURRENT_DIR / "runtime" / "solar"
METADATA_PATH = RUNTIME_DIR / "metadata" / "problems.json"
PROBINFO_PATH = RUNTIME_DIR / "metadata" / "probinfo.csv"
class SolarExecutionError(RuntimeError):
pass
def solar_python_collect_info():
"""
Return the SOLAR problem information table.
This is the Python-specific implementation used by the public entry point
``solar_collect_info``. Users should normally call ``solar_collect_info``,
whose name matches the OptiProfiler problem library name ``solar``.
Returns
-------
list of dict
Rows with the same compact fields used by OptiProfiler custom problem
libraries: ``name``, ``ptype``, ``dim``, ``mb``, ``mlcon``,
``mnlcon``, and ``mcon``.
"""
with PROBINFO_PATH.open(newline="", encoding="utf-8") as handle:
return list(csv.DictReader(handle))
def solar_python_select(options=None):
"""
Select SOLAR problems satisfying OptiProfiler-style criteria.
This is the Python-specific implementation used by the public entry point
``solar_select``. Users should normally call ``solar_select``, whose name
matches the OptiProfiler problem library name ``solar``.
Parameters
----------
options : dict, optional
Selection criteria with the same keys accepted by ``solar_select``.
Returns
-------
list of str
Names of enabled scalar SOLAR problems satisfying the criteria.
"""
if options is None:
options = {}
options = dict(options)
defaults = {
"ptype": "ubln",
"mindim": 1,
"maxdim": math.inf,
"minb": 0,
"maxb": math.inf,
"minlcon": 0,
"maxlcon": math.inf,
"minnlcon": 0,
"maxnlcon": math.inf,
"mincon": 0,
"maxcon": math.inf,
"excludelist": [],
}
for key, value in defaults.items():
options.setdefault(key, value)
enabled_names = {
problem["name"]
for problem in _load_metadata()
if problem.get("enabled", False)
}
selected = []
for row in solar_python_collect_info():
if row["name"] not in enabled_names:
continue
if row["name"] in options["excludelist"]:
continue
if row["ptype"] not in options["ptype"]:
continue
dim = int(row["dim"])
mb = int(row["mb"])
mlcon = int(row["mlcon"])
mnlcon = int(row["mnlcon"])
mcon = int(row["mcon"])
if not (options["mindim"] <= dim <= options["maxdim"]):
continue
if not (options["minb"] <= mb <= options["maxb"]):
continue
if not (options["minlcon"] <= mlcon <= options["maxlcon"]):
continue
if not (options["minnlcon"] <= mnlcon <= options["maxnlcon"]):
continue
if not (options["mincon"] <= mcon <= options["maxcon"]):
continue
selected.append(row["name"])
return selected
def solar_python_load(problem_name):
"""
Convert a SOLAR problem name to an OptiProfiler ``Problem`` instance.
This is the Python-specific implementation used by the public entry point
``solar_load``. Users should normally call ``solar_load``, whose name
matches the OptiProfiler problem library name ``solar``.
Parameters
----------
problem_name : str
Name of an enabled scalar SOLAR problem.
Returns
-------
optiprofiler.Problem
A ``Problem`` instance whose objective is available through ``fun`` and
whose nonlinear inequality constraints, when present, are available
through ``cub``.
"""
metadata = _problem_by_name(problem_name)
if not metadata.get("enabled", False):
raise ValueError(f"SOLAR problem is disabled: {problem_name}")
executable = _ensure_executable()
state = _SolarProblemState(metadata, executable)
cub = state.cub if metadata["m_constraints"] > 0 else None
return Problem(
state.fun,
np.asarray(metadata["x0"], dtype=float),
name=metadata["name"],
xl=_bounds(metadata["xl"], lower=True),
xu=_bounds(metadata["xu"], lower=False),
cub=cub,
)
class _SolarProblemState:
def __init__(self, metadata, executable):
self.metadata = metadata
self.executable = executable
self._last_x = None
self._last_eval = None
def _eval(self, x):
solar_x = _prepare_solar_input(self.metadata, x)
if self._last_x is not None and np.array_equal(solar_x, self._last_x):
return self._last_eval
result = _run_solar(
self.executable,
self.metadata["pb_id"],
solar_x,
self.metadata["m_objectives"],
self.metadata["m_constraints"],
)
self._last_x = solar_x.copy()
self._last_eval = result
return result
def fun(self, x):
return float(self._eval(x)["objectives"][0])
def cub(self, x):
if _is_problem_constructor_probe():
return np.full(self.metadata["m_constraints"], np.nan)
return np.asarray(self._eval(x)["constraints"], dtype=float)
def _load_metadata():
with METADATA_PATH.open(encoding="utf-8") as handle:
return json.load(handle)
def _problem_by_name(problem_name):
for problem in _load_metadata():
if problem["name"] == problem_name:
return problem
raise KeyError(f"Unknown SOLAR problem: {problem_name}")
def _row(problem):
mb = int(problem.mb)
return {
"name": problem.name,
"ptype": problem.ptype,
"dim": int(problem.n),
"mb": mb,
"mlcon": int(problem.mlcon),
"mnlcon": int(problem.mnlcon),
"mcon": int(problem.mcon),
}
def _bounds(values, *, lower):
fallback = -np.inf if lower else np.inf
return np.asarray([fallback if value is None else value for value in values], dtype=float)
def _prepare_solar_input(metadata, x):
x = np.asarray(x, dtype=float).reshape(-1).copy()
if x.size != int(metadata["n"]):
raise SolarExecutionError(
f"SOLAR input has dimension {x.size}, expected {metadata['n']}"
)
input_type = metadata.get("input_type", [])
lower_bounds = metadata.get("xl", [None] * x.size)
upper_bounds = metadata.get("xu", [None] * x.size)
for i, variable_type in enumerate(input_type):
if variable_type != "I" or not np.isfinite(x[i]):
continue
value = math.floor(float(x[i]) + 0.5)
lower = lower_bounds[i]
upper = upper_bounds[i]
if lower is not None and math.isfinite(float(lower)):
value = max(value, math.ceil(float(lower)))
if upper is not None and math.isfinite(float(upper)):
value = min(value, math.floor(float(upper)))
x[i] = value
return x
def _ensure_executable():
configured = os.environ.get("SOLAR_EXECUTABLE")
executable = Path(configured) if configured else _default_executable()
if executable.exists():
return executable
with _build_lock():
if executable.exists():
return executable
executable.parent.mkdir(parents=True, exist_ok=True)
completed = subprocess.run(
_make_command(),
check=False,
capture_output=True,
text=True,
)
if completed.returncode != 0:
raise SolarExecutionError(
"Failed to build SOLAR executable: "
f"{completed.stdout}{completed.stderr}"
)
if not executable.exists():
raise SolarExecutionError(f"SOLAR executable was not built: {executable}")
return executable
def _default_executable():
suffix = ".exe" if os.name == "nt" else ""
return RUNTIME_DIR / "bin" / f"solar{suffix}"
def _make_command():
command = ["make", "-C", str(RUNTIME_DIR / "src")]
if os.name == "nt":
command.append("EXEEXT=.exe")
command.append("LIBS=-lm")
return command
@contextmanager
def _build_lock(timeout_sec=600.0):
lock_path = RUNTIME_DIR / ".build.lock.d"
RUNTIME_DIR.mkdir(parents=True, exist_ok=True)
deadline = time.monotonic() + timeout_sec
acquired = False
while True:
try:
lock_path.mkdir()
acquired = True
break
except FileExistsError as exc:
if time.monotonic() >= deadline:
raise SolarExecutionError(
"Timed out waiting for another process to build SOLAR"
) from exc
time.sleep(0.1)
try:
yield
finally:
if acquired:
try:
lock_path.rmdir()
except OSError:
pass
def _run_solar(executable, problem_id, x, n_objectives, n_constraints, timeout_sec=300.0):
with tempfile.TemporaryDirectory(prefix="solar-python-") as tmp:
input_path = Path(tmp) / "x.txt"
input_path.write_text(_format_point(x), encoding="utf-8")
cmd = [
str(executable),
str(problem_id),
str(input_path),
"-seed=0",
"-fid=1.0",
"-rep=1",
]
started = time.perf_counter()
completed = subprocess.run(
cmd,
check=False,
capture_output=True,
text=True,
timeout=timeout_sec,
)
elapsed = time.perf_counter() - started
expected = n_objectives + n_constraints
try:
values = _parse_numeric_output(completed.stdout)
except SolarExecutionError as exc:
if completed.returncode != 0:
raise SolarExecutionError(
f"SOLAR failed with return code {completed.returncode}: "
f"{completed.stderr.strip()}"
) from exc
raise
if len(values) != expected:
if completed.returncode != 0:
raise SolarExecutionError(
f"SOLAR failed with return code {completed.returncode} and "
f"returned {len(values)} numeric values, expected {expected}: "
f"{completed.stderr.strip()}"
)
raise SolarExecutionError(
f"SOLAR returned {len(values)} numeric values, expected {expected}"
)
return {
"objectives": tuple(values[:n_objectives]),
"constraints": tuple(values[n_objectives:]),
"elapsed_sec": elapsed,
"raw_stdout": completed.stdout,
}
def _parse_numeric_output(stdout):
for line in reversed(stdout.splitlines()):
line = line.strip()
if not line:
continue
try:
return [float(token) for token in line.split()]
except ValueError:
continue
raise SolarExecutionError(f"SOLAR output could not be parsed: {stdout!r}")
def _format_point(x):
return " ".join(f"{float(value):.17g}" for value in x) + "\n"
def _is_problem_constructor_probe():
for frame in inspect.stack(context=0):
if frame.function == "__init__" and frame.filename.endswith("opclasses.py"):
return True
return False
def solar_collect_info():
"""
Return the SOLAR problem information table used by `solar_select`.
This is the public collect-info entry point for the OptiProfiler problem
library named ``solar``. The returned rows are read from
``runtime/solar/metadata/probinfo.csv``. This file is generated by
``scripts/collect_info.py`` by loading each enabled SOLAR problem through
``solar_load`` and recording the resulting OptiProfiler ``Problem`` fields.
Returns
-------
list of dict
Problem information rows with keys ``name``, ``ptype``, ``dim``,
``mb``, ``mlcon``, ``mnlcon``, and ``mcon``.
See Also
--------
solar_select : Select SOLAR problems using the generated table.
solar_load : Load one SOLAR problem as an OptiProfiler ``Problem``.
Examples
--------
.. code-block:: python
from solar_tools import solar_collect_info
rows = solar_collect_info()
print(rows[0]["name"])
"""
return solar_python_collect_info()
def solar_select(options=None):
"""
Select SOLAR problems satisfying OptiProfiler-style criteria.
SOLAR is a black-box solar-plant simulation benchmark. More details about
the upstream benchmark can be found at
``https://github.com/bbopt/solar``.
Parameters
----------
options : dict, optional
Selection criteria. Supported keys:
- ``ptype`` : str
Problem types to select. Use any combination of ``'u'``
(unconstrained), ``'b'`` (bound constrained), ``'l'`` (linearly
constrained), and ``'n'`` (nonlinearly constrained). Default is
``'ubln'``.
- ``mindim`` : int
Minimum dimension. Default is ``1``.
- ``maxdim`` : int or float
Maximum dimension. Default is ``math.inf``.
- ``minb`` : int
Minimum number of bound constraints. Default is ``0``.
- ``maxb`` : int or float
Maximum number of bound constraints. Default is ``math.inf``.
- ``minlcon`` : int
Minimum number of linear constraints. Default is ``0``.
- ``maxlcon`` : int or float
Maximum number of linear constraints. Default is ``math.inf``.
- ``minnlcon`` : int
Minimum number of nonlinear constraints. Default is ``0``.
- ``maxnlcon`` : int or float
Maximum number of nonlinear constraints. Default is ``math.inf``.
- ``mincon`` : int
Minimum total number of linear and nonlinear constraints. Default
is ``0``.
- ``maxcon`` : int or float
Maximum total number of linear and nonlinear constraints. Default
is ``math.inf``.
- ``excludelist`` : list of str
Problem names to exclude. Default is ``[]``.
Returns
-------
list of str
Names of enabled scalar SOLAR problems satisfying the criteria.
Notes
-----
SOLAR 8 and 9 are multiobjective and are not returned by this first scalar
OptiProfiler integration. SOLAR 11 is disabled because the current upstream
snapshot returns an empty output at the documented initial point.
Some SOLAR instances contain integer or categorical variables. Before
calling the SOLAR executable, this wrapper rounds every ``I`` coordinate to
the nearest integer and clips it to the recorded integer bounds.
See Also
--------
solar_load : Load one selected SOLAR problem.
solar_collect_info : Return the problem information table used here.
Examples
--------
.. code-block:: python
from solar_tools import solar_select
names = solar_select({"ptype": "n", "maxdim": 20})
"""
return solar_python_select(options)
def solar_load(problem_name):
"""
Convert a SOLAR problem name to an OptiProfiler ``Problem`` instance.
Parameters
----------
problem_name : str
Name of an enabled scalar SOLAR problem. Use ``solar_select`` to obtain
names satisfying dimension and constraint criteria.
Returns
-------
optiprofiler.Problem
A ``Problem`` instance for the named SOLAR problem. The instance
provides the objective through ``fun`` and, when present, nonlinear
inequality constraints through ``cub``. SOLAR does not provide
derivatives in this wrapper.
Notes
-----
The SOLAR C++ executable is built on first use if it is missing. This first
build can take noticeably longer than an ordinary load call.
SOLAR returns objective and constraint values from one executable call. The
wrapper may cache that raw executable result inside one loaded problem
object, but it does not call OptiProfiler-visible ``cub`` from ``fun`` or
``fun`` from ``cub``. This preserves OptiProfiler's separate evaluation
histories.
Some SOLAR instances contain integer or categorical variables. The wrapper
rounds every ``I`` coordinate before calling the SOLAR executable, as
described in the repository README.
See Also
--------
solar_select : Select SOLAR problem names by criteria.
solar_collect_info : Return the generated SOLAR problem information table.
Examples
--------
.. code-block:: python
from solar_tools import solar_load
problem = solar_load("SOLAR6_MINCOST_TS")
print(problem.fun(problem.x0))
"""
return solar_python_load(problem_name)