Commit 3bf032b3 authored by Julien Lin's avatar Julien Lin
Browse files

fix eaf

parent 5ea90939
......@@ -4,12 +4,28 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: eaf debug",
"type": "python",
"request": "launch",
"module": "eaf",
"args": [
"--nb-run", "7", "--quality-threshold", "660", "670", "675", "680", "--solver", "num_evolutionary", "--iters", "200"
]
},
{
"name": "Python: eaf",
"type": "python",
"request": "launch",
"module": "eaf",
"args": ["--nb-run","20","--quality-threshold","660","--solver","num_greedy"]
"args": [
"--nb-run",
"20",
"--quality-threshold",
"660",
"--solver",
"num_greedy"
]
},
{
......
......@@ -14,7 +14,11 @@ def get_args_parse():
can.add_argument("--nb-run", type=int, default=10, help="number of runs")
can.add_argument(
"--quality-threshold", type=float, default=660, nargs="+", help="Quality threshold"
"--quality-threshold",
type=float,
default=660,
nargs="+",
help="Quality threshold",
)
can.add_argument(
......@@ -23,6 +27,8 @@ def get_args_parse():
can.add_argument("--solver", type=str, default="num_random", help="Solver to use.")
can.add_argument("--iters", type=int, default=200)
return can
......@@ -32,7 +38,7 @@ def main(eaf_args=None):
the = can.parse_args(eaf_args)
args = ["--solver", the.solver, "--iters", "2000", "--steady-delta", "500"]
args = ["--solver", the.solver, "--iters", str(the.iters), "--steady-delta", "500"]
if the.not_parallel:
results = []
......@@ -45,31 +51,46 @@ def main(eaf_args=None):
# print(f"{i}th run")
return run(args)
results = Parallel(n_jobs=num_cores)(delayed(f)(i) for i in range(the.nb_run))
results = Parallel(n_jobs=num_cores - 1)(
delayed(f)(i) for i in range(the.nb_run)
)
values = [(val, iter) for val, iter, _, _ in results]
lens =[len(result[-1]) for result in results]
histories = [
np.array([val for val, _ in history] + [1] * (the.iters - len(history)))
for _, _, _, _, _, history in results
]
values = sorted(values, key=lambda el: el[1])
histories = np.array([np.maximum.accumulate(history) for history in histories])
masks = []
for idx, _ in enumerate(histories):
masks.append(np.concatenate((np.zeros(lens[idx],),np.ones(the.iters - lens[idx],))))
histories = np.ma.array(histories, mask=masks).T
t = [iter for _, iter in values]
values = np.array([val for val, _ in values])
buff = np.empty((1,the.nb_run))
buff = np.empty((1, the.iters))
for quality_threshold in the.quality_threshold:
curr_qualities = np.cumsum(values >= quality_threshold) / len(values)
curr_qualities = np.expand_dims(curr_qualities, axis=0)
buff = np.concatenate((buff,curr_qualities))
curr_quality = histories >= quality_threshold
curr_quality = curr_quality.mean(axis=-1)
curr_quality = np.expand_dims(curr_quality, axis=0)
buff = np.concatenate((buff, curr_quality))
_, coef, initial, _, _, _ = results[0]
t = [i * coef for i in range(histories.shape[0])]
t[0] = initial
fig = plt.figure()
plt.figure()
plt.title(f"{the.solver}, threshold {the.quality_threshold}")
for i in range(1, buff.shape[0]):
plt.step(t, buff[i])
# ax4 = fig.add_subplot(122, projection="3d")
# plt.title(f"{the.solver}, thresholds : {the.quality_threshold}")
plt.step(t, buff[i,~buff[i].mask][0])
# ax4.plot_surface(t, values, the.quality_threshold )
plt.legend([f'threshold : {the.quality_threshold[i]}' for i in range(len(the.quality_threshold))])
plt.legend(
[
f"threshold : {the.quality_threshold[i]}"
for i in range(len(the.quality_threshold))
]
)
plt.show()
......
......@@ -135,7 +135,7 @@ def get_args_parser():
can.add_argument(
"--nb-offspring",
metavar="NB",
default=100,
default=10,
type=int,
help="Number of offspring for each generation.",
)
......@@ -285,7 +285,6 @@ def run_algorithm(the, iters_func):
nb_offspring=the.nb_offspring,
domain_width=the.domain_width,
variation_scale=the.variation_scale,
nb_sensors=the.nb_sensors,
),
make.replacement(
num.replacement,
......@@ -378,7 +377,7 @@ def main(args=None):
# constant when objective function is called multiples times in one epoch
initial = get_constant_objectiv_func(the)
return val, len(history) * coef + initial, sol, sensors
return val,coef, initial, sol, sensors, history
def get_constant_objectiv_func(the):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment