Skip to content

Instantly share code, notes, and snippets.

@DMTSource
Last active November 26, 2020 05:11
Show Gist options
  • Save DMTSource/21964425b18cd3cf1e5843c2baf62d54 to your computer and use it in GitHub Desktop.
Save DMTSource/21964425b18cd3cf1e5843c2baf62d54 to your computer and use it in GitHub Desktop.
Deap Hypervolume comparison for varying population size and number of objectives, testing for use with high dimensional NSGA III with many objectives (over 8).) Discussion here: https://groups.google.com/g/deap-users/c/XLfLa3at6pw
# Derek Tishler
# Deap Hypervolume comparison for varying population size and number of objectives,
# testing for use with high dimensional NSGA III with many objectives (over 8).)
# Discussion here, also required fitness_values.txt with 212x14 array of fitness values per ind in pop:
# https://groups.google.com/g/deap-users/c/XLfLa3at6pw
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from time import time
from deap.tools._hypervolume import hv
hv_calc_max = 10. #seconds max we allow the hv to compute before stopping scan
def perform_stress_test(pop_fits=None, ref_hyp_vals=None, hypervolume_f=None, label=None):
results = {}
results['ndim'] = []
results['hypervolume'] = []
results['elapsed'] = []
for i in range(1, len(pop_fits)):
start_time = time()
value_hypervolume = hypervolume_f(pop_fits[:i], ref_hyp_vals)
elapsed = time()-start_time
print('n_obj:%d n_pop:%d / %d\thypervolume: %g\telapsed: %g sec' % (pop_fits.shape[1],
i,
len(pop_fits),
value_hypervolume,
elapsed))
results['ndim'].append(i)
results['hypervolume'].append(value_hypervolume)
results['elapsed'].append(elapsed)
# Give up once too slow (>20 sec per hv calc)
if elapsed > hv_calc_max:
break
result_df = pd.DataFrame.from_dict(results)
plt.figure(figsize=(6,4), dpi=150)
plt.plot(result_df['ndim'].values, result_df['elapsed'].values)
plt.title(label)
plt.xlabel('n_pop / %d' %len(ttt))
plt.ylabel('hypervolume comp time (seconds)')
plt.grid()
plt.tight_layout()
plt.savefig(label+'.png')
plt.clf()
plt.close()
return result_df
ttt = np.loadtxt('fitness_values.txt')
ref_hyp = np.array([19.685825,962.5723419,5.69326178,0.043162976,
11.61159696,0.01699155,0.018175168,223965.1889,
2.61386E-06,0.016530917,1.9237E-06,2.82397E-06,
4472.93138,4.81537E-06])
if __name__ == "__main__":
### 64 bit
print('Fitness dtype(pop & ref):')
print(ttt.dtype)
print(ref_hyp.dtype)
n_obj_1 = 7
test_1_df = perform_stress_test(pop_fits = ttt[:,:n_obj_1],
ref_hyp_vals = ref_hyp[:n_obj_1],
hypervolume_f = hv.hypervolume,
label = '%d-Objective'%n_obj_1)
n_obj_2 = 10
test_2_df = perform_stress_test(pop_fits = ttt[:,:n_obj_2],
ref_hyp_vals = ref_hyp[:n_obj_2],
hypervolume_f = hv.hypervolume,
label = '%d-Objective'%n_obj_2)
test_3_df = perform_stress_test(pop_fits = ttt,
ref_hyp_vals = ref_hyp,
hypervolume_f = hv.hypervolume,
label = '%d-Objective'%len(ref_hyp))
plt.figure(figsize=(6,4), dpi=150)
plt.semilogy(test_1_df['ndim'].values, test_1_df['elapsed'].values, label='%d Obj'%n_obj_1, c='dodgerblue')
plt.semilogy(test_2_df['ndim'].values, test_2_df['elapsed'].values, label='%d Obj'%n_obj_2, c='forestgreen')
plt.semilogy(test_3_df['ndim'].values, test_3_df['elapsed'].values, label='%d Obj'%len(ref_hyp), c='orangered')
plt.legend()
plt.axhline(hv_calc_max, c='r', ls='--')
plt.title("deap hypervolume")
plt.xlabel('n_pop / %d' %len(ttt))
plt.ylabel('hypervolume comp time (seconds)')
plt.grid()
plt.tight_layout()
plt.savefig('hv_summary.png')
plt.clf()
plt.close()
@DMTSource
Copy link
Author

Output Plots:
hv_summary

7-Objective
10-Objective
14-Objective

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment