Ale*_*exo 1 list-comprehension julia
我有一个复杂的问题,我必须从先前构建的图形中评估边缘的总和.朱莉娅似乎没有处理带边的条件和.
这是一个类似于我想要解决的问题的简单问题:
module EssaiModule
using LightGraphs, MetaGraphs
g = DiGraph(6)
mg = MetaDiGraph(g, 1.0)
add_vertex!(mg)
add_edge!(mg,1,2)
add_edge!(mg,1,3)
add_edge!(mg,1,4)
add_edge!(mg,2,4)
add_edge!(mg,2,5)
add_edge!(mg,3,5)
add_edge!(mg,5,6)
add_edge!(mg,4,6)
set_props!(mg,3,Dict(:port=>1,:vessel=>2))
set_props!(mg,1,Dict(:port=>1,:vessel=>0))
set_props!(mg,2,Dict(:port=>1,:vessel=>0))
set_props!(mg,4,Dict(:port=>1,:vessel=>2))
set_props!(mg,5,Dict(:port=>0,:vessel=>2))
set_props!(mg,6,Dict(:port=>0,:vessel=>0))
SI = sum(1 for e in edges(mg);get_prop(g,dst(e),:vessel)==2 && get_prop(g,dst(e),:port)==1)
println(SI)
end
Run Code Online (Sandbox Code Playgroud)
当我测试它时,我得到了错误
#LoadError: MethodError: no method matching dst(::Irrational{:e})
Run Code Online (Sandbox Code Playgroud)
我真的需要弄清楚如何与条件求和,因为在我的实际问题中,我将这个和放在一个约束中,就像这样,边ed
和和x
变量之和:
@constraint(model, cM[e in edges(g)], x[e] + sum(x[ed] for ed in edges(g) ; fn1(ed) == 2 && fn2(ed) == 1) <= 1 + y)
Run Code Online (Sandbox Code Playgroud)
我有错误信息说
#LoadError: UndefVarError: ed not defined
Run Code Online (Sandbox Code Playgroud)
所以我的问题如下:
为什么第一个代码不起作用,因为类型e
是Int
?
我是以不好的方式写约束的吗?
对不起我无法上传真正的问题,内容和数据都不会上线,加上它太大了.
对于问题的第一部分,您需要更改函数调用
SI = sum(1 for e in edges(mg);get_prop(g,dst(e),:vessel)==2 && get_prop(g,dst(e),:port)==1)
# ^ you end the statement with a semicolon
# after the semicolon, keyword arguments of a function, in this case `sum`,
# begins. then, the compiler checks the function `dst` and cannot find a proper
# method that is implemented for e, that is, the irrational number e.
Run Code Online (Sandbox Code Playgroud)
如下:
SI = length([e for e in edges(mg) if get_prop(mg, dst(e), :vessel) == 2 &&
get_prop(mg, dst(e), :port ) == 1 ])
println(SI) # prints 3
Run Code Online (Sandbox Code Playgroud)
另请注意代码中的拼写错误.get_prop
需要检查mg
,而不是g
.您还应该查看文档的" 理解"部分.
对于问题的第二部分,您应该为我们提供宏定义,以便更好地帮助您.很可能,您没有在宏内插入表达式/变量.请参阅我对其他问题的回答,并告诉我这是否有帮助.
编辑1.实际上,你的第二部分也有同样的问题.在里面sum
,你用分号结束位置参数并启动关键字参数.
编辑2.根据下面DNF的评论,您甚至应该使用count
更具表现力(源代码)和高效版本:
SI = count(e->(get_prop(mg, dst(e), :vessel) == 2 &&
get_prop(mg, dst(e), :port ) == 1 ), edges(mg))
# or,
SI = count(get_prop(mg, dst(e), :vessel) == 2 &&
get_prop(mg, dst(e), :port ) == 1 for e in edges(mg))
Run Code Online (Sandbox Code Playgroud)
编辑3.表现比较如下:
Pkg.add("BenchmarkTools")
using BenchmarkTools
@benchmark count(e->(get_prop(mg, dst(e), :vessel) == 2 &&
get_prop(mg, dst(e), :port ) == 1 ), edges(mg))
BenchmarkTools.Trial:
memory estimate: 7.73 KiB
allocs estimate: 53
--------------
minimum time: 2.201 ?s (0.00% GC)
median time: 2.494 ?s (0.00% GC)
mean time: 3.191 ?s (15.69% GC)
maximum time: 198.515 ?s (95.69% GC)
--------------
samples: 10000
evals/sample: 9
@benchmark count(get_prop(mg, dst(e), :vessel) == 2 &&
get_prop(mg, dst(e), :port ) == 1 for e in edges(mg))
BenchmarkTools.Trial:
memory estimate: 7.75 KiB
allocs estimate: 54
--------------
minimum time: 2.352 ?s (0.00% GC)
median time: 2.661 ?s (0.00% GC)
mean time: 3.712 ?s (15.10% GC)
maximum time: 236.440 ?s (95.06% GC)
--------------
samples: 10000
evals/sample: 9
@benchmark length([e for e in edges(mg) if get_prop(mg, dst(e), :vessel) == 2 &&
get_prop(mg, dst(e), :port ) == 1])
BenchmarkTools.Trial:
memory estimate: 8.13 KiB
allocs estimate: 60
--------------
minimum time: 2.642 ?s (0.00% GC)
median time: 2.789 ?s (0.00% GC)
mean time: 3.553 ?s (14.54% GC)
maximum time: 231.424 ?s (94.73% GC)
--------------
samples: 10000
evals/sample: 9
Run Code Online (Sandbox Code Playgroud)
count
版本的效率似乎比10%高length
,这可能会对您实际的更大问题产生影响.
编辑4.由于DNF,再次进行基准测试的正确方法如下:
@benchmark count(e->(get_prop($mg, dst(e), :vessel) == 2 &&
get_prop($mg, dst(e), :port ) == 1 ), edges($mg))
BenchmarkTools.Trial:
memory estimate: 7.72 KiB
allocs estimate: 52
--------------
minimum time: 2.320 ?s (0.00% GC)
median time: 2.478 ?s (0.00% GC)
mean time: 2.952 ?s (10.33% GC)
maximum time: 117.306 ?s (93.38% GC)
--------------
samples: 10000
evals/sample: 9
@benchmark count(get_prop($mg, dst(e), :vessel) == 2 &&
get_prop($mg, dst(e), :port ) == 1 for e in edges($mg))
BenchmarkTools.Trial:
memory estimate: 7.73 KiB
allocs estimate: 53
--------------
minimum time: 2.340 ?s (0.00% GC)
median time: 2.524 ?s (0.00% GC)
mean time: 3.030 ?s (11.17% GC)
maximum time: 197.018 ?s (94.35% GC)
--------------
samples: 10000
evals/sample: 9
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
209 次 |
最近记录: |