|
| 1 | +import pandas as pd |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +from numpy import * |
| 5 | +from scipy import ndimage |
| 6 | + |
| 7 | +def imageplot(f, str='', sbpt=[]): |
| 8 | + """ |
| 9 | + Use nearest neighbor interpolation for the display. |
| 10 | + """ |
| 11 | + if sbpt != []: |
| 12 | + plt.subplot(sbpt[0], sbpt[1], sbpt[2]) |
| 13 | + imgplot = plt.imshow(f, interpolation='nearest') |
| 14 | + imgplot.set_cmap('gray') |
| 15 | + plt.axis('off') |
| 16 | + |
| 17 | +def perform_dijstra_fm(W, pstart, niter=np.inf, method='dijstr', bound='sym', svg_rate=10): |
| 18 | + n = W.shape[0] |
| 19 | + neigh = np.array([[1, -1, 0, 0], [0, 0, 1, -1]]) |
| 20 | + |
| 21 | + def symmetrize(x,n): |
| 22 | + if (x<0): |
| 23 | + x = -x; |
| 24 | + elif (x>=n): |
| 25 | + x = 2*(n-1)-x |
| 26 | + return x |
| 27 | + |
| 28 | + if bound=='per': |
| 29 | + boundary = lambda x: np.mod(x,n) |
| 30 | + else: |
| 31 | + boundary = lambda x: [symmetrize(x[0],n), symmetrize(x[1],n)] # todo |
| 32 | + |
| 33 | + ind2sub1 = lambda k: [int( (k-np.fmod(k,n))/n ), np.fmod(k,n)] |
| 34 | + sub2ind1 = lambda u: int( u[0]*n + u[1] ) |
| 35 | + Neigh = lambda k,i: sub2ind1(boundary(ind2sub1(k) + neigh[:,i])) |
| 36 | + extract = lambda x,I: x[I] |
| 37 | + extract1d = lambda x,I: extract(x.flatten(),I) |
| 38 | + |
| 39 | + nstart = pstart.shape[1] |
| 40 | + I = list( np.zeros( (nstart, 1) ) ) |
| 41 | + for i in np.arange(0, nstart): |
| 42 | + I[i] = int( sub2ind1(pstart[:,i]) ) |
| 43 | + |
| 44 | + D = np.zeros( (n,n) ) + np.inf # current distance |
| 45 | + for i in np.arange(0, nstart): |
| 46 | + D[pstart[0,i],pstart[1,i]] = 0 |
| 47 | + |
| 48 | + S = np.zeros( (n,n) ) |
| 49 | + for i in np.arange(0, nstart): |
| 50 | + S[pstart[0,i],pstart[1,i]] = 1 # open |
| 51 | + |
| 52 | + iter = 0 |
| 53 | + q = 100 # maximum number of saves |
| 54 | + Dsvg = np.zeros( (n,n,q) ) |
| 55 | + Ssvg = np.zeros( (n,n,q) ) |
| 56 | + while ( not(I==[]) & (iter<=niter) ): |
| 57 | + iter = iter+1; |
| 58 | + if iter==niter: |
| 59 | + break |
| 60 | + j = np.argsort( extract1d(D,I) ) |
| 61 | + if np.ndim(j)==0: |
| 62 | + j = [j] |
| 63 | + j = j[0] |
| 64 | + i = I[j] |
| 65 | + a = I.pop(j) |
| 66 | + u = ind2sub1(i); |
| 67 | + S[u[0],u[1]] = -1 |
| 68 | + J = [] |
| 69 | + for k in np.arange(0,4): |
| 70 | + j = Neigh(i,k) |
| 71 | + if extract1d(S,j)!=-1: |
| 72 | + J.append(j) |
| 73 | + if extract1d(S,j)==0: |
| 74 | + u = ind2sub1(j) |
| 75 | + S[u[0],u[1]] = 1 |
| 76 | + I.append(j) |
| 77 | + DNeigh = lambda D,k: extract1d(D,Neigh(j,k)) |
| 78 | + for j in J: |
| 79 | + dx = min(DNeigh(D,0), DNeigh(D,1)) |
| 80 | + dy = min(DNeigh(D,2), DNeigh(D,3)) |
| 81 | + u = ind2sub1(j) |
| 82 | + w = extract1d(W,j); |
| 83 | + if method=='dijstr': |
| 84 | + D[u[0],u[1]] = min(dx + w, dy + w) |
| 85 | + else: |
| 86 | + Delta = 2*w - (dx-dy)**2 |
| 87 | + if (Delta>=0): |
| 88 | + D[u[0],u[1]] = (dx + dy + np.sqrt(Delta))/ 2 |
| 89 | + else: |
| 90 | + D[u[0],u[1]] = min(dx + w, dy + w) |
| 91 | + t = iter/svg_rate |
| 92 | + if (np.mod(iter,svg_rate)==0) & (t<q): |
| 93 | + print (t) |
| 94 | + Dsvg[:,:,int(t-1)] = D |
| 95 | + Ssvg[:,:,int(t-1)] = S |
| 96 | + |
| 97 | + Dsvg = Dsvg[:,:,:int(t-1)] |
| 98 | + Ssvg = Ssvg[:,:,:int(t-1)] |
| 99 | + return (D,Dsvg,Ssvg); |
| 100 | + |
| 101 | +def exo1(x0,W): |
| 102 | + n = W.shape[0] |
| 103 | + pstart = transpose(array([x0])) |
| 104 | + [D,Dsvg,Ssvg] = perform_dijstra_fm(W, pstart, inf,'dijstr', 'sym',n*6) |
| 105 | + plt.figure(); |
| 106 | + for i in arange(0,4): |
| 107 | + plt.subplot(2, 2, i+1) |
| 108 | + d = Dsvg[:,:,i] |
| 109 | + d[d==inf] = 0 |
| 110 | + imageplot(d) |
| 111 | + plt.set_cmap('jet') |
| 112 | + plt.savefig('out-360.png') |
| 113 | + return D |
| 114 | + |
| 115 | +def exo2(x0,W): |
| 116 | + n = W.shape[0] |
| 117 | + pstart = transpose(array([x0])) |
| 118 | + [D,Dsvg,Ssvg] = perform_dijstra_fm(W, pstart, inf,'fm', 'sym',n*6) |
| 119 | + plt.figure(); |
| 120 | + for i in arange(0,4): |
| 121 | + plt.subplot(2, 2, i+1) |
| 122 | + d = Dsvg[:,:,i] |
| 123 | + d[d==inf] = 0 |
| 124 | + imageplot(d) |
| 125 | + plt.set_cmap('jet') |
| 126 | + plt.savefig('out-450.png') |
| 127 | + return D |
| 128 | + |
| 129 | +n = 40 |
| 130 | +W = ones( (n,n) ) |
| 131 | +x0 = [int(n/2), int(n/2)] |
| 132 | + |
| 133 | +D = exo2(x0,W) |
| 134 | + |
| 135 | +plt.figure() |
| 136 | +displ = lambda D: cos(2*pi*5*D/max(D.flatten()) ) |
| 137 | +imageplot(displ(D)) |
| 138 | +plt.savefig('out-480.png') |
| 139 | + |
0 commit comments