Skip to content

Commit

Permalink
pep8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
samgdotson committed Jan 21, 2025
1 parent 145f752 commit 98a3131
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 201 deletions.
47 changes: 23 additions & 24 deletions analysis/scripts/farthest_first_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

from osier import n_mga
from osier.utils import *
from osier import distance_matrix
from osier import farthest_first
from osier import distance_matrix
from osier import farthest_first
from osier import check_if_interior

mpl.use("pgf")
Expand All @@ -35,7 +35,6 @@
plt.rcParams['font.family'] = "serif"



if __name__ == "__main__":
problem = get_problem("bnh")

Expand All @@ -44,31 +43,31 @@
algorithm = NSGA2(pop_size=pop_size)

res = minimize(problem,
algorithm,
('n_gen', n_gen),
seed=1,
verbose=False,
save_history=True
)
algorithm,
('n_gen', n_gen),
seed=1,
verbose=False,
save_history=True
)

F = problem.pareto_front()
a = min(F[:,0])
b = max(F[:,0])
f1 = F[:,0]
f2 = F[:,1]
a = min(F[:, 0])
b = max(F[:, 0])
f1 = F[:, 0]
f2 = F[:, 1]
shift = 0.75
slack = 0.2
alpha = 0.5
F1 = f1 * (1+slack)
F2 = f2 * (1+slack)
X_hist = np.array([history.pop.get("X")
for history in res.history]).reshape(n_gen*pop_size,2)
F_hist = np.array([history.pop.get("F")
for history in res.history]).reshape(n_gen*pop_size,2)
F1 = f1 * (1 + slack)
F2 = f2 * (1 + slack)
X_hist = np.array([history.pop.get("X")
for history in res.history]).reshape(n_gen * pop_size, 2)
F_hist = np.array([history.pop.get("F")
for history in res.history]).reshape(n_gen * pop_size, 2)

