Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align plotting from logs and results #553

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 75 additions & 7 deletions src/optimagic/optimization/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

from optimagic.exceptions import InvalidAlgoInfoError, InvalidAlgoOptionError
from optimagic.logging.types import StepStatus
from optimagic.optimization.convergence_report import get_convergence_report
from optimagic.optimization.history import History
from optimagic.optimization.internal_optimization_problem import (
InternalOptimizationProblem,
)
from optimagic.optimization.optimize_result import OptimizeResult
from optimagic.parameters.conversion import Converter
from optimagic.type_conversion import TYPE_CONVERTERS
from optimagic.typing import AggregationLevel
from optimagic.typing import AggregationLevel, Direction, ExtraResultFields
from optimagic.utilities import isscalar


@dataclass(frozen=True)
Expand Down Expand Up @@ -82,7 +86,6 @@ class InternalOptimizeResult:
max_constraint_violation: float | None = None
info: dict[str, typing.Any] | None = None
history: History | None = None
multistart_info: dict[str, typing.Any] | None = None

def __post_init__(self) -> None:
report: list[str] = []
Expand Down Expand Up @@ -142,6 +145,56 @@ def __post_init__(self) -> None:
)
raise TypeError(msg)

def create_optimize_result(
self,
converter: Converter,
solver_type: AggregationLevel,
extra_fields: ExtraResultFields,
) -> OptimizeResult:
"""Process an internal optimizer result."""
params = converter.params_from_internal(self.x)
if isscalar(self.fun):
fun = float(self.fun)
elif solver_type == AggregationLevel.LIKELIHOOD:
fun = float(np.sum(self.fun))
elif solver_type == AggregationLevel.LEAST_SQUARES:
fun = np.dot(self.fun, self.fun)

if extra_fields.direction == Direction.MAXIMIZE:
fun = -fun

if self.history is not None:
conv_report = get_convergence_report(
history=self.history, direction=extra_fields.direction
)
else:
conv_report = None

out = OptimizeResult(
params=params,
fun=fun,
start_fun=extra_fields.start_fun,
start_params=extra_fields.start_params,
algorithm=extra_fields.algorithm,
direction=extra_fields.direction.value,
n_free=extra_fields.n_free,
message=self.message,
success=self.success,
n_fun_evals=self.n_fun_evals,
n_jac_evals=self.n_jac_evals,
n_hess_evals=self.n_hess_evals,
n_iterations=self.n_iterations,
status=self.status,
jac=self.jac,
hess=self.hess,
hess_inv=self.hess_inv,
max_constraint_violation=self.max_constraint_violation,
history=self.history,
algorithm_output=self.info,
convergence_report=conv_report,
)
return out


class AlgorithmMeta(ABCMeta):
"""Metaclass to get repr, algo_info and name for classes, not just instances."""
Expand Down Expand Up @@ -234,25 +287,40 @@ def solve_internal_problem(
problem: InternalOptimizationProblem,
x0: NDArray[np.float64],
step_id: int,
) -> InternalOptimizeResult:
) -> OptimizeResult:
problem = problem.with_new_history().with_step_id(step_id)

if problem.logger:
problem.logger.step_store.update(
step_id, {"status": str(StepStatus.RUNNING.value)}
)

result = self._solve_internal_problem(problem, x0)
raw_res = self._solve_internal_problem(problem, x0)

if (not self.algo_info.disable_history) and (result.history is None):
result = replace(result, history=problem.history)
if (not self.algo_info.disable_history) and (raw_res.history is None):
raw_res = replace(raw_res, history=problem.history)

if problem.logger:
problem.logger.step_store.update(
step_id, {"status": str(StepStatus.COMPLETE.value)}
)

return result
# make sure the start params provided in static_result_fields are the same as x0
extra_fields = problem.static_result_fields
x0_problem = problem.converter.params_to_internal(extra_fields.start_params)
if not np.allclose(x0_problem, x0):
start_params = problem.converter.params_from_internal(x0)
extra_fields = replace(
extra_fields, start_params=start_params, start_fun=None
)

res = raw_res.create_optimize_result(
converter=problem.converter,
solver_type=self.algo_info.solver_type,
extra_fields=problem.static_result_fields,
)

return res

def with_option_if_applicable(self, **kwargs: Any) -> Self:
"""Call with_option only with applicable keyword arguments."""
Expand Down
11 changes: 11 additions & 0 deletions src/optimagic/optimization/internal_optimization_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
Direction,
ErrorHandling,
EvalTask,
ExtraResultFields,
PyTree,
)

