接上篇学会了如何用python调用gurobipy之后,这篇总结一些学到的基本操作。
tuplelist、tupledict、multidict、创建list、
tuplelist是Python list的扩展对象,使用tuplelist()不能忘记from gurobipy import *,tuplelist增加了快速筛选select功能,比传统的if...else...筛选速度快。
from gurobipy import *
import time
T1 = time.time()
Cities=[("A","B"),("A","C"),("B","C"),("B","D"),("C","D")]
Routes=tuplelist(Cities)
print(Routes.select("A","*"))
T2 =time.time()
print('程序运行时间:%s毫秒' % ((T2 - T1)*1000))
tuplelist运行结果:
C:\Users\xzr\.conda\envs\py310gurobi\python.exe F:\PycharmProjects\workspace\untitled\jizulunban\caogao.py
<gurobi.tuplelist (2 tuples, 2 values each):
( A , B )
( A , C )
>
程序运行时间:1.9958019256591797毫秒
进程已结束,退出代码0
for...else...方法:
from gurobipy import *
import time
T1 = time.time()
Cities=[("A","B"),("A","C"),("B","C"),("B","D"),("C","D")]
Result=[]
for i,j in Cities:
if i=="A":
Result.append((i,j))
print(Result)
T2 =time.time()
print('程序运行时间:%s毫秒' % ((T2 - T1)*1000))
for...else...运行结果:
C:\Users\xzr\.conda\envs\py310gurobi\python.exe F:\PycharmProjects\workspace\untitled\jizulunban\caogao.py
[('A', 'B'), ('A', 'C')]
程序运行时间:0.0毫秒
进程已结束,退出代码0
尴尬了,竟然是for...else...的运行速度更快,原因是数据量太少了体现不出效果。
tupledict是Python Dictionary的扩展对象,键值是tuple(元组),可以使用select、sum、prob函数。用于变量和约束。后面有详细介绍。
multidict()创建tuplelist和tupledict的便捷方法。代码示例:
from gurobipy import *
import time
# T1 = time.time()
cities,supply,demand = multidict({
"A":[100,20],
"B":[150,50],
"C":[20,300],
"D":[10,200]})
print(cities)
print(supply)
print(demand)
# T2 =time.time()
运行结果:
C:\Users\xzr\.conda\envs\py310gurobi\python.exe F:\PycharmProjects\workspace\untitled\jizulunban\caogao.py
['A', 'B', 'C', 'D']
{'A': 100, 'B': 150, 'C': 20, 'D': 10}
{'A': 20, 'B': 50, 'C': 300, 'D': 200}
进程已结束,退出代码0
运行结果第一行是list,第二三行是dictionary。
python有多种创建list的方法:
a=[]
a.append("A")
b=[i**2 for i in range(6)] #[0,1,4,9,16,25]
c=[(i,j)for j in range(4) for i in range(j)] #[(0,1),(0,2),(1,2),(0,3),(1,3),(2,3)]
d=[i for i in range(10) if i not in b] #[2,3,5,6,7,8]
Pairs=[]
for j in range(4):
for i in range(j):
Pairs.append((i,j))
对于求和,Python的Generator(生成器):
SumSquares=sum(i**2 for i in range(6)) #55
Gurobi中采用quicksum,效率更高:
obj=quicksum(cost[i,j]*x[i,j] for i,j in arcs)
tupledict(Gurobi变量一般都是tupledict类型)有sum函数
from gurobipy import *
import time
# T1 = time.time()
m=Model()
x=m.addVars(3,4,vtype=GRB.BINARY,name="x")
m.addConstrs((x.sum(i,"*")<=1 for i in range(3)),name="con")
m.update()
m.write("test.lp")
# T2 =time.time()
# print('程序运行时间:%s毫秒' % ((T2 - T1)*1000))
运行后,打开“test.lp”文件查看写入的模型
\ LP format - for model browsing. Use MPS format to capture full model detail.
Minimize
Subject To
con[0]: x[0,0] + x[0,1] + x[0,2] + x[0,3] <= 1
con[1]: x[1,0] + x[1,1] + x[1,2] + x[1,3] <= 1
con[2]: x[2,0] + x[2,1] + x[2,2] + x[2,3] <= 1
Bounds
Binaries
x[0,0] x[0,1] x[0,2] x[0,3] x[1,0] x[1,1] x[1,2] x[1,3] x[2,0] x[2,1]
x[2,2] x[2,3]
End
太妙了太妙了
tupledict(Gurobi变量一般都是tupledict类型)还有prob函数,用于变量和系数相乘后累加
以下表达式等效
obj=quicksum(cost[i,j]*x[i,j] for i,j in arcs)
obj=x.prod(cost)
建模建议,尽量采用稀疏方式,采用tuplelists筛选和指定合适的下标组合关系,基于这些组合关系建立变量和数据字典,利用tuplelist.select()以及tupledict.select(),tupledict.sum(),tupledict.prob()来对下标进行组合处理。












