Skip to content

Commit

Permalink
'v2.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
smasky committed May 7, 2024
1 parent 3e376d4 commit 18e6f82
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 110 deletions.
2 changes: 1 addition & 1 deletion UQPyL/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . import problems, surrogates, optimization, sensibility, DoE, utility

__version__ = "2.0.1"
__version__ = "2.0.4"
__author__ = "wmtSky"

__all__=[
Expand Down
25 changes: 14 additions & 11 deletions UQPyL/optimization/asmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Optional

from .sce_ua import SCE_UA
from .pso import PSO
from ..DoE import LHS
from ..problems import Problem
from ..surrogates import Surrogate
Expand Down Expand Up @@ -46,11 +47,11 @@ def __init__(self, problem: Problem, surrogate: Surrogate,
#construct optimization problem to combine surrogate and algorithm
self.subProblem=Problem(self.surrogate.predict, self.n_input, 1, self.ub, self.lb)

def run(self,maxFE=1000, Tolerate=0.001, maxTolerateTime=50, oneStep=False):
def run(self, oneStep=False):
'''
main procedure
'''
show_process=tqdm(total=maxFE)
show_process=tqdm(total=self.maxFE)
FE=0
TT=0
n_input=self.n_input
Expand All @@ -65,8 +66,8 @@ def run(self,maxFE=1000, Tolerate=0.001, maxTolerateTime=50, oneStep=False):
XPop=self.x_init
YPop=self.y_init

fe=YPop.shape[0]
show_process.update(fe)
FE=YPop.shape[0]
show_process.update(FE)
###
idx=np.argsort(YPop, axis=0)
BestY=YPop[idx[0,0],0]
Expand All @@ -78,11 +79,11 @@ def run(self,maxFE=1000, Tolerate=0.001, maxTolerateTime=50, oneStep=False):
# history_BestY.append(BestY)

if (oneStep==False):
while fe<self.maxFE and TT<self.maxTolerateTime:
while FE<self.maxFE and TT<self.maxTolerateTime:
show_process.update(1)
# Build surrogate model
self.surrogate.fit(XPop, YPop)
res=SCE_UA(self.subProblem).run()
res=PSO(self.subProblem).run()
BestX_SM=res['best_dec']

TempY=self.evaluate(BestX_SM)
Expand All @@ -91,26 +92,28 @@ def run(self,maxFE=1000, Tolerate=0.001, maxTolerateTime=50, oneStep=False):
YPop=np.vstack((YPop,TempY))

if TempY[0,0]<BestY:
BestY=np.copy(TempY)
BestY=np.copy(TempY[0,0])
BestX=np.copy(BestX_SM)
history_best_decs[FE]=BestX
history_best_objs[FE]=BestY
else:
self.surrogate.fit(XPop, YPop)
res=SCE_UA(self.subProblem).run()
BestX_SM=res['best_dec']

TempY=self.evaluate(BestX_SM)

fe+=1
FE+=1
XPop=np.vstack((XPop,BestX_SM))
YPop=np.vstack((YPop,TempY))

if TempY[0,0]<BestY:
BestY=np.copy(TempY)
BestX=np.copy(BestX_SM)
history_best_decs[fe]=BestX
history_best_objs[fe]=BestY
history_best_decs[FE]=BestX
history_best_objs[FE]=BestY

Result={'best_dec':BestX, 'best_obj':BestY, 'history_best_decs': history_best_decs, 'history_best_objs':history_best_objs ,'FE':fe}
Result={'best_dec':BestX, 'best_obj':BestY, 'history_best_decs': history_best_decs, 'history_best_objs':history_best_objs ,'FE':FE}

return Result

Expand Down
195 changes: 98 additions & 97 deletions examples/example_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,74 +19,75 @@
import numpy as np

################1. Genetic Algorithm (GA) - Single objective################
print('################1. Genetic Algorithm (GA) - Single objective################')
from UQPyL.optimization import GA
from UQPyL.problems import Sphere
# print('################1. Genetic Algorithm (GA) - Single objective################')
# from UQPyL.optimization import GA
# from UQPyL.problems import Sphere

problem=Sphere(n_input=30, ub=100, lb=-100)
ga=GA(problem, n_samples=50)
res=ga.run()
print('Best objective:', res['best_obj'])
print('Best decisions:', res['best_dec'])
print('FE:', res['FEs'])
# problem=Sphere(n_input=30, ub=100, lb=-100)
# ga=GA(problem, n_samples=50)
# res=ga.run()
# print('Best objective:', res['best_obj'])
# print('Best decisions:', res['best_dec'])
# print('FE:', res['FEs'])

import matplotlib.pyplot as plt
FEs_objs=res['history_best_objs']
FEs=list(FEs_objs.keys())
objs=list(FEs_objs.values())
plt.plot(FEs, np.log10(objs))
# import matplotlib.pyplot as plt
# FEs_objs=res['history_best_objs']
# FEs=list(FEs_objs.keys())
# objs=list(FEs_objs.values())
# plt.plot(FEs, np.log10(objs))


################2. SCE-UA - Single objective################
print('################2. SCE-UA - Single objective################')
from UQPyL.optimization import SCE_UA
problem=Sphere(n_input=30, ub=100, lb=-100)
sce=SCE_UA(problem)
res=sce.run()
print('Best objective:', res['best_obj'])
print('Best decisions:', res['best_dec'])
print('FE:', res['FEs'])
# print('################2. SCE-UA - Single objective################')
# from UQPyL.optimization import SCE_UA
# problem=Sphere(n_input=30, ub=100, lb=-100)
# sce=SCE_UA(problem)
# res=sce.run()
# print('Best objective:', res['best_obj'])
# print('Best decisions:', res['best_dec'])
# print('FE:', res['FEs'])

objs=res['history_best_objs']
FEs_objs=res['history_best_objs']
FEs=list(FEs_objs.keys())
objs=list(FEs_objs.values())
plt.plot(FEs, np.log10(objs))
# objs=res['history_best_objs']
# FEs_objs=res['history_best_objs']
# FEs=list(FEs_objs.keys())
# objs=list(FEs_objs.values())
# plt.plot(FEs, np.log10(objs))

###############3. PSO - Single objective################
print('###############3. PSO - Single objective################')
from UQPyL.optimization import PSO
# print('###############3. PSO - Single objective################')
# from UQPyL.optimization import PSO
# from UQPyL.problems import Sphere
# problem=Sphere(n_input=30, ub=100, lb=-100)
# pso=PSO(problem, n_sample=50, w=0.5, c1=1.5, c2=1.5)
# res=pso.run()
# print('Best objective:', res['best_obj'])
# print('Best decisions:', res['best_decs'])
# print('FE:', res['FEs'])

# objs=res['history_best_objs']
# FEs_objs=res['history_best_objs']
# FEs=list(FEs_objs.keys())
# objs=list(FEs_objs.values())
# plt.plot(FEs, np.log10(objs))

# plt.title('Best Objective Over Iterations')
# plt.xlabel('Function Evbaluations')
# plt.ylabel('Best Objective (log10)')
# plt.show()
################3. ASMO - Single-objective - Surrogate-assisted################
print('################3. ASMO - Single-objective################')
from UQPyL.optimization import ASMO
from UQPyL.surrogates import RBF
from UQPyL.surrogates.rbf_kernels import Cubic
from UQPyL.problems import Sphere
problem=Sphere(n_input=30, ub=100, lb=-100)
pso=PSO(problem, n_sample=50, w=0.5, c1=1.5, c2=1.5)
res=pso.run()
rbf=RBF(kernel=Cubic())
asmo=ASMO(problem, rbf, n_init=50)
res=asmo.run(maxFE=1000)
print('Best objective:', res['best_obj'])
print('Best decisions:', res['best_decs'])
print('Best decisions:', res['best_dec'])
print('FE:', res['FEs'])

objs=res['history_best_objs']
FEs_objs=res['history_best_objs']
FEs=list(FEs_objs.keys())
objs=list(FEs_objs.values())
plt.plot(FEs, np.log10(objs))

plt.title('Best Objective Over Iterations')
plt.xlabel('Function Evbaluations')
plt.ylabel('Best Objective (log10)')
plt.show()
################3. ASMO - Single-objective - Surrogate-assisted################
# print('################3. ASMO - Single-objective################')
# from UQPyL.optimization import ASMO
# from UQPyL.surrogates import RBF
# from UQPyL.surrogates.rbf_kernels import Cubic
# problem=Sphere(n_input=30, ub=100, lb=-100)
# rbf=RBF(kernel=Cubic())
# asmo=ASMO(problem, rbf, n_init=50)
# res=asmo.run(maxFE=1000)
# print('Best objective:', res['best_obj'])
# print('Best decisions:', res['best_dec'])
# print('FE:', res['FEs'])

#############4. NSGA-II - Multi-objective#################
# print('#############4. NSGA-II - Multi-objective#################')
# from UQPyL.optimization import NSGAII
Expand All @@ -101,48 +102,48 @@
# plt.show()

#############5. MOEAD - Multi-objective#################
print('#############5. MOEAD - Multi-objective#################')
from UQPyL.optimization import MOEA_D
from UQPyL.problems import ZDT6
problem=ZDT6(n_input=30)
optimum=problem.get_PF()
moead=MOEA_D(problem, aggregation_type='TCH_M',maxFEs=50000, maxIters=1000)
_, res=moead.run()
import matplotlib.pyplot as plt
plt.scatter(res[:,0], res[:,1])
plt.plot(optimum[:,0], optimum[:,1], 'r')
plt.show()
# print('#############5. MOEAD - Multi-objective#################')
# from UQPyL.optimization import MOEA_D
# from UQPyL.problems import ZDT6
# problem=ZDT6(n_input=30)
# optimum=problem.get_PF()
# moead=MOEA_D(problem, aggregation_type='TCH_M',maxFEs=50000, maxIters=1000)
# _, res=moead.run()
# import matplotlib.pyplot as plt
# plt.scatter(res[:,0], res[:,1])
# plt.plot(optimum[:,0], optimum[:,1], 'r')
# plt.show()

#############5. MO_ASMO - Multi-objective - Surrogate-assisted#################
from UQPyL.optimization import MOASMO
from UQPyL.problems import ZDT1
from UQPyL.surrogates import RBF, KRG
from UQPyL.surrogates import Mo_Surrogates
from UQPyL.surrogates.rbf_kernels import Cubic
from UQPyL.surrogates.krg_kernels import Guass_Kernel
import matplotlib.pyplot as plt
# from UQPyL.optimization import MOASMO
# from UQPyL.problems import ZDT1
# from UQPyL.surrogates import RBF, KRG
# from UQPyL.surrogates import Mo_Surrogates
# from UQPyL.surrogates.rbf_kernels import Cubic
# from UQPyL.surrogates.krg_kernels import Guass_Kernel
# import matplotlib.pyplot as plt

rbf1=RBF(kernel=Cubic())
rbf2=RBF(kernel=Cubic())
guass1=Guass_Kernel(theta=1e-3, theta_lb=1e-5, theta_ub=1, heterogeneous=True)
guass2=Guass_Kernel(theta=1e-3, theta_lb=1e-5, theta_ub=1, heterogeneous=True)
krg1=KRG(kernel=guass1)
krg2=KRG(kernel=guass2)
mo_surr=Mo_Surrogates(2,[krg1, krg2])
problem=ZDT1(n_input=30)
mo_amso=MOASMO(problem=problem, surrogates=mo_surr, maxFEs=300, advance_infilling=False)
res=mo_amso.run()
y=res[1]
fig, axs = plt.subplots(1, 2)
axs[0].scatter(y[:,0], y[:,1], color='b')
axs[0].set_title('Advanced')
rbf1=RBF(kernel=Cubic())
rbf2=RBF(kernel=Cubic())
mo_surr=Mo_Surrogates(2,[rbf1, rbf2])
problem=ZDT1(n_input=30)
mo_amso=MOASMO(problem=problem, surrogates=mo_surr, maxFEs=300, advance_infilling=False)
res=mo_amso.run()
yy=res[1]
axs[1].scatter(yy[:,0], yy[:,1], color='r')
axs[1].set_title('Origin')
plt.show()
# rbf1=RBF(kernel=Cubic())
# rbf2=RBF(kernel=Cubic())
# guass1=Guass_Kernel(theta=1e-3, theta_lb=1e-5, theta_ub=1, heterogeneous=True)
# guass2=Guass_Kernel(theta=1e-3, theta_lb=1e-5, theta_ub=1, heterogeneous=True)
# krg1=KRG(kernel=guass1)
# krg2=KRG(kernel=guass2)
# mo_surr=Mo_Surrogates(2,[krg1, krg2])
# problem=ZDT1(n_input=30)
# mo_amso=MOASMO(problem=problem, surrogates=mo_surr, maxFEs=300, advance_infilling=False)
# res=mo_amso.run()
# y=res[1]
# fig, axs = plt.subplots(1, 2)
# axs[0].scatter(y[:,0], y[:,1], color='b')
# axs[0].set_title('Advanced')
# rbf1=RBF(kernel=Cubic())
# rbf2=RBF(kernel=Cubic())
# mo_surr=Mo_Surrogates(2,[rbf1, rbf2])
# problem=ZDT1(n_input=30)
# mo_amso=MOASMO(problem=problem, surrogates=mo_surr, maxFEs=300, advance_infilling=False)
# res=mo_amso.run()
# yy=res[1]
# axs[1].scatter(yy[:,0], yy[:,1], color='r')
# axs[1].set_title('Origin')
# plt.show()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "UQPyL"
version = "2.0.3"
version = "2.0.4"
authors = [
{name = "wmtSky", email = "[email protected]"}
]
Expand Down

0 comments on commit 18e6f82

Please sign in to comment.