<commit_before>import sys
import os
sys.path.insert(0, os.path.abspath('.'))

import numpy as np
from random import gauss

POS_CHANGE = 0.1
ACCURACY = 0.25

class Planning(object):

    def __init__(self, environment):
        self.static_objects = environment.static_objects
        self.dynamic_objects = environment.dynamic_objects
        self.robot = environment.robot
        self.env = environment

        # Assume target is first dynamic object
        self.target = self.dynamic_objects[0]

        # Define our actions and observation data
        self.actions = [-1, 1] * self.robot.active_links
        self.move_count = 0
        self.collected_rewards = []
        self.num_actions = len(self.actions)
        self.observation_size = len(self.actions)

    def observe(self):
        """Returns current observation"""
        distances = []
        for i, action in enumerate(self.actions):
            link = i / 2

            # Calculate old and new link positions
            q_old = self.robot.links[link].theta
            q_new = q_old + action * POS_CHANGE

            # Get the end effector position after joint rotation
            current_config = self.robot.get_current_joint_config()
            current_config[link] = q_new
            end_effector_pos = self.robot.end_effector_position(
                current_config)

            # Find the distance from our target (the ball)
            distance = np.linalg.norm(end_effector_pos - self.target.position)
            # Distances + some noise
            distances.append(distance)
        return np.array(distances)

    def perform_action(self, action):
        """Update internal state to reflect the fact that an
           action was taken
        :param action: Number of the action performed
        """
        # Update a specific links velocity
        link = action / 2
        q_old = self.robot.links[link].theta
        q_new = q_old + self.actions[action] * POS_CHANGE

        self.robot.update_link_angle(link, q_new, True)
        self.move_count += 1

    def is_over(self):
        """Check if simulation is over"""
        if self.collected_rewards and self.collected_rewards[-1] == -1:
            return True

        target = self.target.position
        end_effector = self.robot.end_effector_position()
        return np.linalg.norm(target - end_effector) < ACCURACY

    def collect_reward(self, action):
        """Returns reward accumulated since last time this
        function was called.
        """
        # Calculate previous distance to target object (ball)
        old_dist = np.linalg.norm((self.robot.end_effector_position() -
                                   self.target.position))
        # Then perform the action
        self.perform_action(action)

        for obstacle in self.static_objects:
            if self.robot.is_in_collision(obstacle):
                self.collected_rewards.append(-1)
                return -1

        # Find the distance from our target (the ball)
        new_dist = np.linalg.norm((self.robot.end_effector_position() -
                                   self.target.position))<commit_msg>Adding change per iter and accuracy params to planning<commit_after>import sys
import os
sys.path.insert(0, os.path.abspath('.'))

import numpy as np
from random import gauss


class Planning(object):

    def __init__(self, environment, change_per_iter=0.1, target_accuracy=0.25):
        """Planning module to take an environment and run RL experiments on
        :param environment: Environment containing robot, obstacles, 
                            target etc.
        :param change_per_iter: Change of a joint each action
        :param target_accuracy: Accuracy needed to declare target hit
        """
        self.change_per_iter = change_per_iter
        self.target_accuracy = target_accuracy
        self.static_objects = environment.static_objects
        self.dynamic_objects = environment.dynamic_objects
        self.robot = environment.robot
        self.env = environment

        # Assume target is first dynamic object
        self.target = self.dynamic_objects[0]

        # Define our actions and observation data
        self.actions = [-1, 1] * self.robot.active_links
        self.move_count = 0
        self.collected_rewards = []
        self.num_actions = len(self.actions)
        self.observation_size = len(self.actions)

    def observe(self):
        """Returns current observation"""
        distances = []
        for i, action in enumerate(self.actions):
            link = i / 2

            # Calculate old and new link positions
            q_old = self.robot.links[link].theta
            q_new = q_old + action * self.change_per_iter

            # Get the end effector position after joint rotation
            current_config = self.robot.get_current_joint_config()
            current_config[link] = q_new
            end_effector_pos = self.robot.end_effector_position(
                current_config)

            # Find the distance from our target (the ball)
            distance = np.linalg.norm(end_effector_pos - self.target.position)
            # Distances + some noise
            distances.append(distance)
        return np.array(distances)

    def perform_action(self, action):
        """Update internal state to reflect the fact that an
           action was taken
        :param action: Number of the action performed
        """
        # Update a specific links velocity
        link = action / 2
        q_old = self.robot.links[link].theta
        q_new = q_old + self.actions[action] * self.change_per_iter

        self.robot.update_link_angle(link, q_new, True)
        self.move_count += 1

    def is_over(self):
        """Check if simulation is over"""
        for obstacle in self.static_objects:
            if self.robot.is_in_collision(obstacle):
                return True

        target = self.target.position
        end_effector = self.robot.end_effector_position()
        return np.linalg.norm(target - end_effector) < self.target_accuracy

    def collect_reward(self, action):
        """Returns reward accumulated since last time this
        function was called.
        """
        # Calculate previous distance to target object (ball)
        old_dist = np.linalg.norm((self.robot.end_effector_position() -
                                   self.target.position))
        # Then perform the action
        self.perform_action(action)

        for obstacle in self.static_objects:
            if self.robot.is_in_collision(obstacle):
                self.collected_rewards.append(-1)
                return -1

        # Find the distance from our target (the ball)
        new_dist = np.linalg.norm((self.robot.end_effector_position() -
                                   self.target.position))
{'fraction_non_alphanumeric': 0.07959034381858084, 'fraction_numerical': 0.004681784930504755, 'mean_word_length': 2.1228871630881683, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '502102', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1829, 'n_words': 495}