-
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
Showing
1 changed file
with
37 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,37 @@ | ||
import propulate | ||
from mpi4py import MPI | ||
import random | ||
|
||
# Set the communicator and the optimization parameters | ||
comm = MPI.COMM_WORLD | ||
rng = random.Random(MPI.COMM_WORLD.rank) | ||
population_size = comm.size * 2 | ||
generations = 100 | ||
checkpoint = "./propulate_checkpoints" | ||
propulate.utils.set_logger_config() | ||
|
||
|
||
# Define the function to minimize and the search space, e.g., a 2D sphere function on (-5.12, 5.12)^2. | ||
def loss_fn(params): | ||
"""Loss function to minimize.""" | ||
return params["x"] ** 2 + params["y"] ** 2 | ||
|
||
|
||
limits = {"x": (-5.12, 5.12), "y": (-5.12, 5.12)} | ||
|
||
# Initialize the propagator and propulator with default parameters. | ||
propagator = propulate.utils.get_default_propagator( | ||
pop_size=population_size, limits=limits, rng=rng | ||
) | ||
propulator = propulate.Propulator( | ||
loss_fn=loss_fn, | ||
propagator=propagator, | ||
rng=rng, | ||
island_comm=comm, | ||
generations=generations, | ||
checkpoint_path=checkpoint, | ||
) | ||
|
||
# Run optimization and get summary of results. | ||
propulator.propulate() | ||
propulator.summarize() |