Commit f63019d5 authored by Ahmad Reza Cheraghi's avatar Ahmad Reza Cheraghi
Browse files

Replace world.py

parent 6411814b
......@@ -9,6 +9,7 @@ It also have the the coordination system and stated the maximum of the x and y c
import importlib
import datetime
import os
import random
import logging
from robotsOnTiles import csv_generator, particle, tile, location
......@@ -70,46 +71,139 @@ class World:
self.__size_x = size_x
self.__size_y = size_y
self.max_particles = max_particles
random.seed(seed)
#csv attributes
now = datetime.datetime.today()
nTime = now.strftime("%d-%m-%Y_%H-%M-%S")
self.directory = "./outputs/"+ nTime + "_" + solution_name + "_" + "seed" + str(seed)
self.directory = "./outputs/"+ nTime + "_" + solution_name + "_" +\
str(seed)
if not os.path.exists(self.directory):
os.makedirs(self.directory)
self.csv_round_writer = csv_generator.CsvRoundData(actual_round=0, seed=seed,
self.csv_round_writer = csv_generator.CsvRoundData(actual_round=0, solution=solution_name, seed=seed,
tiles_num=0, particle_num=0,
steps=0, directory=self.directory)
self.csv_particle_file = csv_generator.CsvParticleFile(self.directory)
mod = importlib.import_module('robotsOnTiles.worlds.' + world_name.strip('.py'))
mod.create_world(self)
def get_actual_particle_num(self):
def get_particles_num(self):
"""
Returns the actual number of particles in the world
:return: The actual number of Particles
"""
return self.tiles_num
def get_particle_list(self):
"""
Returns the actual number of particles in the world
:return: The actual number of Particles
"""
return self.particles
def get_particle_map_coords(self):
"""
Get a dictionary with all particles mapped with their actual coordinates
:return: a dictionary with particles and their coordinates
"""
return self.particle_map
def get_particle_map_id(self):
"""
Get a dictionary with all particles mapped with their own ids
:return: a dictionary with particles and their own ids
"""
return self.particle_id_map
def get_tiles_num(self):
"""
Returns the actual number of particles in the world
:return: The actual number of Particles
"""
return len(self.particles)
return self.tiles_num
def get_tiles_list(self):
"""
Returns the actual number of tiles in the world
:return: a list of all the tiles in the world
"""
return self.tiles
def get_tile_map_coords(self):
"""
Get a dictionary with all tiles mapped with their actual coordinates
:return: a dictionary with particles and their coordinates
"""
return self.tile_map
def get_tile_map_id(self):
"""
Get a dictionary with all particles mapped with their own ids
:return: a dictionary with particles and their own ids
"""
return self.tile_id_map
def get_location_num(self):
"""
Returns the actual number of locations in the world
:return: The actual number of locations
"""
return self.locations_num
def get_location_list(self):
"""
Returns the actual number of locations in the world
:return: The actual number of locations
"""
return self.locations
def get_location_map_coords(self):
"""
Get a dictionary with all locations mapped with their actual coordinates
:return: a dictionary with locations and their coordinates
"""
return self.location_map
def get_location_map_id(self):
"""
Get a dictionary with all locations mapped with their own ids
:return: a dictionary with locations and their own ids
"""
return self.location_id_map
def get_coords_in_dir(self, coords, dir):
"""
Returns the coordination data of the pointed directions
:param coords: Actual particles coordination
:param coords: particles actual staying coordination
:param dir: The direction. Options: E, SE, SW, W, NW, or NE
:return: The coordinaiton of the pointed directions
"""
return coords[0] + x_offset[dir], coords[1] + y_offset[dir]
def max_x_size(self):
def get_world_x_size(self):
"""
:return: Returns the maximal x size of the world
"""
return self.__size_x
def max_y_size(self):
def get_world_y_size(self):
"""
:return: Returns the maximal y size of the world
"""
......@@ -166,18 +260,18 @@ class World:
logging.info("Max of particles reached and no more particles can be created")
return False
def remove_particle(self,particle_id):
def remove_particle(self,id):
""" Removes a particle with a given particle id from the world database
:param particle_id: particle id
:return: True: Successful removed; False: Unsuccessful
"""
rm_particle = self.particle_id_map[particle_id]
rm_particle = self.particle_id_map[id]
if rm_particle:
self.particles.remove(rm_particle)
del self.particle_map[rm_particle.coords]
del self.particle_id_map[particle_id]
del self.particle_id_map[id]
self.particle_rm.append(rm_particle)
self.csv_round_writer.update_particle_num(len(self.particles))
self.csv_round_writer.update_metrics(self.sim.get_actual_round(),particle_deleted=1)
......@@ -231,15 +325,15 @@ class World:
return False
def remove_tile(self,tile_id):
def remove_tile(self,id):
"""
Removes a tile with a given tile_id from to the world database
:param tile_id: The tiles id that should be removec
:return: True: Successful removed; False: Unsuccessful
"""
if tile_id in self.tile_id_map:
rm_tile = self.tile_id_map[tile_id]
if id in self.tile_id_map:
rm_tile = self.tile_id_map[id]
self.tiles.remove(rm_tile)
self.tiles_rm.append(rm_tile)
logging.info("Deleted tile with tile id %s on %s", str(rm_tile.id), str(rm_tile.coords) )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment