-
Notifications
You must be signed in to change notification settings - Fork 15
/
env.py
1112 lines (956 loc) · 37.5 KB
/
env.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import numpy as np
import random
from pfilter import ParticleFilter
from pfilter import systematic_resample
from scipy.ndimage import gaussian_filter
from timeit import default_timer as timer
# from .pfrnn.pfrnn import pfrnn
from .utils import particle_swap
from .utils import particles_mean_belief
from .utils import pol2cart
def pffilter_copy(pf, n_downsample=None):
"""Modified from https://github.com/johnhw/pfilter/blob/master/pfilter/pfilter.py, because missing noise_fn
Copy this filter at its current state. Returns
an exact copy, that can be run forward indepedently of the first.
Beware that if your passed in functions (e.g. dynamics) are stateful, behaviour
might not be independent! (tip: write stateless functions!)
Returns:
---------
A new, independent copy of this filter.
"""
# construct the filter
new_copy = ParticleFilter(
observe_fn=pf.observe_fn,
resample_fn=pf.resample_fn,
n_particles=pf.n_particles,
prior_fn=pf.prior_fn,
dynamics_fn=pf.dynamics_fn,
noise_fn=pf.noise_fn,
weight_fn=pf.weight_fn,
resample_proportion=pf.resample_proportion,
column_names=pf.column_names,
internal_weight_fn=pf.internal_weight_fn,
transform_fn=pf.transform_fn,
n_eff_threshold=pf.n_eff_threshold,
)
# copy particle state
for array in ["particles", "original_particles", "original_weights", "weights"]:
setattr(new_copy, array, np.array(getattr(pf, array)))
# copy any attributes
for array in [
"mean_hypothesis",
"mean_state",
"map_state",
"map_hypothesis",
"hypotheses",
"n_eff",
"weight_informational_energy",
"weight_entropy",
]:
if hasattr(pf, array):
setattr(new_copy, array, getattr(pf, array).copy())
if n_downsample:
new_copy.n_particles = n_downsample
new_copy.weights = np.ones(n_downsample) / n_downsample
new_copy.particles = new_copy.particles[
np.random.randint(len(new_copy.particles), size=n_downsample)
]
return new_copy
class RFMultiSeparableEnv:
def __init__(
self,
sensor=None,
actions=None,
state=None,
simulated=True,
num_particles=2000,
resample_proportion=0.1,
):
# Sensor definitions
self.sensor = sensor
# Action space and function to convert from action to index and vice versa
self.actions = actions
# Setup initial state
self.state = state
# Flag for simulation vs real data
self.simulated = simulated
self.n_particles = num_particles
self.resample_proportion = resample_proportion
# self.pfrnn = pfrnn()
self.last_observation = None
self.pf = None
self.iters = 0
def dynamics(
self,
particles,
control=None,
distance=None,
course=None,
heading=None,
**kwargs
):
"""Helper function for particle filter dynamics
Returns
-------
array_like
Updated particle state information
"""
start = timer()
n_particles, n_states = particles.shape
updated_particles = []
if not self.simulated:
for p in range(n_particles):
updated_particles.append(
self.state.update_state(
particles[p],
control=control,
distance=distance,
course=course,
heading=heading,
)
)
else:
updated_particles = self.state.update_state_vectorized(
particles, control=control
)
# if not np.allclose(updated_particles, updated_particles2):
# #if not np.all(updated_particles==updated_particles2):
# print(f"{updated_particles=}")
# print(f"{updated_particles2=}")
# print(updated_particles==updated_particles2)
end = timer()
# print(f"dynamics: {end-start}")
return np.array(updated_particles)
def particle_noise(self, particles, sigmas=[1, 2, 2], xp=None):
start = timer()
n_particles, n_states = particles.shape
# debug: assert n_states == self.state.state_dim
# particles[:,0] += np.random.normal(0, sigmas[0], (n_particles))
# particles[:,0] = np.clip(particles[:,0], a_min=1, a_max=None)
# particles[:,1] += np.random.normal(0, sigmas[1], (n_particles))
# particles[:,2] += np.random.normal(0, sigmas[2], (n_particles))
particles[:, [0, 1, 2]] += np.random.normal([0, 0, 0], sigmas, (n_particles, 3))
particles[:, 0] = np.clip(particles[:, 0], a_min=1, a_max=None)
end = timer()
# print(f"noise = {end-start}")
return particles
def reset(
self,
):
"""Reset initial state and particle filter
Parameters
----------
num_particles : integer
Number of particles to build particle filter
Returns
-------
env_obs : array_like
Heatmap distribution of observed particles from reset filter
"""
self.iters = 0
if self.simulated:
self.state.target_state = self.state.init_target_state()
self.state.sensor_state = self.state.init_sensor_state()
self.pf = []
for t in range(self.state.n_targets):
target_pf = ParticleFilter(
prior_fn=lambda n: np.array(
[self.state.random_particle_state() for _ in range(n)]
),
# observe_fn=lambda states, **kwargs: np.array(
# [
# self.sensor.observation(
# x,
# t,
# fading_sigma=0
# )
# for x in states
# ]
# ),
observe_fn=lambda states, **kwargs: np.array(
self.sensor.observation_vectorized(states, t)
),
n_particles=self.n_particles,
dynamics_fn=self.dynamics,
resample_proportion=self.resample_proportion, # 0.1, # 0.005,
noise_fn=lambda x, **kwargs: self.particle_noise(x, sigmas=[1, 2, 2]),
weight_fn=lambda hyp, o, xp=None, **kwargs: self.sensor.weight(hyp, o),
resample_fn=systematic_resample,
n_eff_threshold=1,
column_names=["range", "heading", "relative_course", "own_speed"],
)
self.pf.append(target_pf)
def pf_copy(self, n_downsample=None):
return [pffilter_copy(pf, n_downsample=n_downsample) for pf in self.pf]
def random_state(self, pf):
state = [
pf[i].particles[np.random.choice(pf[i].particles.shape[0], 1, False)][0]
for i in range(self.state.n_targets)
]
return state
# returns observation, reward, done, info
def real_step(self, data):
# action = data['action_taken'] if data.get('action_taken', None) else (0,0)
if not data["needs_processing"]:
data["distance"] = None
data["course"] = None
distance = data["distance"]
course = data["course"]
heading = data["heading"]
data["needs_processing"] = False
# Update position of sensor
self.state.update_real_sensor(
distance,
course,
heading,
# data.get("distance", None),
# data.get("course", None),
# data.get("heading", None),
)
# Get sensor observation
observation = self.sensor.real_observation()
observation = np.array(observation) # if observation is not None else None
# Update particle filter
for t in range(self.state.n_targets):
self.pf[t].update(
observation[t],
xp=self.pf[t].particles,
distance=distance,
course=course,
heading=heading,
# distance=data.get("distance", None),
# course=data.get("course", None),
# heading=data.get("heading", None),
)
# particle_swap(self)
# Calculate reward based on updated state & action
control_heading = heading if heading is not None else self.state.sensor_state[2]
control_delta_heading = (control_heading - self.state.sensor_state[2]) % 360
# reward = self.state.reward_func(
# state=None,
# action=(control_delta_heading, data.get("distance", 0)),
# particles=self.pf.particles,
# )
reward = None
# belief_obs = self.env_observation()
belief_obs = None
self.last_observation = observation
# return (belief_obs, reward, observation)
return observation
def void_probability(self, actions, r_min, min_bound=0.8):
p_outside_void = []
updated_particles = [
self.pf[t].particles.copy() for t in range(self.state.n_targets)
]
for action in actions:
for t in range(self.state.n_targets):
target_particles = self.dynamics(updated_particles[t], control=action)
updated_particles[t] = target_particles
B = 1 - np.mean(target_particles[:, 0] < r_min)
p_outside_void.append(B)
# print(f"probability outside void = {B}")
updated_particles = np.array(updated_particles)
if np.min(p_outside_void) >= min_bound:
return True, updated_particles
return False, updated_particles
# returns observation, reward, done, info
def step(self, action):
"""Function to make step based on
state variables and action index
Parameters
----------
action_idx : integer
Index for action to make step
Returns
-------
env_obs : array_like
Heatmap distribution of observed particles from filter
reward : float
Reward value for specified action
0 : int
Placeholder integer value
info : dict
Dictionary to track step specific values (reward, iteration)
"""
# Get action based on index
# action = self.actions.index_to_action(action_idx)
# Determine next state based on action & current state variables
# next_state = np.array(
# [
# self.state.update_state(target_state, action)
# for target_state in self.state.target_state
# ]
# )
next_state = self.state.update_state_vectorized(
np.array(self.state.target_state), control=action
)
# Update absolute position of sensor
self.state.update_sensor(action)
observations = []
for t in range(self.state.n_targets):
# Get sensor observation
observation = self.sensor.observation(next_state[t], t)
observations.append(observation)
# Update particle filter
self.pf[t].update(
np.array(observation), xp=self.pf[t].particles, control=action
)
# particle_swap(self)
# Calculate reward based on updated state & action
reward = None
# reward = self.state.reward_func(
# state=next_state, action=action, particles=self.pf.particles
# )
# reward = -1. * self.get_distance_error()
# Update the state variables
self.state.target_state = next_state
# env_obs = self.env_observation()
env_obs = None
self.iters += 1
info = {"episode": {}}
info["episode"]["l"] = self.iters
info["episode"]["r"] = reward
info["observation"] = observations
return (env_obs, reward, 0, info)
def env_observation(self):
"""Helper function for environment observation
Returns
-------
array_like
Heatmap distribution of current observed particles
"""
# return np.expand_dims(self.particle_heatmap_obs(self.pf.particles), axis=0)
belief = self.pf.particles.reshape(
len(self.pf.particles), self.state.n_targets, 4
)
# flattened pf map [2 x 100 x 100] -> [20000]
pf_map = self.particle_heatmap_obs(belief).reshape(-1)
mean_belief = []
for t in range(self.state.n_targets):
(
_,
_,
_,
_,
mean_r,
mean_theta,
mean_heading,
mean_spd,
) = particles_mean_belief(belief[:, t, :])
mean_belief.extend([mean_r, mean_theta, mean_heading, mean_spd])
# flattened mean belief [2 x 4] -> [8]
mean_belief = np.array(mean_belief)
return np.concatenate((mean_belief, pf_map))
def particle_heatmap_obs(self, belief):
"""Function to build histogram representing
belief distribution in cart coords
Parameters
----------
belief : array_like
Belief distribution parameters
Returns
-------
heatmap : array_like
Histogram of belief state
"""
# Transformation of belief to cartesian coords
heatmaps = []
map_width = 600
min_map = -1 * int(map_width / 2)
max_map = int(map_width / 2)
cell_size = 2 # (max_map - min_map)/max_map
xedges = np.arange(min_map, max_map + cell_size, cell_size)
yedges = np.arange(min_map, max_map + cell_size, cell_size)
for t in range(self.state.n_targets):
cart = np.array(
list(map(pol2cart, belief[:, t, 0], np.radians(belief[:, t, 1])))
)
x = cart[:, 0]
y = cart[:, 1]
# Build two-dim histogram distribution
h, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges))
h = gaussian_filter(h, sigma=8)
heatmaps.append(h)
heatmaps = np.array(heatmaps)
return heatmaps
def get_absolute_particles(self):
return np.array(
[
[self.state.get_absolute_state(p) for p in self.pf[t].particles]
for t in range(self.state.n_targets)
]
)
def get_absolute_target(self):
return np.array(
[self.state.get_absolute_state(state) for state in self.state.target_state]
)
def get_particle_centroids(self, particles=None):
centroids = []
if particles is None:
for t in range(self.state.n_targets):
particles_x, particles_y = pol2cart(
self.pf[t].particles[:, 0], np.radians(self.pf[t].particles[:, 1])
)
centroids.append([np.mean(particles_x), np.mean(particles_y)])
else:
n_targets, n_particles, n_states = particles.shape
for t in range(n_targets):
particles_x, particles_y = pol2cart(
particles[t, :, 0], np.radians(particles[t, :, 1])
)
centroids.append([np.mean(particles_x), np.mean(particles_y)])
return np.array(centroids)
def get_particle_std_dev_cartesian(self, particles=None):
std_dev = []
if particles is None:
for t in range(self.state.n_targets):
particles_x, particles_y = pol2cart(
self.pf[t].particles[:, 0], np.radians(self.pf[t].particles[:, 1])
)
std_dev.append([np.std(particles_x), np.std(particles_y)])
else:
n_targets, n_particles, n_states = particles.shape
# debug: assert n_targets == self.state.n_targets
for t in range(n_targets):
particles_x, particles_y = pol2cart(
particles[t, :, 0], np.radians(particles[t, :, 1])
)
std_dev.append([np.std(particles_x), np.std(particles_y)])
return np.array(std_dev)
def get_particle_std_dev_polar(self, particles=None):
std_dev = []
if particles is None:
for t in range(self.state.n_targets):
std_dev.append(
[
np.std(self.pf[t].particles[:, 0]),
np.std(self.pf[t].particles[:, 1]),
]
)
else:
n_targets, n_particles, n_states = particles.shape
# debug assert n_targets == self.state.n_targets
for t in range(n_targets):
std_dev.append([np.std(particles[t, :, 0]), np.std(particles[t, :, 1])])
return np.array(std_dev)
def get_all_particles(self):
return np.array([self.pf[t].particles for t in range(self.state.n_targets)])
class RFMultiEnv:
def __init__(self, sensor=None, actions=None, state=None, simulated=True):
# Sensor definitions
self.sensor = sensor
# Action space and function to convert from action to index and vice versa
self.actions = actions
# Setup initial state
self.state = state
# Flag for simulation vs real data
self.simulated = simulated
# self.pfrnn = pfrnn()
self.last_observation = None
self.pf = None
self.iters = 0
def dynamics(
self,
particles,
control=None,
distance=None,
course=None,
heading=None,
**kwargs
):
"""Helper function for particle filter dynamics
Returns
-------
array_like
Updated particle state information
"""
updated_particles = []
for p in particles:
new_p = []
for t in range(self.state.n_targets):
new_p.extend(
self.state.update_state(
p[4 * t : 4 * (t + 1)],
control=control,
distance=distance,
course=course,
heading=heading,
)
)
# new_p = np.array([self.state.update_state(target_state, control) for target_state in p])
updated_particles.append(new_p)
return np.array(updated_particles)
def particle_noise(self, particles, sigmas=[1, 2, 2], xp=None):
for t in range(self.state.n_targets):
particles[:, [4 * t]] += np.random.normal(0, sigmas[0], (len(particles), 1))
particles[:, [4 * t]] = np.clip(particles[:, [4 * t]], a_min=1, a_max=None)
particles[:, [(4 * t) + 1]] += np.random.normal(
0, sigmas[1], (len(particles), 1)
)
particles[:, [(4 * t) + 2]] += np.random.normal(
0, sigmas[2], (len(particles), 1)
)
# particles[:,[0,4]] += np.random.normal(0,sigmas[0], (len(particles), 2))
# particles[:,[0,4]] = np.clip(particles[:,[0,4]], a_min=1, a_max=None)
# particles[:,[1,5]] += np.random.normal(0,sigmas[1], (len(particles), 2))
# particles[:,[2,6]] += np.random.normal(0,sigmas[2], (len(particles), 2))
return particles
def pf_copy(self, n_downsample=None):
return [pffilter_copy(self.pf, n_downsample=n_downsample)]
def random_state(self, pf):
return [random.choice(self.pf.particles)]
def reset(self, num_particles=2000):
"""Reset initial state and particle filter
Parameters
----------
num_particles : integer
Number of particles to build particle filter
Returns
-------
env_obs : array_like
Heatmap distribution of observed particles from reset filter
"""
self.iters = 0
if self.simulated:
self.state.target_state = self.state.init_target_state()
self.state.sensor_state = self.state.init_sensor_state()
# Setup particle filter
self.pf = ParticleFilter(
prior_fn=lambda n: np.array(
[
np.array(self.state.init_particle_state()).reshape(-1)
for i in range(n)
]
),
observe_fn=lambda states, **kwargs: np.array(
[
self.sensor.observation(
[x[4 * t : 4 * (t + 1)] for t in range(self.state.n_targets)],
fading_sigma=0,
)
for x in states
]
),
n_particles=num_particles,
dynamics_fn=self.dynamics,
resample_proportion=0.005, # 0.005,
# noise_fn=lambda x, **kwargs: x,
noise_fn=lambda x, **kwargs: self.particle_noise(x, sigmas=[1, 2, 2]),
# gaussian_noise(x, sigmas=[0.2, 0.2, 0.1, 0.05, 0.05]),
# [self.sensor.weight(None, o, state=x) for x in xp],
weight_fn=lambda hyp, o, xp=None, **kwargs: self.sensor.weight(hyp, o),
resample_fn=systematic_resample,
n_eff_threshold=1,
column_names=["range", "heading", "relative_course", "own_speed"],
)
env_obs = self.env_observation()
return env_obs
# returns observation, reward, done, info
def real_step(self, data):
# action = data['action_taken'] if data.get('action_taken', None) else (0,0)
# Update position of sensor
self.state.update_real_sensor(
data.get("distance", None),
data.get("course", None),
data.get("heading", None),
)
# Get sensor observation
observation = self.sensor.real_observation()
observation = np.array(observation) if observation is not None else None
# Update particle filter
self.pf.update(
observation,
xp=self.pf.particles,
distance=data.get("distance", None),
course=data.get("course", None),
heading=data.get("heading", None),
)
# particle_swap(self)
# Calculate reward based on updated state & action
control_heading = (
data["heading"] if data.get("heading", None) else self.state.sensor_state[2]
)
control_delta_heading = (control_heading - self.state.sensor_state[2]) % 360
reward = self.state.reward_func(
state=None,
action=(control_delta_heading, data.get("distance", 0)),
particles=self.pf.particles,
)
belief_obs = self.env_observation()
self.last_observation = observation
return (belief_obs, reward, observation)
def void_probability(self, actions, r_min, min_bound=0.8):
particles = self.pf.particles
p_outside_void = []
for action in actions:
particles = self.dynamics(particles, control=action)
for t in range(self.state.n_targets):
# print(particles[:,4*t] )
B = 1 - np.mean(particles[:, 4 * t] < r_min)
p_outside_void.append(B)
# print(f"probability outside void = {B}")
if np.min(p_outside_void) >= min_bound:
return True, particles
return False, particles
def rollout(self, actions):
"""Function to make n steps based on
list of action indexes
Parameters
----------
actions : array_like
Actions to perform rollout
Returns
-------
env_obs : array_like
Heatmap distribution of observed particles from filter
reward : float
Reward value for specified action
0 : int
Placeholder integer value
info : dict
Dictionary to track step specific values (reward, iteration)
"""
particles = self.pf.particles
for action in actions:
particles = self.dynamics(particles, control=action)
return particles
# returns observation, reward, done, info
def step(self, action):
"""Function to make step based on
state variables and action index
Parameters
----------
action_idx : integer
Index for action to make step
Returns
-------
env_obs : array_like
Heatmap distribution of observed particles from filter
reward : float
Reward value for specified action
0 : int
Placeholder integer value
info : dict
Dictionary to track step specific values (reward, iteration)
"""
# Get action based on index
# action = self.actions.index_to_action(action_idx)
# Determine next state based on action & current state variables
next_state = np.array(
[
self.state.update_state(target_state, action)
for target_state in self.state.target_state
]
)
# Update absolute position of sensor
self.state.update_sensor(action)
# Get sensor observation
observation = self.sensor.observation(next_state)
# Update particle filter
self.pf.update(np.array(observation), xp=self.pf.particles, control=action)
particle_swap(self)
# Calculate reward based on updated state & action
reward = self.state.reward_func(
state=next_state, action=action, particles=self.pf.particles
)
# reward = -1. * self.get_distance_error()
# Update the state variables
self.state.target_state = next_state
env_obs = self.env_observation()
self.iters += 1
info = {"episode": {}}
info["episode"]["l"] = self.iters
info["episode"]["r"] = reward
info["observation"] = observation
return (env_obs, reward, 0, info)
# def entropy_collision_reward(self, state, action_idx=None, delta=10, collision_weight=1):
# pf_r = self.pf.particles[:,0]
# pf_theta = np.radians(self.pf.particles[:,1])
# pf_x, pf_y = pol2cart(pf_r, pf_theta)
# xedges = np.arange(-150, 153, 3)
# yedges = np.arange(-150, 153, 3)
# b = np.histogram2d(pf_x, pf_y, bins=(xedges, yedges))
# b /= np.sum(b)
# b += 0.0000001
# H = -1. * np.sum([b * np.log(b)])
# collision_rate = np.mean(self.pf.particles[:,0] < delta)
# cost = H + collision_weight * collision_rate
# return -1. * cost
def env_observation(self):
"""Helper function for environment observation
Returns
-------
array_like
Heatmap distribution of current observed particles
"""
# return np.expand_dims(self.particle_heatmap_obs(self.pf.particles), axis=0)
belief = self.pf.particles.reshape(
len(self.pf.particles), self.state.n_targets, 4
)
# flattened pf map [2 x 100 x 100] -> [20000]
pf_map = self.particle_heatmap_obs(belief).reshape(-1)
mean_belief = []
for t in range(self.state.n_targets):
(
_,
_,
_,
_,
mean_r,
mean_theta,
mean_heading,
mean_spd,
) = particles_mean_belief(belief[:, t, :])
mean_belief.extend([mean_r, mean_theta, mean_heading, mean_spd])
# flattened mean belief [2 x 4] -> [8]
mean_belief = np.array(mean_belief)
return np.concatenate((mean_belief, pf_map))
def particle_heatmap_obs(self, belief):
"""Function to build histogram representing
belief distribution in cart coords
Parameters
----------
belief : array_like
Belief distribution parameters
Returns
-------
heatmap : array_like
Histogram of belief state
"""
# Transformation of belief to cartesian coords
heatmaps = []
map_width = 600
min_map = -1 * int(map_width / 2)
max_map = int(map_width / 2)
cell_size = 2 # (max_map - min_map)/max_map
xedges = np.arange(min_map, max_map + cell_size, cell_size)
yedges = np.arange(min_map, max_map + cell_size, cell_size)
for t in range(self.state.n_targets):
cart = np.array(
list(map(pol2cart, belief[:, t, 0], np.radians(belief[:, t, 1])))
)
x = cart[:, 0]
y = cart[:, 1]
# Build two-dim histogram distribution
h, xedges, yedges = np.histogram2d(x, y, bins=(xedges, yedges))
h = gaussian_filter(h, sigma=8)
heatmaps.append(h)
heatmaps = np.array(heatmaps)
return heatmaps
def get_absolute_particles(self):
return np.array(
[
[
self.state.get_absolute_state(x[4 * t : 4 * (t + 1)])
for t in range(self.state.n_targets)
]
for x in self.pf.particles
]
)
def get_absolute_target(self):
return np.array(
[self.state.get_absolute_state(state) for state in self.state.target_state]
)
def get_particle_centroids(self, particles=None):
if particles is None:
particles = self.pf.particles
centroids = []
for t in range(self.state.n_targets):
particles_x, particles_y = pol2cart(
particles[:, 4 * t], np.radians(particles[:, (4 * t) + 1])
)
# centroid of particles x,y
centroids.append([np.mean(particles_x), np.mean(particles_y)])
return np.array(centroids)
def get_particle_std_dev_cartesian(self, particles=None):
if particles is None:
particles = self.pf.particles
std_dev = []
for t in range(self.state.n_targets):
particles_x, particles_y = pol2cart(
particles[:, 4 * t], np.radians(particles[:, (4 * t) + 1])
)
std_dev.append([np.std(particles_x), np.std(particles_y)])
return np.array(std_dev)
def get_particle_std_dev_polar(self, particles=None):
if particles is None:
particles = self.pf.particles
std_dev = []
for t in range(self.state.n_targets):
std_dev.append(
[np.std(particles[:, 4 * t]), np.std(particles[:, (4 * t) + 1])]
)
return np.array(std_dev)
def get_all_particles(self):
return np.array(self.pf.particles)
class RFEnv:
def __init__(self, sensor=None, actions=None, state=None, simulated=False):
# Sensor definitions
self.sensor = sensor
# Action space and function to convert from action to index and vice versa
self.actions = actions
# Setup initial state
self.state = state
# Flag for simulation vs real data
self.simulated = simulated
# self.pfrnn = pfrnn()
self.pf = None
def dynamics(self, particles, control=None, **kwargs):
"""Helper function for particle filter dynamics
Returns
-------
array_like
Updated particle state information
"""
return np.array([list(self.state.update_state(p, control)) for p in particles])
def reset(self, num_particles=2000):
"""Reset initial state and particle filter
Parameters
----------
num_particles : integer
Number of particles to build particle filter
Returns
-------
env_obs : array_like
Heatmap distribution of observed particles from reset filter
"""
self.iters = 0
self.state.target_state = self.state.init_target_state()
self.state.sensor_state = self.state.init_sensor_state()
# Setup particle filter
self.pf = ParticleFilter(
prior_fn=lambda n: np.array([self.state.random_state() for i in range(n)]),
observe_fn=lambda states, **kwargs: np.array(
[np.array(self.sensor.observation(x)) for x in states]
),
n_particles=num_particles,
dynamics_fn=self.dynamics,
noise_fn=lambda x, **kwargs: x,
resample_proportion=0.005,
# noise_fn=lambda x:
# gaussian_noise(x, sigmas=[0.2, 0.2, 0.1, 0.05, 0.05]),
weight_fn=lambda hyp, o, xp=None, **kwargs: [
self.sensor.weight(None, o, state=x) for x in xp
],
resample_fn=systematic_resample,
column_names=["range", "heading", "relative_course", "own_speed"],
)
env_obs = self.env_observation()
return env_obs
# returns observation, reward, done, info
def step(self, action_idx):
"""Function to make step based on
state variables and action index
Parameters
----------
action_idx : integer
Index for action to make step
Returns
-------
env_obs : array_like
Heatmap distribution of observed particles from filter
reward : float
Reward value for specified action
0 : int
Placeholder integer value
info : dict
Dictionary to track step specific values (reward, iteration)
"""
# Get action based on index
action = self.actions.index_to_action(action_idx)
# Determine next state based on action & current state variables
next_state = self.state.update_state(self.state.target_state, action)
# Update absolute position of sensor
self.state.update_sensor(action)
# Get sensor observation
observation = self.sensor.observation(next_state)