def poll(self, poll_time, on_enter=None, on_exit=None): print('starting polling') history = RingBuffer(capacity=20, dtype=np.float) entered = False while True: dist = self.distance() history.append(dist) if len(history) < 10: continue avg = np.median(history) if DEBUG: sys.stdout.write('\rdist: {:06.10f} avg: {:06.10f}'.format( dist, avg)) if not entered and avg < THRESHOLD: entered = True if on_enter: on_enter() elif entered and avg > THRESHOLD: entered = False if on_exit: on_exit() time.sleep(poll_time)
def test_acceptance(self): instance = RingBuffer(5) self.assertEqual(instance.write([1, 2, 3]), 3) self.assertEqual(instance.write([4, 5, 6]), 2) self.assertEqual(instance.read(), 1) self.assertEqual(instance.read(), 2) self.assertEqual(instance.read(), 3)
def test_remove(self): rb = RingBuffer(4) rs = RingStack(4) rq = RingQueue(4) ls = ListStack(4) lq = ListQueue(4) for name in TestRingBufferDS.names: rb.insert_keep_old(name) rs.insert(name) rq.insert(name) ls.insert(name) lq.insert(name) print("Before remove: ") print("RingBuffer:", rb) print("RingStack:", rs) print("RingQueue:", rq) print("ListStack:", ls) print("ListQueue:", lq) for i in range(2): rs.remove() rq.remove() ls.remove() lq.remove() print("After remove: ") print("RingBuffer:", rb) print("RingStack:", rs) print("RingQueue:", rq) print("ListStack:", ls) print("ListQueue:", lq)
def __init__(self, capacity): if capacity < 0: print("Cannot have a negative list") exit() elif isinstance(capacity, int): self.ring_buffer = RingBuffer(capacity) else: print("Incorrect format of Capacity")
def __init__(self, capacity): # store as instance variable self._capacity = capacity # create list of size capacity self.RingBuffer = RingBuffer(self._capacity) # set other instance variable defaults self.top = -1 self.size = 0
def test_add_values_replaces_entire_queue(): ring_buffer = RingBuffer([x for x in range(5)], 5) ring_buffer.add_values([ x for x in range(50) if x % 10 == 0 ]) expected_queue = deque([0, 10, 20, 30, 40]) assert ring_buffer.queue == expected_queue
def test_add_values_replaces_newest_values(): ring_buffer = RingBuffer([x for x in range(5)], 5) ring_buffer.add_values([ x for x in range(100) if x % 10 == 0 ]) expected_queue = deque([50, 60, 70, 80, 90]) assert ring_buffer.queue == expected_queue
def test_basic_wrap(self): b = RingBuffer(4) b.write(b"ab") self.assertEqual(b.read(), b"ab") self.assertEqual(b.bytes_used(), 0) b.write(b"cde") self.assertEqual(b.read(), b"cd") self.assertEqual(b.read(), b"e") self.assertEqual(b.read(), None)
def __init__(self): self.last5_left = RingBuffer(5) self.last5_right = RingBuffer(5) self.avg_left_fit = 0 self.avg_right_fit = 0 self.avg_left_curv = 0 self.avg_right_curv = 0 self.avg_left_dist = 0 self.avg_right_dist = 0
def __init__(self, output_size, layers, memory_size=3000, batch_size=32, use_ddqn=False, default_policy=False, model_filename=None, tb_dir="tb_log", epsilon=1, epsilon_lower_bound=0.1, epsilon_decay_function=lambda e: e - (0.9 / 1000000), gamma=0.95, optimizer=RMSprop(0.00025), learn_thresh=50000, update_rate=10000): """ Args: output_size: number of actions layers: list of Keras layers memory_size: size of replay memory batch_size: size of batch for replay memory use_ddqn: boolean for choosing between DQN/DDQN default_policy: boolean for loading a model from a file model_filename: name of file to load tb_dir: directory for tensorboard logging epsilon: annealing function upper bound epsilon_lower_bound: annealing function lower bound epsilon_decay_function: lambda annealing function gamma: discount factor hyper parameter optimizer: Keras optimiser learn_thresh: number of steps to perform without learning update_rate: number of steps between network-target weights copy """ self.output_size = output_size self.memory = RingBuffer(memory_size) self.use_ddqn = use_ddqn self.default_policy = default_policy # Tensorboard parameters self.tb_step = 0 self.tb_gather = 500 self.tb_dir = tb_dir if tb_dir is not None: self.tensorboard = TensorBoard(log_dir='./Monitoring/%s' % tb_dir, write_graph=False) print("Tensorboard Loaded! (log_dir: %s)" % self.tensorboard.log_dir) # Exploration/Exploitation parameters self.epsilon = epsilon self.epsilon_decay_function = epsilon_decay_function self.epsilon_lower_bound = epsilon_lower_bound self.total_steps = 0 # Learning parameters self.gamma = gamma self.loss = huber_loss #'mean_squared_error' self.optimizer = optimizer self.batch_size = batch_size self.learn_thresh = learn_thresh # Number of steps from which the network starts learning self.update_rate = update_rate self.discrete_state = False if self.default_policy: self.evaluate_model = self.load_model(model_filename) else: self.evaluate_model = self.build_model(layers) if self.use_ddqn: self.target_model = self.build_model(layers)
def test_reading_exact_wrapping_singles(self): b = RingBuffer(4) b.write(b"ab") self.assertEqual(b.read(), b"ab") b.write(b"defg") self.assertEqual(b.read_exactly(1), b"d") self.assertEqual(b.read_exactly(1), b"e") self.assertEqual(b.read_exactly(1), b"f")
def __init__(self, capacity): """ initlizes the Stack class :param own: slef object :param capacity: Capacity of the Stack """ # create list of size capacity #self.list_stack = [None] * capacity self.RB = RingBuffer(capacity) # store as instance variable self.capacity1 = capacity # set other instance variable defaults self.top1 = None
class RingBufferTests(unittest.TestCase): def setUp(self): self.buffer = RingBuffer(5) self.buffer_2 = RingBuffer(5) def test_ring_buffer(self): self.assertEqual(len(self.buffer.storage), 5) self.buffer.append('a') self.buffer.append('b') self.buffer.append('c') self.buffer.append('d') self.assertEqual(len(self.buffer.storage), 5) self.assertEqual(self.buffer.get(), ['a', 'b', 'c', 'd'])
def __init__(self, recent_acceptance_lag=None, verbosity=1): self.start_time = None self.last_update_time = None self.verbosity = verbosity self.recent_acceptance_lag = recent_acceptance_lag self.last_update_iteration = 0 self.n_iter = None self.__total_errors__ = 0 self.__iter_time_buffer__ = RingBuffer(100) self.__text_field__ = ipywidgets.HTML() self.__error_field__ = None self.__error_label__ = None
def test_reading_exact_wrapping_full(self): b = RingBuffer(4) b.write(b"ab") self.assertEqual(b.read(), b"ab") b.write(b"defg") self.assertEqual(b.read_exactly(4), b"defg")
def test_size_wrap_and_full(self): b = RingBuffer(2) b.write(b"a") self.assertEqual(b.read(), b"a") self.assertEqual(b.read(), None) b.write(b"bc") self.assertEqual(b.read(), b"b") self.assertEqual(b.read(), b"c") self.assertEqual(b.read(), None)
def test_read_works_after_several_writes(self): instance = RingBuffer(3) self.assertEqual(instance.write([1, 2]), 2) self.assertEqual(instance.read(), 1) self.assertEqual(instance.write([3, 4]), 2) self.assertEqual(instance.read(), 2) self.assertEqual(instance.write([5, 6]), 1) self.assertEqual(instance.read(), 3)
def __init__(self, lbuf_size, curr_cycle, num_clients): self._curr_cycle = curr_cycle # For when a client connects mid game # Each client has their own buffer of events # Indexing self._lockstep_bufs = {RingBuffer(lbuf_size) * 2} * num_clients self._lbuf_size = lbuf_size
def __init__(self, comparison_function, buffer_len, dimensions, prest_gamma, prest_lmbda, prest_theta, pst_gamma, pst_lmbda, pst_theta, prest_alpha=0.5, prest_beta=0.5, prest_delta=0.5, prest_eta=0.9995, prest_e_w=0.05, prest_e_n=0.0006, pst_alpha=0.5, pst_beta=0.75, pst_delta=0.5, pst_eta=0.9995, pst_e_w=0.05, pst_e_n=0.0006, ma_window_len=None, ma_recalc_delay=1, ddof=1): values = inspect.getargvalues(inspect.currentframe())[3] print('Init parameters: {}'.format(values)) self.comparison_function = comparison_function self.buffer_len = buffer_len self.dimensions = dimensions self.present = mgng.MGNG(dimensions=dimensions, gamma=int(prest_gamma), lmbda=int(prest_lmbda), theta=int(prest_theta), alpha=float(prest_alpha), beta=float(prest_beta), delta=float(prest_delta), eta=float(prest_eta), e_w=float(prest_e_w), e_n=float(prest_e_n)) self.past = mgng.MGNG(dimensions=dimensions, gamma=int(pst_gamma), lmbda=int(pst_lmbda), theta=int(pst_theta), alpha=float(pst_alpha), beta=float(pst_beta), delta=float(pst_delta), eta=float(pst_eta), e_w=float(pst_e_w), e_n=float(pst_e_n)) # self.buffer = deque(maxlen=self.buffer_len) self.buffer = RingBuffer([[np.nan]*dimensions]*buffer_len) if ma_window_len is None: # self.ma_window = deque(maxlen=self.buffer_len) self.ma_window = RingBuffer([np.nan]*buffer_len) else: # self.ma_window = deque(maxlen=ma_window_len) self.ma_window = RingBuffer([np.nan]*ma_window_len) self.ma_recalc_delay = ma_recalc_delay self.ddof = ddof self.anomaly_mean = None self.anomaly_std = None self.t = 0
def __init__(self, update_interval_secs, total_secs=3600, data_type=float): '''XYBuffer constructor Args: update_interval_secs (int): How often is the pyqtgraph updated total_secs (int): What's the maximum length of the moving window. Default size 3600 seconds - i.e. 1 hour data_type(): use numpy data types ''' self.buffer_size = total_secs // update_interval_secs self.x_buffer = RingBuffer(size_max=self.buffer_size, default_value=0, dtype=np.uint32) self.y_buffer = RingBuffer(size_max=self.buffer_size, default_value=0.0, dtype=data_type) self._x_counter = 0
def test_size_zero_buffer(self): b = RingBuffer(0) self.assertEqual(b.bytes_total(), 0) self.assertEqual(b.bytes_free(), 0) self.assertEqual(b.bytes_used(), 0) with self.assertRaises(ValueError): b.write(b"a")
class Stack: __slots__ = "ring_buffer" def __init__(self, capacity): if capacity < 0: print ("Cannot have a negative list") exit() elif isinstance(capacity, int): self.ring_buffer = RingBuffer(capacity) else: print ("Incorrect format of Capacity") def __str__(self): return self.ring_buffer.__str__() def push(self, data): if (data == None): print ("Data has to be sent!!") return -1 else: self.ring_buffer.insert_keep_new(data) def pop(self): self.ring_buffer.remove_newest() def peek(self): return self.ring_buffer.head def capacity(self): self.ring_buffer.capacity()
def test_insert(self): rb = RingBuffer(4) rs = RingStack(4) rq = RingQueue(4) ls = ListStack(4) lq = ListQueue(4) for name in TestRingBufferDS.names: rb.insert_keep_old(name) rs.insert(name) rq.insert(name) ls.insert(name) lq.insert(name) print("RingBuffer:", rb) print("RingStack:", rs) print("RingQueue:", rq) print("ListStack:", ls) print("ListQueue:", lq)
def test_init(self): rb = RingBuffer(4) rs = RingStack(4) rq = RingQueue(4) ls = ListStack(4) lq = ListQueue(4) self.assertIsNotNone(rb.head) self.assertEqual(rb.size(), 0) self.assertEqual(rs.top, -1) self.assertEqual(rs.size, 0) self.assertEqual(ls.top, -1) self.assertEqual(ls.size, 0) self.assertEqual(rq.front, -1) self.assertEqual(rq.back, -1) self.assertEqual(rq.size, 0) self.assertEqual(lq.front, -1) self.assertEqual(lq.back, -1) self.assertEqual(lq.size, 0)
def add_ip(self, ip): self.__lock.acquire() try: current_dt = datetime.datetime.now() if not self.dictionary.__contains__(ip): self.dictionary[ip] = RingBuffer(self.__buffer_size) self.dictionary[ip].append(current_dt) finally: self.__lock.release()
def __init__(self, queue): super().__init__(queue) self._name = "SoundEffectEngine" self._chunk = 1024 self._player = pyaudio.PyAudio() self._wav_dir = config.SOUNDFILE_DIR self._wav_files = {} self._cur_wav_file = None self._stream = None self._dataFile = open("history.csv", "w") self._dataWriter = csv.writer(self._dataFile, delimiter=",", quotechar='"', quoting=csv.QUOTE_MINIMAL) self._currentBpms = RingBuffer(config.AVG_WINDOW) self._heartbeat = config.HEARTBEAT_TIMEOUT if config.BPM_RANGE_LOW >= config.BPM_RANGE_HIGH: raise ValueError( "BPM Ranges are not configured correctly in config")
class XYBuffer(object): '''A class for holding X and Y about the sensor Basically a ring buffer, which serves as the data input for plotting sensor values as a moving windows in pyqtgraph ''' def __init__(self, update_interval_secs, total_secs=3600, data_type=float): '''XYBuffer constructor Args: update_interval_secs (int): How often is the pyqtgraph updated total_secs (int): What's the maximum length of the moving window. Default size 3600 seconds - i.e. 1 hour data_type(): use numpy data types ''' self.buffer_size = total_secs // update_interval_secs self.x_buffer = RingBuffer(size_max=self.buffer_size, default_value=0, dtype=np.uint32) self.y_buffer = RingBuffer(size_max=self.buffer_size, default_value=0.0, dtype=data_type) self._x_counter = 0 def append_value(self, y_value): '''Appends the y_value to the y_buffer and increments the sample number in the x_buffer ''' self.y_buffer.append(y_value) self._x_counter += 1 self.x_buffer.append(self._x_counter) def get_actual_filled_values(self): '''Gets the values from the buffer, which are already filled Used for data plotting ''' return self.x_buffer.partial, self.y_buffer.partial
class Queue: __slots__ = 'ring_buffer', 'max_capacity' """constructor with intial max capacity of 10""" def __init__(self, capacity=10): self.ring_buffer = RingBuffer(capacity) self.max_capacity = capacity """to string function uses the internal to string of ring buffer but points to the end""" def __str__(self): return str(self.ring_buffer) + '<-- end ' """ enqueue function which inserts values in the queue """ def enqueue(self, value): if value is None: raise Exception('cannot enqueue None element in the queue') else: self.ring_buffer.insert_keep_old(value) """ dequeue function which removes values from the queue using FIFO method raises an exception if dequeue is done on the empty queue""" def dequeue(self): if self.ring_buffer.size() is 0: raise Exception('dequeue from a empty queue is not allowed') else: return self.ring_buffer.remove_oldest() """ returns the current size of the stack""" def size(self): return self.ring_buffer.size() """ :return the max capacity of the stack """ def capacity(self): return self.ring_buffer.capacity() """ only returns the value of the first element in the queue does not removed the element from the queue """ def peek(self): if self.size() < 1: raise Exception('peek into a empty queue is not allowed') else: return self.ring_buffer.start_element()
def __init__(self): self.state = Transformer.FIRST_INDEX self.transitions = [ [Transformer.FIRST_INDEX, self.number_exists, self.return_number_as_is, Transformer.SEQUENCE], [Transformer.FIRST_INDEX, self.number_not_exists, self.return_number_as_is, Transformer.NO_SEQUENCE], [Transformer.SEQUENCE, self.number_exists, self.return_number_as_is, Transformer.SEQUENCE], [Transformer.SEQUENCE, self.number_not_exists, self.return_nothing, Transformer.SEQUENCE_BREAK], [Transformer.SEQUENCE_BREAK, self.number_exists, self.return_number_as_is, Transformer.BUFFERING_SEQUENCE_BREAK], #Brain F**k [Transformer.SEQUENCE_BREAK, self.number_not_exists, self.return_nothing, Transformer.SEQUENCE_BREAK], [Transformer.BUFFERING_SEQUENCE_BREAK, all_conditions(self.number_exists, self.is_buffer_not_filled), self.return_number_as_is, Transformer.BUFFERING_SEQUENCE_BREAK], [Transformer.BUFFERING_SEQUENCE_BREAK, all_conditions(self.number_exists, self.is_buffer_filled), self.interpolate_numbers, Transformer.SEQUENCE], [Transformer.BUFFERING_SEQUENCE_BREAK, self.number_not_exists, self.return_buffered_numbers_as_is, Transformer.NO_SEQUENCE], [Transformer.NO_SEQUENCE, self.number_exists, self.return_number_as_is, Transformer.SEQUENCE], [Transformer.NO_SEQUENCE, self.number_not_exists, self.return_number_as_is, Transformer.NO_SEQUENCE] ] self.buffer = RingBuffer(length = BUFFER_FILLED_SIZE * 2 + SEQUENCE_BREAK_MAX_SIZE)
class DetectGoal: _HISTORY_LENGTH = 5 def __init__(self, options): self.options = options["Goals"] self.ball_in_field_history = RingBuffer(self.options['HistoryLength']) self.ball_in_left_goal_history = RingBuffer(2 * self.options['HistoryLength']) self.ball_in_right_goal_history = RingBuffer(2 * self.options['HistoryLength']) def detect(self, image, ball): goal_keep_mask = np.zeros([image.shape[0], image.shape[1]], np.uint8) height, width, channels = image.shape # left goal keep left = self.options['Gates'][0] cv2.rectangle(goal_keep_mask, tuple(left[0]), tuple(left[1]), (255, 255, 255), thickness=-1) # right goal keep right = self.options['Gates'][1] cv2.rectangle(goal_keep_mask, tuple(right[0]), tuple(right[1]), (255, 255, 255), thickness=-1) ball_boundaries = ball.get_boundaries(width) self.ball_in_field_history.extend(np.array(ball.is_visible())) self.ball_in_left_goal_history.extend(goal_keep_mask[ball_boundaries[0][1], ball_boundaries[0][0]] == 255) self.ball_in_right_goal_history.extend(goal_keep_mask[ball_boundaries[1][1], ball_boundaries[1][0]] == 255) if self._detect_goal(self.ball_in_left_goal_history): return [False, True] elif self._detect_goal(self.ball_in_right_goal_history): return [True, False] else: return [False, False] def _detect_goal(self, ball_in_goal_area_history): """ Checks goal within goal area and within history of frames :param ball_in_goal_area_history: :return: whether goal was detected """ if np.any(ball_in_goal_area_history.get()) and not np.any(self.ball_in_field_history.get()): ball_in_goal_area_history.clear() return True return False
class Stack: __slots__ = "ring_buffer" def __init__(self, capacity): self.ring_buffer = RingBuffer(capacity) def __str__(self): return self.ring_buffer.__str__() def push(self, value): self.ring_buffer.insert_keep_new(value) def pop(self): self.ring_buffer.remove_newest() def capacity(self): self.ring_buffer.capacity() def peek(self): return self.ring_buffer.head
class Queue: __slots__ = "ring_buffer", "link" def __init__(self, capacity): self.ring_buffer = RingBuffer(capacity) def __str__(self): return self.ring_buffer.__str__() def enqueue(self, value): self.ring_buffer.insert_keep_old(value) def dequeue(self): self.ring_buffer.remove_oldest() def peek(self): return self.ring_buffer.head def capacity(self): return self.ring_buffer.capacity()
def __init__(self, g, zeta, leader, trajectory_delay=2.0, update_delta_t=0.01, orig_leader=None, orig_leader_delay=None, noise_sigma=0.0, log_file=None, visibility_fov=config.FOV_ANGLE, visibility_radius=None, id=None, dt=0.02): """ Construct a follower Behavior. leader is the Bot to be followed g > 0 is a tuning parameter zeta in (0, 1) is a damping coefficient trajectory_delay is the time interval between leader and follower """ super(Follower, self).__init__() assert(isinstance(leader, Bot)) assert(isinstance(orig_leader, Bot)) self.radius = config.BOT_RADIUS self.leader = leader self.orig_leader = orig_leader self.trajectory_delay = trajectory_delay assert g > 0, "Follower: g parameter must be positive" self.g = g assert 0 < zeta < 1, "Follower: zeta parameter must be in (0, 1)" self.zeta = zeta # leader_positions stores config.POSITIONS_BUFFER_SIZE tuples; # tuple's first field is time, second is the # corresponding position of the leader self.leader_positions = RingBuffer(config.POSITIONS_BUFFER_SIZE) self.noisy_leader_positions = RingBuffer(config.POSITIONS_BUFFER_SIZE) self.leader_is_visible = False # precise_orig_leader_states stores the first leader's precise state; # used to calculate "real" errors (as opposed to calculations # w.r.t. the approximation curve) self.precise_orig_leader_states = [] # precise_leader_states is used for the same purpose, but with the bot's own leader self.precise_leader_states = [] self.update_delta_t = update_delta_t needed_buffer_size = config.SAMPLE_COUNT // 2 + self.trajectory_delay / self.update_delta_t if needed_buffer_size > config.POSITIONS_BUFFER_SIZE: sys.stderr.write("leader_positions buffer is too small for update_delta_t = {}".format(self.update_delta_t) + " (current = {}, required = {})".format(config.POSITIONS_BUFFER_SIZE, needed_buffer_size)) raise RuntimeError, "leader_positions buffer is too small" self.last_update_time = 0.0 self.noise_sigma = noise_sigma self.log_file = log_file self.visibility_fov = visibility_fov if visibility_radius is None: visibility_radius = 2.0 * trajectory_delay * config.BOT_VEL_CAP self.visibility_radius = visibility_radius self.id = id; if orig_leader_delay is None: orig_leader_delay = trajectory_delay self.orig_leader_delay = orig_leader_delay if self.log_file is not None: log_dict = {"id": self.id, "g": self.g, "zeta": self.zeta, "noise_sigma": self.noise_sigma, "reference_points_cnt": config.SAMPLE_COUNT, "trajectory_delay": trajectory_delay} print >> self.log_file, log_dict q = 0.01 # std of process r = 0.05 # std of measurement self.delta_time = dt if not config.DISABLE_UKF: points = MerweScaledSigmaPoints(n=6, alpha=.1, beta=2., kappa=-3) self.ukf = UKF(dim_x=6, dim_z=2, fx=f, hx=h, dt=dt, points=points) self.ukf_x_initialized = 0 #self.ukf.x = np.array([0, 0, 1, pi/4, 0, 0]) self.ukf.R = np.diag([r, r]); self.ukf.Q = np.diag([0, 0, 0, 0, q, q])
class Follower(BehaviorBase): def __init__(self, g, zeta, leader, trajectory_delay=2.0, update_delta_t=0.01, orig_leader=None, orig_leader_delay=None, noise_sigma=0.0, log_file=None, visibility_fov=config.FOV_ANGLE, visibility_radius=None, id=None, dt=0.02): """ Construct a follower Behavior. leader is the Bot to be followed g > 0 is a tuning parameter zeta in (0, 1) is a damping coefficient trajectory_delay is the time interval between leader and follower """ super(Follower, self).__init__() assert(isinstance(leader, Bot)) assert(isinstance(orig_leader, Bot)) self.radius = config.BOT_RADIUS self.leader = leader self.orig_leader = orig_leader self.trajectory_delay = trajectory_delay assert g > 0, "Follower: g parameter must be positive" self.g = g assert 0 < zeta < 1, "Follower: zeta parameter must be in (0, 1)" self.zeta = zeta # leader_positions stores config.POSITIONS_BUFFER_SIZE tuples; # tuple's first field is time, second is the # corresponding position of the leader self.leader_positions = RingBuffer(config.POSITIONS_BUFFER_SIZE) self.noisy_leader_positions = RingBuffer(config.POSITIONS_BUFFER_SIZE) self.leader_is_visible = False # precise_orig_leader_states stores the first leader's precise state; # used to calculate "real" errors (as opposed to calculations # w.r.t. the approximation curve) self.precise_orig_leader_states = [] # precise_leader_states is used for the same purpose, but with the bot's own leader self.precise_leader_states = [] self.update_delta_t = update_delta_t needed_buffer_size = config.SAMPLE_COUNT // 2 + self.trajectory_delay / self.update_delta_t if needed_buffer_size > config.POSITIONS_BUFFER_SIZE: sys.stderr.write("leader_positions buffer is too small for update_delta_t = {}".format(self.update_delta_t) + " (current = {}, required = {})".format(config.POSITIONS_BUFFER_SIZE, needed_buffer_size)) raise RuntimeError, "leader_positions buffer is too small" self.last_update_time = 0.0 self.noise_sigma = noise_sigma self.log_file = log_file self.visibility_fov = visibility_fov if visibility_radius is None: visibility_radius = 2.0 * trajectory_delay * config.BOT_VEL_CAP self.visibility_radius = visibility_radius self.id = id; if orig_leader_delay is None: orig_leader_delay = trajectory_delay self.orig_leader_delay = orig_leader_delay if self.log_file is not None: log_dict = {"id": self.id, "g": self.g, "zeta": self.zeta, "noise_sigma": self.noise_sigma, "reference_points_cnt": config.SAMPLE_COUNT, "trajectory_delay": trajectory_delay} print >> self.log_file, log_dict q = 0.01 # std of process r = 0.05 # std of measurement self.delta_time = dt if not config.DISABLE_UKF: points = MerweScaledSigmaPoints(n=6, alpha=.1, beta=2., kappa=-3) self.ukf = UKF(dim_x=6, dim_z=2, fx=f, hx=h, dt=dt, points=points) self.ukf_x_initialized = 0 #self.ukf.x = np.array([0, 0, 1, pi/4, 0, 0]) self.ukf.R = np.diag([r, r]); self.ukf.Q = np.diag([0, 0, 0, 0, q, q]) def point_in_fov(self, p): if length(p - self.pos) > self.visibility_radius: return False d = p - self.pos ang = atan2(cross(self.real_dir, d), dot(self.real_dir, d)) return -0.5 * self.visibility_fov < ang < 0.5 * self.visibility_fov def point_is_visible(self, p, obstacles): if not self.point_in_fov(p): return False try: ray = Ray(self.pos, p - self.pos) except NormalizationError: ray = Ray(self.pos, Vector(1.0, 0.0)) i = first_intersection(ray, obstacles) return (i is None) or (length(i - self.pos) > length(p - self.pos)) def store_leaders_state(self, engine, obstacles): if self.point_is_visible(self.leader.real.pos, obstacles): self.leader_is_visible = True noisy_pos = self.leader.real.pos noisy_pos += Vector(gauss(0.0, self.noise_sigma), gauss(0.0, self.noise_sigma)) if config.DISABLE_UKF: filtered_pos = noisy_pos else: if (self.ukf_x_initialized == 0): self.ukf_x1 = noisy_pos self.ukf_x_initialized += 1 filtered_pos = noisy_pos self.ukf.x = np.array([noisy_pos.x, noisy_pos.y, 0, pi/2, 0, 0]) #elif (self.ukf_x_initialized == 1): # self.ukf_x2 = noisy_pos # self.ukf_x_initialized += 1 # self.ukf.x = np.array([noisy_pos.x, noisy_pos.y, # length((noisy_pos - self.ukf_x1) / self.delta_time), # atan2(noisy_pos.y - self.ukf_x1.y, # noisy_pos.x - self.ukf_x1.x), # 0, 0]) # filtered_pos = noisy_pos else: # UKF is initialized self.ukf.predict() self.ukf.update(np.array(noisy_pos)) filtered_pos = Point(self.ukf.x[0], self.ukf.x[1]) self.leader_noisy_pos = noisy_pos self.noisy_leader_positions.append(TimedPosition(engine.time, noisy_pos)) self.leader_positions.append(TimedPosition(engine.time, filtered_pos)) self.last_update_time = engine.time else: self.leader_is_visible = False if not config.DISABLE_UKF: self.ukf.predict() #TODO: do magic with UKF? orig_leader_theta = atan2(self.orig_leader.real.dir.y, self.orig_leader.real.dir.x) self.precise_orig_leader_states.append(TimedState(time=engine.time, pos=self.orig_leader.real.pos, theta=orig_leader_theta)) leader_theta = atan2(self.leader.real.dir.y, self.leader.real.dir.x) self.precise_leader_states.append(TimedState(time=engine.time, pos=self.leader.real.pos, theta=leader_theta)) def polyfit_trajectory(self, pos_data, t): x_pos = np.array([el.pos.x for el in pos_data]) y_pos = np.array([el.pos.y for el in pos_data]) times = np.array([el.time for el in pos_data]) # needed for trajectory rendering self.t_st = times[0] self.t_fn = max(times[-1], t) # calculate quadratic approximation of the reference trajectory x_poly = np.polyfit(times, x_pos, deg=2) y_poly = np.polyfit(times, y_pos, deg=2) known = Trajectory.from_poly(x_poly, y_poly) return known, x_pos[-1], y_pos[-1], times[-1] def extend_trajectory(self, known, last_x, last_y, last_t): # now adding a circle to the end of known trajectory # k is signed curvature of the trajectry at t_fn try: k = known.curvature(last_t) except (ValueError, ZeroDivisionError): k = config.MIN_CIRCLE_CURVATURE if abs(k) < config.MIN_CIRCLE_CURVATURE: k = copysign(config.MIN_CIRCLE_CURVATURE, k) if abs(k) > config.MAX_CURVATURE: k = copysign(config.MAX_CURVATURE, k) radius = abs(1.0/k) # trajectory direction at time t_fn try: d = normalize(Vector(known.dx(last_t), known.dy(last_t))) except NormalizationError: d = self.real_dir r = Vector(-d.y, d.x) / k center = Point(last_x, last_y) + r phase = atan2(-r.y, -r.x) #freq = known.dx(last_t) / r.y freq = k self.x_approx = lambda time: known.x(time) if time < last_t else \ center.x + radius * cos(freq * (time - last_t) + phase) self.y_approx = lambda time: known.y(time) if time < last_t else \ center.y + radius * sin(freq * (time - last_t) + phase) dx = lambda time: known.dx(time) if time < last_t else \ -radius * freq * sin(freq * (time - last_t) + phase) dy = lambda time: known.dy(time) if time < last_t else \ radius * freq * cos(freq * (time - last_t) + phase) ddx = lambda time: known.ddx(time) if time < last_t else \ -radius * freq * freq * cos(freq * (time - last_t) + phase) ddy = lambda time: known.ddy(time) if time < last_t else \ -radius * freq * freq * sin(freq * (time - last_t) + phase) # FIXME: don't use division by y if y == 0 try: if isnan(self.x_approx(last_t + 1)): return known except Exception: return known return Trajectory(x=self.x_approx, y=self.y_approx, dx=dx, dy=dy, ddx=ddx, ddy=ddy) def generate_trajectory(self, leader_positions, t): arr = get_interval(self.leader_positions, t, config.SAMPLE_COUNT) self.traj_interval = arr if len(arr) == 0: return None known, last_x, last_y, last_t = self.polyfit_trajectory(arr, t) if config.DISABLE_CIRCLES: return known else: return self.extend_trajectory(known, last_x, last_y, last_t) def calc_desired_velocity(self, bots, obstacles, targets, engine): # update trajectory if engine.time - self.last_update_time > self.update_delta_t: self.store_leaders_state(engine, obstacles) # reduce random movements at the start # TODO: this causes oscillations that smash bots into each other. # Change to something smoother. PID control? #if self.leader_is_visible and length(self.pos - self.leader_noisy_pos) < MIN_DISTANCE_TO_LEADER: # return Instr(0.0, 0.0) t = engine.time - self.trajectory_delay self.trajectory = self.generate_trajectory(self.leader_positions, t) if self.trajectory is None: return Instr(0.0, 0.0) dx = self.trajectory.dx dy = self.trajectory.dy ddx = self.trajectory.ddx ddy = self.trajectory.ddy # calculate the feed-forward velocities v_fun = lambda time: sqrt(dx(time)**2 + dy(time)**2) #omega_fun = lambda time: (dx(time) * 2 * y_poly[0] - dy(time) * 2 * x_poly[0]) / (dx(time)**2 + dy(time)**2) omega_fun = lambda time: (dx(time) * ddy(time) - dy(time) * ddx(time)) / (dx(time)**2 + dy(time)**2) v_ff = v_fun(t) omega_ff = omega_fun(t) # x_r, y_r and theta_r denote the reference robot's state r = State(x=self.trajectory.x(t), y=self.trajectory.y(t), theta=atan2(self.trajectory.dy(t), self.trajectory.dx(t))) self.target_point = Point(r.x, r.y) if isnan(v_ff): v_ff = 0.0 if isnan(omega_ff): omega_ff = 0.0 # cur is the current state cur = State(x=self.pos.x, y=self.pos.y, theta=atan2(self.real_dir.y, self.real_dir.x)) # error in the global (fixed) reference frame delta = State(x=r.x - cur.x, y=r.y - cur.y, theta=normalize_angle(r.theta - cur.theta)) # translate error into the follower's reference frame e = State(x= cos(cur.theta) * delta.x + sin(cur.theta) * delta.y, y=-sin(cur.theta) * delta.x + cos(cur.theta) * delta.y, theta=delta.theta) # calculate gains k_x, k_y, k_theta # these are just parameters, not a state in any sense! omega_n = sqrt(omega_ff**2 + self.g * v_ff**2) k_x = 2 * self.zeta * omega_n k_y = self.g k_theta = 2 * self.zeta * omega_n # calculate control velocities v = v_ff * cos(e.theta) + k_x * e.x se = sin(e.theta) / e.theta if e.theta != 0 else 1.0 omega = omega_ff + k_y * v_ff * se * e.y + k_theta * e.theta if config.DISABLE_FEEDBACK: v = v_ff omega = omega_ff real_e = State(0.0, 0.0, 0.0) try: orig_t = engine.time - self.orig_leader_delay orig_leader_state = lerp_precise_states(orig_t, self.precise_orig_leader_states) real_e = State(x=orig_leader_state.x - cur.x, y=orig_leader_state.y - cur.y, theta=normalize_angle(orig_leader_state.theta - cur.theta)) except LerpError: # not enough data is yet available to calculate error pass try: leader_t = engine.time - self.trajectory_delay leader_state = lerp_precise_states(leader_t, self.precise_leader_states) approx_e = State(x=leader_state.x - cur.x, y=leader_state.y - cur.y, theta=normalize_angle(leader_state.theta - cur.theta)) except LerpError: # same as above pass if self.log_file is not None: log_dict = {"id": self.id, "time": engine.time, "delta_time": engine.time_since_last_bot_update, "x": cur.x, "y": cur.y, "theta": cur.theta, "v": v, "omega": omega, "v_ff": v_ff, "omega_ff": omega_ff, "e_x": e.x, "e_y": e.y, "e_theta": e.theta, "real_e_x": real_e.x, "real_e_y": real_e.y, "real_e_theta": real_e.theta, "approx_e_x": approx_e.x, "approx_e_y": approx_e.y, "approx_e_theta": approx_e.theta, "leader_is_visible": self.leader_is_visible } print >> self.log_file, log_dict return Instr(v, omega) def draw(self, screen, field): if config.DISPLAYED_POINTS_COUNT > 0: k = config.POSITIONS_BUFFER_SIZE / config.DISPLAYED_POINTS_COUNT if k == 0: k = 1 for index, (time, point) in enumerate(self.leader_positions): if index % k == 0: draw_circle(screen, field, config.TRAJECTORY_POINTS_COLOR, point, 0.03, 1) for index, (time, point) in enumerate(self.noisy_leader_positions): if index % k == 0: draw_circle(screen, field, config.NOISY_TRAJECTORY_POINTS_COLOR, point, 0.03, 1) if config.DISPLAYED_USED_POINTS_COUNT > 0: k = len(self.traj_interval) / config.DISPLAYED_USED_POINTS_COUNT if k == 0: k = 1 for index, (time, point) in enumerate(self.traj_interval): if index % k == 0: draw_circle(screen, field, config.USED_TRAJECTORY_POINTS_COLOR, point, 0.03, 1) if config.DRAW_DELAYED_LEADER_POS: try: orig_leader_dir = Vector(cos(self.orig_leader_theta), sin(self.orig_leader_theta)) draw_directed_circle(screen, field, (0, 255, 0), self.orig_leader_pos, 0.2, orig_leader_dir, 1) except AttributeError: pass if config.DRAW_SENSOR_RANGE: ang = atan2(self.real_dir.y, self.real_dir.x) draw_arc(screen, field, config.SENSOR_COLOR, self.pos, self.visibility_radius, ang - 0.5 * self.visibility_fov, ang + 0.5 * self.visibility_fov, 1) draw_line(screen, field, config.SENSOR_COLOR, self.pos, self.pos + rotate(self.real_dir * self.visibility_radius, 0.5 * self.visibility_fov), 1) draw_line(screen, field, config.SENSOR_COLOR, self.pos, self.pos + rotate(self.real_dir * self.visibility_radius, -0.5 * self.visibility_fov), 1) try: if config.DRAW_VISIBILITY and self.leader_is_visible: draw_circle(screen, field, config.config.VISIBILITY_COLOR, self.leader.real.pos, 0.5 * config.BOT_RADIUS) except AttributeError: pass try: if config.DRAW_REFERENCE_POS: draw_circle(screen, field, config.TARGET_POINT_COLOR, self.target_point, 0.2) if config.DRAW_APPROX_TRAJECTORY and self.trajectory is not None: #if len(self.traj_interval) > 1: # p2 = Point(self.traj_interval[0].pos.x, # self.traj_interval[0].pos.y) # for t, p in self.traj_interval: # draw_line(screen, field, config.APPROX_TRAJECTORY_COLOR, p, p2) # p2 = p step = (self.t_fn - self.t_st) / config.TRAJECTORY_SEGMENT_COUNT for t in (self.t_st + k * step for k in xrange(config.TRAJECTORY_SEGMENT_COUNT)): p = Point(self.trajectory.x(t), self.trajectory.y(t)) p2 = Point(self.trajectory.x(t + step), self.trajectory.y(t + step)) draw_line(screen, field, config.APPROX_TRAJECTORY_COLOR, p, p2) p_st = Point(self.trajectory.x(self.t_st), self.trajectory.y(self.t_st)) p_fn = Point(self.trajectory.x(self.t_fn), self.trajectory.y(self.t_fn)) step = 0.5 / config.TRAJECTORY_SEGMENT_COUNT p = p_fn p2 = p t = self.t_fn it = 0 while it < config.TRAJECTORY_SEGMENT_COUNT and min(dist(p, p_fn), dist(p, p_st)) < 1.0: it += 1 t += step p2 = p p = Point(self.trajectory.x(t), self.trajectory.y(t)) draw_line(screen, field, config.APPROX_TRAJECTORY_COLOR, p, p2) except AttributeError as e: # approximation hasn't been calculated yet pass
# Some common settings HISTORY_LENGTH = 4 MAX_STEPS = 1000000 MAX_EPOCHS = 10 MINIBATCH_SIZE = 32 LONG_PRESS_TIMES = 4 GAMMA = 0.9 EPSILON = 0.1 UPDATE_FREQUENCY = 4 MAX_LIVES = ale.lives() episode_sum = 0 episode_sums = [] # Define history variables here images = RingBuffer(shape=(MAX_STEPS, 84, 84)) actions = RingBuffer(shape=(MAX_STEPS, 1)) rewards = RingBuffer(shape=(MAX_STEPS, 1)) terminals = RingBuffer(shape=(MAX_STEPS, 1)) # Initialize a neural network according to nature paper # Defining the neural net architecture model = Sequential() model.add(Convolution2D(16, 8, 8, subsample=(4,4), input_shape=(HISTORY_LENGTH,84,84))) model.add(Activation('relu')) model.add(Convolution2D(32, 4, 4, subsample=(2,2))) model.add(Activation('relu')) model.add(Flatten()) model.add(Dense(256)) model.add(Activation('relu')) model.add(Dense(legal_actions.shape[0]))
class Transformer: FIRST_INDEX = "FIRST_INDEX" SEQUENCE = "SEQUENCE" NO_SEQUENCE = "NO_SEQUENCE" SEQUENCE_BREAK = "SEQUENCE_BREAK" BUFFERING_SEQUENCE_BREAK = "BUFFERING_SEQUENCE_BREAK" def __init__(self): self.state = Transformer.FIRST_INDEX self.transitions = [ [Transformer.FIRST_INDEX, self.number_exists, self.return_number_as_is, Transformer.SEQUENCE], [Transformer.FIRST_INDEX, self.number_not_exists, self.return_number_as_is, Transformer.NO_SEQUENCE], [Transformer.SEQUENCE, self.number_exists, self.return_number_as_is, Transformer.SEQUENCE], [Transformer.SEQUENCE, self.number_not_exists, self.return_nothing, Transformer.SEQUENCE_BREAK], [Transformer.SEQUENCE_BREAK, self.number_exists, self.return_number_as_is, Transformer.BUFFERING_SEQUENCE_BREAK], #Brain F**k [Transformer.SEQUENCE_BREAK, self.number_not_exists, self.return_nothing, Transformer.SEQUENCE_BREAK], [Transformer.BUFFERING_SEQUENCE_BREAK, all_conditions(self.number_exists, self.is_buffer_not_filled), self.return_number_as_is, Transformer.BUFFERING_SEQUENCE_BREAK], [Transformer.BUFFERING_SEQUENCE_BREAK, all_conditions(self.number_exists, self.is_buffer_filled), self.interpolate_numbers, Transformer.SEQUENCE], [Transformer.BUFFERING_SEQUENCE_BREAK, self.number_not_exists, self.return_buffered_numbers_as_is, Transformer.NO_SEQUENCE], [Transformer.NO_SEQUENCE, self.number_exists, self.return_number_as_is, Transformer.SEQUENCE], [Transformer.NO_SEQUENCE, self.number_not_exists, self.return_number_as_is, Transformer.NO_SEQUENCE] ] self.buffer = RingBuffer(length = BUFFER_FILLED_SIZE * 2 + SEQUENCE_BREAK_MAX_SIZE) def number_exists(self, index, number): return number is not None def number_not_exists(self, index, number): return not self.number_exists(index, number) def return_number_as_is(self, index, number): self.buffer.add_item((index, number)) return [(index, number)] def return_nothing(self, index, number): self.buffer.add_item((index, number)) return [] def interpolate_numbers(self, index, number): groups = self.buffer.last_item_groups(3, grouper = none_number_grouper) extraction = sum(groups, []) e = filter(lambda p: p[1] is not None, extraction) t = map(lambda p: p[0], e) n = map(lambda p: p[1], e) f = interpolate.interp1d(t, n) d = [] for j, _ in groups[1]: d.append((j, f(j))) self.buffer.add_item((index, number)) return d + [(index, number)] def is_buffer_filled(self, index, number): group = self.buffer.last_item_group(grouper = none_number_grouper) print "group = %s" % group return len(group) >= BUFFER_FILLED_SIZE def is_buffer_not_filled(self, index, number): return not self.is_buffer_filled(index, number) def return_buffered_numbers_as_is(self, index, number): self.buffer.add_item((index, number)) groups = self.buffer.last_item_groups(2, grouper = none_number_grouper) print " =========> group = %s" % groups return groups[1] def is_sequence_break_too_long(self, index, number): sequence_break = self.buffer.last_item_group(grouper = none_number_grouper) return len(sequence_break) > SEQUENCE_BREAK_MAX_SIZE def is_sequence_break_short_enough(self, index, number): return not self.is_sequence_break_too_long(index, number) def transform(self, index, number): print "transform(%s, %s)" % (index, number) for transition in self.transitions: if transition[STATE] == self.state: print "self.state = %s" % self.state if transition[CONDITION](index, number): print " ---> %s success" % transition[CONDITION].__name__ pairs = transition[ACTION](index, number) next_state = transition[NEXT_STATE] self.state = next_state return pairs else: print " ---> %s failed" % transition[CONDITION].__name__ raise Exception("Unable to continue")
class JupyterProgress(base.ProgressBase): def __init__(self, recent_acceptance_lag=None, verbosity=1): self.start_time = None self.last_update_time = None self.verbosity = verbosity self.recent_acceptance_lag = recent_acceptance_lag self.last_update_iteration = 0 self.n_iter = None self.__total_errors__ = 0 self.__iter_time_buffer__ = RingBuffer(100) self.__text_field__ = ipywidgets.HTML() self.__error_field__ = None self.__error_label__ = None def initialise(self, n_iter): self.start_time = self.last_update_time = time.time() self.n_iter = n_iter if self.recent_acceptance_lag is None: self.recent_acceptance_lag = min(int(n_iter * 1.0 / 100), max_accept_lag) self.__text_field__.value = "<i>Waiting for {} iterations to have passed...</i>".format( self.recent_acceptance_lag ) # display(self.__progress_field__) display(self.__text_field__) def report_error(self, iter, error): if self.__error_field__ is None: self.__initialise_errors__() if self.verbosity > 0: self.__error_field__.value += "Iteration {}: {}\n".format(iter, error) self.__total_errors__ += 1 def update(self, iteration, acceptances): if self.n_iter is None: raise Exception("First pass in the number of iterations!") recent_acceptance_lag = self.recent_acceptance_lag now = time.time() toc = now - self.last_update_time do_update = toc > update_frequency_seconds and iteration > self.last_update_iteration if do_update or iteration == self.n_iter: delta_accept = ( acceptances[-recent_acceptance_lag:].mean() * 100 if iteration > recent_acceptance_lag else np.nan ) tot_accept = acceptances.mean() * 100 new_iterations = iteration - self.last_update_iteration time_per_iter = toc * 1.0 / new_iterations self.__iter_time_buffer__.append(time_per_iter) # exclude outliers all_iter_times = np.array(self.__iter_time_buffer__.data) if len(all_iter_times) > 1: all_iter_times = all_iter_times[ abs(all_iter_times - np.mean(all_iter_times)) < 2 * np.std(all_iter_times) ] time_per_iter = all_iter_times.mean() eta = (self.n_iter - iteration) * time_per_iter html = self.__get_text_field__(iteration, self.n_iter, delta_accept, tot_accept, time_per_iter, eta) self.__text_field__.value = html self.last_update_time = now self.last_update_iteration = iteration def __initialise_errors__(self): if self.verbosity > 0: self.__error_label__ = ipywidgets.HTML(value="<span style='color: red'>Errors:</span>") self.__error_field__ = ipywidgets.Textarea(disabled=True) display(self.__error_label__) display(self.__error_field__) def __get_text_field__(self, iteration, n_iter, delta_accept, total_accept, time_per_iter, eta): template = """ <div class="progress"> <div class="progress-bar" role="progressbar" aria-valuenow="{}" aria-valuemin="0" aria-valuemax="{}" style="width:{:.2f}%"> <span class="sr-only">{:.2f}% Complete</span> </div> </div> <table class="table"> <tr> <td>Current iteration</td> <td>{}</td> </tr> <tr> <td>Accept rate (last {})</td> <td>{:.2f}%</td> </tr> <tr> <td>Accept rate (overall)</td> <td>{:.2f}%</td> </tr> <tr> <td>T/iter</td> <td>{:.4f} seconds</td> </tr> <tr> <td>ETA</td> <td>{}</td> </tr> <tr> <td>Errors</td> <td>{}</td> </tr> </table> """ return template.format( iteration, n_iter, iteration * 100.0 / n_iter, iteration * 100.0 / n_iter, iteration, self.recent_acceptance_lag, delta_accept, total_accept, time_per_iter, pretty_time_delta(eta), self.__total_errors__, )
class AMGNG: def __init__(self, comparison_function, buffer_len, dimensions, prest_gamma, prest_lmbda, prest_theta, pst_gamma, pst_lmbda, pst_theta, prest_alpha=0.5, prest_beta=0.5, prest_delta=0.5, prest_eta=0.9995, prest_e_w=0.05, prest_e_n=0.0006, pst_alpha=0.5, pst_beta=0.75, pst_delta=0.5, pst_eta=0.9995, pst_e_w=0.05, pst_e_n=0.0006, ma_window_len=None, ma_recalc_delay=1, ddof=1): values = inspect.getargvalues(inspect.currentframe())[3] print('Init parameters: {}'.format(values)) self.comparison_function = comparison_function self.buffer_len = buffer_len self.dimensions = dimensions self.present = mgng.MGNG(dimensions=dimensions, gamma=int(prest_gamma), lmbda=int(prest_lmbda), theta=int(prest_theta), alpha=float(prest_alpha), beta=float(prest_beta), delta=float(prest_delta), eta=float(prest_eta), e_w=float(prest_e_w), e_n=float(prest_e_n)) self.past = mgng.MGNG(dimensions=dimensions, gamma=int(pst_gamma), lmbda=int(pst_lmbda), theta=int(pst_theta), alpha=float(pst_alpha), beta=float(pst_beta), delta=float(pst_delta), eta=float(pst_eta), e_w=float(pst_e_w), e_n=float(pst_e_n)) # self.buffer = deque(maxlen=self.buffer_len) self.buffer = RingBuffer([[np.nan]*dimensions]*buffer_len) if ma_window_len is None: # self.ma_window = deque(maxlen=self.buffer_len) self.ma_window = RingBuffer([np.nan]*buffer_len) else: # self.ma_window = deque(maxlen=ma_window_len) self.ma_window = RingBuffer([np.nan]*ma_window_len) self.ma_recalc_delay = ma_recalc_delay self.ddof = ddof self.anomaly_mean = None self.anomaly_std = None self.t = 0 def time_step(self, xt): xt = np.reshape(xt, newshape=self.dimensions) ret_val = 0. self.buffer.append(xt) self.present.time_step(xt) if self.t >= self.buffer_len: pst_xt = self.buffer[0] self.past.time_step(pst_xt) if self.t >= self.present.theta + self.past.theta: ret_val = self.comparison_function(self.present, self.past, self.present.alpha) self.ma_window.append(ret_val) if self.t % self.ma_recalc_delay == 0: self.anomaly_mean = bn.nanmean(self.ma_window) self.anomaly_std = bn.nanstd(self.ma_window, ddof=self.ddof) if self.anomaly_std is None or self.t < len(self.ma_window): anomaly_density = 0 else: normalized_score = (ret_val - self.anomaly_mean)/self.anomaly_std if -4 <= normalized_score <= 4: anomaly_density = CDF_TABLE[round(normalized_score, 3)] elif normalized_score > 4: anomaly_density = 1. else: anomaly_density = 0. self.t += 1 return ret_val, anomaly_density