-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oskar Taubert
committed
Oct 23, 2023
1 parent
89a5cc5
commit 7d94860
Showing
2 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import random | ||
import tempfile | ||
from typing import Dict | ||
from operator import attrgetter | ||
|
||
import numpy as np | ||
|
||
from propulate import Propulator | ||
from propulate.propagators import CMAPropagator, BasicCMA | ||
|
||
|
||
def sphere(params: Dict[str, float]) -> float: | ||
""" | ||
Sphere function: continuous, convex, separable, differentiable, unimodal | ||
Input domain: -5.12 <= x, y <= 5.12 | ||
Global minimum 0 at (x, y) = (0, 0) | ||
Parameters | ||
---------- | ||
params: dict[str, float] | ||
function parameters | ||
Returns | ||
------- | ||
float | ||
function value | ||
""" | ||
return np.sum(np.array(list(params.values())) ** 2) | ||
|
||
|
||
def test_PSO(): | ||
""" | ||
Test single worker using Propulator to optimize sphere using a PSO propagator. | ||
""" | ||
rng = random.Random(42) # Separate random number generator for optimization. | ||
limits = { | ||
"a": (-5.12, 5.12), | ||
"b": (-5.12, 5.12), | ||
} | ||
with tempfile.TemporaryDirectory() as checkpoint_path: | ||
# Set up evolutionary operator. | ||
|
||
adapter = BasicCMA() | ||
propagator = CMAPropagator(adapter, limits, rng=rng) | ||
|
||
# Set up propulator performing actual optimization. | ||
propulator = Propulator( | ||
loss_fn=sphere, | ||
propagator=propagator, | ||
generations=10, | ||
checkpoint_path=checkpoint_path, | ||
rng=rng, | ||
) | ||
|
||
# Run optimization and print summary of results. | ||
propulator.propulate() | ||
propulator.summarize() | ||
best = min(propulator.population, key=attrgetter("loss")) | ||
|
||
assert best.loss < 10.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import random | ||
import tempfile | ||
from typing import Dict | ||
from operator import attrgetter | ||
|
||
import numpy as np | ||
|
||
from propulate import Propulator | ||
from propulate.propagators import Conditional | ||
from propulate.propagators.pso import BasicPSO, InitUniformPSO | ||
|
||
|
||
def sphere(params: Dict[str, float]) -> float: | ||
""" | ||
Sphere function: continuous, convex, separable, differentiable, unimodal | ||
Input domain: -5.12 <= x, y <= 5.12 | ||
Global minimum 0 at (x, y) = (0, 0) | ||
Parameters | ||
---------- | ||
params: dict[str, float] | ||
function parameters | ||
Returns | ||
------- | ||
float | ||
function value | ||
""" | ||
return np.sum(np.array(list(params.values())) ** 2) | ||
|
||
|
||
def test_PSO(): | ||
""" | ||
Test single worker using Propulator to optimize sphere using a PSO propagator. | ||
""" | ||
rng = random.Random(42) # Separate random number generator for optimization. | ||
limits = { | ||
"a": (-5.12, 5.12), | ||
"b": (-5.12, 5.12), | ||
} | ||
with tempfile.TemporaryDirectory() as checkpoint_path: | ||
# Set up evolutionary operator. | ||
|
||
pso_propagator = BasicPSO( | ||
0.729, | ||
1.49334, | ||
1.49445, | ||
0, # MPI rank TODO fix when implemented proper MPI parallel tests | ||
limits, | ||
rng, | ||
) | ||
init = InitUniformPSO(limits, rng=rng, rank=0) | ||
propagator = Conditional(1, pso_propagator, init) # TODO MPIify | ||
|
||
# Set up propulator performing actual optimization. | ||
propulator = Propulator( | ||
loss_fn=sphere, | ||
propagator=propagator, | ||
generations=10, | ||
checkpoint_path=checkpoint_path, | ||
rng=rng, | ||
) | ||
|
||
# Run optimization and print summary of results. | ||
propulator.propulate() | ||
propulator.summarize() | ||
best = min(propulator.population, key=attrgetter("loss")) | ||
|
||
assert best.loss < 30.0 |