-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradientDescent.py
More file actions
55 lines (44 loc) · 1.11 KB
/
gradientDescent.py
File metadata and controls
55 lines (44 loc) · 1.11 KB
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
48
49
50
51
52
53
54
from scipy import io
import numpy as np
import imageio
import matplotlib.pyplot as plt
solutionMat = np.load('solution.npy')
if False:
plt.imshow(solutionMat)
plt.show()
voids = np.where(solutionMat!=1)
print(voids[0].size)
randomNumber = 4107;
startingPoint = (voids[0][randomNumber],voids[1][randomNumber])
x0,y0 = (startingPoint[0],startingPoint[1]);
#x0,y0 = (34,90)
x0, y0 = (31,1)
x=x0
y=y0
pathX = [x0]
pathY = [y0]
maxIterations = solutionMat.size;
iteration = 0;
while True:
#print(iteration)
currentVoid = solutionMat[x,y]
d = 1;
neighbours = [(x - d, y), (x + d, y), (x, y + d), (x, y - d)]
xmin,ymin,valmin = (x, y, currentVoid)
for neighbour in neighbours:
xn, yn = neighbour
if abs(solutionMat[xn,yn]) < abs(valmin):
xmin = xn
ymin = yn
valmin = solutionMat[xn,yn]
x=xmin
y=ymin
pathX.append(x)
pathY.append(y)
if currentVoid==0 or iteration==maxIterations:
break
iteration+=1
#plt.imshow(np.array(imageio.imread('maze3.bmp')))
plt.imshow(solutionMat)
plt.scatter(pathY,pathX,color='r')
plt.show()