-
Notifications
You must be signed in to change notification settings - Fork 0
/
smcRowStrips.py
executable file
·187 lines (169 loc) · 8.9 KB
/
smcRowStrips.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""smc.py
2D constrained channel using fully adapted SMC adding several columns at a time.
"""
from optparse import OptionParser
import numpy as np
from scipy import misc
from numpy.random import random_sample
import time
def main():
# Parse command-line arguments
parser = OptionParser()
parser.add_option("--nx", type=int, help="number of rows")
parser.add_option("--ny", type=int, help="number of columns")
parser.add_option("--iter", type=int, help="iterations to run")
parser.add_option("--particles", type=str, help="list with particles N")
parser.add_option("--stripWidth", type=int, help="width of strip")
(args, options) = parser.parse_args()
# Number of particles - N (design variable)
particles = args.particles.split(',')
iterations = args.iter
CM = np.zeros( iterations )
# Initialize arrays/matrices
nx = args.nx
ny = args.ny
stripWidth = args.stripWidth
nrInd = int(ny/stripWidth)
# Interaction potentials
interactionPotentials = np.ones( (2, 2) )
interactionPotentials[1, 1] = 0
intPot = np.ones( (2, 2) )
intPot[1, 1] = 0
nrComb = 2**stripWidth
xDomain = np.zeros((nrComb,stripWidth), dtype=np.uint64)
for iIter in range(nrComb):
tempStr = np.binary_repr(iIter, width=stripWidth)
for iStrip in range(stripWidth):
xDomain[iIter, iStrip] = int(tempStr[iStrip])
fileName = str(nx)+'x'+str(ny)+'informationRateROWstripW' + str(stripWidth) + '.txt'
# New file, print initial info, first line
f = open(fileName, 'w')
f.write('nx ny\n')
f.write(str(nx) + ' ' + str(ny) + '\n')
f.write('particles timeElapsed informationRateEst \n')
f.close()
for iIter in particles:
print iIter
startSMC = time.time()
CM = np.zeros( iterations )
for i in np.arange(0,iterations):
N = int(iIter)
# SMC initializations
trajectorySMC = np.zeros( (N, nx, ny) )
tempWeights = np.ones( N )
ancestors = np.zeros( N, np.int )
# BP initializations
messages = np.zeros( (N, nrComb, nx-1) )
normConstMessages = np.zeros( (N, nx) )
# ---------------
# SMC
# ---------------
# CSMC first iteration, forward filtering
for iParticle in range(N):
for iRow in range(nx-1):
for iCur in range(nrComb):
tempDist = np.ones(nrComb)
for iPrev in range(nrComb):
for iStrip in range(stripWidth):
if iStrip != stripWidth-1:
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iPrev, iStrip+1]]
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iCur, iStrip]]
if iRow > 0:
tempDist[iPrev] *= messages[iParticle,iPrev, iRow-1]
messages[iParticle,iCur,iRow] = np.sum(tempDist)
normConstMessages[iParticle,iRow] = np.sum(messages[iParticle,:,iRow])
messages[iParticle,:,iRow] = messages[iParticle,:,iRow] / normConstMessages[iParticle,iRow]
# Column sum
tempDist = np.ones(nrComb)
for iCur in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
tempDist[iCur] *= messages[iParticle, iCur, nx-2]
normConstMessages[iParticle,-1] = np.sum( tempDist )
tempWeights = np.prod(normConstMessages, axis=1)
CM[i] += np.log2(np.mean( tempWeights ))
# First iteration, Backward sampling
for iParticle in range(N):
for iRow in range(nx)[::-1]:
tempDist = np.ones( nrComb )
for iCur in range(nrComb):
for iStrip in range(stripWidth):
if iStrip != stripWidth-1:
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
if iRow < nx-1:
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], trajectorySMC[iParticle,iRow+1, iStrip]]
if iRow > 0:
tempDist[iCur] *= messages[iParticle, iCur, iRow-1]
tempDist = tempDist / np.sum(tempDist)
curInd = discreteSampling( tempDist, range(nrComb), 1 )
for iStrip in range(stripWidth):
trajectorySMC[iParticle,iRow,iStrip] = xDomain[curInd,iStrip]
# SMC MAIN LOOP
for iSMC in np.arange(1,nrInd):
# BP initializations
messages = np.zeros( (N, nrComb, nx-1) )
normConstMessages = np.zeros( (N, nx) )
# Forward filtering
for iParticle in range(N):
for iRow in range(nx-1):
for iCur in range(nrComb):
tempDist = np.ones(nrComb)
for iPrev in range(nrComb):
for iStrip in range(stripWidth):
if iStrip != stripWidth-1:
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iPrev, iStrip+1]]
tempDist[iPrev] *= intPot[xDomain[iPrev,iStrip], xDomain[iCur, iStrip]]
tempDist[iPrev] *= intPot[xDomain[iPrev,0], trajectorySMC[iParticle, iRow, iSMC*stripWidth-1]]
if iRow > 0:
tempDist[iPrev] *= messages[iParticle,iPrev, iRow-1]
messages[iParticle,iCur,iRow] = np.sum(tempDist)
normConstMessages[iParticle,iRow] = np.sum(messages[iParticle,:,iRow])
messages[iParticle,:,iRow] = messages[iParticle,:,iRow] / normConstMessages[iParticle,iRow]
# Column sum
tempDist = np.ones(nrComb)
for iCur in range(nrComb):
for iStrip in range(stripWidth-1):
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
tempDist[iCur] *= intPot[xDomain[iCur,0], trajectorySMC[iParticle, nx-1, iSMC*stripWidth-1]]
tempDist[iCur] *= messages[iParticle, iCur, nx-2]
normConstMessages[iParticle,nx-1] = np.sum( tempDist )
tempWeights = np.prod(normConstMessages, axis=1)
CM[i] += np.log2(np.mean( tempWeights ))
# Sample ancestors
ancestors = discreteSampling( tempWeights / np.sum( tempWeights ), np.arange(N), N)
# Backward sampling
for iParticle in range(N):
for iRow in range(nx)[::-1]:
tempDist = np.ones( nrComb )
for iCur in range(nrComb):
for iStrip in range(stripWidth):
if iStrip != stripWidth-1:
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], xDomain[iCur, iStrip+1]]
if iRow < nx-1:
tempDist[iCur] *= intPot[xDomain[iCur,iStrip], trajectorySMC[iParticle,iRow+1, iSMC*stripWidth+iStrip]]
tempDist[iCur] *= intPot[xDomain[iCur,0], trajectorySMC[ancestors[iParticle], iRow, iSMC*stripWidth-1]]
if iRow > 0:
tempDist[iCur] *= messages[ancestors[iParticle], iCur, iRow-1]
tempDist = tempDist / np.sum(tempDist)
# Sampling
curInd = discreteSampling( tempDist, range(nrComb), 1 )
for iStrip in range(stripWidth):
trajectorySMC[iParticle,iRow,iSMC*stripWidth+iStrip] = xDomain[curInd,iStrip]
trajectorySMC[:, :, :iSMC*stripWidth] = trajectorySMC[ancestors.astype(int), :, :iSMC*stripWidth]
CM[i] = CM[i] / (nx*ny)
f = open(fileName, 'a')
f.write(str(iIter) + ' ' + str(time.time() - startSMC) + ' ')
np.savetxt(f, CM.reshape( (1,iterations) ))
f.close()
#print 'MI est:',CM
def discreteSampling(weights, domain, nrSamples):
bins = np.cumsum(weights)
return domain[np.digitize(random_sample(nrSamples), bins)]
def ravel_multi_index(coord, shape):
return coord[0] * shape[1] + coord[1]
def unravel_index(coord, shape):
iy = np.remainder(coord, shape[1])
ix = (coord - iy) / shape[1]
return ix, iy
if __name__ == "__main__":
main()