<commit_before>import math
import os
import logging
from micropsi_core.world.world import World
from micropsi_core.world.worldadapter import WorldAdapter
from micropsi_core.world.worldobject import WorldObject
from micropsi_core.world.island import png


class Island(World):

    """ mandatory: list of world adapters that are supported"""
    supported_worldadapters = ['Braitenberg', 'Survivor', 'StructuredObjects']

    groundmap = {
        'image': "psi_1.png",
        'start_position': (700, 400),
        'scaling': (8, 8)
    }

    assets = {
        'background': "island/psi_1.png",
        'template': 'island/island.tpl',
        'js': "island/island.js",
        'x': 2048,
        'y': 2048,
        'icons': {
            'Lightsource': 'island/lamp.png',
            'Braitenberg': 'island/braitenberg.png',
            'Survivor': 'island/Micropsi.png',
            'PalmTree': 'island/palm-tree.png',
            'Maple': 'island/maple.png',
            'Braintree': 'island/braintree.png',
            'Wirselkraut': 'island/wirselkraut.png',
            'Thornbush': 'island/unknownbox.png',
            'Juniper': 'island/juniper-berries.png',
            'Champignon': 'island/boletus-edulis.png',
            'FlyAgaric': 'island/fly-agaris.png',
            'Stone': 'island/rock.png',
            'Boulder': 'island/boulder.png',
            'Menhir': 'island/menhir.png',
            'Waterhole': 'island/well.png'
        }
    }

    def __init__(self, filename, world_type="Island", name="", owner="", uid=None, version=1):
        World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version)
        self.load_groundmap()
        self.current_step = 0
        self.data['assets'] = self.assets

    def load_groundmap(self):
        """
        Imports a groundmap for an island world from a png file. We expect a bitdepth of 8 (i.e. each pixel defines
        a point with one of 256 possible values).
        """
        filename = os.path.join(os.path.dirname(__file__), 'resources', 'groundmaps', self.groundmap["image"])
        with open(filename, 'rb') as file:
            png_reader = png.Reader(file)
            x, y, image_array, image_params = png_reader.read()
            self.ground_data = list(image_array)
            self.scale_x = self.groundmap["scaling"][0]
            self.scale_y = self.groundmap["scaling"][1]
            self.x_max = x - 1
            self.y_max = y - 1

    def get_ground_at(self, x, y):
        """
        returns the ground type (an integer) at the given position
        """
        _x = int(min(self.x_max, max(0, round(x / self.scale_x))))
        _y = int(min(self.y_max, max(0, round(y / self.scale_y))))
        return self.ground_data[_y][_x]

    def get_brightness_at(self, position):
        """calculate the brightness of the world at the given position; used by sensors of agents"""
        brightness = 0
        for key in self.objects:
            if hasattr(self.objects[key], "get_intensity"):
                # adapted from micropsi1
                pos = self.objects[key].position
                diff = (pos[0] - position[0], pos[1] - position[1])
                dist = _2d_vector_norm(diff) + 1
                lightness = self.objects[key].get_intensity()
                brightness += (lightness /dist /dist)
        return brightness

    def get_movement_result(self, start_position, effort_vector, diameter=0):
        """determine how much an agent moves in the direction of the effort vector, starting in the start position.
        Note that agents may be hindered by impassable terrain and other objects"""

        efficiency = ground_types[self.get_ground_at(*start_position)]['move_efficiency']
        if not efficiency:
            return start_position
        movement_vector = (effort_vector[0] * efficiency, effort_vector[1] * efficiency)

        # make sure we don't bump into stuff
        target_position = None
        while target_position is None and _2d_distance_squared((0, 0), movement_vector) > 0.01:
            target_position = _2d_translate(start_position, movement_vector)

            for i in self.objects.values():
                if _2d_distance_squared(target_position, i.position) < (diameter + i.diameter) / 2:
                    movement_vector = (movement_vector[0] * 0.5, movement_vector[1] * 0.5)  # should be collision point
                    target_position = None
                    break

        if target_position is None:
            return start_position
        return target_position


class Lightsource(WorldObject):
    """A pretty inert and boring light source, with a square falloff"""

    @property
    def diameter(self):
        return self.data.get('diameter', 1.)

    @diameter.setter
    def diameter(self, diameter):
        self.data['diameter'] = diameter

    @property
    def intensity(self):
        return self.data.get('intensity', 10000.)

    @intensity.setter<commit_msg>prevent agent from getting stuck.<commit_after>import math
import os
import logging
from micropsi_core.world.world import World
from micropsi_core.world.worldadapter import WorldAdapter
from micropsi_core.world.worldobject import WorldObject
from micropsi_core.world.island import png


