-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkov.py
151 lines (128 loc) · 3.65 KB
/
markov.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
import dataprocessor as dp
import numpy as np
import random
import scipy.io as sio
'''
Similar to markov 1
2 additional spaces
'''
R1 = 0.1
R2 = 0.2
R3 = 5.7
def get_markov_probabilities(trajectory):
total_number_of_moves = (trajectory.shape[1]-1)*(trajectory.shape[2])
markov_space_count = {}
'''
-1 = move anywhere within current hexagon
0 = stationary
1-6 = move to hexagon [1-6]
'''
for i in range(-1,7):
markov_space_count[i] = 0
distances = []
for ts in range(trajectory.shape[1] - 1):
#get positions at time ts and ts+1
positions0 = dp.returnTimeMap(ts, trajectory)
positions1 = dp.returnTimeMap(ts+1, trajectory)
#get com at time ts
com0 = dp.com(positions0)
#center position matrixes around current com
positions0 = dp.position_matrix_around_new_center(positions0, com0)
positions1 = dp.position_matrix_around_new_center(positions1, com0)
#get change in postions
delta_pos = positions1 - positions0
delta_pos = delta_pos.swapaxes(0,1)
for p in delta_pos:
distance = np.linalg.norm(p - np.zeros(2))
distances.append(distance)
angle = dp.vector_to_angle(p)
if distance < R1:
markov_space_count[0] += 1
elif distance < R2:
markov_space_count[-1] +=1
#which hexagon to move to
elif angle <= np.pi/float(3):
markov_space_count[1] += 1
elif angle <= np.pi*(2/float(3)):
markov_space_count[2] += 1
elif angle <= np.pi:
markov_space_count[3] += 1
elif angle <= np.pi*(4/float(3)):
markov_space_count[4] += 1
elif angle <= np.pi*(5/float(3)):
markov_space_count[5] += 1
else:
markov_space_count[6] += 1
#get probabilities
for i in range(-1,7):
markov_space_count[i] = markov_space_count[i]/float(total_number_of_moves)
return markov_space_count
def get_transformation(mp):
'''
return transition vector
mp = markov_probabilities
returns transition vector
'''
p = random.random()
#a = random angle between 0-60 degrees
a = random.uniform(0,np.pi/3)
angle = 0
if p < mp[-1]:
#move a random direction
angle = random.uniform(0,2*np.pi)
d = random.uniform(R1,R2)
return d*dp.angle_to_unit_vector(angle)
elif p < mp[-1] + mp[0]:
#stay in position
return np.zeros(2)
elif p <mp[-1] + mp[0] + mp[1]:
angle = a
elif p < mp[-1] + mp[0] + mp[1] + mp[2]:
#pi/3 - 2pi/3
angle = np.pi/float(3) + a
elif p < mp[-1] + mp[0] + mp[1] + mp[2] + mp[3]:
#2pi/3 - pi
angle = 2*np.pi/float(3) + a
elif p < mp[-1] + mp[0] + mp[1] + mp[2] + mp[3] + mp[4]:
#pi - 2
angle = np.pi + a
elif p < mp[-1] + mp[0] + mp[1] + mp[2] + mp[3] + mp[4] + mp[5]:
angle = 4*np.pi/float(3) + a
else:
angle = 5*np.pi/float(3) + a
'''
magnitude for subspace 1-6
Other settings:
d = np.random.wald(2.9,1.587)
d = np.random.wald(2.9,3.255)
d = np.random.weibull(0.808)
'''
d = random.uniform(R2,R3)
vector = d*dp.angle_to_unit_vector(angle)
return vector
def create_markov_trajectory(initial_position, time, mp = None):
'''
given a 2D matrix create a trajectory for time
using markov probabilities
'''
if mp == None:
#1 minute
trajectory0 = sio.loadmat('../basematrixes/trajectory0.mat')['trajectory']
mp = get_markov_probabilities(trajectory0)
trajectory = initial_position.swapaxes(0,1)
p0 = initial_position.swapaxes(0,1)
for ts in range(1,time):
com = dp.com(p0.transpose())
#print ts
for i in range(p0.shape[0]):
#the transformation is relative to COM
v = get_transformation(mp)
#p0[i] = np.add(p0[i], v)
pt = p0[i] - com
pt = np.add(pt, v)
pt = pt + com
p0[i] = pt
trajectory = np.dstack((trajectory,p0.copy()))
trajectory = trajectory.swapaxes(0,1)
trajectory = trajectory.swapaxes(1,2)
return trajectory