Gurobi 没有最小化 Objective 函数
Gurobi is not Minimizing the Objective function
问题如下:
令 predict = [15, 15, 17, 16, 14, 15, 16, 14, 14, 22]
为数字列表。我想要一个数字序列 x = [x0,x2,x3,....,x9]
这样
|x[0] - predict[0]| + |x[1] - predict[1]| + ...+ |x[9]-predict[9]|
最低。约束是 |x[i]-x[i+1]| <= 1 for i = 0 to 9
。我在jupyter notebook的python3中写了一段代码如下:
original = [16, 15, 16, 15, 15, 14, 14, 15, 15, 15]
predict = [17, 15, 17, 16, 14, 13, 15, 14, 16, 24]
varlen = len(original)
import gurobipy as gp
from gurobipy import GRB
model = gp.Model()
I = range(varlen)
x = model.addVars(I, vtype = GRB.INTEGER, name = "x")
y = model.addVars(I, vtype = GRB.INTEGER, name = "y")
z = model.addVars(I, vtype=GRB.INTEGER, name="z")
#Adding objective function sum of z[i] = sum|y[i]| = sum |x[i]-predict[i]|
model.setObjective(gp.quicksum(z[i] for i in I), GRB.MINIMIZE)
model.addConstrs(z[i] == gp.abs_(y[i]) for i in I)
model.addConstrs(y[i] == (x[i]-predict[i]) for i in I)
#constraint |x[i] - x[i+1]| <= diff
model.addConstrs(x[i]-x[i+1] <= diff for i in range(varlen-1))
model.addConstrs(x[i]-x[i+1] >= -diff for i in range(varlen-1))
model.params.outputflag = 0
model.optimize()
temp = model.x
out = [0]*len(x)
for i in range(len(out)):
out[i] = int(temp[i])
print(out)
print(model.ObjVal)
输出:
[17, 16, 17, 18, 19, 20, 21, 22, 23, 24]
36.0
但是 x = original = [16, 15, 16, 15, 15, 14, 14, 15, 15, 15]
满足所有约束并给出 ObjVal = 17
小于 36.0
.
这意味着代码没有最小化给定的 objective 函数。我无法弄清楚代码有什么问题。我是 gurobi 的初学者,如果你们能提供帮助那就太好了。
感谢任何解决方案。
注意addVars
方法的签名:
model.addVars(*indexes, lb=0.0, ub=GRB.INFINITY, obj=0.0, vtype=GRB.CONTINUOUS, name="")
即每个变量的默认下限是 0
。因此,我们有 y[i] == x[i] - predict[i] >= 0
,这意味着 x[i]
不允许小于 predict[i]
。
通过
删除变量的下限
x = model.addVars(I, lb=-1*GRB.INFINITY, vtype = GRB.INTEGER, name = "x")
y = model.addVars(I, lb=-1*GRB.INFINITY, vtype = GRB.INTEGER, name = "y")
z = model.addVars(I, lb=-1*GRB.INFINITY, vtype = GRB.INTEGER, name = "z")
您将获得 objective 值为 11 的解。
问题如下:
令 predict = [15, 15, 17, 16, 14, 15, 16, 14, 14, 22]
为数字列表。我想要一个数字序列 x = [x0,x2,x3,....,x9]
这样
|x[0] - predict[0]| + |x[1] - predict[1]| + ...+ |x[9]-predict[9]|
最低。约束是 |x[i]-x[i+1]| <= 1 for i = 0 to 9
。我在jupyter notebook的python3中写了一段代码如下:
original = [16, 15, 16, 15, 15, 14, 14, 15, 15, 15]
predict = [17, 15, 17, 16, 14, 13, 15, 14, 16, 24]
varlen = len(original)
import gurobipy as gp
from gurobipy import GRB
model = gp.Model()
I = range(varlen)
x = model.addVars(I, vtype = GRB.INTEGER, name = "x")
y = model.addVars(I, vtype = GRB.INTEGER, name = "y")
z = model.addVars(I, vtype=GRB.INTEGER, name="z")
#Adding objective function sum of z[i] = sum|y[i]| = sum |x[i]-predict[i]|
model.setObjective(gp.quicksum(z[i] for i in I), GRB.MINIMIZE)
model.addConstrs(z[i] == gp.abs_(y[i]) for i in I)
model.addConstrs(y[i] == (x[i]-predict[i]) for i in I)
#constraint |x[i] - x[i+1]| <= diff
model.addConstrs(x[i]-x[i+1] <= diff for i in range(varlen-1))
model.addConstrs(x[i]-x[i+1] >= -diff for i in range(varlen-1))
model.params.outputflag = 0
model.optimize()
temp = model.x
out = [0]*len(x)
for i in range(len(out)):
out[i] = int(temp[i])
print(out)
print(model.ObjVal)
输出:
[17, 16, 17, 18, 19, 20, 21, 22, 23, 24]
36.0
但是 x = original = [16, 15, 16, 15, 15, 14, 14, 15, 15, 15]
满足所有约束并给出 ObjVal = 17
小于 36.0
.
这意味着代码没有最小化给定的 objective 函数。我无法弄清楚代码有什么问题。我是 gurobi 的初学者,如果你们能提供帮助那就太好了。 感谢任何解决方案。
注意addVars
方法的签名:
model.addVars(*indexes, lb=0.0, ub=GRB.INFINITY, obj=0.0, vtype=GRB.CONTINUOUS, name="")
即每个变量的默认下限是 0
。因此,我们有 y[i] == x[i] - predict[i] >= 0
,这意味着 x[i]
不允许小于 predict[i]
。
通过
删除变量的下限x = model.addVars(I, lb=-1*GRB.INFINITY, vtype = GRB.INTEGER, name = "x")
y = model.addVars(I, lb=-1*GRB.INFINITY, vtype = GRB.INTEGER, name = "y")
z = model.addVars(I, lb=-1*GRB.INFINITY, vtype = GRB.INTEGER, name = "z")
您将获得 objective 值为 11 的解。