SCIPY-建立约束,而不必单独列出每个变量 [英] SCIPY - building constraints without listing each variable separately

查看:102
本文介绍了SCIPY-建立约束,而不必单独列出每个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SCIPY优化存储设施,该存储设施使用1年交易期限的远期价格.可以根据每月的价差(例如3月21日对5月20日的价差)足够高以支付可变的运营成本,从该设施中注入和提取天然气.所附图片表示问题(此处的值是任意的,与代码中的值不匹配;图片仅用于概念)

I am using SCIPY to optimize a storage facility that uses forward prices for a deal term of 1 year. Gas can be injected and withdrawn from this facility, based on monthly spreads (e.g. March 21 vs May 20 spread) being high enough to cover the variable cost of operation. The attached picture represents the problem (the values here are arbitrary, don't match the values in the code; pic is just for concept)

蓝色的单元格是不断变化的单元格",SCIPY将进行调整以最大化利润.需要分别为每个月设置约束.尝试在SCIPY中设置这些约束时出现错误.这是该问题的可复制版本:

The cells in blue are the "changing cells", volumes that SCIPY will adjust to maximize profits. The constraints need to be set up for each month separately. I get errors when I attempt to set up these constraints in SCIPY. Here's a reproducible version of the problem:

 import numpy as np
import scipy.optimize as opt

p= np.array([4, 5, 6.65, 12]) #p = prices
pmx = np.triu(p - p[:, np.newaxis]) #pmx = price matrix, upper triangular

q =np.triu(np.ones((4,4))) # q = quantity, upper triangular

def profit(q):
    profit = -np.sum(q.flatten() * pmx.flatten())
    return profit

bnds = (0,10)
bnds = [bnds for i in q.flatten()]

def cons1(q):
    np.sum(q,axis=1) -  10

#def cons2(q):
#    np.sum(q,axis=0) -  8

#con1 = {'type':'ineq','fun':cons1}
#con2 = {'type':'ineq','fun':cons2}
cons = [con1]    # using only  1 constraint (con1) to test the model

#sol = opt.minimize(profit,q,method='SLSQP', bounds= bnds,constraints = cons)
sol = opt.minimize(profit,q,method='SLSQP', bounds= bnds)
sol

当我排除约束时,模型运行良好.当我添加约束之一时,我得到的错误是:

The model runs fine when I exclude the constraints. When I add one of the constraints, the error I get is:

AxisError: axis 1 is out of bounds for array of dimension 1

我认为这与我指定约束的方式有关……我不确定.对于约束,我确实需要确定注入和撤出并按图片所示设置约束.帮助将不胜感激.谢谢!

I think this has to do with the the way I'm specifying the constraints....I'm not sure though. For the constraints, I do need to identify injections and withdrawals and set the constraints as shown in the picture. Help would be appreciated. Thanks!

推荐答案

作为Scipy.minimize.optimize的替代方法,以下是使用

As an alternative to Scipy.minimize.optimize, here is a solution with Python gekko.

import numpy as np
import scipy.optimize as opt
from gekko import GEKKO

p= np.array([4, 5, 6.65, 12]) #p = prices
pmx = np.triu(p - p[:, np.newaxis]) #pmx = price matrix, upper triangular

m = GEKKO(remote=False)
q = m.Array(m.Var,(4,4),lb=0,ub=10)
# only upper triangular can change
for i in range(4):
    for j in range(4):
        if j<=i:
            q[i,j].upper=0 # set upper bound = 0

def profit(q):
    profit = np.sum(q.flatten() * pmx.flatten())
    return profit

for i in range(4):
    m.Equation(np.sum(q[i,:])<=10)
    m.Equation(np.sum(q[:,i])<=8)
m.Maximize(profit(q))

m.solve()

print(q)

这提供了解决方案:

[[[0.0] [2.5432017412] [3.7228765674] [3.7339217013]]
 [[0.0] [0.0] [4.2771234426] [4.2660783187]]
 [[0.0] [0.0] [0.0] [0.0]]
 [[0.0] [0.0] [0.0] [0.0]]]

这篇关于SCIPY-建立约束,而不必单独列出每个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