简体   繁体   English

通过纸浆最大化决策变量的标准差

[英]Maximising standard deviation of decision variables via PuLP

I'm new to using PuLP and am trying to programme the standard deviation as my objective function in an optimisation problem.我是使用 PuLP 的新手,我正在尝试将标准偏差编程为优化问题中的目标函数。 I've read this answer and, though I know it's related, am having trouble applying it to my specific situation.我已经阅读了这个答案,虽然我知道它是相关的,但我无法将其应用于我的具体情况。

I'm trying to solve the following optimisation problem: maximise the standard deviation for a set of 3 decision variables with an associated weight vector of [0.25, 0.40, and 0.35].我正在尝试解决以下优化问题:最大化一组 3 个决策变量的标准差,相关权重向量为 [0.25、0.40 和 0.35]。 The constraints I have are that each decision variable should range between 0.5 and 2.0.我的限制是每个决策变量应该在 0.5 到 2.0 之间。 (This is a simplified example; in practice, I will have a much larger set of decision variables and a larger corresponding weight vector). (这是一个简化的示例;在实践中,我将拥有更大的决策变量集和更大的相应权重向量)。

So far, my code is the following:到目前为止,我的代码如下:

from pulp import LpMaximize, LpProblem, LpVariable

# Create the model
model = LpProblem(name="max_stdev", sense=LpMaximize)

# Define the decision variables
x = {i: LpVariable(name=f"x{i}", lowBound=0.5, upBound=2.0) for i in range(3)}

# Add the constraints to the model
model += (0.25*x[0] + 0.40*x[1] + 0.35*x[2] == 1, "weight_constraint")

# Add the objective function to the model, which should be the standard deviation of the x vector
model += ??

# Solve the problem
status = model.solve()

I'm just not sure how to apply the standard deviation formula in the form of an objective function (see above).我只是不确定如何以目标函数的形式应用标准偏差公式(见上文)。 Again, I know this answer could be useful, but am just not sure how to make this work.同样,我知道这个答案可能很有用,但我只是不确定如何使它工作。

Many thanks in advance for any help!非常感谢您的帮助!

It may not be so easy to do this with Pulp.使用 Pulp 做到这一点可能并不容易。 It only accepts linear models and this is inherently non-linear and non-convex.它只接受线性模型,这本质上是非线性和非凸的。 Using a non-convex quadratic solver, we can just maximize使用非凸二次求解器,我们可以最大化

sum(i, (x[i]-μ)^2)

This gives:这给出了:

----     30 VARIABLE x.L  

i1 0.500,    i2 0.500,    i3 1.929


----     35 VARIABLE z.L                   =        1.361  obj
            VARIABLE mu.L                  =        0.976  mean
            PARAMETER stdev                =        0.825  standard deviation

Possible solvers include Cplex, Gurobi, Baron, Antigone.可能的求解器包括 Cplex、Gurobi、Baron、Antigone。

It may be possible to replace the squared deviation objective by some absolute value term.可以用一些绝对值项代替平方偏差目标。 But this will be messy as the problem is non-convex.但这会很混乱,因为问题是非凸的。 That would require some extra binary variables.这将需要一些额外的二进制变量。 Something along the lines of:类似于以下内容:

   max sum(i, splus[i] + smin[i])
       μ = sum(i, x[i])/n
       splus[i] - smin[i] = x(i)-μ
       splus[i] ≤ b[i]*M
       smin[i]  ≤ (1-b[i])*M 
       0.25*x[0] + 0.40*x[1] + 0.35*x[2] = 1
       smin[i],splus[i] ≥ 0
       b[i] ∈ {0,1}
       x[i] ∈ [0.5,2]
       M = 2-0.5

I get the same results for this example:对于这个例子,我得到了相同的结果:

----     85 VARIABLE x.L  

i1 0.500,    i2 0.500,    i3 1.929


----     85 VARIABLE z.L                   =        1.905  obj
            VARIABLE mu.L                  =        0.976  mean
            PARAMETER stdev                =        0.825  standard deviation
     

(Usually the solution will not be exactly the same). (通常解决方案不会完全相同)。

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

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