slack_front = np.c_[F1,F2]
int_pts = check_if_interior(points=F_hist,
par_front=F,
slack_front = np.c_[F1, F2]
int_pts = check_if_interior(points=F_hist,
par_front=F,
slack_front=slack_front)
X_int = X_hist[int_pts]
F_int = F_hist[int_pts]
Expand All @@ -79,4 +78,4 @@
idxs = farthest_first(X=X_int, D=D, n_points=n_pts, seed=45)

F_select = F_int[idxs]
X_select = X_int[idxs]
X_select = X_int[idxs]
40 changes: 23 additions & 17 deletions analysis/scripts/mga_fft_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@

PF = problem.pareto_front()
F = res.F
a = min(F[:,0])
b = max(F[:,0])
f1 = PF[:,0]
f2 = PF[:,1]
a = min(F[:, 0])
b = max(F[:, 0])
f1 = PF[:, 0]
f2 = PF[:, 1]
slack = 0.2
alpha = 0.5
F1 = f1 * (1+slack)
F2 = f2 * (1+slack)
F1 = f1 * (1 + slack)
F2 = f2 * (1 + slack)

X_hist = np.array([history.pop.get("X")
for history in res.history]).reshape(n_gen * pop_size, 2)
Expand Down Expand Up @@ -85,43 +85,49 @@


cmap_name = 'tab10'
color1=mcp.gen_color(cmap=cmap_name,n=n_pts)
color1 = mcp.gen_color(cmap=cmap_name, n=n_pts)
cmap = plt.get_cmap(cmap_name, n_pts)

# with plt.style.context('dark_background'):
fig, ax = plt.subplots(1,2, figsize=(14,6))
fig, ax = plt.subplots(1, 2, figsize=(14, 6))

ax[0].set_title("Objective Space", fontsize=16)
ax[0].scatter(F_hist[:n_hist,0], F_hist[:n_hist,1], facecolor="none", edgecolor="lightgray", alpha=1,label='tested points')
ax[0].scatter(res.F[:,0], res.F[:,1], facecolor="none", edgecolor="red", label='optimal points')
ax[0].scatter(F_hist[:n_hist, 0], F_hist[:n_hist, 1], facecolor="none",
edgecolor="lightgray", alpha=1, label='tested points')
ax[0].scatter(res.F[:, 0], res.F[:, 1], facecolor="none",
edgecolor="red", label='optimal points')
# ax[0].plot(PF[:,0], PF[:,1], color="g", alpha=1, label='Pareto front', lw=2)
ax[0].scatter(F_select[:,0], F_select[:,1], c=color1, s=80, label='selected points')
ax[0].scatter(F_select[:, 0], F_select[:, 1],
c=color1, s=80, label='selected points')

ax[0].fill(np.append(f1, F1[::-1]), np.append(f2, F2[::-1]), 'lightgrey', alpha=0.3, label="Near-optimal space")
ax[0].fill(np.append(f1, F1[::-1]), np.append(f2, F2[::-1]),
'lightgrey', alpha=0.3, label="Near-optimal space")
ax[0].legend(fontsize=14)


ax[1].set_title("Design Space", fontsize=16)
ax[1].scatter(X_int[:n_hist,0], X_int[:n_hist,1], facecolor="none", edgecolor="lightgray", alpha=1, label='tested points')
ax[1].scatter(res.X[:,0], res.X[:,1], facecolor="none", edgecolor="red", label='optimal points')
ax[1].scatter(X_int[:n_hist, 0], X_int[:n_hist, 1], facecolor="none",
edgecolor="lightgray", alpha=1, label='tested points')
ax[1].scatter(res.X[:, 0], res.X[:, 1], facecolor="none",
edgecolor="red", label='optimal points')
ax[0].tick_params(labelsize=14)
ax[1].tick_params(labelsize=14)
ax[1].set_xlim(0, 5)
ax[1].set_ylim(0, 3)
ax[0].set_xlim(0, 160)
ax[0].set_ylim(min(res.F[:,1]), 60)
ax[0].set_ylim(min(res.F[:, 1]), 60)
# ax[1].legend(loc='upper left')

style = "Simple, tail_width=0.5, head_width=4, head_length=8"
arrows = []
prev = X_select[0]
for i, (c, (x, y)) in enumerate(zip(color1,X_select)):
for i, (c, (x, y)) in enumerate(zip(color1, X_select)):
ax[1].scatter(x, y, color=c, s=100)
if i == 0:
pass
else:
kw = dict(arrowstyle=style, color=c, linewidth=3)
curr = (x,y)
curr = (x, y)
arrows.append(patches.FancyArrowPatch(prev, curr, **kw))
prev = curr

Expand Down
26 changes: 13 additions & 13 deletions analysis/scripts/pareto_3D.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pymoo.util.ref_dirs import get_reference_directions
from pymoo.problems import get_problem
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
Expand All @@ -12,8 +14,6 @@
plt.rcParams['savefig.bbox'] = 'tight'
plt.rcParams['font.family'] = "serif"

from pymoo.problems import get_problem
from pymoo.util.ref_dirs import get_reference_directions

if __name__ == "__main__":

Expand All @@ -22,50 +22,50 @@
pf = get_problem("mw8").pareto_front(ref_dirs)
slack = 0.1
N = 1000
pf_slack = pf*(1+slack)
pf_slack = pf * (1 + slack)
R = np.random.rand(N, 3)
int_pts = []
for p in R:
cond1 = np.any((p < pf_slack).sum(axis=1)==3)
cond2 = np.any((p > pf).sum(axis=1)==3)
cond1 = np.any((p < pf_slack).sum(axis=1) == 3)
cond2 = np.any((p > pf).sum(axis=1) == 3)
if cond1 and cond2:
int_pts.append(p)

fig = plt.figure(figsize=(12,6))
fig = plt.figure(figsize=(12, 6))
ax = fig.add_subplot(132, projection="3d")
ax2 = fig.add_subplot(133,projection="3d")
ax3= fig.add_subplot(131,projection="3d")
ax2 = fig.add_subplot(133, projection="3d")
ax3 = fig.add_subplot(131, projection="3d")
pt_size = 30
pt_color = "tab:red"
pf_color = "tab:blue"

ax.plot_trisurf(*zip(*pf), alpha=1, color=pf_color)
ax.scatter3D(*zip(*np.array(int_pts)), facecolor=pt_color, s=pt_size)
ax.plot_trisurf(*zip(*pf_slack),color='tab:green', alpha=0.2)
ax.plot_trisurf(*zip(*pf_slack), color='tab:green', alpha=0.2)
ax.view_init(azim=45, elev=45)
ax.set_xlabel('f1', fontsize=14, labelpad=2)
ax.set_ylabel('f2', fontsize=14, labelpad=2)
ax.set_zlabel('f3', fontsize=14, labelpad=2)

ax3.plot_trisurf(*zip(*pf), alpha=1, color=pf_color)
ax3.scatter3D(*zip(*np.array(int_pts)), facecolor=pt_color, s=pt_size)
ax3.plot_trisurf(*zip(*pf_slack),color='tab:green', alpha=1)
ax3.plot_trisurf(*zip(*pf_slack), color='tab:green', alpha=1)
ax3.view_init(azim=45, elev=45)
ax3.set_xlabel('f1', fontsize=14, labelpad=2)
ax3.set_ylabel('f2', fontsize=14, labelpad=2)
ax3.set_zlabel('f3', fontsize=14, labelpad=2)

ax2.plot_trisurf(*zip(*pf), alpha=1, color=pf_color)
ax2.scatter3D(*zip(*np.array(int_pts)), facecolor=pt_color, s=pt_size)
ax2.plot_trisurf(*zip(*pf_slack),color='tab:green', alpha=0.2)
ax2.plot_trisurf(*zip(*pf_slack), color='tab:green', alpha=0.2)

ax2.view_init(azim=225, elev=-45)

ax2.set_xlabel('f1', fontsize=14, labelpad=2)
ax2.set_ylabel('f2', fontsize=14, labelpad=2)
ax2.set_zlabel('f3', fontsize=14, labelpad=2)

# turn off tick labels
# turn off tick labels
ax.axes.xaxis.set_ticklabels([])
ax.axes.yaxis.set_ticklabels([])
ax.axes.zaxis.set_ticklabels([])
Expand All @@ -79,4 +79,4 @@
ax3.axes.zaxis.set_ticklabels([])

plt.tight_layout()
plt.savefig("../docs/figures/3d-mga-paretofront.pgf")
plt.savefig("../docs/figures/3d-mga-paretofront.pgf")
88 changes: 45 additions & 43 deletions analysis/scripts/pareto_front.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from pymoo.problems import get_problem
import pandas as pd
import seaborn as sb
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import patches
from scipy.optimize import nnls
from scipy.optimize import curve_fit

mpl.use("pgf")
plt.rcParams['pgf.texsystem'] = 'pdflatex'
Expand All @@ -14,78 +15,77 @@
plt.rcParams['savefig.dpi'] = 600
plt.rcParams['savefig.bbox'] = 'tight'
plt.rcParams['font.family'] = "serif"
import seaborn as sb
import pandas as pd

from pymoo.problems import get_problem
from pymoo.util.plotting import plot
from pymoo.visualization.scatter import Scatter

if __name__ == "__main__":
# F = get_problem("truss2d").pareto_front()
F = get_problem("bnh").pareto_front()
a = min(F[:,0])
b = max(F[:,0])
f1 = F[:,0]
f2 = F[:,1]
a = min(F[:, 0])
b = max(F[:, 0])
f1 = F[:, 0]
f2 = F[:, 1]
shift = 0.75


"""
Truss 2D example plot
"""
fig, ax = plt.subplots(figsize=(8,6), facecolor='w', edgecolor='k')
ax.scatter(F[:,0], F[:,1], label="Solutions", facecolor='none', edgecolor='r')
ax.plot(F[:,0], F[:,1], label="Pareto-front", color='k', lw=2.5)
fig, ax = plt.subplots(figsize=(8, 6), facecolor='w', edgecolor='k')
ax.scatter(F[:, 0], F[:, 1], label="Solutions",
facecolor='none', edgecolor='r')
ax.plot(F[:, 0], F[:, 1], label="Pareto-front", color='k', lw=2.5)
ax.set_xlabel('f1', fontsize=14)
ax.set_ylabel('f2', fontsize=14)
ax.fill_between(f1, f2, alpha=0.2, label='Infeasible Space', color='gray')
ax.fill_between(f1, f2, max(F[:,1]), alpha=0.2, label='Feasible Space', color='tab:blue')
ax.fill_between(f1, f2, max(F[:, 1]), alpha=0.2,
label='Feasible Space', color='tab:blue')

ax.set_xlim(a,b)
ax.set_ylim(min(F[:,1]),max(F[:,1]))
ax.scatter((2-shift)*min(F[:,0]),(2-shift)*min(F[:,1]), label='Ideal', marker="*", s=300, color='tab:blue')
ax.scatter(0.95*max(F[:,0]),0.95*max(F[:,1]), label='Nadir', marker="s", s=200, color='tab:orange')
ax.set_xlim(a, b)
ax.set_ylim(min(F[:, 1]), max(F[:, 1]))
ax.scatter((2 -
shift) *
min(F[:, 0]), (2 -
shift) *
min(F[:, 1]), label='Ideal', marker="*", s=300, color='tab:blue')
ax.scatter(0.95 * max(F[:, 0]), 0.95 * max(F[:, 1]),
label='Nadir', marker="s", s=200, color='tab:orange')
ax.legend(fontsize=14)
plt.savefig("../docs/figures/truss2d_pareto.pgf")



"""
Pareto Near-Optimal Space
"""
slack = 0.2
alpha = 0.5
F1 = f1 * (1+slack)
F2 = f2 * (1+slack)
fig, ax = plt.subplots(figsize=(8,6), facecolor='w', edgecolor='k')
subopt = ax.plot(F1, F2, label="", color='lightgray',alpha=alpha)
opt=ax.plot(f1, f2, label="Pareto-front", color='k', lw=2.5)
F1 = f1 * (1 + slack)
F2 = f2 * (1 + slack)
fig, ax = plt.subplots(figsize=(8, 6), facecolor='w', edgecolor='k')
subopt = ax.plot(F1, F2, label="", color='lightgray', alpha=alpha)
opt = ax.plot(f1, f2, label="Pareto-front", color='k', lw=2.5)
ax.set_xlabel('f1', fontsize=14)
ax.set_ylabel('f2', fontsize=14)
plt.fill(np.append(f1, F1[::-1]), np.append(f2, F2[::-1]), 'lightgrey', alpha=alpha, label="Near-optimal space")
plt.fill(np.append(f1, F1[::-1]), np.append(f2, F2[::-1]),
'lightgrey', alpha=alpha, label="Near-optimal space")

ax.set_xlim(min(F1),max(F1))
ax.set_ylim(min(F2),max(F2))
ax.set_xlim(min(F1), max(F1))
ax.set_ylim(min(F2), max(F2))
ax.legend(fontsize=14)
plt.savefig("../docs/figures/near-optimal-pareto.pgf")


"""
Visualize interior points
"""
F3 = F*(1+slack)
F3 = F * (1 + slack)

rng = np.random.default_rng(seed=1234)
R = rng.uniform(size=(1000,2))
R = rng.uniform(size=(1000, 2))
# for Truss2d
# R[:,1] = R[:,1]*15e4
# R[:,0] = R[:,0]*8e-2
# R_sub = R[(R[:,1] < 12e4) & (R[:,0] < 0.06)]
# for BNH
R[:,1] = R[:,1]*65
R[:,0] = R[:,0]*165
R_sub = R[(R[:,1] < 65) & (R[:,0] < 165)]
R[:, 1] = R[:, 1] * 65
R[:, 0] = R[:, 0] * 165
R_sub = R[(R[:, 1] < 65) & (R[:, 0] < 165)]

interior_pts = []
for p in R_sub:
Expand All @@ -94,17 +94,19 @@
if cond_1 and cond_2:
interior_pts.append(p)

fig, ax = plt.subplots(figsize=(8,6))
fig, ax = plt.subplots(figsize=(8, 6))
ax.plot(*zip(*F), color='black', lw=3, label='Pareto Front')
ax.plot(*zip(*F3), color='black', lw=1, alpha=0.2)
ax.scatter(*zip(*R_sub), c='tab:blue', s=3, label='Tested points')
ax.scatter(*zip(*np.array(interior_pts)), c='tab:red', s=20, label="Alternative solutions")
ax.fill(np.append(F[:,0], F3[:,0][::-1]), np.append(F[:,1],F3[:,1][::-1]), alpha=0.2, color='gray')
ax.fill_between(f1*0.98, f2*0.98, alpha=1, color='w')
ax.set_xlim(min(F1),max(F1))
ax.set_ylim(min(F2),max(F2))
ax.scatter(*zip(*np.array(interior_pts)), c='tab:red',
s=20, label="Alternative solutions")
ax.fill(np.append(F[:, 0], F3[:, 0][::-1]),
np.append(F[:, 1], F3[:, 1][::-1]), alpha=0.2, color='gray')
ax.fill_between(f1 * 0.98, f2 * 0.98, alpha=1, color='w')
ax.set_xlim(min(F1), max(F1))
ax.set_ylim(min(F2), max(F2))
ax.set_xlabel('f1', fontsize=14)
ax.set_ylabel('f2', fontsize=14)
ax.legend(fontsize=14, shadow=True, loc='upper right')
ax.tick_params(axis='both', which='major', labelsize=14)
plt.savefig("../docs/figures/nd-mga-paretofront.pgf")
plt.savefig("../docs/figures/nd-mga-paretofront.pgf")
Loading

0 comments on commit 98a3131

Please sign in to comment.