Skip to content

Commit

Permalink
Refactor experiment 002 code
Browse files Browse the repository at this point in the history
  • Loading branch information
mlojek committed Jan 13, 2025
1 parent 003db5f commit 6af0e45
Showing 1 changed file with 5 additions and 25 deletions.
30 changes: 5 additions & 25 deletions experiments/002_cmaes_knn_metamodel/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
"""
Benchmarking CMA-ES algorithm on CEC 2017
Benchmarking of CMA-ES with KNN metamodel
"""

# pylint: disable=import-error

import numpy as np

from optilab.data_classes import Bounds
from optilab.functions.unimodal import SphereFunction
from optilab.plotting import plot_ecdf_curves
from optilab.utils import dump_to_pickle
from optilab.optimizers import CmaEs, KnnCmaEs

Expand All @@ -25,29 +20,14 @@
# optimized problem
BOUNDS = Bounds(-100, 100)
FUNC = SphereFunction(DIM)
TARGET = 0.0

# optimize using CMA-ES
cmaes_optimizer = CmaEs(POPSIZE, SIGMA0)
cmaes_results = cmaes_optimizer.run_optimization(NUM_RUNS, FUNC, BOUNDS, CALL_BUDGET, TOL)

# optimize using KNN-CMA-ES
knn_optimizer = KnnCmaEs(POPSIZE, SIGMA0, NUM_NEIGHBORS)
knn_results = knn_optimizer.run_optimization(NUM_RUNS, FUNC, BOUNDS, CALL_BUDGET, TOL)

# print stats
vanilla_times = [len(log) for log in cmaes_results.logs]
knn_times = [len(log) for log in knn_results.logs]

print(f'vanilla {np.average(vanilla_times)} {np.std(vanilla_times)}')
print(f'knn {np.average(knn_times)} {np.std(knn_times)}')

# plot results
plot_ecdf_curves(
{
"cma-es": cmaes_results.logs,
"knn-cma-es": knn_results.logs,
},
n_dimensions=DIM,
savepath=f"ecdf_{FUNC.name}_{DIM}.png",
allowed_error=TOL
)

dump_to_pickle([cmaes_results, knn_results], f'knn_reproduction_{FUNC.name}_{DIM}.pkl')
dump_to_pickle([cmaes_results, knn_results], f'002_knn_cma_es_{FUNC.name}_{DIM}.pkl')

0 comments on commit 6af0e45

Please sign in to comment.