英文:
Gurobi Model search for variable with exact match (or exact match string search)
问题
尝试使用以下代码来搜索包含"732913342082_MAN-1"的变量:
[var for var in m.getVars() if '732913342082_MAN-1' in var.VarName]
但是它给我返回以下结果:
[<gurobi.Var v1_Severn_732913342082_MAN-1>,
<gurobi.Var v1_Severn_732913342082_MAN-10>,
<gurobi.Var v5_27_732913342082_MAN-1>,
<gurobi.Var v5_27_732913342082_MAN-10>,
<gurobi.Var v0_Windsor Locks_732913342082_MAN-1>,
<gurobi.Var v0_Windsor Locks_732913342082_MAN-10>,
<gurobi.Var v2_Chesterfield_732913342082_MAN-1>,
<gurobi.Var v2_Chesterfield_732913342082_MAN-10>,
<gurobi.Var v6_Harrisburg_732913342082_MAN-10>,
<gurobi.Var v6_Harrisburg_732913342082_MAN-1>]
而我想要的是:
[<gurobi.Var v1_Severn_732913342082_MAN-1>,
<gurobi.Var v5_27_732913342082_MAN-1>,
<gurobi.Var v0_Windsor Locks_732913342082_MAN-1>,
<gurobi.Var v2_Chesterfield_732913342082_MAN-1>,
<gurobi.Var v6_Harrisburg_732913342082_MAN-1>]
基本上,它还返回了'732913342082_MAN-10',我认为这是因为它们看起来相似。我认为这更多是与精确字符串搜索部分有关,希望有人能帮忙解决!
英文:
Trying to use the following code to search for variables that have "732913342082_MAN-1"
[var for var in m.getVars() if '732913342082_MAN-1' in var.VarName]
but then it is giving me the following result:
[<gurobi.Var v1_Severn_732913342082_MAN-1>,
<gurobi.Var v1_Severn_732913342082_MAN-10>,
<gurobi.Var v5_27_732913342082_MAN-1>,
<gurobi.Var v5_27_732913342082_MAN-10>,
<gurobi.Var v0_Windsor Locks_732913342082_MAN-1>,
<gurobi.Var v0_Windsor Locks_732913342082_MAN-10>,
<gurobi.Var v2_Chesterfield_732913342082_MAN-1>,
<gurobi.Var v2_Chesterfield_732913342082_MAN-10>,
<gurobi.Var v6_Harrisburg_732913342082_MAN-10>,
<gurobi.Var v6_Harrisburg_732913342082_MAN-1>]
And what I want is:
[<gurobi.Var v1_Severn_732913342082_MAN-1>,
<gurobi.Var v5_27_732913342082_MAN-1>,
<gurobi.Var v0_Windsor Locks_732913342082_MAN-1>,
<gurobi.Var v2_Chesterfield_732913342082_MAN-1>,
<gurobi.Var v6_Harrisburg_732913342082_MAN-1>]
Basically, it is giving me '732913342082_MAN-10' as well which I think is due to them looking alike. I think this is more toward the exact string search part and hope someone can help!
Thanks!
答案1
得分: 1
Option 1: 使用 Python 的 str.endswith() 方法:
[var for var in m.getVars() if var.VarName.endswith('732913342082_MAN-1')]
Option 2: 创建一个正则表达式:
import re
[var for var in m.getVars() if re.search('732913342082_MAN-1$', var.VarName)]
Option 3: 重构您的 gurobipy 代码,使用 Model.addVars() 创建一个 gurobipy.tupledict 的 Var 对象,这样您可以使用 tupledict.select() 来获取您想要的子集:
x = m.addVars(index1, index2, index3, index4, ...)
# ...
x.select('*', '*', '732913342082', 'MAN-1')
对我来说,选项 1 和 2 是快速且不太规范的方法;选项 3 更干净和高效,但可能需要对您的代码进行一些更改。
英文:
There are multiple ways to do this:
Option 1: Use Python str.endswith():
[var for var in m.getVars() if var.VarName.endswith('732913342082_MAN-1')]`
Option 2: Create a regular expression:
import re
[var for var in m.getVars() if re.search('732913342082_MAN-1$', var.VarName)]
Option 3: Refactor your gurobipy code to use Model.addVars() to create a gurobipy.tupledict of Var objects, so that you can use tupledict.select() to get the subset you want:
x = m.addVars(index1, index2, index3, index4, ...)
# ...
x.select('*', '*', '732913342082', 'MAN-1')
For me, options 1 and 2 are quick-and-dirty; option 3 is cleaner and more efficient but may require some changes to your code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论