简体   繁体   English

scipy.optimize.linprog 等式约束不满足

[英]scipy.optimize.linprog equality constraint are not satisfied

I am currently trying to solve a LP optimization problem using scipy.optimize.linprog.我目前正在尝试使用 scipy.optimize.linprog 解决 LP 优化问题。

I triple checked the whole code but I am not able to understand why the only equality constraint of the problem is not satisfied.我对整个代码进行了三次检查,但我无法理解为什么不满足问题的唯一等式约束。

import numpy as np
import scipy.optimize as opt
import scipy.stats as sts
import pandas as pd
    
def problems(a, t, alpha):
    #
    y = []
    
    for i in a.columns:
        loc, scale = sts.gumbel_l.fit(a[i])
        y.append(sts.gumbel_l.rvs(loc, scale, t))
    
    obj = [1] + [0 for x in range(len(a.columns))] + [(1 / (1 - alpha) * t) for x in range(t)]
    
    ttt = np.zeros((t, t))
    np.fill_diagonal(ttt, 1.)
    
    aub = np.concatenate((np.asmatrix([1 for x in range(t)]).T, np.asmatrix(y).T, ttt), axis = 1)
    aeq = np.asmatrix(([0] + [1 for x in range(len(a.columns))] + [0 for x in range(t)]))
    
    bub = [0. for x in range(t)]
    beq = [1.]
    
    res = opt.linprog(
        c = obj, 
        A_ub = aub,
        b_ub = bub,
        A_eq = aeq,
        b_eq = beq,
    )

    return res['x']
    
if __name__ == "__main__":
    np.random.seed(2)
    a = pd.DataFrame(np.random.rand(400, 3))
    res = problems(a, t = 100, alpha = 0.95)
    print(res.round(3))

Please note that this Is a three variable optimization problem:请注意,这是一个三变量优化问题:

obj = [1] + [0 for x in range(len(a.columns))] + [(1 / (1 - alpha) * t) for x in range(t)]

Specifically I need that after the optimization the sum of the elements of the second variable that needs to be determined (which is an array of len(a.columns)) is equale to 1. Hence the equality constraint defined by aeq and bee`.具体来说,我需要在优化之后,需要确定的第二个变量的元素之和(它是len(a.columns)) is equale to 1. Hence the equality constraint defined by aeq and bee 定义的等式约束。

Unfortunately the result is the following (2.013 + 1.573 + 2.413 is not equal to 1.):不幸的是,结果如下(2.013 + 1.573 + 2.413 不等于 1。):

[0.661 2.013 1.573 2.413 1.995 2.038 0.695 1.975 1.056 2.202 1.728 1.494
 1.214 1.419 1.382 1.632 1.36  1.004 1.283 2.314 2.286 1.651 0.929 1.834
 1.663 1.346 2.34  2.09  1.831 1.886 1.845 2.222 2.318 2.064 1.242 1.632
 0.33  1.77  0.687 0.618 0.07  1.148 1.939 2.08  2.18  1.143 2.051 0.616
 1.396 1.027 0.978 1.014 1.038 1.62  0.999 2.103 1.639 2.198 2.445 2.171
 0.862 1.556 0.608 1.465 1.372 2.143 0.738 1.99  1.525 1.344 1.798 2.111
 1.168 2.331 2.082 1.248 2.27  1.738 1.936 1.957 0.661 1.783 1.509 0.522
 1.785 2.095 1.537 2.229 1.065 1.973 2.193 0.132 1.997 0.984 1.359 1.761
 1.809 0.247 1.893 2.04  1.873 2.019 2.124 2.107]

The documentation asks for: 文档要求:

A_ub: 2-D array, optional A_ub:二维数组,可选

A_eq: 2-D array, optional A_eq:二维数组,可选

You use np.matrix -> read the note!您使用np.matrix -> 阅读注释!

Base your code on numpy's array-type将代码基于 numpy 的数组类型

(which is more than just copy-paste -> this also somewhat indicates, what possibly could go wrong inside of linprog). (这不仅仅是复制粘贴-> 这也有点表明,go 可能在 linprog 内部出错)。

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

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