[英]Gurobi linear programming with Python
being so new to Gurobi and optimization. 对Gurobi和优化如此陌生。 I want to solve this a simple liner model but seems that I made some mistake that I can not detect. 我想解决这个简单的班轮模型,但似乎犯了一些我无法发现的错误。 so among 3 features which each consumes specific amount of effort to implement I want to select the ones with highest value given a specific capacity. 因此,在3个功能中,每个功能都需要花费特定的精力来实施,因此我想在给定特定容量的情况下选择具有最高价值的功能。 Feature, value, effort f1,3,2 f2,5,1 f3,1,2 capacity = 3 特征,价值,努力f1,3,2 f2,5,1 f3,1,2 capacity = 3
here is my code: 这是我的代码:
from gurobipy import *
m = Model("C1")
featuresname, value, effort = multidict({
"F1":[3,2],
"F2":[5,1],
"F3":[1,2]
})
featureset = {}
for f in featuresname:
featureset[f] = m.addVar(obj=value[f], name=f)
m.modelSense = GRB.MAXIMIZE
m.update()
capacity = 3
m.addConstr(quicksum(effort[f] * featureset[f] for f in featuresname) <=capacity,f)
result = m.optimize()
print(result)
if m.status == GRB.Status.OPTIMAL:
print('Optimal objective: %g' % m.objVal)
elif m.status != GRB.Status.INFEASIBLE:
print('Optimization was stopped with status %d' % m.status)
I read all these examples http://www.gurobi.com/resources/examples/example-models-overview but can not find the problem .... can you please give me some hints? 我阅读了所有这些示例http://www.gurobi.com/resources/examples/example-models-overview,但是找不到问题....您能给我一些提示吗?
If would be helpful to have a look at the Gurobi output and/or errors you are getting, but just by looking at the code, it seems you forgot to update the model after adding the constraint. 如果有助于查看Gurobi输出和/或您获得的错误,但只是通过查看代码,您似乎忘记在添加约束后更新模型。
So I would add " m.update() " just before "result = m.optimize()" 所以我会在“result = m.optimize()”之前添加“m.update()”
Other than that code looks Ok. 除了那段代码看起来还不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.