搜索具有完全匹配(或完全匹配字符串搜索)的变量的Gurobi模型。

huangapple go评论65阅读模式
英文:

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 &#39;732913342082_MAN-1&#39; in var.VarName]

but then it is giving me the following result:

[&lt;gurobi.Var v1_Severn_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v1_Severn_732913342082_MAN-10&gt;,
 &lt;gurobi.Var v5_27_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v5_27_732913342082_MAN-10&gt;,
 &lt;gurobi.Var v0_Windsor Locks_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v0_Windsor Locks_732913342082_MAN-10&gt;,
 &lt;gurobi.Var v2_Chesterfield_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v2_Chesterfield_732913342082_MAN-10&gt;,
 &lt;gurobi.Var v6_Harrisburg_732913342082_MAN-10&gt;,
 &lt;gurobi.Var v6_Harrisburg_732913342082_MAN-1&gt;]

And what I want is:

[&lt;gurobi.Var v1_Severn_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v5_27_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v0_Windsor Locks_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v2_Chesterfield_732913342082_MAN-1&gt;,
 &lt;gurobi.Var v6_Harrisburg_732913342082_MAN-1&gt;]

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(&#39;732913342082_MAN-1&#39;)]`

Option 2: Create a regular expression:

import re
[var for var in m.getVars() if re.search(&#39;732913342082_MAN-1$&#39;, 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(&#39;*&#39;, &#39;*&#39;, &#39;732913342082&#39;, &#39;MAN-1&#39;)

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.

huangapple
  • 本文由 发表于 2023年4月20日 08:44:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定