Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 纸浆目标函数中ABS()的数学运算_Python 3.x_Linear Programming_Pulp - Fatal编程技术网

Python 3.x 纸浆目标函数中ABS()的数学运算

Python 3.x 纸浆目标函数中ABS()的数学运算,python-3.x,linear-programming,pulp,Python 3.x,Linear Programming,Pulp,我正试图在纸浆中构建一个LP问题,由于我是python新手,我想知道如何使用绝对值操作编写目标函数 到目前为止,我一直在使用AMPL来表示问题,现在我想将整个模型转换为Python。有人能帮我理解如何编码吗 SUM(ABS(x)) in objective function of PulP x is the decision variable which is output of the model and objective function of the model is SUM(ABS(x

我正试图在纸浆中构建一个LP问题,由于我是python新手,我想知道如何使用绝对值操作编写目标函数

到目前为止,我一直在使用AMPL来表示问题,现在我想将整个模型转换为Python。有人能帮我理解如何编码吗

SUM(ABS(x)) in objective function of PulP
x is the decision variable which is output of the model and objective function of the model is SUM(ABS(x))

欢迎来到SO!到目前为止你试过什么?你能分享你的代码吗?你想让它做什么?它目前以什么方式不工作。有关编写好的问题(可能得到好的答案)的更多建议,请参阅
minsum(abs(x))
is easy。可以应用标准公式(参见任何关于线性规划的书)
max sum(abs(x))
更难:它需要二进制变量。Hi@kabdulla:我不能共享代码,因为它包含我的雇主专有信息。然而,我正在构建一个模型,根据所看到的不足优化生产计划。因此,如果某一产品出现短缺,那么库存水平将为-ve,而对于某些产品,库存水平将为正值。为了优化库存水平,我需要目标函数作为最小和(abs(x))。我以前使用过AMPL,它工作得很好,但是我没有得到任何好的资源来理解palphi@ErwinKalvelagen的语法:谢谢你提供的信息。我得到了它。我用下面的链接来解决这个问题。谢谢!我做了同样的事情,得到了预期的结果。谢谢你的帮助,太好了。为便于将来参考,上述内容将是a的一个示例。-在一个工作表单中,允许其他人非常容易地向您显示缺失步骤的最少代码量。
from pulp import *

N = 3
x_vars = LpVariable.dicts("x",range(N))
x_vars_abs = LpVariable.dicts("x_abs",range(N))
prob = LpProblem("min_sum_abs", LpMinimize)

# OBJECTIVE
prob += lpSum(x_vars_abs)

# ABS CONSTRAINTS
for i in range(N):
    prob += x_vars_abs[i] >= x_vars[i]
    prob += x_vars_abs[i] >= -x_vars[i]

# OTHER MODEL CONSTRAINTS
prob += lpSum(x_vars) >= 2.0
prob += x_vars[0] >= x_vars[1] + 1.0
prob += x_vars[1] <= x_vars[2] - 2.0

prob.solve()

print ("Status: " + str(LpStatus[prob.status]))
print ("Objective: " + str(value(prob.objective)))

for v in prob.variables():
    print (v.name + " = " + str(v.varValue))
Status: Optimal
Objective: 2.6666667
x_0 = 0.66666667
x_1 = -0.33333333
x_2 = 1.6666667
x_abs_0 = 0.66666667
x_abs_1 = 0.33333333
x_abs_2 = 1.6666667