简体   繁体   English

Python Gekko 中目标函数的约束

[英]Constraint on objective function in Python Gekko

Is there a way to constrain the objective function to be within a range in Python Gekko?有没有办法将目标函数限制在 Python Gekko 的范围内? I am working through the example optimization problem on the economics of a commercial fishery over a 10 year operation.我正在研究一个商业渔业超过 10 年的经营经济学的示例优化问题 The adjustable parameter is the production rate (harvest rate) of the fish.可调参数是鱼的生产速度(收获率)。 The objective function is the profit from the operation over the 10 year period.目标函数是 10 年期间的运营利润。 The optimization problem in mathematical terms is:数学上的优化问题是:

渔业经济优化

The solution and Python Gekko code are:解决方案和Python Gekko代码是:

钓鱼优化结果

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

# create GEKKO model
m = GEKKO()

# time points
n=501
m.time = np.linspace(0,10,n)

# constants
E = 1
c = 17.5
r = 0.71
k = 80.5
U_max = 20

# fishing rate
u = m.MV(value=1,lb=0,ub=1)
u.STATUS = 1
u.DCOST = 0

# fish population
x = m.Var(value=70)

# fish population balance
m.Equation(x.dt() == r*x*(1-x/k)-u*U_max)

# objective (profit)
J = m.Var(value=0)
# final objective
Jf = m.FV()
Jf.STATUS = 1
m.Connection(Jf,J,pos2='end')
m.Equation(J.dt() == (E-c/x)*u*U_max)
# maximize profit
m.Maximize(Jf)

# options
m.options.IMODE = 6  # optimal control
m.options.NODES = 3  # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)

# solve optimization problem
m.solve()

# print profit
print('Optimal Profit: ' + str(Jf.value[0]))

# plot results
plt.figure(1)
plt.subplot(2,1,1)
plt.plot(m.time,J.value,'r--',label='profit')
plt.plot(m.time[-1],Jf.value[0],'ro',markersize=10,\
         label='final profit = '+str(Jf.value[0]))
plt.plot(m.time,x.value,'b-',label='fish population')
plt.ylabel('Value')
plt.legend()
plt.subplot(2,1,2)
plt.plot(m.time,u.value,'k.-',label='fishing rate')
plt.ylabel('Rate')
plt.xlabel('Time (yr)')
plt.legend()
plt.show()

One of the observations I've had in chemical manufacturing is that optimization sometimes leads to non-intuitive solutions because the optimizer (IPOPT) is going to take the process to the absolute limit to achieve even a few dollars more of profitability.我在化学制造中的观察结果之一是,优化有时会导致非直观的解决方案,因为优化器 (IPOPT) 会将过程带到绝对极限,以实现甚至多出几美元的盈利能力。 Is there a way to constrain the objective function (profit in this case) so that the business stays viable but the optimizer doesn't give a solution that is pushing the limits of the equipment (or the fish population in this case).有没有办法限制目标函数(在这种情况下是利润),以便业务保持可行,但优化器不会提供推动设备(或在这种情况下的鱼类种群)极限的解决方案。

You can set a limit on the objective by adding an upper bound such as:您可以通过添加上限来设置目标的限制,例如:

Jf = m.FV(ub=100)  # final objective

This sets an upper bound ub of 100 .这将上限ub100 Even though you can do this, there are a variety of reasons why you may not want to put a constraint on the objective.尽管您可以这样做,但您可能不想对目标施加限制的原因有很多。 One is that this could lead to an infeasible solution if the optimizer cannot achieve that limit.一是如果优化器无法达到该限制,这可能会导致不可行的解决方案。 Another is that you may still not achieve an "intuitive solution" or one that wouldn't push your equipment (such as fishing boats or your chemical manufacturing plant) to the limit in certain periods.另一个原因是,您可能仍然无法实现“直观的解决方案”,或者在某些时期内不会将您的设备(例如渔船或化学制造厂)推向极限。 As Tim mentioned in the comment, a better way is to put constraints on things that you can control such as fishing rate.正如 Tim 在评论中提到的,更好的方法是对您可以控制的事物(例如钓鱼率)施加限制。 A few parameters that you may find useful are the move suppression factor DCOST , maximum movement DMAX , and cost for fishing rate COST .您可能会发现一些有用的参数是移动抑制因子DCOST 、最大移动DMAX和钓鱼速率COST Maybe it isn't reasonable to ramp up to full fishing rate in one year and then drop to 40% for 5+ years.也许在一年内提高到全捕捞率然后在 5 年以上下降到 40% 是不合理的。 There may also be costs associated with building up the fishing fleet that would be not be reflected in your current solution.建立捕捞船队可能还会产生相关成本,而这些成本不会反映在您当前的解决方案中。

You can also set up the objective as a soft constraint between certain target values and then let other objectives such as maximize fish population be the driving force for decisions between the upper and lower profitability targets.您还可以将目标设置为某些目标值之间的软约束,然后让其他目标(例如最大化鱼类种群)作为决定盈利能力上限和下限目标的驱动力。

# objective (profit)
J = m.CV(value=0)
J.SPHI = 100; J.WSPHI = 1000
J.SPLO = 80;  J.WSPLO = 1000
m.options.CV_WGT_START = n-1 # start at n-1
J.STATUS = 1

m.Maximize(x) # maximize fish population

This gives a solution between profit limits of 80 to 100 but also maximizes the fish population.这提供了介于 80 到 100 之间的利润限制的解决方案,但也最大化了鱼类种群。

钓鱼人口

from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt

# create GEKKO model
m = GEKKO()

# time points
n=501
m.time = np.linspace(0,10,n)

# constants
E = 1
c = 17.5
r = 0.71
k = 80.5
U_max = 20

# fishing rate
u = m.MV(value=1,lb=0,ub=1)
u.STATUS = 1
u.DCOST = 0

# fish population
x = m.Var(value=70)

# fish population balance
m.Equation(x.dt() == r*x*(1-x/k)-u*U_max)

# objective (profit)
J = m.CV(value=0)
J.SPHI = 100; J.WSPHI = 1000
J.SPLO = 80;  J.WSPLO = 1000
m.options.CV_WGT_START = n-1 # start at n-1
J.STATUS = 1

m.Maximize(x) # maximize fish population

m.Equation(J.dt() == (E-c/x)*u*U_max)

# options
m.options.IMODE = 6  # optimal control
m.options.NODES = 3  # collocation nodes
m.options.SOLVER = 3 # solver (IPOPT)

# solve optimization problem
m.solve()

# print profit
print('Optimal Profit: ' + str(J.value[-1]))

# plot results
plt.figure(1)
plt.subplot(2,1,1)
plt.plot([0,10],[100,100],'k:',label='target profit range')
plt.plot([0,10],[80,80],'k:')
plt.plot(m.time,J.value,'r--',label='profit')
plt.plot(m.time[-1],J.value[-1],'ro',markersize=10,\
         label='final profit = '+str(J.value[-1]))
plt.plot(m.time,x.value,'b-',label='fish population')
plt.ylabel('Value')
plt.legend()
plt.subplot(2,1,2)
plt.plot(m.time,u.value,'k.-',label='fishing rate')
plt.ylabel('Rate')
plt.xlabel('Time (yr)')
plt.legend()
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM