def homing(T, tb1, memory, cx, acceleration=default_acc, drag=default_drag, current_heading=0.0, current_velocity=np.array([0.0, 0.0]), turn_sharpness=1.0, logging=True, bump_shift=0.0, filtered_steps=0.0): """Based on current state, return home. First is duplicate""" headings = np.empty(T + 1) headings[0] = current_heading velocity = np.empty([T + 1, 2]) velocity[0, :] = current_velocity if logging: cx_log = CXLogger(0, T + 1, cx) else: cx_log = None for t in range(1, T + 1): r = headings[t - 1] - headings[t - 2] r = (r + np.pi) % (2 * np.pi) - np.pi tl2, cl1, tb1, tn1, tn2, memory, cpu4, cpu1, motor = update_cells( heading=headings[t - 1] + np.sign(r) * bump_shift, # Remove sign to use proportionate shift velocity=velocity[t - 1], tb1=tb1, memory=memory, cx=cx, filtered_steps=filtered_steps) if logging: cx_log.update_log(t, tl2, cl1, tb1, tn1, tn2, memory, cpu4, cpu1, motor) rotation = turn_sharpness * motor headings[t], velocity[t, :] = bee_simulator.get_next_state( headings[t - 1], velocity[t - 1, :], rotation, acceleration, drag) return headings, velocity, cx_log
def walking(T, tb1, memory, cx, acceleration=default_acc, drag=default_drag, current_heading=0.0, current_velocity=np.array([0.0, 0.0]), turn_sharpness=1.0, logging=True, bump_shift=0.0, filtered_steps=0.0, current_pos=[0, 0], reward=None): headings = np.empty(T + 1) headings[0] = current_heading velocity = np.empty([T + 1, 2]) velocity[0, :] = current_velocity positions = np.empty([T + 1, 2]) positions[0, :] = current_pos if logging: cx_log = CXLogger(0, T + 1, cx) else: cx_log = None inrew = [] for t in range(1, T + 1): r = headings[t - 1] - headings[t - 2] r = (r + np.pi) % (2 * np.pi) - np.pi tl2, cl1, tb1, tn1, tn2, memory, cpu4, cpu1, motor = update_cells( heading=headings[t - 1] + np.sign(r) * bump_shift, # Remove sign to use proportionate shift velocity=velocity[t - 1], tb1=tb1, memory=memory, cx=cx, filtered_steps=filtered_steps) if logging: cx_log.update_log(t, tl2, cl1, tb1, tn1, tn2, memory, cpu4, cpu1, motor) rotation = turn_sharpness * motor headings[t], velocity[t, :] = bee_simulator.get_next_state( headings[t - 1], velocity[t - 1, :], rotation, acceleration, drag) positions[t, :] = positions[t - 1, :] + velocity[t, :] if point_in_reward(positions[t, :], reward): # print('in') memory = init_memory() inrew.append(positions[t]) return dict(h=headings, v=velocity, pos=positions, log=cx_log, in_reward=inrew)
def generate_route(T=1500, mean_acc=default_acc, drag=default_drag, kappa=100.0, max_acc=default_acc, min_acc=0.0, vary_speed=False, seed=None): """Generate a random outbound route using bee_simulator physics. The rotations are drawn randomly from a von mises distribution and smoothed to ensure the agent makes more natural turns.""" if seed: np.random.seed(seed) # Generate random turns mu = 0.0 vm = np.random.vonmises(mu, kappa, T) rotation = lfilter([1.0], [1, -0.4], vm) rotation[0] = 0.0 # Randomly sample some points within acceptable acceleration and # interpolate to create smoothly varying speed. if vary_speed: if T > 200: num_key_speeds = T / 50 else: num_key_speeds = 4 x = np.linspace(0, 1, num_key_speeds) y = np.random.random(num_key_speeds) * (max_acc - min_acc) + min_acc f = interp1d(x, y, kind='cubic') xnew = np.linspace(0, 1, T, endpoint=True) acceleration = f(xnew) else: acceleration = mean_acc * np.ones(T) # Get headings and velocity for each step headings = np.zeros(T) velocity = np.zeros([T, 2]) for t in range(1, T): headings[t], velocity[t, :] = bee_simulator.get_next_state( heading=headings[t - 1], velocity=velocity[t - 1, :], rotation=rotation[t], acceleration=acceleration[t], drag=drag) return headings, velocity
def update(t, x): self.heading, self.velocity = bee_simulator.get_next_state( heading=self.heading, velocity=self.velocity, rotation=x[0] * dt, acceleration=self.acceleration, drag=self.drag) self.x += self.velocity[1] * dt self.y += self.velocity[0] * dt if self.trail_length > 0: if self.last_trail_time is None or t > self.last_trail_time + self.trail_dt: self.trail.append((self.x, self.y)) if len(self.trail) > self.trail_length: self.trail = self.trail[1:] self.last_trail_time = t return x