def update_particles(particles, action_policy, observation):
(particles, particle_weights) = particles
new_particles = []
new_particle_weights = {}
n = len(particles)
for i in range(n):
# sample() will pick a key from a dictionary,
# with probablity := value of that key
j = sample(particle_weights)
# action_policy() will return next state of agent, given current state
# Note that action_policy() itself can be stochastic.
j = action_policy(j)
# observation() gives probability of state, using evidence
# An example of evidence is sensor data of a robot.
new_particle_weights[j] = observation(j)
new_particles.append(j)
# normalize(dict) will divide all values in dict
# by sum([v for k,v in dict.items()])
normalize(new_particle_weights)
return (new_particles,new_particle_weights)