-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaterVelocityPrediction.py
168 lines (129 loc) · 5.04 KB
/
WaterVelocityPrediction.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# numpy 없이는 살 수가 없다.
import numpy
# Temporal Memory의 파이썬 구현
from nupic.research.temporal_memory import TemporalMemory as TM
from nupic.encoders.scalar import ScalarEncoder
# izip은 최대의 효율을 위해서 필요하다.
from itertools import izip as zip, count
import random
import matplotlib.pyplot as plt
# Unit 인코더 (범위: -100~100)
UnitEncoder= ScalarEncoder(21, -100, 100, False, 0, 0, 1, "unit")
EncodedUnitTable = {}
def CreateEncodedUnitTable():
for i in range(-100, 101):
EncodedUnitTable[i] = UnitEncoder.encode(i)
def DecodeUnit( U ):
#return int(float(UnitEncoder.decode(U)[0]['unit'][1]))
dic, name = UnitEncoder.decode(U)
if len(name) > 0:
return int(numpy.mean(dic['unit'][0]))
return -999
# Vector 인코더
def EncodeVector( u, v ):
U = EncodedUnitTable[ u ]
V = EncodedUnitTable[ v ]
return numpy.append(U, V)
#return U + V
def DecodeVector( Vec ):
EncodedWidth = UnitEncoder.getWidth();
U = Vec[0 :EncodedWidth]
V = Vec[EncodedWidth:EncodedWidth*2]
return DecodeUnit(U), DecodeUnit(V)
u = -999
v = -999
for item in EncodedUnitTable.items():
if numpy.array_equal( item[1], U ):
u = item[0]
if numpy.array_equal( item[1], V ):
v = item[0]
return u, v
def DecodeVector2( Vec ):
EncodedWidth = UnitEncoder.getWidth();
U = Vec[0 :EncodedWidth]
V = Vec[EncodedWidth:EncodedWidth*2]
u = numpy.asarray(U)
v = numpy.asarray(V)
du = UnitEncoder.decode(u)
dv = UnitEncoder.decode(v)
return DecodeUnit(numpy.asarray(U)), DecodeUnit(numpy.asarray(V))
# 일단 크기와 각도가 랜덤인 Vector Field(10 by 10)를 만들어보자.
VectorField = numpy.zeros((0,), dtype=numpy.uint8)
CreateEncodedUnitTable()
xRange = 10
yRange = 10
colDims = UnitEncoder.getWidth() * 2 * xRange * yRange
tm = TM(columnDimensions=(colDims,),
cellsPerColumn=2,
initialPermanence=0.5,
connectedPermanence=0.5,
minThreshold=10,
maxNewSynapseCount=20,
permanenceIncrement=0.1,
permanenceDecrement=0.0,
activationThreshold=8,
)
print "Started!"
#plt.ion()
plt.figure()
plt.title('Vector Field Prediction Using Machine Learning')
for i in range(100):
Vec = []
for x in range(xRange):
for y in range(yRange):
V = EncodeVector(random.randint(-100, 100), random.randint(-100, 100))
u, v = DecodeVector(V)
#print "(%d, %d) = v(%d, %d)" % (x, y, u, v)
#plt.quiver(x, y, u, v, pivot='mid', scale=10, units='dots', width=1)
Vec = numpy.append(Vec, V)
activeColumns = set([j for j, k in zip(count(), Vec) if k == 1])
tm.compute(activeColumns, learn = True)
print "learned #%d" % i
#plt.draw()
#plt.pause(0.001)
#plt.clf()
#numpy.savetxt("Vec%d.txt" % i, Vec)
Vec = []
for x in range(xRange):
for y in range(yRange):
V = EncodeVector(random.randint(-100, 100), random.randint(-100, 100))
u, v = DecodeVector(V)
#print "(%d, %d) = v(%d, %d)" % (x, y, u, v)
#plt.quiver(x, y, u, v, pivot='mid', scale=10, units='dots', width=1)
Vec = numpy.append(Vec, V)
activeColumns = set([j for j, k in zip(count(), Vec) if k == 1])
tm.compute(activeColumns, learn = False)
activeColumnsIndeces = [tm.columnForCell(i) for i in tm.getActiveCells()]
predictedColumnIndeces = [tm.columnForCell(i) for i in tm.getPredictiveCells()]
actColState = [1 if i in activeColumnsIndeces else 0 for i in range(tm.numberOfColumns())]
predColState = [1 if i in predictedColumnIndeces else 0 for i in range(tm.numberOfColumns())]
z = 0
for x in range(xRange):
for y in range(yRange):
AV = actColState[z: z + UnitEncoder.getWidth() * 2]
PV = predColState[z: z + UnitEncoder.getWidth() * 2]
PV = numpy.asarray( PV )
u, v = DecodeVector( PV )
if u != -999 and v != -999:
print "(%d, %d) = v(%d, %d)" % (x, y, u, v)
plt.quiver(x, y, u, v, pivot='mid', scale=10, units='dots', width=1)
else:
print "(%d, %d) = v(unpredictable)" % (x, y)
plt.quiver(x, y, 100, 100, pivot='mid', scale=10, units='dots', width=1, color='r')
plt.quiver(x, y, -100, 100, pivot='mid', scale=10, units='dots', width=1, color='r')
plt.quiver(x, y, 100, -100, pivot='mid', scale=10, units='dots', width=1, color='r')
plt.quiver(x, y, -100, -100, pivot='mid', scale=10, units='dots', width=1, color='r')
#print "(%d, %d) = v(%d, %d)" % (x, y, u, v)
#plt.quiver(x, y, u, v, pivot='mid', scale=10, units='dots', width=1)
#plt.quiver(x, y, u, v, pivot='mid')
z = z + UnitEncoder.getWidth() * 2
plt.show()
# 1
#Q = plt.quiver(U, V)
#qk = plt.quiverkey(Q, 0.5, 0.92, 2, r'$2 \frac{m}{s}$', labelpos='W',
# fontproperties={'weight': 'bold'})
#l, r, b, t = plt.axis()
#dx, dy = r - l, t - b
#plt.axis([l - 0.05*dx, r + 0.05*dx, b - 0.05*dy, t + 0.05*dy])