Skip to content

Commit

Permalink
Add example of IPOP-CMA-ES
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bata committed Jul 25, 2020
1 parent 1d0e49a commit 449763e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ jobs:
pip install --progress-bar off optuna
pip install --progress-bar off -U .
- run: python examples/quadratic_function.py
- run: python examples/ipop_cmaes.py
- run: python examples/optuna_sampler.py
38 changes: 38 additions & 0 deletions examples/ipop_cmaes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import numpy as np
from cmaes import CMA


def quadratic(x1, x2):
return (x1 - 3) ** 2 + (10 * (x2 + 2)) ** 2


def main():
cma_opts = {"mean": np.zeros(2), "sigma": 1.3}
optimizer = CMA(**cma_opts)

# Multiplier for increasing population size before each restart.
inc_popsize = 2

print(" g f(x1,x2) x1 x2 ")
print("=== ========== ====== ======")
for generation in range(150):
solutions = []
for _ in range(optimizer.population_size):
x = optimizer.ask()
value = quadratic(x[0], x[1])
solutions.append((x, value))

msg = "{g:3d} {value:10.5f} {x1:6.2f} {x2:6.2f}".format(
g=generation, value=value, x1=x[0], x2=x[1],
)
print(msg)
optimizer.tell(solutions)

if optimizer.should_terminate():
popsize = optimizer.population_size * inc_popsize
optimizer = CMA(population_size=popsize, **cma_opts)
print("Restart CMA-ES with popsize={}".format(popsize))


if __name__ == "__main__":
main()
7 changes: 5 additions & 2 deletions examples/quadratic_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,22 @@ def main():
print(" g f(x1,x2) x1 x2 ")
print("=== ========== ====== ======")

for generation in range(50):
while True:
solutions = []
for _ in range(optimizer.population_size):
x = optimizer.ask()
value = quadratic(x[0], x[1])
solutions.append((x, value))

msg = "{g:3d} {value:10.5f} {x1:6.2f} {x2:6.2f}".format(
g=generation, value=value, x1=x[0], x2=x[1],
g=optimizer.generation, value=value, x1=x[0], x2=x[1],
)
print(msg)
optimizer.tell(solutions)

if optimizer.should_terminate():
break


if __name__ == "__main__":
main()

0 comments on commit 449763e

Please sign in to comment.