class Island(World):

    """ mandatory: list of world adapters that are supported"""
    supported_worldadapters = ['Braitenberg', 'Survivor', 'StructuredObjects']

    groundmap = {
        'image': "psi_1.png",
        'start_position': (700, 400),
        'scaling': (8, 8)
    }

    assets = {
        'background': "island/psi_1.png",
        'template': 'island/island.tpl',
        'js': "island/island.js",
        'x': 2048,
        'y': 2048,
        'icons': {
            'Lightsource': 'island/lamp.png',
            'Braitenberg': 'island/braitenberg.png',
            'Survivor': 'island/Micropsi.png',
            'PalmTree': 'island/palm-tree.png',
            'Maple': 'island/maple.png',
            'Braintree': 'island/braintree.png',
            'Wirselkraut': 'island/wirselkraut.png',
            'Thornbush': 'island/unknownbox.png',
            'Juniper': 'island/juniper-berries.png',
            'Champignon': 'island/boletus-edulis.png',
            'FlyAgaric': 'island/fly-agaris.png',
            'Stone': 'island/rock.png',
            'Boulder': 'island/boulder.png',
            'Menhir': 'island/menhir.png',
            'Waterhole': 'island/well.png'
        }
    }

    def __init__(self, filename, world_type="Island", name="", owner="", uid=None, version=1):
        World.__init__(self, filename, world_type=world_type, name=name, owner=owner, uid=uid, version=version)
        self.load_groundmap()
        self.current_step = 0
        self.data['assets'] = self.assets

    def load_groundmap(self):
        """
        Imports a groundmap for an island world from a png file. We expect a bitdepth of 8 (i.e. each pixel defines
        a point with one of 256 possible values).
        """
        filename = os.path.join(os.path.dirname(__file__), 'resources', 'groundmaps', self.groundmap["image"])
        with open(filename, 'rb') as file:
            png_reader = png.Reader(file)
            x, y, image_array, image_params = png_reader.read()
            self.ground_data = list(image_array)
            self.scale_x = self.groundmap["scaling"][0]
            self.scale_y = self.groundmap["scaling"][1]
            self.x_max = x - 1
            self.y_max = y - 1

    def get_ground_at(self, x, y):
        """
        returns the ground type (an integer) at the given position
        """
        _x = int(min(self.x_max, max(0, round(x / self.scale_x))))
        _y = int(min(self.y_max, max(0, round(y / self.scale_y))))
        return self.ground_data[_y][_x]

    def get_brightness_at(self, position):
        """calculate the brightness of the world at the given position; used by sensors of agents"""
        brightness = 0
        for key in self.objects:
            if hasattr(self.objects[key], "get_intensity"):
                # adapted from micropsi1
                pos = self.objects[key].position
                diff = (pos[0] - position[0], pos[1] - position[1])
                dist = _2d_vector_norm(diff) + 1
                lightness = self.objects[key].get_intensity()
                brightness += (lightness /dist /dist)
        return brightness

    def get_movement_result(self, start_position, effort_vector, diameter=0):
        """determine how much an agent moves in the direction of the effort vector, starting in the start position.
        Note that agents may be hindered by impassable terrain and other objects"""

        efficiency = ground_types[self.get_ground_at(*start_position)]['move_efficiency']
        if not efficiency:
            return start_position
        movement_vector = (effort_vector[0] * efficiency, effort_vector[1] * efficiency)

        # make sure we don't bump into stuff
        target_position = None
        while target_position is None and _2d_distance_squared((0, 0), movement_vector) > 0.01:
            target_position = _2d_translate(start_position, movement_vector)

            for i in self.objects.values():
                if _2d_distance_squared(target_position, i.position) < (diameter + i.diameter) / 2:
                    movement_vector = (movement_vector[0] * 0.5, movement_vector[1] * 0.5)  # should be collision point
                    target_position = None
                    break

        if target_position is not None and ground_types[self.get_ground_at(target_position[0], target_position[1])]['agent_allowed']:
            return target_position
        else:
            return start_position


class Lightsource(WorldObject):
    """A pretty inert and boring light source, with a square falloff"""

    @property
    def diameter(self):
        return self.data.get('diameter', 1.)

    @diameter.setter
    def diameter(self, diameter):
        self.data['diameter'] = diameter

    @property
    def intensity(self):
        return self.data.get('intensity', 10000.)

    @intensity.setter
{'fraction_non_alphanumeric': 0.11654506014592783, 'fraction_numerical': 0.01242358509169789, 'mean_word_length': 2.4128532974427994, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '913336', 'n_tokens_mistral': 3176, 'n_tokens_neox': 2997, 'n_words': 750}