i have to code a Shorter Paths in Julia using Linear Programming, here it is the template.
function shorterpaths(Mat,s)
g = metaGrapher(Mat)# fonction how return a matrix which represents
a graph
model = Model(CPLEX.Optimizer)
#code here...
end
function metaGrapher(mat)
mg = MetaDiGraph(size(mat,1))
set_props!.([mg],vertices(mg),[Dict(:marque => false,
:parent=>-1, :distSource => Inf)])
for i in 1:size(mat,1), j in 1:size(mat,2)
if mat[i,j] != 0
add_edge!(mg,i,j,Dict(:weight => mat[i,j],:flow => -1))
end
end
return mg
end
I hope this code would help you solve your problem,
#Code for paths in Julia
function generate_sample_path(adj_mtx, origin,
destination)
# Make a copy of adj_mtx
adj_copy = copy(adj_mtx)
# Initializing variables
path = [origin]
g = 1
current = origin
# Disconnect origin from all other nodes
adj_copy[:, origin] = 0
while current != destination
# Find all nodes connected to current
V = find(adj_copy[current,:])
if length(V)==0
break
end
# Choose a node randomly and add to path
next = rand(V)
path = [path; next]
# Update variables for the next iteration
current = next
adj_copy[:, next] = 0
g = g / length(V)
end
I = 0
if path[end]==destination
I = 1
end
return I, g
end
pleasde feel free try to make changes in the code based on your requirement.
Get Answers For Free
Most questions answered within 1 hours.