Skip to content

Commit

Permalink
add minimum working example
Browse files Browse the repository at this point in the history
  • Loading branch information
mcw92 committed Mar 13, 2024
1 parent dc06d53 commit 0656395
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tutorials/minimum_working_example.py
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()

0 comments on commit 0656395

Please sign in to comment.