我正在尝试使用具有线性约束的粒子群优化(PSO)算法来优化我的问题,到目前为止,该算法提供了以下错误, 如果还有其他首选算法可以解决我在python中的问题,我非常欢迎,我尝试过 COBYLA 和 SLSQP ,这些算法无法提供适当的解决方案
我正在PC上使用spyder for python 3.6运行,我已经从anaconda提示符
导入了 psopy代码如下 参数
import numpy as np
import psopy
from scipy.optimize import rosen
#from scipy.optimize import rosen, rosen_der
#from ipopt import minimize_ipopt
n = 12
x0 = np.zeros(n)
x=np.array (n)
#Factors for the third party in the supply chain%%
U_Demand = 12000#normrnd(mn,std)%Monte carlo simulation mean 12000 unit%
alpha = 0.1#Percentage of rate of return of products from third party%
lamda = alpha * U_Demand
miu =0.1#Probability of havieng a returned product in a good working condition%
gamma=0.7#Probability of having a rerurned part after disassembly with good working condition%
Q_TP=(lamda*(miu))+(lamda*(1-miu)*gamma)#Quantity of the third party%
std = 200 # 10000 runslamda and %
#for daikin australia
pd_1 = 600#round(random('Normal',600,24.49));q
pd_2 = 60#round(random('Normal',60,7.749));
Z_var = U_Demand - pd_1-pd_2
#Transportation Costs#
TS_s = 5000 #Transportation cost for the supplier(From alexandria road to downtown cairo)%
TS_m = 5000 #Transportation cost for the manufacturer(Assumed to be almost fixed due to practicallity)%
TS_d = 5000 #Transportation cost for the distributer%
TS_rt = 5000 #Transportation cost for the retailer%
TS_tp = 5000 #Transportation cost for the third party%
#collection Costs%%
C_tp = 5.1 #collection cost of recovering used product from the customer%
#facility opening Costs%%
F_rt = 10000000 #facility opening cost for the recovery center(Assumed to be 10 million Egyptian pound)%
#Ordering Costs%%
S_s = 5.1
S_ms = 58.956
S_m1 = 700
S_m2 = 800
S_m3 = 850
S_d = 173.4
S_r = 204
S_tp = 42.5
#Holding Costs%%
H_s = 50.126
H_ms = 589.56
H_m = 1473.9
H_dr = 1734
H_rt = 2040
H_tp = 425.9571
#Production Rates%%
P_m1=200 #Production Rates assumed to be 200 unit per day for the power plant %%
P_m2=210
P_m3=220
#U_Demand = 400000 #Demand rate is asumed to be 400,000 unit per month%
P_m = P_m1+P_m2+P_m3 # Production rate of the manufacuter
#i_m #conunting of manufacturer%
#i_mp
#i_d #Counting of Distributer
##Factors for the third party in the supply chain##
alpha = 0.1 #Percentage of rate of return of products from third party%
lamda =(alpha*U_Demand)
miu =0.1 #Probability of havieng a returned product in a good working condition%
gamma=0.7 #Probability of having a rerurned part after disassembly with good working condition%
Q_TP =(lamda*(miu))+(lamda*(1-miu)*gamma) #Quantity of the third party%
#Values of supplied chain quantities
n_s = 5
n_m = 1 #1:2
n_d = 1
决策变量
#input of optimized models data
#(lambda x : x[0])
def Q_rt1(x):
return x[0] # quantity of the retailer in the forward cycle
def Q_rt2(x):
return x[1] # quantity of the retailer in the forward cycle
def Q_rt3(x):
return x[2] # quantity of the retailer in the forward cycle
def Q_d1(x):
return x[3] # Quantity of the distributer
def Q_d2(x):
return x[4] # Quantity of the distributer
def Q_d3(x):
return x[5] # Quantity of the distributer
def Q_m1(x):
return x[6] # Quantity of the Manufacturer
def Q_m2(x):
return x[7] # Quantity of the Manufacturer
def Q_m3(x):
return x[8] # Quantity of the Manufacturer
def Q_s1(x):
return x[9] #Quantity of Supplied Parts
def Q_s2(x):
return x[10] #Quantity of Supplied Parts
def Q_s3(x):
return x[11] #Quantity of Supplied Parts #1:2
#Cycle time of the supply chain#
目标函数
#Cycle time of the supply chain#
def t_r(x):
return (U_Demand)/ Q_rt1(x) #cycle time of the retailer
def t_d(x):
return n_d * t_r(x) #cycle time of the Distribiter
def t_m(x):
return (n_m * n_d* t_r(x)) #cycle time of the Manufacturer
def t_s(x):
return n_s *n_m *n_d *t_r(x) #cycle time of the supplier
def t_tp(x):
return t_s(x) #cycle time of the third party
S_jfS=30 #Job Index factor number of fixed jobs at the supplier assumed to be 30 fixed employees %
S_jfM=30 #Job index for the number of fixed jobs by Mamufacturer assumed to be 30 fixed employees %
S_jfD=30 #Job index for the number of fixed jobs by distributer assumed to be 30 fixed employees%
S_jfRT=30 #Job index for the number of fixed jobs by retialer assumed to be 30 fixed employees%
S_jfTP=20 #Job index for the number of fixed jobs by third party recovery assumed to be 20 fixed employees%
S_jvS=270 #Job Index factor number of variable jobs at the supplier assumed to be 270 workers per facility%
S_jvM=270 #Job index for the number of variable jobs by Mamufacturer 270 workers per facility%
S_jvD=270 #Job index for the number of variable jobs by distributer 270 workers per facility%
S_jvRT=270#Job index for the number of variable jobs by retialer 270 workers per facility%
S_jvTP=100#Job index for the number of variable jobs by third party recovery 100 workers per facility%
S_u=20 #Employee satisfaction factor of the refurbrished parts for the third party disassembler%
S_rt=30 #Customer satisfaction factor of the refurbrished parts%
#Number of lost days at work%
S_ds=5 # Number of lost days from injuries or work damage at the suppliers / month%
S_dm=5 #Number of lost days from injuries or work damage at the manufactuer%
S_dd=5 #Number of lost days from injuries or work damage at the distributer%
S_drt=5 #Number of lost days from injuries or work damage at the retailer%
S_dtp=5 #Number of lost days from injuries or work damage at the third party%
#Enviromental Aspect of the supply chain (Emissions calculated from carbon footprint)%
E_q=10 #Emission factor from production line
E_tp=10 #Emission from wastes removal%
#Transportation emission cost%
E_ts=20 #Emission from Transportation made by the supplier%
E_tm=20 #Emission from Transportation made by the manufacturer%
E_td=20 #Emission from Transportation made by the distributer%
E_trt=20 #Emission from Transportation made by the retailer%
E_ttp=20 #Emission from Transportation made by the third party%
i_s = 1
i_ss=np.arange(i_s,n_s+1,1)
tc_s1= list(range(i_s,n_s+1))
for i_s in i_ss:
def tc_s1(x):
tc_s1 = (np.sum(i_ss/n_s))*(Q_s1(x))*(t_s(x))
return tc_s1
i_s=i_s + 1 # Adding value of Supplier integer#
def tc_s4(x):
tc_s4 = (tc_s1(x))
return tc_s4
def TC_s1(x):
TC_s1 = (S_s/(n_s*t_s(x)))+(tc_s4(x)*((H_s+TS_s)/(n_s*(t_s(x))))) #cost of the supplier for component 1%
return TC_s1
i_s= 1 #starting of the loop#
i_ss=np.arange(i_s,n_s+1,1)
#for w1 in w11:
tc_s2= list(range(i_s,n_s+1))
for i_s in i_ss:
def tc_s2(x):
tc_s2=(np.sum(i_ss/n_s))*Q_s2(x)*t_s(x) #((x(11)) +Q_TP#
return tc_s2
i_s = i_s + 1 #Adding value of Supplier integer
def tc_s5(x):
tc_s5 = (tc_s2(x))
return tc_s5
def TC_s2(x):
TC_s2 = (S_s/(n_s*t_s(x)))+(tc_s5(x)*(H_s+TS_s)/(n_s*t_s(x)))
return TC_s2
i_s=1 #starting of the loop#
tc_s3= list(range(i_s,n_s+1))
for i_s in i_ss:
def tc_s3(x):
tc_s3=(np.sum(i_ss/n_s))*Q_s3(x)*t_s(x) #((x(12)+ Q_TP))%
return tc_s3
i_s = i_s + 1 #Adding value of Supplier integer (No addition for Q_TP )%
def tc_s6(x):
tc_s6 = tc_s3(x)
return tc_s6
def TC_s3(x):
TC_s3 = (S_s/(n_s*t_s(x)))+(tc_s6(x)*((H_s+TS_s)/(n_s*(t_s(x))))) #cost of the supplier for component 3%
return TC_s3
i_m = 1 #starting of the loop#
i_mm=np.arange(i_m,n_m+1,1)
#for w1 in w11:
tc_m2= list(range(i_m,n_m+1))
for i_m in i_mm:
tc_m1 =np.arange(1,n_m,1) #Defining range with starting and ending point
def tc_m2(x):
tc_m2 = np.sum((1-((i_mm)/(n_m)))*((Q_m1(x))+Q_TP)) #Defining range with start & ending point#
return tc_m2
i_m=i_m + 1 # Adding value of manufacturer integer#
def tc_m3(x):
tc_m3=(tc_m2(x))
return tc_m3
tc_s7 =np.arange(1,n_s,1)
#Total cost of manufacturer#
tc_m = sum(tc_m1)
tc_s8 = sum(tc_s7)
def TC_m(x):
TC_m =(H_m*((0.5*(Q_m1(x)**2)*(1/P_m1))\
+(tc_m*(Q_m1(x)*t_m(x)*(1/(n_m**2))))))\
+((S_m1+TS_m)*(1/t_m(x)))+((S_ms+TS_tp)*(1/t_s(x)))\
+((H_ms/t_s(x))*(((((Q_s1(x)+Q_TP)*Q_m1(x))/P_m1))\
+(tc_s8*(((Q_s1(x))+Q_TP)/n_s)*(t_m(x)-(Q_m1(x)/P_m1)))))
return TC_m
def TC_m2(x):
TC_m2 =(H_m*((0.5*(Q_m2(x)**2)*(1/P_m2))\
+(tc_m*(Q_m2(x)*t_m(x)*(1/(n_m**2))))))\
+((S_m2+TS_m)*(1/t_m(x)))+((S_ms+TS_tp)*(1/t_s(x)))\
+((H_ms/t_s(x))*(((((Q_s2(x)+Q_TP)*Q_m2(x))/P_m2))\
+(tc_s8*(((Q_s2(x))+Q_TP)/n_s)*(t_m(x)-(Q_m2(x)/P_m2)))))
return TC_m2
def TC_m3(x):
TC_m3 =(H_m*((0.5*(Q_m3(x)**2)*(1/P_m3))\
+(tc_m*(Q_m3(x)*t_m(x)*(1/(n_m**2))))))\
+((S_m3+TS_m)*(1/t_m(x)))+((S_ms+TS_tp)*(1/t_s(x)))\
+((H_ms/t_s(x))*(((((Q_s3(x)+Q_TP)*Q_m3(x))/P_m3))\
+(tc_s8*(((Q_s3(x))+Q_TP)/n_s)*(t_m(x)-(Q_m3(x)/P_m2)))))
#Cost of the manufacturer for product 3
return TC_m3
i_d=1 #starting of the loop#
i_dd=np.arange(i_d,n_d+1,1)
#for w1 in w11:
tc_d1= list(range(i_d,n_d+1))
tc_d2= list(range(i_d,n_d+1))
tc_d3= list(range(i_d,n_d+1))
for i_d in i_dd:
def tc_d1(x):
tc_d1=np.sum(((i_dd)/(n_d))*(Q_d1(x))) #Cost of the Distributer for Product 1%%
return tc_d1
def tc_d2(x):
tc_d2=np.sum(((i_dd)/(n_d))*(Q_d2(x))) #Cost of the Distributer for Product 2%%
return tc_d2
def tc_d3(x):
tc_d3=np.sum(((i_d)/(n_d))*(Q_d3(x))) #Cost of the Distributer for Product 3%%
return tc_d3
i_d = i_d + 1
def tc_d_f(x):
tc_d_f = (tc_d1(x))+(tc_d2(x))+(tc_d3(x))
return tc_d_f
def TC_d(x):
TC_d = (H_dr*(tc_d_f(x)/n_d))+((S_d+TS_d)*(1/t_d(x))) #Total cost of the distributer of the supply chain%
return TC_d
#Total cost of retailer
def TC_rt(x):
TC_rt = (H_rt*((Q_rt1(x))/2))+ ((S_r+TS_rt)*(1/t_r(x))) #Cost of the retailer%%
return TC_rt
def TC_rt2(x):
TC_rt2=(H_rt*((Q_rt2(x))/2))+ ((S_r+TS_rt)*(1/t_r(x))) #Cost of the retailer for product 2%%
return TC_rt2
def TC_rt3(x):
TC_rt3 = (H_rt*((Q_rt3(x))/2))+ ((S_r+TS_rt)*(1/t_r(x))) #Cost of the retailer for product 3%%
return TC_rt3
#Total cost of third party recovery
def TC_tp(x):
TC_tp = ((H_tp/2)*Q_TP)+((S_tp+TS_tp)*(1/t_tp(x)))
return TC_tp
S_jfS=30 #Job Index factor number of fixed jobs at the supplier assumed to be 30 fixed employees %
S_jfM=30 #Job index for the number of fixed jobs by Mamufacturer assumed to be 30 fixed employees %
S_jfD=30 #Job index for the number of fixed jobs by distributer assumed to be 30 fixed employees%
S_jfRT=30 #Job index for the number of fixed jobs by retialer assumed to be 30 fixed employees%
S_jfTP=20 #Job index for the number of fixed jobs by third party recovery assumed to be 20 fixed employees%
S_jvS=270 #Job Index factor number of variable jobs at the supplier assumed to be 270 workers per facility%
S_jvM=270 #Job index for the number of variable jobs by Mamufacturer 270 workers per facility%
S_jvD=270 #Job index for the number of variable jobs by distributer 270 workers per facility%
S_jvRT=270#Job index for the number of variable jobs by retialer 270 workers per facility%
S_jvTP=100#Job index for the number of variable jobs by third party recovery 100 workers per facility%
S_u=20 #Employee satisfaction factor of the refurbrished parts for the third party disassembler%
S_rt=30 #Customer satisfaction factor of the refurbrished parts%
#Number of lost days at work%
S_ds=5 # Number of lost days from injuries or work damage at the suppliers / month%
S_dm=5 #Number of lost days from injuries or work damage at the manufactuer%
S_dd=5 #Number of lost days from injuries or work damage at the distributer%
S_drt=5 #Number of lost days from injuries or work damage at the retailer%
S_dtp=5 #Number of lost days from injuries or work damage at the third party%
#Enviromental Aspect of the supply chain (Emissions calculated from carbon footprint)%
E_q=10 #Emission factor from production line
E_tp=10 #Emission from wastes removal%
#Transportation emission cost%
E_ts=20 #Emission from Transportation made by the supplier%
E_tm=20 #Emission from Transportation made by the manufacturer%
E_td=20 #Emission from Transportation made by the distributer%
E_trt=20 #Emission from Transportation made by the retailer%
E_ttp=20 #Emission from Transportation made by the third party%
#Cycle time%
t_r #cycle time of the retailer
t_d #cycle time of the Distribiter
t_m #cycle time of the Manufacturer
t_s #cycle time of the supplier
t_tp #cycle time of the third party
def EQO(x):
EQO = TC_s1(x)+TC_s2(x)+TC_s3(x)+TC_m(x)+TC_m2(x)+TC_m3(x)+TC_d(x)+TC_rt(x)\
+TC_rt2(x)+TC_rt3(x)+TC_tp(x)
return EQO
#Economical aspect#
def LSC(x):
LSC =(S_jfS+S_jfM+S_jfD+S_jfRT+S_jfTP)\
+((S_jvS*Q_s1(x))+(S_jvD*Q_d1(x))+(S_jvM*Q_m1(x))\
+(S_jvRT*Q_rt1(x))+(S_jvTP*Q_TP))\
+(S_u*(U_Demand))+(S_rt*Q_rt1(x))-(S_ds*Q_s1(x))\
+(S_dd*Q_d1(x))+(S_dm*Q_m1(x))+(S_drt*Q_rt1(x))\
+(S_dtp*Q_TP)#Social aspect equation%
return LSC
def ESC(x):
ESC=(E_q*(Q_s1(x)+Q_d1(x)+Q_m1(x)+Q_rt1(x)))\
+(E_ts*(1/t_s(x)))+(E_td*(1/t_d(x)))\
+(E_tm*(1/t_m(x)))+(E_trt*(1/t_r(x)))\
+(E_ts*(1/t_tp(x)))+(E_tp*Q_TP) #Enviromental aspect
return ESC
w1 = 0.98
w2 = 0.01
w3 = 0.01
def objective(x):
return (EQO(x)*w1) + (LSC(x)*w2) + (ESC(x)*w3)
约束与界限
U_Demand = 12000#normrnd(mn,std)%Monte carlo simulation mean 12000 unit%
def constraint1(x):
return x[0]-12000
def constraint2(x):
return x[1]-12000
def constraint3(x):
return (x[2]-12000)
def constraint6(x):
return objective(x)
def constraint7(x):
return ((x[9])+Q_TP)-(n_s*x[6])
def constraint8(x):
return ((x[10])+Q_TP)-(n_s*x[7])
def constraint9(x):
return ((x[11])+Q_TP)-(n_s*x[8])
def constraint13(x):
return -(n_m*(x[3]))+x[6]
def constraint14(x):
return -(n_m*(x[4]))+x[7]
def constraint15(x):
return -(n_m*(x[5]))+x[8]
def constraint16(x):
return x[3]-(n_d*x[0])
def constraint17(x):
return x[4]- (n_d*x[1])
def constraint18(x):
return x[5]- (n_d*x[2])
def constraint19(x):
return x[0]
def constraint20(x):
return x[1]
def constraint21(x):
return x[2]
# Boundaries
lb = [1,1,1,1,1,1,1,1,1,1,1,1]
ub = [12800000000000,12800000000000,12800000000000,12800000000000,\
12800000000000,12800000000000,12800000000000,12800000000000,\
12800000000000,12800000000000,12800000000000,12800000000000,]
#b = (0,12800000000000)
#bnds = (b, b, b, b, b, b, b, b, b, b, b, b)
con1 = {'type': 'ineq', 'fun': constraint1}
con2 = {'type': 'ineq', 'fun': constraint2}
con3 = {'type': 'ineq', 'fun': constraint3}
#con4 = {'type': 'ineq', 'fun': constraint4}
#con5 = {'type': 'ineq', 'fun': constraint5}
con6 = {'type': 'ineq', 'fun': constraint6}
con7 = {'type': 'ineq', 'fun': constraint7}
con8 = {'type': 'ineq', 'fun': constraint8}
con9 = {'type': 'ineq', 'fun': constraint9}
#con10 = {'type': 'eq', 'fun': constraint10}
#con11 = {'type': 'eq', 'fun': constraint11}
#con12 = {'type': 'eq', 'fun': constraint12}
con13 = {'type': 'ineq', 'fun': constraint13}
con14 = {'type': 'ineq', 'fun': constraint14}
con15 = {'type': 'ineq', 'fun': constraint15}
con16 = {'type': 'ineq', 'fun': constraint16}
con17 = {'type': 'ineq', 'fun': constraint17}
con18 = {'type': 'ineq', 'fun': constraint18}
con19 = {'type': 'ineq', 'fun': constraint19}
con20 = {'type': 'ineq', 'fun': constraint20}
con21 = {'type': 'ineq', 'fun': constraint21}
cons = ([con1,con2,con3,con6,con7,con8,con9,con13,con14,con15,con16,con17,con18,con19,con20,con21])
运行解决方案行
from psopy import init_feasible
x0 = init_feasible(cons, low=1., high=12800000000000., shape=(1000, 2))
错误显示如下
runfile('C:/Users/omar.elfarouk/.spyder-py3/Stack overflow PSO 2.py', wdir='C:/Users/omar.elfarouk/.spyder-py3')
Traceback (most recent call last):
File "<ipython-input-1-6784bfea4830>", line 1, in <module>
runfile('C:/Users/omar.elfarouk/.spyder-py3/Stack overflow PSO 2.py', wdir='C:/Users/omar.elfarouk/.spyder-py3')
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/omar.elfarouk/.spyder-py3/Stack overflow PSO 2.py", line 390, in <module>
x0 = init_feasible(cons, low=1., high=12800000000000., shape=(1000, 2))
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\psopy\constraints.py", line 247, in init_feasible
condn = (cfunc(out).sum(1) != 0)
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\psopy\constraints.py", line 189, in confunc
return np.apply_along_axis(_check, axis=1, arr=points)
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\numpy\lib\shape_base.py", line 380, in apply_along_axis
res = asanyarray(func1d(inarr_view[ind0], *args, **kwargs))
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\psopy\constraints.py", line 167, in _check
return np.array([f(x) for f in funlist])
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\psopy\constraints.py", line 167, in <listcomp>
return np.array([f(x) for f in funlist])
File "C:\Users\omar.elfarouk\Anaconda3\lib\site-packages\psopy\constraints.py", line 155, in <lambda>
-fun(x, *args), 0)))
File "C:/Users/omar.elfarouk/.spyder-py3/Stack overflow PSO 2.py", line 324, in constraint3
return (x[2]-12000)
IndexError: index 2 is out of bounds for axis 0 with size 2