def test_discrete_handling(): variances = [[[5.320099817666392, 0.6070740587168416], [0.6070740587168416, 2.91511327850999]], [[2.4071373546900015, 0.22786169765608927], [0.22786169765608927, 2.4736622912606343]], [[3.065924316519349, 0.9318565801601824], [0.9318565801601824, 3.269782301092388]]] means = [[2.3804013106244195, -5.959479465161133], [0.4759797740245023, -2.7971912319791215], [-3.137589475680539, -5.78236605039465]] weights = [0.2931922485762746, 0.33288265886527635, 0.3739250925584491] mix = GM(means, variances, weights) delta = 0.1 bounds = [-9.6, -3.6, 4, 3.6] discrete_mix = mix.discretize2D(low=[bounds[0], bounds[1]], high=[bounds[2], bounds[3]], delta=delta) flat_belief = discrete_dehydrate(discrete_mix) # print(flat_belief) shapes = [ int((bounds[2] - bounds[0]) / delta), int((bounds[3] - bounds[1]) / delta) ] print(shapes) unflat_belief = discrete_rehydrate(flat_belief, shapes) print(discrete_mix == unflat_belief)
def testRectangleModel(): pz = Softmax(); pz.buildRectangleModel([[2,2],[3,4]],1); #print('Plotting Observation Model'); #pz.plot2D(low=[0,0],high=[10,5],vis=True); prior = GM(); for i in range(0,10): for j in range(0,5): prior.addG(Gaussian([i,j],[[1,0],[0,1]],1)); # prior.addG(Gaussian([4,3],[[1,0],[0,1]],1)); # prior.addG(Gaussian([7,2],[[4,1],[1,4]],3)) prior.normalizeWeights(); dela = 0.1; x, y = np.mgrid[0:10:dela, 0:5:dela] fig,axarr = plt.subplots(6); axarr[0].contourf(x,y,prior.discretize2D(low=[0,0],high=[10,5],delta=dela)); axarr[0].set_title('Prior'); titles = ['Inside','Left','Right','Up','Down']; for i in range(0,5): post = pz.runVBND(prior,i); c = post.discretize2D(low=[0,0],high=[10,5],delta=dela); axarr[i+1].contourf(x,y,c,cmap='viridis'); axarr[i+1].set_title('Post: ' + titles[i]); plt.show();
def testGeneralModel(): pz = Softmax() pz.buildGeneralModel(2, 4, [[1, 0], [2, 0], [3, 0]], np.matrix([-1, 1, -1, 1, 1, -1, 0, -1, -1]).T) #print('Plotting Observation Model'); #pz.plot2D(low=[0,0],high=[10,5],vis=True); prior = GM() for i in range(0, 10): for j in range(0, 5): prior.addG(Gaussian([i, j], [[1, 0], [0, 1]], 1)) # prior.addG(Gaussian([4,3],[[1,0],[0,1]],1)); # prior.addG(Gaussian([7,2],[[4,1],[1,4]],3)) prior.normalizeWeights() dela = 0.1 x, y = np.mgrid[0:10:dela, 0:5:dela] fig, axarr = plt.subplots(5) axarr[0].contourf(x, y, prior.discretize2D(low=[0, 0], high=[10, 5], delta=dela)) axarr[0].set_title('Prior') titles = ['Inside', 'Left', 'Right', 'Down'] for i in range(0, 4): post = pz.runVBND(prior, i) c = post.discretize2D(low=[0, 0], high=[10, 5], delta=dela) axarr[i + 1].contourf(x, y, c, cmap='viridis') axarr[i + 1].set_title('Post: ' + titles[i]) plt.show()
def buildRectangleModel(): #Specify the lower left and upper right points recBounds = [[2, 2], [3, 4]] #recBounds = [[1,1],[8,4]]; B = np.matrix([ -1, 0, recBounds[0][0], 1, 0, -recBounds[1][0], 0, 1, -recBounds[1][1], 0, -1, recBounds[0][1] ]).T M = np.zeros(shape=(12, 15)) #Boundry: Left|Near rowSB = 0 classNum1 = 1 classNum2 = 0 for i in range(0, 3): M[3 * rowSB + i, 3 * classNum2 + i] = -1 M[3 * rowSB + i, 3 * classNum1 + i] = 1 #Boundry: Right|Near rowSB = 1 classNum1 = 2 classNum2 = 0 for i in range(0, 3): M[3 * rowSB + i, 3 * classNum2 + i] = -1 M[3 * rowSB + i, 3 * classNum1 + i] = 1 #Boundry: Up|Near rowSB = 2 classNum1 = 3 classNum2 = 0 for i in range(0, 3): M[3 * rowSB + i, 3 * classNum2 + i] = -1 M[3 * rowSB + i, 3 * classNum1 + i] = 1 #Boundry: Down|Near rowSB = 3 classNum1 = 4 classNum2 = 0 for i in range(0, 3): M[3 * rowSB + i, 3 * classNum2 + i] = -1 M[3 * rowSB + i, 3 * classNum1 + i] = 1 A = np.hstack((M, B)) #print(np.linalg.matrix_rank(A)) #print(np.linalg.matrix_rank(M)) Theta = linalg.lstsq(M, B)[0].tolist() weight = [] bias = [] for i in range(0, len(Theta) // 3): weight.append([Theta[3 * i][0], Theta[3 * i + 1][0]]) bias.append(Theta[3 * i + 2][0]) steep = 1 weight = (np.array(weight) * steep).tolist() bias = (np.array(bias) * steep).tolist() pz = Softmax(weight, bias) #print('Plotting Observation Model'); #pz.plot2D(low=[0,0],high=[10,5],vis=True); prior = GM() for i in range(0, 10): for j in range(0, 5): prior.addG(Gaussian([i, j], [[1, 0], [0, 1]], 1)) # prior.addG(Gaussian([4,3],[[1,0],[0,1]],1)); # prior.addG(Gaussian([7,2],[[4,1],[1,4]],3)) prior.normalizeWeights() dela = 0.1 x, y = np.mgrid[0:10:dela, 0:5:dela] fig, axarr = plt.subplots(6) axarr[0].contourf(x, y, prior.discretize2D(low=[0, 0], high=[10, 5], delta=dela)) axarr[0].set_title('Prior') titles = ['Inside', 'Left', 'Right', 'Up', 'Down'] for i in range(0, 5): post = pz.runVBND(prior, i) c = post.discretize2D(low=[0, 0], high=[10, 5], delta=dela) axarr[i + 1].contourf(x, y, c, cmap='viridis') axarr[i + 1].set_title('Post: ' + titles[i]) plt.show()