from gurobipy import *
# T1 = time.time()
try:
#Create a new model
m=Model("mip1")
#Create variables
x=m.addVar(vtype=GRB.BINARY,name="x")
y=m.addVar(vtype=GRB.BINARY,name="y")
z=m.addVar(vtype=GRB.BINARY,name="z")
#Set objective
m.setObjective(x+y+2*z,GRB.MAXIMIZE)
#Add constraint:x+2y+3z<=4
m.addConstr(x+2*y+3*z<=4,"c0")
#Add constraint:x+y>=1
m.addConstr(x+y>=1,"c1")
m.optimize()
for v in m.getVars(): #getVars获取所有变量
print("%s %g" % (v.varName,v.x)) #(v.varName,v.x)是(变量名字,优化结果)
print("Obj: %g" % m.objVal)
except GurobiError as e:
print("Error code" + str(e.errno)+":"+str(e))
except AttributeError:
print("Encountered an attribute error")
# T2 =time.time()
# print('程序运行时间:%s毫秒' % ((T2 - T1)*1000))
运行结果
C:\Users\xzr\.conda\envs\py310gurobi\python.exe F:\PycharmProjects\workspace\untitled\jizulunban\caogao.py
Gurobi Optimizer version 10.0.0 build v10.0.0rc2 (win64)
CPU model: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 2 rows, 3 columns and 5 nonzeros
Model fingerprint: 0x98886187
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
Matrix range [1e+00, 3e+00]
Objective range [1e+00, 2e+00]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+00]
Found heuristic solution: objective 2.0000000
Presolve removed 2 rows and 3 columns
Presolve time: 0.02s
Presolve: All rows and columns removed
Explored 0 nodes (0 simplex iterations) in 0.03 seconds (0.00 work units)
Thread count was 1 (of 4 available processors)
Solution count 2: 3 2
Optimal solution found (tolerance 1.00e-04)
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x 1
y 0
z 1
Obj: 3
进程已结束,退出代码0

