<commit_before># -*- coding: utf-8 -*-
"""
Autopilots for the DriveIt Gym environment.
@author: <NAME>
"""
import numpy as np
from belief import BeliefTracking

epsilon = 0.05

class Autopilot(object):
    def __init__(self, car, other_cars=None):
        self.car = car
        self.tracker = BeliefTracking(car, other_cars, normalize=True)
        self.belief, self.deltas = [], []
        self.action = 0

    def reset(self, observation):
        belief = self.tracker.reset(observation)
        self.deltas = np.zeros(np.shape(belief))
        self.belief = belief
        return belief

    def observe(self, observation, dt):
        belief = self.tracker.update(self.action, observation, dt)
        self.deltas = belief - self.belief
        self.belief = belief
        return belief

    def act(self):
        self.action = self._act()
        return self.action

    def _act(self): raise NotImplementedError


class LookAheadPilot(Autopilot):
    def __init__(self, car, other_cars=None, 
                 ky=3.0, kdy=10.0, 
                 kth=3.0, kdth=10.0, 
                 kka=3.0, kdka=-3.0):
        super().__init__(car, other_cars)
        self.params = ky, kdy, kth, kdth, kka, kdka

    def _danger(self, dist):
        for d in dist[:3]:
            if d < 0.18 \
            or d < 0.5 and self.tracker.observation[-1]:
                return True
        return False

    def _act(self):
        y, th, v, k, kt, ka, *dist = self.belief #pylint: disable=W0612
        dy, dth, dv, dk, dkt, dka, *ddist = self.deltas #pylint: disable=W0612
        ky, kdy, kth, kdth, kka, kdka = self.params

        fy = ky * y + kdy * dy
        fth = kth * dth + kdth * dth
        fk = kka * (ka - k) + kdka * (dka - k)
        f = -fy + fth + fk - k
        if f > epsilon: action = 1
        elif f < -epsilon: action = 2
        else: action = 0
        
        if self._danger(dist):
            action += 6
        else:
            safe_throttle = self.car.specs.safe_turn_speed( \
                max(abs(k), abs(ka)), 0.9) / self.car.specs.v_max
            if v < safe_throttle - epsilon:
                action += 3
            elif v > safe_throttle + epsilon:
                action += 6
<commit_msg>Improved collision avoidance.<commit_after># -*- coding: utf-8 -*-
"""
Autopilots for the DriveIt Gym environment.
@author: <NAME>
"""
import numpy as np
from belief import BeliefTracking
from DriveItCircuit import cartesian_to_median

epsilon = 0.05

class Autopilot(object):
    def __init__(self, car, other_cars=None):
        self.car = car
        self.tracker = BeliefTracking(car, other_cars, normalize=True)
        self.belief, self.deltas = [], []
        self.action = 0

    def reset(self, observation):
        belief = self.tracker.reset(observation)
        self.deltas = np.zeros(np.shape(belief))
        self.belief = belief
        return belief

    def observe(self, observation, dt):
        belief = self.tracker.update(self.action, observation, dt)
        self.deltas = belief - self.belief
        self.belief = belief
        return belief

    def act(self):
        self.action = self._act()
        return self.action

    def _act(self): raise NotImplementedError


class LookAheadPilot(Autopilot):
    def __init__(self, car, other_cars=None, 
                 ky=3.0, kdy=10.0, 
                 kth=3.0, kdth=10.0, 
                 kka=3.0, kdka=-3.0):
        super().__init__(car, other_cars)
        self.params = ky, kdy, kth, kdth, kka, kdka

    def _danger(self, dist):
        x, _, _ = cartesian_to_median(*self.tracker.position)
        if dist[0] < 0.25:
            return True
        if x > -1.0 and x < 0.0 and dist[0] < 0.5:
            return True
        if len(dist) > 2:
            if dist[1] < 0.9 or dist[2] < 0.9:
                return True
        return False

    def _act(self):
        y, th, v, k, kt, ka, *dist = self.belief #pylint: disable=W0612
        dy, dth, dv, dk, dkt, dka, *ddist = self.deltas #pylint: disable=W0612
        ky, kdy, kth, kdth, kka, kdka = self.params

        fy = ky * y + kdy * dy
        fth = kth * dth + kdth * dth
        fk = kka * (ka - k) + kdka * (dka - k)
        f = -fy + fth + fk - k
        if f > epsilon: action = 1
        elif f < -epsilon: action = 2
        else: action = 0
        
        if self._danger(dist):
            action += 6
        else:
            safe_throttle = self.car.specs.safe_turn_speed( \
                max(abs(k), abs(ka)), 0.9) / self.car.specs.v_max
            if v < safe_throttle - epsilon:
                action += 3
            elif v > safe_throttle + epsilon:
                action += 6

{'fraction_non_alphanumeric': 0.11890504704875962, 'fraction_numerical': 0.02031650983746792, 'mean_word_length': 1.7922388059701493, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '47612', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1576, 'n_words': 421}