-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding CMA-ES (both inside MVRSM and separate) and Hospital simulator…
… problem
- Loading branch information
Showing
8 changed files
with
275 additions
and
20 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
expensiveoptimbenchmark/problems/DockerHospitalBenchmark.py
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,62 @@ | ||
from .base import BaseProblem | ||
|
||
import subprocess | ||
import platform | ||
import numpy as np | ||
import os | ||
|
||
class DockerHospitalBenchmarkProblem(BaseProblem): | ||
|
||
def __init__(self, name, d, lbs, ubs, vartype, direction, errval): | ||
self.name = name | ||
self.d = d | ||
self.lb = np.asarray(lbs) | ||
self.ub = np.asarray(ubs) | ||
self.vt = np.asarray(vartype) | ||
self.direction = 1 if direction == "min" else -1 | ||
self.errval = errval | ||
|
||
if os.path.exists("./evaluate.sh"): | ||
self.evalCommand = ["./evaluate.sh"] | ||
elif os.path.exists("/evaluate.sh"): | ||
self.evalCommand = ["/evaluate.sh"] | ||
else: | ||
# Note: experiment should be ran with sudo under linux to allow this command to work, | ||
# or under an user in the docker group. | ||
self.evalCommand = ["docker", "run", "--rm", "mrebolle/r-geccoc:Track1", "-c"] | ||
|
||
def evaluate(self, xs): | ||
concatenatedCandidate = ",".join(["%.8f" % x if xvt == 'cont' else "%i" % x for (x, xvt) in zip(xs, self.vt)]) | ||
parsedCandidate = f"Rscript objfun.R \"{concatenatedCandidate}\"" | ||
cmd = f"{self.evalCommand + [parsedCandidate]}" | ||
print(f"Running '{cmd}'") | ||
# res = subprocess.check_output(cmd, shell=True) | ||
res = subprocess.check_output(self.evalCommand + [parsedCandidate]) | ||
reslast = res.strip().split(b"\n")[-1] | ||
print(f"Result: {res}. Objective: {reslast}") | ||
try: | ||
return self.direction * float(reslast) | ||
except: | ||
return self.errval | ||
|
||
def lbs(self): | ||
return self.lb | ||
|
||
def ubs(self): | ||
return self.ub | ||
|
||
def vartype(self): | ||
return self.vt | ||
|
||
def dims(self): | ||
return self.d | ||
|
||
def __str__(self): | ||
return f"Hospital()" | ||
|
||
Hospital = DockerHospitalBenchmarkProblem("hospital", 29, | ||
np.array([ 6, 7, 3, 3, 3, 5, 3, 3, 25, 17, 2, 1, 0.25, 0.05, 0.07, 0.005, 0.07, 1e-04, 0.08, 0.25, 0.08, 0.5, 1e-6, 2, 1e-6, 1e-6, 1, 2, 0.5 ]), | ||
np.array([14, 13, 7, 9, 7, 9, 5, 7, 35, 25, 5, 7, 2 , 0.15, 0.11, 0.02 , 0.13, 0.002, 0.12, 0.35, 0.12, 0.9, 0.01, 4, 1.1, 0.0625, 2, 5, 0.75]), | ||
['cont'] * 29, "min", 10000.0) | ||
|
||
dockerhospitalsimbenches = [Hospital] |
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
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,74 @@ | ||
import numpy as np | ||
|
||
from cma import BoundPenalty, BoundTransform, fmin, CMAOptions | ||
from ..utils import Monitor, Binarizer | ||
|
||
def optimize_CMA(problem, max_evals, bound_h='transform', binarize_categorical=False, log=None): | ||
d = problem.dims() | ||
|
||
vartypes = problem.vartype() | ||
|
||
if binarize_categorical: | ||
raise Exception("While sort-of implemented, this implementation has issues. Please do not enable binarization.") | ||
|
||
# Bounds | ||
lb = problem.lbs() | ||
ub = problem.ubs() | ||
|
||
# Generate initial point, round the integers. | ||
|
||
if binarize_categorical: | ||
b = Binarizer(problem.vartype()[perm] == 'cat', lb, ub) | ||
x0 = b.binarize(x0) | ||
# Get new bounds. | ||
lb = b.lbs() | ||
ub = b.ubs() | ||
# All new variables introduced by the binarized are 'integer'. | ||
# print(f"Binarization introduced {b.dout - b.din} new variables.") | ||
vartypes = vartypes[self.origin_mapping] | ||
|
||
cmascale = (ub - lb) | ||
x0 = (0.5*(ub-lb) + lb) / cmascale | ||
not_continuous_variables = vartypes != "cont" | ||
x0[not_continuous_variables] = np.round(x0[not_continuous_variables]) | ||
sigma0 = 1/4 | ||
|
||
mon = Monitor(f"CMAES{'/cbinarized' if binarize_categorical else ''}/{bound_h}", problem, log=log) | ||
def f(x): | ||
x = x * cmascale | ||
if binarize_categorical: | ||
xred = b.unbinarize(x) | ||
else: | ||
xred = x | ||
x = x[vartypes != "cont"] | ||
# print(xred) | ||
mon.commit_start_eval() | ||
r = problem.evaluate(xred) | ||
mon.commit_end_eval(xred, r) | ||
return r | ||
|
||
# lb, ub, max_evals | ||
opts = CMAOptions() | ||
opts['bounds'] = [list(lb / cmascale), list(ub / cmascale)] | ||
opts['BoundaryHandler'] = BoundPenalty if bound_h == "penalty" else BoundTransform | ||
opts['maxfevals'] = max_evals | ||
opts['verbose'] = 0 # Supress printing! | ||
opts['integer_variables'] = [i for i,t in enumerate(vartypes) if t != 'cont'] | ||
mon.start() | ||
res = fmin(f, x0 / cmascale, sigma0, options=opts) | ||
mon.end() | ||
|
||
# """ | ||
# - `res[0]` (`xopt`) -- best evaluated solution | ||
# - `res[1]` (`fopt`) -- respective function value | ||
# - `res[2]` (`evalsopt`) -- respective number of function evaluations | ||
# - `res[3]` (`evals`) -- number of overall conducted objective function evaluations | ||
# - `res[4]` (`iterations`) -- number of overall conducted iterations | ||
# - `res[5]` (`xmean`) -- mean of the final sample distribution | ||
# - `res[6]` (`stds`) -- effective stds of the final sample distribution | ||
# - `res[-3]` (`stop`) -- termination condition(s) in a dictionary | ||
# - `res[-2]` (`cmaes`) -- class `CMAEvolutionStrategy` instance | ||
# - `res[-1]` (`logger`) -- class `CMADataLogger` instance | ||
# """ | ||
|
||
return mon.best_x, mon.best_fitness, mon |
Oops, something went wrong.