from gurobipy import *
categories,minNutrition,maxNutrition=multidict({
"calories":[1800,2200],
"protein":[91,GRB.INFINITY],
"fat":[0,65],
"sodium":[0,1779]
})
foods,cost=multidict({
"hamburger":2.49,
"chicken":2.89,
"hot dog":1.50,
"fries":1.89,
"macaroni":2.09,
"pizza":1.99,
"salad":2.49,
"milk":0.89,
"ice cream":1.59
})
#Nutrition values for the foods
nutritionValues={
("hamburger","calories"):410,
("hamburger","protein"):24,
("hamburger","fat"):26,
("hamburger","sodium"):730,
("chicken","calories"):420,
("chicken", "protein"):32,
("chicken", "fat"):10,
("chicken", "sodium"):1190,
("hot dog","calories"):560,
("hot dog", "protein"):20,
("hot dog", "fat"):32,
("hot dog", "sodium"):1800,
("fries","calories"):380,
("fries", "protein"):4,
("fries", "fat"):19,
("fries", "sodium"):720,
("macaroni", "calories"): 320,
("macaroni", "protein"): 12,
("macaroni", "fat"): 10,
("macaroni", "sodium"): 930,
("pizza", "calories"): 320,
("pizza", "protein"): 15,
("pizza", "fat"): 12,
("pizza", "sodium"): 820,
("salad", "calories"): 320,
("salad", "protein"): 31,
("salad", "fat"): 12,
("salad", "sodium"): 1230,
("milk", "calories"): 100,
("milk", "protein"): 8,
("milk", "fat"): 2.5,
("milk", "sodium"): 125,
("ice cream", "calories"): 330,
("ice cream", "protein"): 8,
("ice cream", "fat"): 10,
("ice cream", "sodium"): 180
}
#Model
m=Model("diet")
#Create decision variables for the foods to buy
buy=m.addVars(foods,name="buy")
#也可以是:
# buy=[]
# for f in foods:
# buy[f]=m.addVar(name=f)
#目标函数是最小化cost
m.setObjective(buy.prod(cost),GRB.MINIMIZE)
#如果使用循环结构,应该是:
# m.setObjective(sum(buy[f]*cost[f] for f in foods),GRB.MINIMIZE)
#Nutrition constraints
m.addConstrs(
(quicksum(nutritionValues[f,c]*buy[f] for f in foods)
== [minNutrition[c],maxNutrition[c]]
for c in categories),"_")
#如果使用循环结构,应该是:
# for c in categories:
# m.addRange(
# sum(nutritionValues[f,c] * buy[f] for f in foods),minNutrition[c],maxNutrition[c],c)
# )
def printSolution():
if m.status == GRB.Status.OPTIMAL:
print("\nCost:%g" % m.objval)
print("\nBuy:")
buyx=m.getAttr("x",buy)
for f in foods:
if buy[f].x>0.0001:
print("%s %g" % (f,buyx[f]))
else:
print("No solution")
#solve
m.optimize()
printSolution()
# T1 = time.time()
# T2 =time.time()
# print('程序运行时间:%s毫秒' % ((T2 - T1)*1000))
运行结果
C:\Users\xzr\.conda\envs\py310gurobi\python.exe F:\PycharmProjects\workspace\untitled\jizulunban\caogao.py
Gurobi Optimizer version 10.0.0 build v10.0.0rc2 (win64)
CPU model: Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz, instruction set [SSE2|AVX|AVX2]
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 4 rows, 12 columns and 39 nonzeros
Model fingerprint: 0xed649f3c
Coefficient statistics:
Matrix range [1e+00, 2e+03]
Objective range [9e-01, 3e+00]
Bounds range [7e+01, 2e+03]
RHS range [7e+01, 2e+03]
Presolve removed 0 rows and 2 columns
Presolve time: 0.01s
Presolved: 4 rows, 10 columns, 37 nonzeros
Iteration Objective Primal Inf. Dual Inf. Time
0 0.0000000e+00 1.472500e+02 0.000000e+00 0s
4 1.1828861e+01 0.000000e+00 0.000000e+00 0s
Solved in 4 iterations and 0.03 seconds (0.00 work units)
Optimal objective 1.182886111e+01

—————————————————————————————————————————
注意:
若出现众多warning,删去warning给的路径下的带“~”的文件
若出现ERROR: Could not find a version that satisfies the requirement time (from versions: none),是找不到适应现有python版本的包


关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我需要一些关于TDD概念的帮助。假设我有以下代码defexecute(command)casecommandwhen"c"create_new_characterwhen"i"display_inventoryendenddefcreate_new_character#dostufftocreatenewcharacterenddefdisplay_inventory#dostufftodisplayinventoryend现在我不确定要为什么编写单元测试。如果我为execute方法编写单元测试,那不是几乎涵盖了我对create_new_character和display_invent
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
在应用开发中,有时候我们需要获取系统的设备信息,用于数据上报和行为分析。那在鸿蒙系统中,我们应该怎么去获取设备的系统信息呢,比如说获取手机的系统版本号、手机的制造商、手机型号等数据。1、获取方式这里分为两种情况,一种是设备信息的获取,一种是系统信息的获取。1.1、获取设备信息获取设备信息,鸿蒙的SDK包为我们提供了DeviceInfo类,通过该类的一些静态方法,可以获取设备信息,DeviceInfo类的包路径为:ohos.system.DeviceInfo.具体的方法如下:ModifierandTypeMethodDescriptionstatic StringgetAbiList()Obt
?博客主页:https://xiaoy.blog.csdn.net?本文由呆呆敲代码的小Y原创,首发于CSDN??学习专栏推荐:Unity系统学习专栏?游戏制作专栏推荐:游戏制作?Unity实战100例专栏推荐:Unity实战100例教程?欢迎点赞?收藏⭐留言?如有错误敬请指正!?未来很长,值得我们全力奔赴更美好的生活✨------------------❤️分割线❤️-------------------------