Quickstart
This page shows the smallest useful ropt program: minimizing the deterministic
multi-dimensional Rosenbrock function with the
BasicOptimizer class.
Prerequisites
Install ropt (see Installation):
A minimal optimization
import numpy as np
from numpy.typing import NDArray
from ropt.evaluation import EvaluationBatchContext, EvaluationBatchResult
from ropt.workflow import BasicOptimizer
DIM = 5
CONFIG = {
"variables": {
"variable_count": DIM,
"perturbation_magnitudes": 1e-6,
},
}
def rosenbrock(variables: NDArray[np.float64], _: EvaluationBatchContext) -> EvaluationBatchResult:
objectives = np.zeros((variables.shape[0], 1), dtype=np.float64)
for v_idx in range(variables.shape[0]):
for d_idx in range(DIM - 1):
x, y = variables[v_idx, d_idx : d_idx + 2]
objectives[v_idx, 0] += (1.0 - x) ** 2 + 100 * (y - x * x) ** 2
return EvaluationBatchResult(objectives=objectives)
initial_values = 2 * np.arange(DIM) / DIM + 0.5
optimizer = BasicOptimizer(CONFIG, rosenbrock)
optimizer.run(initial_values)
print(f"Optimal variables: {optimizer.results.evaluations.variables}")
print(f"Optimal objective: {optimizer.results.functions.target_objective}")
Running this script optimizes the 5-dimensional Rosenbrock function from the
starting point initial_values and prints the best variables and weighted
objective value found.
What just happened
Three pieces of information drive every ropt optimization:
- A configuration dict — describes the optimization problem (variables,
objectives, constraints, gradient settings, optimizer choice). Here we set
only the minimum: how many variables there are, and a small
perturbation_magnitudesvalue used to estimate gradients numerically. - An evaluator — a Python callable that takes a matrix of variable vectors
plus an
EvaluationBatchContextand returns anEvaluationBatchResultwith the objective values for each row. The variable matrix has one row per requested evaluation.roptmay request several rows in a single call so that an evaluator can compute them in parallel. - A driver — here,
BasicOptimizer. It wires the configuration and evaluator together, executes the optimization, and exposes the best result through itsresultsproperty.
Where to next
- A guided walkthrough of
BasicOptimizerfeatures: Running a basic optimization task. - The full configuration vocabulary, section by section: Configuration.
- Writing more advanced evaluation callbacks (per-realization data, partial failures): Writing Evaluation Callbacks.
- Full runnable example: examples/deterministic.py.