简体   繁体   English

使用scipy.optimize.linprog的权重中间范围(Simplex算法)

[英]Weight Middle Range for use of scipy.optimize.linprog (Simplex algorithm)

The basic problem is given these constraints (this is a simple example, my use case follows the same format but can introduce more variables): 基本问题被赋予了这些约束(这是一个简单的示例,我的用例遵循相同的格式,但可以引入更多的变量):

x + y = C1
C2 <= x <= C3
C4 <= y <= C5

(where CX are constants, and x and y are variables), solve. (其中CX是常量,x和y是变量),求解。

Easy enough with scipy.optimize.linprog . 使用scipy.optimize.linprog很容易。 I'm able to get a technically correct result. 从技术上讲,我可以得到正确的结果。

Example. 例。 Using these specific constraints: 使用以下特定约束:

x + y = 50
5 <= x <= 65
10 <= y <= 40

We get: 我们得到:

>>> res = scipy.optimize.linprog([1,1], bounds=([5, 65], [10, 40]), A_eq=[[1,1]], b_eq=[50]  )
>>> res.x
array([ 40.,  10.])

which is technically correct. 从技术上讲这是正确的。 Notice that the 2nd variable is at its minimum. 请注意,第二个变量最小。 But due to business reasons, we would prefer something more "fair" where each variable gets to go above its min if possible, something more along the lines of (but not necessarily the same as): 但是由于商业原因,我们希望有一个更“公平”的方法,如果可能的话,每个变量都应超过其最小值,而更多的是(但不一定与)相同:

array([ 25.,  25.])

So I've been thinking about weighting the midpoints. 所以我一直在考虑加权中点。 How can I use this scipy.optimize.linprog api (or some other scipy optimization api) to modify the function being minimized such that it will give higher priorities to values closer to the midpoint of each variable range? 我如何使用此scipy.optimize.linprog api(或其他一些scipy优化api)来修改最小化的函数,从而为接近每个变量范围中点的值赋予更高的优先级?

Please see this answer as a draft to get an idea how to approach those kinds of problems. 请将此答案作为草稿,以了解如何解决此类问题。 This is certainly not the best way to do it and certainly not the most effective algorithm for this kind of problem. 这当然不是解决问题的最佳方法,也不是解决此类问题的最有效算法。


The problem here is, that you can probably not express your idea as a smooth linear target function. 这里的问题是,您可能无法将您的想法表达为平滑的线性目标函数。 You need some kind of distance measurement, which probably has to be at least quadratic in case of smooth functions. 您需要某种距离测量,在使用平滑函数的情况下,距离测量可能至少必须是二次的。

The following code adds the L2 of the x vector norm as a penalty. 以下代码将x向量范数的L2添加为惩罚。 This helps in this case, because the L2 norm is quadratic in its components and therefore prefers all components to be equally small over one larger and one smaller. 这在这种情况下会有所帮助,因为L2范数的成分是二次方的,因此,希望所有成分在一个大一点和一个小一点上都相等。

from scipy.optimize import fmin_slsqp

# equality constraints as h(x) = 0
def h(x):
    return x[0] + x[1] - 50

# inequality constraints as g(x) >= 0
def g(x):
    return [
        x[0] - 5,
        -x[0] + 65,
        x[1] - 10,
        -x[1] + 40,
    ]

# target functions
f1 = lambda x: x[0] + x[1]
f2 = lambda x: x[0] + x[1] + sum(x**2)

The results: 结果:

x = fmin_slsqp(f1, (5,45), f_eqcons=h, f_ieqcons=g)
print(x)

outputs: 输出:

Optimization terminated successfully.    (Exit mode 0)
            Current function value: 50.0
            Iterations: 1
            Function evaluations: 5
            Gradient evaluations: 1
[ 10.  40.]

And the penalized version: 和惩罚版本:

x = fmin_slsqp(f2, (5,45), f_eqcons=h, f_ieqcons=g)
print(x)

prints 版画

Optimization terminated successfully.    (Exit mode 0)
            Current function value: 1300.0
            Iterations: 3
            Function evaluations: 12
            Gradient evaluations: 3
[ 25.  25.]

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

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