我试图在我的目标函数中加入“大 M” 惩罚。
我想使用入口x 向量值作为函数中的入口值。该函数的返回值最初采用一个固定的最大值,我想避免在给定 x 值的情况下返回高于固定值的值的解决方案。
最好的方法是什么?
我用一种方法写了一些代码,但我不知道这是否是一个好方法
#from calculator import calculateConcentration
'GEKKO MODELING'
from gekko import GEKKO
m = GEKKO()
m.options.SOLVER=1 # APOPT is an MINLP solver
a_max = 30
# Initialize variables
x = []
x1 = m.Var(value=20,lb=20, ub=6555) #integer=True
x2 = m.Var(value=0,lb=0,ub=10000) #integer=True
x3 = m.sos1([30, 42, 45, 55])
x = [x1, x2, x3]
# Equations
m.Equation((x1 * x2* x3) * 10 ** (-6)>=50)
def fun(x):
return 44440 + ((np.pi * x[0] * x[1] * x[2]) * 10 ** (-4))**0.613 #+ penalty(x)
#def penalty(x):
# a = calculateConcentration(x)
# if (a>a_max):
# return 10**10
# else:
# return 0
x = [400,300,19]
'GEKKO Optimization'
m.Obj(fun(x))
m.solve(disp=False) # Solve
print('Results')
print('x1: ' + str(x1.value))
print('x2: ' + str(x2.value))
print('x3: ' + str(x3.value))
print('Objective: ' + str(m.options.objfcnval))