简体   繁体   English

Scipy Minimize 限制变量总和?

[英]Scipy Minimize with constraint on sum of variables?

Problem问题

Take a sum of cash allocated to multiple currencies and re-allocate the cash based on a defined yield schedule.取一笔分配给多种货币的现金,并根据定义的收益率计划重新分配现金。 If a cash balance is negative, it will use the credit rate schedule (debit if positive).如果现金余额为负,它将使用贷方利率表(如果为正则借方)。 Additionally, a cash balance can't "go negative" or go further negative than it already is (I have not yet built this constraint yet).此外,现金余额不能“变成负”或比现在更负(我还没有建立这个约束)。

When I build try to optimize (as per below), the display options show no iteration taking place and return the same results.当我构建尝试优化(如下所示)时,显示选项显示没有发生迭代并返回相同的结果。 What I would expect to have happen is for all of the cash to move into the highest rate (in this case GBP), thus maximizing the total_return.我希望所有现金都进入最高利率(在这种情况下为英镑),从而使总回报最大化。

Data数据

+----------+---------+
| Currency | Balance |
+----------+---------+
| GBP      | 799181  |
+----------+---------+
| JPY      | -411087 |
+----------+---------+
| EUR      | 141234  |
+----------+---------+
| USD      | 2650988 |
+----------+---------+

+---------------+--------+--------+--------+--------+
| Currency      | GBP    | JPY    | EUR    | USD    |
+---------------+--------+--------+--------+--------+
| Rate (Credit) | .00196 | .05320 | -.0003 | 0.0    |
+---------------+--------+--------+--------+--------+
| Rate (Debit)  | .01    | .00865 | -.0076 | .00028 |
+---------------+--------+--------+--------+--------+

Current Scipy Minimize Setup当前 Scipy 最小化设置

import numpy as np
from scipy.optimize import shgo

def objective(x):             
     applied_rate = np.where((x<0), rates_sched[0], rates_sched[1])
     total_return = np.dot(applied_rate, x)
     return -total_return

def constraint1(x):
     return sum(x) - sum(cash_start) # constrain cash to starting point

cash_start = np.array([799181, -411087, 141234,2650988])

rates_sched = np.array([[.00196, .05320, -.0003, 0.0],
                        [0.01, .00865, -.0076, .00028]])


bnds = []

for value in cash_start:
    if value <0:
        bnd = (value, sum(cash_start))
    else:
        bnd = (0, sum(cash_start))
    bnds.append(bnd)

con1 = {'type': 'eq', 'fun': constraint1}
solution = shgo(objective, bounds = bnds, constraints = con1)

Update更新

I switched the scipy.optimize algorithm to shgo and I am getting closer.我将 scipy.optimize 算法切换到 shgo 并且我越来越接近了。 Current issue is that the resulting array (x) isn't respecting the constraint.当前的问题是结果数组 (x) 不遵守约束。

In the documentation of shgo I read:shgo的文档中,我读到:

Only the COBYLA and SLSQP local minimize methods currently support constraint arguments.目前只有 COBYLA 和 SLSQP 局部最小化方法支持约束参数。 If the constraints sequence used in the local optimization problem is not defined in minimizer_kwargs and a constrained method is used then the global constraints will be used.如果在minimumr_kwargs 中未定义局部优化问题中使用的约束序列并且使用了约束方法,则将使用全局约束。 (Defining a constraints sequence in minimizer_kwargs means that constraints will not be added so if equality constraints and so forth need to be added then the inequality functions in constraints need to be added to minimizer_kwargs too). (在minimumr_kwargs 中定义约束序列意味着不会添加约束,因此如果需要添加等式约束等,则也需要将约束中的不等式函数添加到minimumr_kwargs)。

Maybe that is why these lines fixes the problem for you:也许这就是这些行为您解决问题的原因:

solution2 = shgo(objective, bounds = bnds, iters=5, constraints = [con1],
                   minimizer_kwargs={'method':'SLSQP'}, 
                   sampling_method='sobol'
                )

But I am still not sure why this code works...但我仍然不确定为什么这段代码有效......

Note that performing simple local minimization:请注意,执行简单的局部最小化:

from scipy.optimize import minimize

... ...

solution = minimize(objective, x0=cash_start, method='SLSQP', 
    bounds = bnds, constraints = con1)

gives a bad result.给出了不好的结果。

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

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