Usage for Python#
OptiProfiler provides a benchmark() function. This is the main entry point to the package. It benchmarks given solvers on the selected test suite.
We provide below simple examples on how to use OptiProfiler in Python. For more details on the signature of the benchmark() function, please refer to the Python API documentation.
Examples#
Example 1: first example to try out#
Let us first try to benchmark two callable optimization solvers solver1 and solver2 on the default test suite.
(Note that each solver must accept signatures mentioned in the Cautions part of the benchmark() function according to the type of problems you want to solve.)
To do this, run:
from optiprofiler import benchmark
scores = benchmark([solver1, solver2])
This will benchmark the two solvers under the default test setting, which means 'plain' feature (see Feature) and unconstrained problems from the default problem library whose dimension is smaller or equal to 2. It will also return the scores of the two solvers based on the profiles.
There will be a new folder named out in the current working directory, which contains a subfolder named plain_<timestamp> with all the detailed results.
Figure 1: Screenshot of the subfolder containing detailed results of the benchmarking run.#
Additionally, a PDF file named summary_<stamp>.pdf is generated in the experiment directory, summarizing all the performance profiles and data profiles.
Figure 2: Screenshot of the summary PDF file summarizing all the performance profiles and data profiles.#
The subfolder test_log contains diagnostic files for the experiment. In
particular, test_log/report.txt records selected problem names, timing
information, and special cases detected while building the profiles: problems
where merit_init = phi(x_0) = inf (valid evaluations pass by convention,
but undefined objective, constraint or merit values do not), solver runs that
terminated abnormally, and solver outputs that
were replaced by the initial point as an output-based penalty. A solver that
raised an error or returned nothing has no output, so such a run never passes
the output-based test, whatever the target; its history-based results stand, and
a solver that returns the initial point itself is not such a run. The file
test_log/log.txt contains the messages printed during the run.
If history plots are enabled, each problem library has a subfolder under
history_plots. The top-level PROBLEM.pdf files keep the combined view
with both raw and cumulative-minimum histories. The raw and cummin
subfolders contain the corresponding single-view PDFs. The merged
*_history_plots_summary.pdf file uses the top-level combined PDFs.
History plots with extreme values#
History plots use a display copy of the recorded values. In both Python
and MATLAB, finite values outside [-1e100, 1e100] are clipped to that
interval before calculating the displayed means and error bands. This
large plotting limit leaves room for squared deviations, translations and
axis margins without floating-point overflow. It is not an assumption that
all valid objective values lie in that interval.
Clipping diagnostics remain available in the experiment report and structured plot data, but are not drawn inside history panels. NaN and Inf entries are counted separately and displayed above the finite range of the corresponding run (including its finite initial value). If that run has no finite history or initial value, their display placeholder is 1. A constant finite range gets a small positive gap so its nonfinite placeholders remain distinguishable. These placeholders are not successful function evaluations.
The clipping occurs before both raw-view and cumulative-minimum-view statistics. The term “raw” in a plot filename denotes the non-cumulative view, not an unmodified serialization of the experiment. Original saved histories, oracle evaluations, output points, merit values and profile scores are unchanged. For numerical analysis of extreme values, use the saved data rather than recovering values from a capped plot.
Positive tiny values are not replaced by a fixed display floor. Nonpositive
histories are shifted when a logarithmic view is needed, and the shift is
shown in the axis label. Logarithmic limits and extreme-range ticks are kept
within representable floating-point values, so a finite history cannot be
silently replaced by a default [1, 10] viewing range.
For errorbar_type='meanstd', the two interfaces retain their established
standard-deviation conventions across the N runs: Python divides the
sum of squared deviations by N (population normalization), while MATLAB
divides by N-1 when N > 1 (sample normalization). With one run,
both use zero standard deviation. The same convention is used to select the
logarithmic shift and to draw the band within each interface. Therefore,
unequal-run meanstd bands, and sometimes their displayed shifts, are
not expected to match exactly between Python and MATLAB. This compatibility
choice does not change either interface’s saved values or profile scores.
Example 2: one step further by adding options#
You can also add options to the benchmark function. For example, if you want to benchmark three solvers solver1, solver2, and solver3 on the test suite with the 'noisy' feature and all the unconstrained and bound-constrained problems with dimension between 6 and 10 from the default problem set, you can run:
from optiprofiler import benchmark
scores = benchmark(
[solver1, solver2, solver3],
ptype='ub',
mindim=6,
maxdim=10,
feature_name='noisy',
)
This will create the corresponding folders out/noisy_<timestamp> and files as in Example 1. More details on the options can be found in the benchmark() function documentation.
For the deterministic noisy variant from Moré and Wild’s benchmarking model,
set noise_mode='deterministic'. If n_runs is not provided, OptiProfiler
uses one run for this deterministic feature unless solver_isrand marks at
least one solver as randomized, in which case OptiProfiler uses five runs as
usual.
scores = benchmark(
[solver1, solver2, solver3],
feature_name='noisy',
noise_mode='deterministic',
noise_map='chebyshev',
)
By default, n_jobs is set conservatively to about half of the available
workers instead of all workers. For the most reproducible timing experiments,
set n_jobs explicitly, for example n_jobs=1 for sequential runs.
Composing features#
Several features can be applied in order by joining their names with +.
The first name is applied to the original problem first: 'noisy+truncated'
adds noise to every objective value and then truncates the noisy value to
significant_digits digits, whereas 'truncated+noisy' truncates first and
adds noise afterwards. Any number of stages is allowed, names may repeat, and
'plain' is an identity that can appear anywhere.
scores = benchmark(
[solver1, solver2],
feature_name='perturbed_x0+noisy+truncated',
perturbation_level=1e-2,
noise_level=1e-4,
significant_digits=4,
)
Order matters for features that move the query point as well. With
feature_name='quantized+linearly_transformed+quantized' a solver point x
is first snapped to the mesh by the last stage, then mapped through the linear
transformation, and snapped again by the first stage before the original
objective is evaluated. For this chain, an objective query reads the original
objective once at snap(A @ snap(x)) for the value the solver observes and
once more for the scoring reference recorded in the history; each constraint
channel behaves the same way. That count is a property of this chain of lazy
point maps and linear transport, not a general promise. The general rule is
only that a composition performs no unused or exponentially repeated reads:
each stage reads its predecessor exactly as its operation requires, so chains
of point maps stay linear in length. Additional reads of the original problem
are legitimate wherever a stage asks for them: a custom callback that
probes its predecessor, the unrelaxable_constraints gate reading the
predecessor’s constraints during an objective query, and the reference reads
for the initial point, the histories and the final scoring.
The rules are:
Every supplied feature option is passed to every stage that accepts it, and each stage validates it with its own rules.
feature_name='noisy+perturbed_x0'therefore uses Gaussian noise and a spherical perturbation by default,distribution='gaussian'configures both stages, anddistribution='uniform'is rejected with an error naming theperturbed_x0stage. Options accepted by no stage are rejected. Repeated stages share the supplied options; configuring two occurrences of the same feature differently is not expressible through the flat option list. Option names are case-insensitive, and two spellings of one name (noise_levelandNOISE_LEVEL) in one call or one stage entry are rejected as a duplicate: the configuration would be ambiguous.n_runsis an experiment option, never a feature option:benchmark(..., n_runs=N)sets it, the experiment plan records it once, and no stage carries a run count (Feature(..., n_runs=N)is an error naming the benchmark option); a boolean is not a count and is rejected. Unless given explicitly, it is 5 whensolver_israndmarks a randomized solver (not in a load) and otherwise the largest established default of the effective stages. Those defaults are the ones of the single features: five forperturbed_x0,noisy(one withnoise_mode='deterministic'),permuted,linearly_transformed(also withrotated=False, although that variant is deterministic),random_nanandtruncatedwithperturbed_trailing_digits=True; one forplain,truncated,unrelaxable_constraints,nonquantifiable_constraints,quantizedandcustom(althoughcustomcounts as stochastic). So'noisy+truncated'uses five runs and'truncated+quantized'uses one. The composition counts as stochastic when any stage is stochastic.Value changes (
noisy,truncated,random_nan,nonquantifiable_constraints) affect what solvers observe, never the scoring reference recorded in the histories.permutedandlinearly_transformedtransport both.perturbed_x0only moves the initial point.quantizedwithground_truth=Falsesnaps observations only; withground_truth=Truethe inherited reference objective and nonlinear constraints are read at the snapped point as well, while bounds and linear constraints are always checked at the unsnapped point.Problem.referenceis an optionalProblemReferenceor record withfun,maxcv,kindandsource. The two kinds arefeasible(zero violation) andmerit(possibly nonzero violation; the user chooses the intended merit function). Optionalx_refis source metadata in original problem coordinates, not a point in featured coordinates. It is never eagerly transported or evaluated for metadata. Observation-only built-ins,perturbed_x0,permutedandlinearly_transformedretain reference values, including nonzero violation.customalways falls back to the default target in this release.quantized(ground_truth=True)also falls back, even withx_ref;ground_truth=Falseretains the reference. In a composition, an invalidated reference cannot be restored by later stages. The first reason is retained in the archive and report asreference_fallback_reasons.A retained reference supplies
merit_fun(fun, maxcv, maxcv_init)for each run. Missing references keep the V1 solver-history/initial/plain minimum. No reference changes the recorded observations or clamps measured merits. Save/load retains raw facts and recomputes targets with the current merit function. Earlier kinds and known mapping tokens remain readable, but new records need no mapping token. See Reference targets.Use Native plot styling to customize native figure fonts, ticks, legends, lines and layout without changing numerical results.
unrelaxable_constraintsmakes the objective infinite where the constraints of the problem it wraps are violated, as that problem observes them: after'noisy+unrelaxable_constraints'the gate reads noisy constraint samples, after'unrelaxable_constraints+noisy'it reads the original constraints. After a rotation, former bounds are linear constraints of the wrapped problem and belong to the linear category. Violations that contain NaN never close the gate, as for the single feature.Custom callbacks of a
'custom'stage receive the problem produced by the preceding stages, soproblem.fun(x)inside a callback is a genuine query of that problem. Each call is a separately served query: a predecessor whose observations are randomized per query (such asnoisyorrandom_nan) draws its own sample for it, while randomness fixed when the problem is built (perturbed_x0,permuted,linearly_transformed) and a deterministic predecessor return the same value again. Callback outputs are validated at the custom stage (objective values follow theProblem.funscalar policy and are recorded as NaN with a logged warning when they are not real scalars; constraint outputs must be real one-dimensional arrays of the predecessor’s size, otherwise an error names the stage and callback).A name whose only effective stage is a single feature, such as
'plain+noisy', behaves exactly like that feature, including its random streams and its output folder name. A genuine composition seeds every channel of every stage separately from the run seed (policyseedsequence-v2): the seed is the first 32-bit word ofnumpy.random.SeedSequence(run_seed, spawn_key=(code, occurrence, tag))with a frozen numeric code per feature, the occurrence index among stages of the same name, and the tagsfun=0,cub=1,ceq=2andconstruction=3(draws made when the problem is built). Inserting'plain'therefore does not change the derivation identity of any other stage, and distinct identities remove the structural alias that omitting the channel tag would cause between the objective and constraint channels. This is not an independence proof of the generator, and 32-bit seeds can still coincide. Archives record the policy identifier; archives written under an earlier policy keep their own identifier when loaded.The output folder name joins the stamps of the stages with
__and is shortened with a digest for long chains. The archive and the structured report record the declared specification, the effective stages with their local options and the seed policy, and separately the experiment plan (see Structured feature specification for the payload). Derivatives of a composed problem are not provided.
Structured feature specification#
The feature_name shorthand broadcasts every supplied option to all stages
that accept it, so two occurrences of the same feature, or a shared key such
as distribution for noisy and perturbed_x0, cannot be configured
differently. The feature option gives each stage its own options. It is
one stage entry {'name': ..., 'options': {...}} or an ordered list (or
tuple) of entries; a bare name is a stage with default options, options
may be omitted, and 'plain' entries are identities.
scores, profile_scores, curves = benchmark(
[solver1, solver2],
n_runs=3,
feature=[
{'name': 'noisy', 'options': {'distribution': 'uniform', 'noise_level': 1e-2}},
{'name': 'perturbed_x0', 'options': {'distribution': 'gaussian'}},
{'name': 'noisy', 'options': {'noise_level': 1e-4, 'noise_type': 'absolute'}},
],
)
# The same experiment as feature_name='noisy', noise_level=1e-2:
scores, _, _ = benchmark([solver1, solver2], feature={'name': 'noisy', 'options': {'noise_level': 1e-2}})
The rules are:
Option ownership is explicit.
n_runsbelongs to the experiment and is given at the top level; inside an entry it is rejected. Every other option belongs to the stage named in its entry and is validated with that stage’s rules, so{'name': 'perturbed_x0', 'options': {'noise_level': 1e-2}}is an error naming the entry and the stage. Profile options such asseedormax_eval_factorare never stage options.One route per call.
featureandfeature_namecannot both be given (an explicitfeature_name='plain'counts as given),feature=Noneand an empty specification are errors, a string belongs tofeature_name, and withfeatureevery flat stage option is rejected: there is no override hierarchy. These errors are raised before any output directory is created; a requestedreport_pathstill records the failure.Every entry,
'plain'ones included, is validated before'plain'entries are removed, so an invalid option on an identity stage is never dropped silently. Names are stripped and lowercased, and stage names are atomic ({'name': 'noisy+truncated'}is an error). A specification with one effective stage runs the established single-feature path, with its seeds and folder name.Equal settings give equal numbers.
feature=[{'name': 'noisy', 'options': {'noise_level': 1e-2}}, 'truncated']andfeature_name='noisy+truncated', noise_level=1e-2produce the same histories, outputs, folder stamp and scores for the same seed. Stage identities stay name plus occurrence, so differently configured repeats keep their seeds and differ only by the configured amounts.featurecannot be combined withload. A specification means transformations to execute, and loading keeps the archived pipeline of the saved experiment. Experiments created withfeatureload like any other, with their archived pipeline retained;feature_namekeeps labelling a load as before.
Feature objects. benchmark(feature=...) also accepts an already built
Feature, and both routes build one. A Feature is an
immutable pipeline specification: feature.stages is the tuple of effective
stage records, each with name, occurrence, identity
('noisy#0'), code (the frozen seed code), position and options
(a read-only view of the validated stage-local options after defaults:
arrays come back as isolated read-only copies and lists/tuples as fresh
copies, while callables and other opaque values keep their identity);
feature.name is the effective name ('plain' for the identity, which
has no stage), feature.declared the declaration as given (route and entries
before defaults, with the same ownership rule) and feature.is_stochastic
whether any stage draws random numbers. Ownership of option values is
explicit: exact built-in data containers (list, tuple, dict) and
NumPy arrays are copied when the specification is built and the views return
isolated copies, so changing such a value afterwards, or what a view returned,
does not change the specification. Opaque values are kept by identity and are
not copied: callables, container subclasses, other objects and the elements
of object-dtype arrays; the specification never mutates them, but their own
state remains yours to keep unchanged. No copy, iteration or reduction hook
of your objects is ever invoked. The declaration route ('feature_name' for the shorthand string,
'feature' for structured entries) is a property of the specification: a
Feature declared by shorthand and passed as benchmark(feature=obj)
keeps its declaration and its receipt. The provenance records that route as
declaration_route and, separately, the benchmark keyword that carried the
specification as route. A specification imported from a historical object
keeps the declaration that object recorded (plain entries included); one
imported from an object that recorded no declaration has declaration route
None, no declared entries and no declared name. In both cases its
effective stages are complete. Each trial builds fresh
runtime state from the records, so reusing
one Feature object across benchmarks gives the same numbers as building it
again. Pickling a Feature writes its versioned native form: the effective
stages with every validated local option explicit (so the defaults of the
version that loads it cannot change the numbers) and, separately, the
declaration as provenance; loading validates the effective options again,
callables included as the native objects, and rejects a native form version
it does not know. Objects pickled by OptiProfiler 1.x are read only through
the trusted decoder described below; pickles of other layouts or from other
versions are not a supported input. Feature.options
and the modifier_* methods remain as deprecated one-stage conveniences
(DeprecationWarning; an error on a composition) and are used by nothing
in the engine, the archives or the reports.
Provenance and replay. The archive entry feature_pipeline (payload
feature_pipeline-v3) has two blocks. The feature block records the
benchmark keyword that carried the specification (route), the declaration
route of the specification (declaration_route), the declared name, the
declared entries with the options as supplied, the effective name, the seed
policy, the stamps with their origin (explicit or generated) and, for every effective
stage, its position, name, code, occurrence, identity and local options
(callables are described by name, never executed). The experiment block
records the resolved plan of the primary role: n_runs and where it came
from (explicit, randomized_solvers or stage_hints), the run
policy, the execution strategy and the language-local runtime identifier; run
counts appear there and nowhere inside a stage. The structured report shows the same feature block under
configuration.effective.feature and the plans of every executing role
under configuration.effective.experiment. Archives written by earlier
versions (feature_pipeline-v1 and -v2, or none at all) keep their own
payload verbatim when loaded; nothing is migrated or relabelled. The file
test_log/options_refined.pkl (schema options_refined-v2) stores
feature_route, feature_name (the declared name), n_runs and
feature_specification: the ordered effective stages with their validated
options as native Python values, callables included, and no flat stage keys.
That entry is valid feature input, so
scores, _, _ = benchmark(feature=refined['feature_specification'], n_runs=refined['n_runs'], ...)
reproduces the effective experiment for both routes; forwarding the whole
refined dictionary is not a supported call. The file written by a load
(re-plot) invocation additionally records operation='load' and describes
the archived experiment, never the load label: its feature_specification
and n_runs are recovered from the source experiment’s own native
options_refined.pkl (exact values, callables included) and cross-checked
against the archive (run axis, feature stamp, plain reference and, for a
feature_pipeline-v3 payload, every stage record including its options;
callbacks compare by descriptor module and name only, which the record says),
with the archived seed, run_plain, the fixed plain-reference count and the
archived problem options under archived_experiment; when that file is missing, a flat 1.x file without
feature identity, or in disagreement with the archive, the recipe fails
closed (replayable=False, feature_specification=None, n_runs=None
and a replay_reason) instead of inventing defaults. The report’s JSON
callback descriptions are never used as a recipe. optiprofiler.load_benchmark_options reads trusted native files or already-loaded
mappings and returns keyword arguments for a fresh benchmark. It handles current
user/refined settings and supported earlier layouts, separating the retained
run count from the Feature. Solvers and profile options are supplied by the
caller; in particular, seed, merit and silent settings are not forwarded from
the saved file. A flat 1.x file without feature identity requires an explicit
feature_name. A load-written file replays only through its recovered
archived_experiment; otherwise LegacyConfigurationError explains why.
from optiprofiler import benchmark, load_benchmark_options
options = load_benchmark_options("test_log/options_refined.pkl")
scores, _, _ = benchmark(solvers, **options, silent=True)
optiprofiler.legacy_compat retains the historical object decoder and
import_legacy_feature for separately saved 1.x Features. Only load pickle
and H5 files from trusted sources: unpickling can execute code. Callback
metadata in reports is informational, never a reconstruction recipe.
New options_user.pkl files retain the original input values and add
schema='options_user-v2'. Replay them with load_benchmark_options rather than
forwarding the entire dictionary to benchmark. The marker distinguishes
fresh input from historical effective settings: fresh mesh_type='RELATIVE'
means relative quantization, whereas older Python versions actually executed
the absolute grid for that accepted spelling. Historical replay preserves the
grid that ran. Prefer options_refined.pkl when the resolved defaults and
run count must also be preserved.
options_refined.pkl records problem_route. It does not store a Problem
passed through problem, so load_benchmark_options raises
LegacyConfigurationError for such a run instead of returning settings that
would benchmark a problem library. Set the original Problem first, with
options['problem'] = problem, and call load_benchmark_options again; the run’s
options_user.pkl also records the Problem. A record older than this field
has default library selectors whatever the experiment used, so its replay warns:
a direct-Problem experiment would run on those selectors.
MATLAB mapping. The MATLAB implementation follows the same contract:
options.feature as a cell array of structs
(struct('name', 'noisy', 'options', struct('noise_level', 1e-3))) or
names, options.n_runs at the top level, the same feature_pipeline-v3
fields and report version, and a language-local seed policy for compositions
(matlab-stage-horner32-v2); random samples are not matched across the two
languages. options.feature_name keeps its current meaning. See the MATLAB
user guide for the native replay helper loadBenchmarkOptions.
Example 3: useful option load#
Warning
Load only experiment archives from trusted sources. Python HDF5 archives may contain pickled objects; loading an untrusted file can execute code. The loader is not a safe reader for public uploads.
OptiProfiler provides a practically useful option named load. This option allows you to load the results from a previous benchmarking run (without solving all the problems again) and use them to draw new profiles with different options. For example, if you have just run Example 2 and OptiProfiler has finished the job and successfully created the folder out in the current working directory, you can run:
from optiprofiler import benchmark
scores = benchmark(
load='latest',
solvers_to_load=[0, 2],
ptype='u',
mindim=7,
maxdim=9,
)
This will directly draw the profiles for the solver1 and solver3 with the 'noisy' feature and all the unconstrained problems with dimension between 7 and 9 selected from the previous run. The results will also be saved under the current directory with a new subfolder named noisy_<timestamp> with the new timestamp.
A load runs no solver, so it cannot be combined with problem, which runs the solvers on one problem; the combination is rejected before any solver runs or any experiment output is created.
The saved experiment is searched for in the current working directory joined with benchmark_id (up to five levels of subdirectories). savepath does not change this search; it only sets where the new results are written. To reload an experiment saved under another savepath, change to that directory first.
Example 4: testing parametrized solvers#
If you want to benchmark a solver with one variable parameter, you can define callables by looping over the parameter values. For example, if solver accepts the signature solver(fun, x0, para), and you want to benchmark it with the parameter para taking values from 1 to 3, you can run:
from functools import partial
from optiprofiler import benchmark
solvers = [partial(solver, para=i) for i in range(1, 4)]
solver_names = [f'solver{i}' for i in range(1, 4)]
if __name__ == '__main__':
scores = benchmark(solvers, solver_names=solver_names)
Note
Define solver at module level. A partial of that function remains
picklable, unlike a factory returning a nested function, so the benchmark
can run in parallel when n_jobs > 1.
See Callable arguments must be picklable when running in parallel for the full list of affected
callables and the rationale.
Example 5: customizing the test suite#
OptiProfiler allows you to customize the test suite by creating your own feature and loading your own problem library. For example, if you want to create a new feature that adds noise to the objective function and perturbs the initial guess at the same time, you can try the following:
from optiprofiler import benchmark
def mod_fun(x, rand_stream, problem):
return problem.fun(x) + 1e-3 * rand_stream.standard_normal()
def mod_x0(rand_stream, problem):
return problem.x0 + 1e-3 * rand_stream.standard_normal(problem.n)
scores = benchmark(
[solver1, solver2],
feature_name='custom',
mod_fun=mod_fun,
mod_x0=mod_x0,
)
Note
Again, mod_fun and mod_x0 are defined with def rather than
lambda so that the benchmark can run in parallel when
n_jobs > 1. See Callable arguments must be picklable when running in parallel for details.
Problem libraries may be distributed as independent Python packages. See
Python problem libraries for installation and lifecycle management and
Python providers for the supported providers. Once a
compatible package is installed, its public library name can be passed directly
to plibs; no filesystem path is needed. For example, a package named
optiprofiler-myproblems may register the library 'myproblems', after
which it can be used as follows:
scores = benchmark(
[solver1, solver2],
plibs=['s2mpj', 'myproblems'],
)
OptiProfiler discovers these packages without importing them. Their optional dependencies are imported only when the corresponding library is selected for a benchmark. See Custom Python problem libraries to create a local or installable provider.
Library-specific experiment options#
Different problem libraries may expose entirely different options. Keep these
options separated by library name with plib_options instead of mixing them
with the common problem filters such as ptype, mindim, and maxdim.
For example, using two hypothetical installable plugins:
scores = benchmark(
[solver1, solver2],
plibs=['myproblems', 'otherproblems'],
plib_options={
'myproblems': {
'variant': 'large',
},
'otherproblems': {
'data_split': 'validation',
},
},
)
Each library validates only its own mapping. Options for a library not listed
in plibs and unknown option names are errors. plib_options is only for
runs that select problems from libraries; it is rejected with problem and
load because those modes do not call a library adapter. The precedence is:
values in
benchmark(..., plib_options=...);process-level overrides from
set_plib_config();environment or package configuration owned by the library;
the library’s built-in defaults.
Use get_plib_config() to inspect the effective
process-level values. The per-experiment values do not mutate them. The raw
plib_options supplied by the user are saved in options_user.pkl. The
validated mapping with defaults filled in is saved in options_refined.pkl
and, as one type-preserving serialized mapping, in the library’s group in
data_for_loading.h5. These files are under the experiment’s test_log
directory.
Paths to external runtimes, licenses, compiled binaries, and caches are
installation concerns rather than benchmark semantics. A library should check
those in its availability hook instead of placing them in plib_options.
For a local filesystem adapter or an independently distributed plugin, follow Custom Python problem libraries. The provider-authoring contract is kept out of this quick-start tutorial so that ordinary benchmark usage remains focused.
Example 6: wrapping SciPy solvers with nonlinear constraints#
(See also the file in the repository: python/examples/scipy_cobyqa_wrapper.py)
For nonlinearly constrained problems, OptiProfiler calls each solver with the signature
x = solver(fun, x0, xl, xu, aub, bub, aeq, beq, cub, ceq)
where cub(x) <= 0 contains the nonlinear inequality constraints and
ceq(x) = 0 contains the nonlinear equality constraints. SciPy’s
minimize interface represents constraints with objects such as
Bounds, LinearConstraint, and NonlinearConstraint. The adapter in
the wrapper below is the conversion from OptiProfiler’s callback signature to
SciPy’s constraint objects: linear constraints become LinearConstraint
objects, while cub and ceq are wrapped as NonlinearConstraint
objects with bounds (-inf, 0) and (0, 0), respectively. The SciPy
documentation for COBYQA
and the optimization tutorial
show this object-based constraint interface; see also the API references for
LinearConstraint
and NonlinearConstraint.
import numpy as np
from scipy.optimize import Bounds, LinearConstraint, NonlinearConstraint, minimize
def scipy_cobyqa_wrapper(fun, x0, xl, xu, aub, bub, aeq, beq, cub, ceq,
maxfev=200):
constraints = []
if bub.size > 0:
# OptiProfiler gives aub @ x <= bub; SciPy stores it as a
# LinearConstraint with lower bound -inf and upper bound bub.
constraints.append(LinearConstraint(aub, -np.inf, bub))
if beq.size > 0:
# Equality constraints use identical lower and upper bounds.
constraints.append(LinearConstraint(aeq, beq, beq))
c_ub_x0 = np.atleast_1d(cub(x0))
if c_ub_x0.size > 0:
# Convert cub(x) <= 0 to a SciPy NonlinearConstraint.
constraints.append(NonlinearConstraint(cub, -np.inf, np.zeros_like(c_ub_x0)))
c_eq_x0 = np.atleast_1d(ceq(x0))
if c_eq_x0.size > 0:
# Convert ceq(x) = 0 by using zero lower and upper bounds.
constraints.append(NonlinearConstraint(ceq, np.zeros_like(c_eq_x0),
np.zeros_like(c_eq_x0)))
result = minimize(
fun,
x0,
method='COBYQA',
bounds=Bounds(xl, xu),
constraints=constraints,
options={'maxfev': maxfev},
)
return result.x
Then pass the wrapper to benchmark as an ordinary solver. Since
benchmark compares at least two solvers, this example compares two
COBYQA wrappers with different function-evaluation budgets:
def scipy_cobyqa_short(fun, x0, xl, xu, aub, bub, aeq, beq, cub, ceq):
return scipy_cobyqa_wrapper(
fun, x0, xl, xu, aub, bub, aeq, beq, cub, ceq, maxfev=100
)
def scipy_cobyqa_long(fun, x0, xl, xu, aub, bub, aeq, beq, cub, ceq):
return scipy_cobyqa_wrapper(
fun, x0, xl, xu, aub, bub, aeq, beq, cub, ceq, maxfev=200
)
scores = benchmark(
[scipy_cobyqa_short, scipy_cobyqa_long],
solver_names=['SciPy COBYQA short', 'SciPy COBYQA long'],
ptype='n',
problem_names=['HS10', 'HS11', 'HS12'],
mindim=2,
maxdim=5,
max_eval_factor=500,
plibs=['s2mpj'],
draw_hist_plots='none',
n_jobs=1,
)
Cautions#
Callable arguments must be picklable when running in parallel#
When n_jobs > 1, OptiProfiler dispatches problems to worker
processes via multiprocessing. It uses fresh spawn workers and
a matching logging queue, without changing the application’s global start
method. This avoids inheriting output locks from active logging threads.
The following callable
arguments are sent across process boundaries and must therefore be
picklable:
the entries of
solvers;feature options:
distribution,noise_map,mod_x0,mod_affine,mod_bounds,mod_linear_ub,mod_linear_eq,mod_fun,mod_cub,mod_ceq;profile options:
merit_fun,score_fun,score_weight_fun.
Lambda expressions and locally-defined nested functions are not
picklable. If any of the callables above is a lambda, OptiProfiler
detects the failure when serializing the worker arguments, logs the reason
(unless silent is true), and falls back to sequential mode (n_jobs = 1),
which can be much slower.
To enable parallel execution, define these callables as module-level
functions using def. For parametrized solvers, use
functools.partial() with a module-level function. A factory that returns
a nested function still requires sequential execution.
Put the benchmark call in an importable script and protect its entry point.
This is required on every platform, Linux included, whenever n_jobs > 1:
OptiProfiler starts its workers with the spawn method, which imports the
script again in each worker, so an unprotected call would run the benchmark
again there. OptiProfiler detects such a call while a worker imports the
script and raises a RuntimeError there, before the worker creates any
output; the parallel benchmark then fails instead of saving duplicate
experiments.
if __name__ == '__main__':
benchmark(solvers, n_jobs=2)
Notebooks, interactive consoles, and standard-input scripts have no importable
entry point. OptiProfiler uses sequential execution in these cases and logs the
reason. Python also skips rebuilding package __main__.py entry points in
spawned workers. Callables defined there require sequential execution; functions
imported from other modules can still run in parallel.
This decision is made before any solver runs. It does not retry completed work
after a parallel failure.