Skip to content

Commit

Permalink
more attempts to stabilize objective and speed up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Oct 28, 2023
1 parent 06469d3 commit 642ed8b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 23 deletions.
7 changes: 5 additions & 2 deletions moptipyapps/dynamic_control/objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,13 @@ def evaluate(self, x: np.ndarray) -> float:
np.copy(start.flatten()) # <--- This should make no sense...
ode = run_ode(
start, equations, controller, x, controller_dim, steps, time)
results[i] = j_from_ode(ode, state_dim, state_dims_in_j, gamma)
results[i] = z = j_from_ode(ode, state_dim, state_dims_in_j, gamma)
if not (0.0 <= z <= 1e100):
return 1e200
if collector is not None:
collector(diff_from_ode(ode, state_dim))
return self.sum_up_results(results)
z = self.sum_up_results(results)
return z if 0.0 <= z <= 1e100 else 1e200

def sum_up_results(self, results: np.ndarray) -> float:
"""
Expand Down
3 changes: 2 additions & 1 deletion tests/dynamic_control/test_experiment_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ def test_experiment_raw(random: Generator = default_rng()) -> None:
:param random: a randomizer
"""
n_runs: Final[int] = int(random.integers(1, 5))
n_runs: Final[int] = 1
er: list[EndResult] = []
insts: list[Callable[[], Instance]] = __make_instances()
insts = [insts[random.integers(len(insts))]]

with TempDir.create() as use_dir:
er.clear()
Expand Down
7 changes: 4 additions & 3 deletions tests/dynamic_control/test_experiment_surrogate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""A test for the surrogate experiment of the dynamic control problem."""

from typing import Callable, cast
from typing import Callable, Final, cast

from moptipy.api.execution import Execution
from moptipy.api.experiment import run_experiment
Expand Down Expand Up @@ -66,6 +66,7 @@ def test_experiment_surrogate(random: Generator = default_rng()) -> None:
:param random: a randomizer
"""
n_runs: Final[int] = 1
er: list[EndResult] = []
insts: list[Callable[[], SystemModel]] = list(__make_instances(random))
insts = [insts[random.integers(len(insts))]]
Expand All @@ -75,8 +76,8 @@ def test_experiment_surrogate(random: Generator = default_rng()) -> None:
run_experiment(base_dir=use_dir,
instances=insts,
setups=[__cmaes],
n_runs=1,
n_runs=n_runs,
perform_warmup=False,
perform_pre_warmup=False)
EndResult.from_logs(use_dir, er.append)
assert len(er) == len(insts)
assert len(er) == (len(insts) * n_runs)
20 changes: 3 additions & 17 deletions tests/dynamic_control/test_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,15 @@
from moptipyapps.dynamic_control.controllers.ann import anns
from moptipyapps.dynamic_control.controllers.cubic import cubic
from moptipyapps.dynamic_control.controllers.linear import linear
from moptipyapps.dynamic_control.controllers.min_ann import min_anns
from moptipyapps.dynamic_control.controllers.partially_linear import (
partially_linear,
)
from moptipyapps.dynamic_control.controllers.peaks import peaks
from moptipyapps.dynamic_control.controllers.predefined import predefined
from moptipyapps.dynamic_control.controllers.quadratic import quadratic
from moptipyapps.dynamic_control.instance import Instance
from moptipyapps.dynamic_control.objective import (
FigureOfMerit,
FigureOfMeritLE,
)
from moptipyapps.dynamic_control.system import System
from moptipyapps.dynamic_control.systems.lorenz import LORENZ_4, LORENZ_111
from moptipyapps.dynamic_control.systems.stuart_landau import (
STUART_LANDAU_4,
STUART_LANDAU_111,
)
from moptipyapps.dynamic_control.systems.lorenz import LORENZ_4
from moptipyapps.dynamic_control.systems.stuart_landau import STUART_LANDAU_4


def __objective_test(fc: Callable[[Instance, bool], FigureOfMerit],
Expand Down Expand Up @@ -63,8 +54,7 @@ def __objective_tests(fc: Callable[[Instance, bool], FigureOfMerit],
:param fc: the figure of merit
:param random: the generator
"""
for orig_system in (STUART_LANDAU_111, LORENZ_111, STUART_LANDAU_4,
LORENZ_4):
for orig_system in (STUART_LANDAU_4, LORENZ_4):
system = System(orig_system.name,
orig_system.state_dims, orig_system.control_dims,
orig_system.state_dim_mod,
Expand All @@ -78,10 +68,6 @@ def __objective_tests(fc: Callable[[Instance, bool], FigureOfMerit],

controllers = [linear(system), quadratic(system), cubic(system)]
controllers.extend(anns(system))
controllers.extend(min_anns(system))
controllers.extend(partially_linear(system))
controllers.extend(predefined(system))
controllers.extend(peaks(system))
for c in controllers:
__objective_test(fc, Instance(system, c))

Expand Down

0 comments on commit 642ed8b

Please sign in to comment.