%run custom.ipynb
%matplotlib inline
%reload_ext autoreload
%autoreload 2
problems = ['zdt1', 'zdt2', 'zdt3', 'zdt4','zdt6', 'kursawe', 'bnh', 'osy']
# read the setup file to get the experiment details
def get_setup(str_problem):
with open("input_data/%s.in" % str_problem) as f:
setup = f.readlines()
setup = [x.strip() for x in setup]
val = {
'pop_size' : int(setup[0]),
'n_gen' : int(setup[1]),
'n_obj' :int(setup[2]),
'n_var' : int(setup[4]),
'problem' : get_problem(str_problem)
}
return val
def load_results(algorithm, func_load=None, func_load_history=None):
data = []
# for each problem to compare
for problem in problems:
setup = get_setup(problem)
p = setup['problem']
pf = p.pareto_front()
folder = os.path.join(".", algorithm, problem)
if not os.path.exists(folder):
continue
files = [f for f in os.listdir(folder) if re.match(r'.*out', f)]
print("%s: Parsing %s files." % (problem, len(files)))
# for each run that was executed
for run, f in enumerate(files):
entry = {}
path_to_file = os.path.join(folder, f)
if func_load is None:
F = np.loadtxt(path_to_file)[:, :setup['n_obj']]
else:
F = func_load(path_to_file, setup)
I = NonDominatedSorting().do(F, only_non_dominated_front=True)
F = F[I,:]
igd = IGD(pf).calc(F)
hv = Hypervolume(pf=pf).calc(F)
data.append({'problem' : problem, 'algorithm' : algorithm,
'run' : run, 'igd' : igd, 'hv' : hv, 'fname' : f})
pickle.dump(data, open("%s.dat" % algorithm, "wb" ))
return data
Load the data executed from the C code and convert them into the right format.
def func_load(fname, setup):
M = np.genfromtxt(fname,delimiter='\t', comments='#')
F = M[:,0:setup['n_obj']]
return F
def func_load_history(fname, problem):
H = np.genfromtxt(fname,delimiter='\t', comments='#')[:, 0:n_obj]
H = np.reshape(H, (n_gen, pop_size, problem.n_obj))
return H
cnsga2 = load_results("cnsga2",func_load, func_load_history)
pynsga2 = load_results("pynsga2")
df = pd.DataFrame(cnsga2 + pynsga2,columns=['problem', 'algorithm' , 'run', 'igd', 'hv', 'fname'])
metric = 'igd'
for name in problems:
_df = df[df['problem'] == name]
val = _df.groupby(['algorithm']).agg({metric: ['median', 'min','max','mean', 'std']})
print(val)
_df.boxplot(column=metric, by='algorithm')
plt.title(name)
plt.suptitle("")
plt.show()
print("----------------------------------------------")