Expand Down Expand Up @@ -55,6 +56,7 @@ def __init__(
linear_constraints: list[dict[str, Any]] | None,
nonlinear_constraints: list[dict[str, Any]] | None,
logger: LogStore[Any, Any] | None,
static_result_fields: ExtraResultFields,
# TODO: add hess and hessp
):
self._fun = fun
Expand All @@ -73,6 +75,7 @@ def __init__(
self._nonlinear_constraints = nonlinear_constraints
self._logger = logger
self._step_id: int | None = None
self._static_result_fields = static_result_fields

# ==================================================================================
# Public methods used by optimizers
Expand Down Expand Up @@ -218,6 +221,14 @@ def bounds(self) -> InternalBounds:
def logger(self) -> LogStore[Any, Any] | None:
return self._logger

@property
def converter(self) -> Converter:
return self._converter

@property
def static_result_fields(self) -> ExtraResultFields:
return self._static_result_fields

# ==================================================================================
# Implementation of the public functions; The main difference is that the lower-
# level implementations return a history entry instead of adding it to the history
Expand Down
39 changes: 26 additions & 13 deletions src/optimagic/optimization/multistart.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"""

import warnings
from dataclasses import replace
from typing import Literal

import numpy as np
Expand All @@ -21,7 +20,7 @@

from optimagic.logging.logger import LogStore
from optimagic.logging.types import StepStatus
from optimagic.optimization.algorithm import Algorithm, InternalOptimizeResult
from optimagic.optimization.algorithm import Algorithm
from optimagic.optimization.internal_optimization_problem import (
InternalBounds,
InternalOptimizationProblem,
Expand All @@ -30,6 +29,8 @@
from optimagic.optimization.optimization_logging import (
log_scheduled_steps_and_get_ids,
)
from optimagic.optimization.optimize_result import OptimizeResult
from optimagic.optimization.process_multistart_result import process_multistart_result
from optimagic.typing import AggregationLevel, ErrorHandling
from optimagic.utilities import get_rng

Expand All @@ -42,7 +43,7 @@ def run_multistart_optimization(
options: InternalMultistartOptions,
logger: LogStore | None,
error_handling: ErrorHandling,
) -> InternalOptimizeResult:
) -> OptimizeResult:
steps = determine_steps(options.n_samples, stopping_maxopt=options.stopping_maxopt)

scheduled_steps = log_scheduled_steps_and_get_ids(
Expand Down Expand Up @@ -159,6 +160,7 @@ def single_optimization(x0, step_id):
results=batch_results,
convergence_criteria=convergence_criteria,
solver_type=local_algorithm.algo_info.solver_type,
converter=internal_problem.converter,
)
opt_counter += len(batch)
if is_converged:
Expand All @@ -168,15 +170,20 @@ def single_optimization(x0, step_id):
logger.step_store.update(step, {"status": new_status})
break

multistart_info = {
"start_parameters": state["start_history"],
"local_optima": state["result_history"],
"exploration_sample": sorted_sample,
"exploration_results": exploration_res["sorted_values"],
}

raw_res = state["best_res"]
res = replace(raw_res, multistart_info=multistart_info)

expl_sample = [
internal_problem.converter.params_from_internal(s) for s in sorted_sample
]
expl_res = list(exploration_res["sorted_values"])

res = process_multistart_result(
raw_res=raw_res,
extra_fields=internal_problem.static_result_fields,
local_optima=state["result_history"],
exploration_sample=expl_sample,
exploration_results=expl_res,
)

return res

Expand Down Expand Up @@ -371,7 +378,12 @@ def get_batched_optimization_sample(sorted_sample, stopping_maxopt, batch_size):


def update_convergence_state(
current_state, starts, results, convergence_criteria, solver_type
current_state,
starts,
results,
convergence_criteria,
solver_type,
converter,
):
"""Update the state of all quantities related to convergence.

Expand All @@ -389,6 +401,7 @@ def update_convergence_state(
convergence_criteria (dict): Dict with the entries "xtol" and "max_discoveries"
solver_type: The aggregation level of the local optimizer. Needed to
interpret the output of the internal criterion function.
converter: The converter to map between internal and external parameter spaces.


Returns:
Expand Down Expand Up @@ -422,7 +435,7 @@ def update_convergence_state(
# ==================================================================================
valid_results = [results[i] for i in valid_indices]
valid_starts = [starts[i] for i in valid_indices]
valid_new_x = [res.x for res in valid_results]
valid_new_x = [converter.params_to_internal(res.params) for res in valid_results]
valid_new_y = []

# make the criterion output scalar if a least squares optimizer returns an
Expand Down
Loading