在 Juliabox.org 上使用 Julia 中包 Graphs.jl 的方法 source(edge)

Using method source(edge) of Package Graphs.jl in Julia on Juliabox.org

看看下面的简单代码示例:

Pkg.add("Graphs")
using Graphs

gd = simple_graph(20, is_directed=true) # directed graph with 20 nodes
nodeTo = 2
for nodeFrom in vertices(gd) # add some edges...
    if(nodeTo != 20)
       add_edge!(gd, nodeFrom, nodeTo)
        nodeTo +=1
    end
end
for i in edges(gd) # Print source and target for every edge in gd
    println("Target: ",target(i))
    println("Source: ", source(i))
end

所以它有时会工作,它会打印边缘的源和目标,但大多数时候 运行 单元格(在这个单元格或其他单元格中编程或什么都不做之后)我得到以下错误:

type: anonymous: in apply, expected Function, got Int64
while loading In[11], in expression starting on line 14

 in anonymous at no file:16

我没有更改有关单元格下方方法的任何代码,但它不再起作用了。 target(edge) 方法工作正常,但 source(edge) 方法最常出问题。 http://graphsjl-docs.readthedocs.org/en/latest/graphs.html#graph 我应该怎么办?我很乐意得到一些帮助。

经过一番思考,我发现错误必须在主题标签之间的代码中:

Pkg.add("JuMP")
Pkg.add("Cbc")
# Pkg.update()
using Graphs
using JuMP
using Cbc

function createModel(graph, costs, realConnections)
    m = Model(solver = CbcSolver())
    @defVar(m, 0 <= x[i=1:realConnections] <= 1, Int)
    @setObjective(m, Min, dot(x,costs[i=1:realConnections]))
    println(m)
    for vertex in vertices(graph)
        edgesIn = Int64[] # Array of edges going in the vertex
        edgesOut = Int64[] # Array of Edges going out of the vertex
        for edge in edges(graph)
            if (target(edge) == vertex) # works fine
                push!(edgesIn, edge_index(edge))

            end
            if (source(edge) == vertex) # does not work
                push!(edgesOut, edge_index(edge))
                print(source(edge), " ")
            end

        end
#         @addConstraint(m, sum{x[edgesIn[i]], i=1:length(edgesIn)} - sum{x[edgesOut[j]], j=1:length(edgesOut)} == 0)   
    end
    return m
end


file = open("csp50.txt")
g = createGraph(file) # g = g[1] = simpleGraph Object with 50 nodes and 173 arccs, g[2] = number of Nodes g[3]/g[4] = start/end time g[5] = costs of each arc


# After running this piece of code, the source(edge) method does not work anymore
########################################################################################
# adding a source and sink node and adding edges between every node of the orginal graph and the source and sink node
realConnections = length(g[5]) # speichern der Kanten
source = (num_vertices(g[1])+1)
sink = (num_vertices(g[1])+2)
add_vertex!(g[1], source) 
add_vertex!(g[1], sink) 
push!(g[3], 0) 
push!(g[3], 0)
push!(g[4], 0) 
push!(g[4], 0)
for i in vertices(g[1])
    if (i != source)
        add_edge!(g[1], source, i) # edge from source to i
        push!(g[5], 0)
    end
    if (i != sink)
        add_edge!(g[1], i, sink) # Kante von i zu Senke
        push!(g[5], 0) # Keine Kosten/Zeit fuer diese Kante
    end
end
######################################################################################
numEdges = num_edges(g[1]);
createModel(g[1], g[5], realConnections)

来自Julia's Manual

Julia will even let you redefine built-in constants and functions if needed:

julia> pi
π = 3.1415926535897...

julia> pi = 3
Warning: imported binding for pi overwritten in module Main
3

julia> pi
3

julia> sqrt(100)
10.0

julia> sqrt = 4
Warning: imported binding for sqrt overwritten in module Main
4

However, this is obviously not recommended to avoid potential confusion.

因此从它的函数定义中将 source 作为变量 "unbound" 重用。使用不同的变量名应该保留它的 Graphs.jl 定义。