-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortestpath.jl
47 lines (32 loc) · 1022 Bytes
/
shortestpath.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using Graphs
using MAT
using SparseArrays
println("reading in Q and χ")
Q = matread("anPraktikantin/Q_matrix.mat")["Q"]
chi = matread("anPraktikantin/chi4.mat")["z"]
subsize = size(Q, 1)
q = Q[1:subsize, 1:subsize]
chi1 = chi[1:subsize, 1]
I = Int[]
J = Int[]
V = Float64[]
# 1/q is the expected time for a transition,
# thus the sum of the inverse rates along a path
# should be the average time to observe that path
# minimizing the path time leads to the most probable path
println("creating path matrix")
for (i, j, q) in zip(SparseArrays.findnz(q)...)
delta = chi1[j] - chi1[i]
delta <= 0 && continue
push!(I, i)
push!(J, j)
push!(V, 1 / q)#* delta)
end
w = sparse(I, J, V, subsize, subsize)
println("computing shortest path")
from, to = argmin(chi1), argmax(chi1)
# i have no clue how this is so fast, and whether it makes any sense at all
@time p = a_star(DiGraph(w), from, to, w)
path = [map(x -> x.src, p); to]
println("saving path")
matwrite("path.mat", Dict("path" => path))