Commit 95dc68ff authored by Ahmad Reza's avatar Ahmad Reza
Browse files

Many changes

parent 4c1034f8
[Simulator]
seedvalue = 12
general_delay=0
max_round = 10
max_round = 500
visualization= 1
[World]
size_x = 2000
size_y = 800
max_particles=100
[File]
#world = two_particles_tiles_locations.py
#world = tile_beside_particle.py
#world = lonely_particle.py
#world = two_particles_infront.py
#world= n_particle_on_a_line.py
world=rand_n_tile_one_particle.py
#world=rand_n_tile_one_particle.py
#world = world_five_rings_particle.py
#world = world_five_rings_particle_tiles.py
#world=all_aims_in_1st_ring.py
#world = world_two_rings_particle_tiles.py
world = world_test_interfaces.py
#scenario = read_write.py
#scenario = scanning.py
#scenario = create_delete.py
#scenario = scanning_for_all_aims.py
#scenario = take_drop_aims.py
scenario = take_drop_tile.py
#scenario = take_drop_tile.py
#scenario = randome_walk.py
#scenario = search_for_tile_rw.py
scenario = test_all_the_interfaces.py
[World]
size_x = 800
size_y = 600
max_particles=1000
......@@ -5,7 +5,7 @@ import configparser
import logging
from robotsOnTiles import world, sim, vis
visualization=True
#visualization=True
def main():
"""In the main function first the config is getting parsed and than
......@@ -16,6 +16,7 @@ def main():
config.read("config.ini")
seedvalue = config.getint("Simulator", "seedvalue")
max_round = config.getint("Simulator", "max_round")
visualization = config.getboolean("Simulator", "visualization")
world_file = config.get ("File", "world")
scenario_file = config.get("File", "scenario")
size_x = config.getint("World", "size_x")
......
......@@ -19,12 +19,17 @@ class CsvParticleFile:
csv_file = open(directory + '/particle.csv', 'w', newline='')
self.writer_particle = csv.writer(csv_file)
self.writer_particle.writerow(['Particle ID',
'Location Created', 'Location Deleted', 'Location Read', 'Location Write',
'Memory Read', 'Memory Write',
'Particles Created', 'Particles Deleted', 'Particles Dropped',
'Particle Read', 'Particle Steps', 'Particles Taken', 'Particle Write',
'Tiles Created', 'Tiles Deleted', 'Tiles Dropped',
'Tile Read', 'Tiles Taken', 'Tile Write'
'Location Created', 'Location Deleted',
'Location Read', 'Location Write',
'Memory Read', 'Memory Write',
'Particles Created', 'Particles Deleted',
'Particles Dropped',
'Particle Read', 'Particle Steps',
'Particles Taken', 'Particle Write',
'Tiles Created', 'Tiles Deleted',
'Tiles Dropped',
'Tile Read', 'Tiles Taken',
'Tile Write'
])
......@@ -145,23 +150,31 @@ class CsvRoundData:
self.file_name = directory + '/rounds.csv'
self.csv_file = open(self.file_name, 'w', newline='')
self.writer_round = csv.writer(self.csv_file)
self.writer_round.writerow(['','Round Number', 'Seed','Solution','Task',
self.writer_round.writerow(['',
'Round Number', 'Seed','Scenario',
'Location Counter',
'Location Created', 'Location Created Sum',
'Location Deleted', 'Location Deleted Sum','Location Read', 'Location Read Sum',
'Location Deleted', 'Location Deleted Sum',
'Location Read', 'Location Read Sum',
'Location Write', 'Location Write Sum',
'Memory Read', 'Memory Read Sum', 'Memory Write', 'Memory Write Sum',
'Memory Read', 'Memory Read Sum',
'Memory Write', 'Memory Write Sum',
'Particle Counter',
'Particles Created', 'Particles Created Sum',
'Particles Deleted', 'Particles Deleted Sum',
'Particles Dropped', 'Particles Dropped Sum',
'Particle Read', 'Particle Read Sum', 'Particle Steps', 'Particle Steps Sum',
'Particles Taken', 'Particles Taken Sum', 'Particle Write', 'Particle Write Sum',
'Particle Read', 'Particle Read Sum',
'Particle Steps', 'Particle Steps Sum',
'Particles Taken', 'Particles Taken Sum',
'Particle Write', 'Particle Write Sum',
'Success Counter', 'Success Round',
'Tile Counter',
'Tiles Created', 'Tiles Created Sum', 'Tiles Deleted', 'Tiles Deleted Sum',
'Tiles Dropped', 'Tiles Dropped Sum', 'Tile Read', 'Tile Read Sum',
'Tiles Taken', 'Tiles Taken Sum', 'Tile Write', 'Tile Write Sum',
'Tiles Created', 'Tiles Created Sum',
'Tiles Deleted', 'Tiles Deleted Sum',
'Tiles Dropped', 'Tiles Dropped Sum',
'Tile Read', 'Tile Read Sum',
'Tiles Taken', 'Tiles Taken Sum',
'Tile Write', 'Tile Write Sum',
])
def update_particle_num (self, particle):
......@@ -248,7 +261,7 @@ class CsvRoundData:
logging.debug("CSV: Ending writing_rounds")
def next_line(self, round):
csv_iterator = ['',round, self.seed, self.solution, self.task,
csv_iterator = ['',round, self.seed, self.solution,
self.locations_num, self.location_created, self.location_created_sum,
self.location_deleted, self.location_deleted_sum,
self.location_read, self.location_read_sum,
......@@ -296,8 +309,9 @@ class CsvRoundData:
file_name = self.directory+"/aggregate_rounds.csv"
csv_file = open(file_name, 'w', newline='')
writer_round = csv.writer(csv_file)
"""Average Min Max for all other metrics"""
writer_round.writerow(['Seed', 'Rounds Total',
'Solution', 'Task',
'Solution',
'Location Counter',
'Location Created Sum', 'Location Created Avg',
'Location Created Min', 'Location Created Max',
......
......@@ -5,6 +5,8 @@
.. moduleauthor:: Ahmad Reza Cheraghi
TODO: Erase Memory
"""
import logging
......@@ -206,7 +208,7 @@ class Particle:
def scan_for_aim_in(self, aim='all', hop=1):
"""
Scanns for particles, tiles, or location on a given hop distance
Scanning for particles, tiles, or location on a given hop distance
:param aim: For what aim this method should scan for. Can be either particles, tiles, locations, or (default)all
:param hop: The hop distance from thee actual position of the scanning particle
......@@ -371,9 +373,9 @@ class Particle:
logging.info("Nothing in %s hops", str(hop))
return None
def take(self, coords=0):
def take_me(self, coords=0):
"""
Takes the tile on the given coordinate if it is not taken
The particle is getting taken from the the other particle on the given coordinate
:param coords: Coordination of tile that should be taken
:return: True: Successful taken; False: Cannot be taken or wrong Coordinates
......@@ -396,6 +398,19 @@ class Particle:
else:
return False
def drop_me(self, coords):
"""
The actual particle is getting dropped
:param coords: the given position
:return: None
"""
self.world.particle_map[coords] = self
self.coords = coords
self.__isCarried = False
self.touch()
def create_tile(self):
"""
Creates a tile on the particles actual position
......@@ -413,16 +428,16 @@ class Particle:
:param dir: The direction on which the tile should be created. Options: E, SE, SW, W, NW, NE,
:return: None
"""
coords = (0, 0)
logging.info("particle with id %s is", self.id)
if dir:
logging.info("Going to create a tile in %s ", str(dir) )
if dir != None:
coords = self.world.get_coords_in_dir(self.coords, dir)
logging.info("Going to create a tile in %s on position %s", str(dir), str(coords))
if self.world.add_tile(coords[0], coords[1]) == True:
logging.info("Tile is created")
self.world.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
else:
logging.info("Not created tile on coords %s", str(coords))
logging.info("Not created tile ")
def create_tile_on(self, x=None, y=None):
"""
......@@ -435,20 +450,35 @@ class Particle:
coords = (0, 0)
logging.info("particle with id %s is", self.id)
if x is not None and y is not None:
coords = (x, y)
if self.world.check_coords(coords):
logging.info("Going to create a tile on position %s", str(coords))
if self.world.check_coords(x,y):
logging.info("Going to create a tile on position \(%i , %i\)", y,x )
if self.world.add_tile(coords[0], coords[1]) == True:
self.world.new_tile_flag = True
self.csv_particle_writer.write_particle(tile_created=1)
return True
else:
logging.info("Not created tile on coords %s", str(coords))
logging.info("Not created tile on coords (", y,(","),x, ")")
return False
else:
logging.info("Not created tile on coords %s", str(coords))
logging.info("Not created tile on coords (", y,(","),x, ")")
def delete_tile(self, tile_id):
def delete_tile(self):
"""
Deletes a tile on current position
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.id)
logging.info("is going to delete a tile on current position")
if self.coords in self.world.get_tile_map_coords():
if self.world.remove_tile_on(self.coords):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile")
return False
def delete_tile_with(self, id):
"""
Deletes a tile with a given tile-id
......@@ -456,12 +486,12 @@ class Particle:
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.id)
logging.info("is going to delete a tile with tile id %s", str(tile_id))
if self.world.remove_tile(tile_id):
logging.info("is going to delete a tile with tile id %s", str(id))
if self.world.remove_tile(id):
self.csv_particle_writer.write_particle(tile_deleted=1)
return True
else:
logging.info("Could not delet tile with tile id %s", str(tile_id))
logging.info("Could not delet tile with tile id %s", str(id))
return False
def delete_tile_in(self, dir=E):
......@@ -599,7 +629,7 @@ class Particle:
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.world.check_coords(x=x, y=y):
if self.world.check_coords(x, y):
coords = (x, y)
if coords in self.world.tile_map:
self.carried_tile = self.world.tile_map[coords]
......@@ -631,10 +661,11 @@ class Particle:
if self.carried_tile is not None:
if self.coords not in self.world.tile_map:
try: # cher: insert so to overcome the AttributeError
self.carried_tile.drop(self.coords)
self.carried_tile.drop_me(self.coords)
except AttributeError:
pass
self.carried_tile = None
logging.info("Tile has been dropped on the actual position")
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(), tiles_dropped=1)
self.csv_particle_writer.write_particle(tiles_dropped=1)
return True
......@@ -654,7 +685,7 @@ class Particle:
coords = self.world.get_coords_in_dir(self.coords, dir)
if coords not in self.world.tile_map:
try: # cher: insert so to overcome the AttributeError
self.carried_tile.drop(coords)
self.carried_tile.drop_me(coords)
except AttributeError:
pass
self.carried_tile = None
......@@ -678,24 +709,20 @@ class Particle:
:param y: y coordinate
"""
if self.carried_tile is not None:
if self.carried_particle is None and self.carried_tile is None:
if self.world.check_coords(x=x, y=y):
coords = (x, y)
if coords in self.world.tile_map:
try: # cher: insert so to overcome the AttributeError
self.carried_tile.drop(coords)
except AttributeError:
pass
self.carried_tile = None
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(), tiles_dropped=1)
self.csv_particle_writer.write_particle(tiles_dropped=1)
logging.info("Dropped tile on %s coordinate", str(coords))
return True
else:
logging.info("Is not possible to drop the tile on that position because it is occupied")
return False
if self.world.check_coords(x, y):
coords = (x, y)
if coords not in self.world.get_tile_map_coords():
try: # cher: insert so to overcome the AttributeError
self.carried_tile.drop_me(coords)
except AttributeError:
pass
self.carried_tile = None
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(), tiles_dropped=1)
self.csv_particle_writer.write_particle(tiles_dropped=1)
logging.info("Dropped tile on %s coordinate", str(coords))
return True
else:
logging.info("No tile in the given coodinates")
logging.info("Is not possible to drop the tile on that position because it is occupied")
return False
else:
logging.info("Wrong coordinates for dropping the tile")
......@@ -748,10 +775,10 @@ class Particle:
"""
coords = (0, 0)
if x is not None and y is not None:
if self.world.check_coords(x=x, y=y):
if self.world.check_coords(x, y):
coords = (x, y)
logging.info("Going to create a particle on position %s", str(coords))
if self.world.add_particle(coords[0], coords[1], state) == True:
if self.world.add_particle(coords[0], coords[1]) == True:
logging.info("Created particle on coords %s", str(coords))
self.csv_particle_writer.write_particle(particle_created=1)
return True
......@@ -763,7 +790,23 @@ class Particle:
logging.info("Not created particle on coords %s", str(coords))
return False
def delete_particle(self, id):
def delete_particle(self):
"""
Deletes a tile on current position
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.id)
logging.info("is going to delete a particle on current position")
if self.coords in self.world.get_particle_map_coords():
if self.world.remove_particle_on(self.coords):
self.csv_particle_writer.write_particle(particle_deleted=1)
return True
else:
logging.info("Could not delet particle")
return False
def delete_particle_with(self, id):
"""
Deletes a particle with a given id
......@@ -803,7 +846,7 @@ class Particle:
:return: True: Deleting successful; False: Deleting unsuccessful
"""
if x is not None and y is not None:
if self.world.check_coords(x=x, y=y):
if self.world.check_coords(x, y):
coords = (x, y)
if self.world.remove_particle_on(coords):
logging.info("Deleted particle with particle on coords %s", str(coords))
......@@ -817,6 +860,29 @@ class Particle:
else:
return False
def take_particle(self):
"""
Takes a particle on the actual position
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_particle is None and self.carried_tile is None:
if self.coords in self.world.particle_map:
self.carried_particle = self.world.particle_map[self.coords]
if self.carried_particle.take_me(coords=self.coords):
logging.info("particle has been taken")
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(), particles_taken=1)
self.csv_particle_writer.write_particle(particles_taken=1)
return True
else:
logging.info("particle could not be taken")
return False
else:
logging.info("No particle on the actual position not in the world")
return False
else:
logging.info("particle cannot taken because particle is carrieng either a particle or a particle")
return False
def take_particle_with(self, id):
"""
......@@ -826,10 +892,10 @@ class Particle:
:return: True: successful taken; False: unsuccessful taken
"""
if self.carried_tile is None and self.carried_particle is None:
if id in self.world.id_map:
if id in self.world.get_particle_map_id():
logging.info("particle with particle id %s is in the world", str(id))
self.carried_particle = self.world.particle_id_map[id]
if self.take(self.coords):
if self.carried_particle.take_me(self.coords):
logging.info("particle with particle id %s has been taken", str(id))
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(),
......@@ -856,7 +922,7 @@ class Particle:
if coords in self.world.particle_map:
logging.info("Take particle")
self.carried_particle = self.world.particle_map[coords]
if self.carried_particle.take(coords=self.coords):
if self.carried_particle.take_me(coords=self.coords):
logging.info("particle with particle id %s has been taken", str(self.carried_particle.id))
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(),
particles_taken=1)
......@@ -880,24 +946,27 @@ class Particle:
:return: True: Successful taken; False: Cannot be taken or wrong Coordinates
"""
if self.carried_particle is None and self.carried_tile is None:
if x is not None and y is not None:
if self.world.check_coords(x=x, y=y):
coords = (x, y)
if not self.__isCarried:
del self.world.particle_map[coords]
self.__isCarried = True
self.touch()
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(),
particles_taken=1)
if self.world.check_coords(x=x, y=y):
coords = (x, y)
if coords in self.world.particle_map:
self.carried_particle = self.world.particle_map[coords]
logging.info("Particle with id %s is in the world", str(self.carried_particle.id))
if self.carried_particle.take_me(coords=self.coords):
self.world.csv_round_writer.update_metrics(self.world.sim.get_actual_round(), particles_taken=1)
self.csv_particle_writer.write_particle(particles_taken=1)
logging.info("particle with tile id %s has been taken", str(self.carried_particle.id))
return True
else:
logging.info("Particle with id %s could not be taken", str(self.carried_particle.id))
return False
else:
logging.info("Particle is not in the world")
return False
else:
logging.info("Coordinates are wrong")
return False
else:
logging.info("Particle cannot taken because particle is carrieng either a tile or a particle")
return False
def drop_particle(self):
......@@ -909,7 +978,7 @@ class Particle:
if self.carried_particle is not None:
if self.coords not in self.world.particle_map:
try: # cher: insert so to overcome the AttributeError
self.carried_particle.drop(self.coords)
self.carried_particle.drop_me(self.coords)
except AttributeError:
pass
self.carried_particle = None
......@@ -931,7 +1000,7 @@ class Particle:
coords = self.world.get_coords_in_dir(self.coords, dir)
if coords not in self.world.particle_map:
try: # cher: insert so to overcome the AttributeError
self.carried_particle.drop(coords)
self.carried_particle.drop_me(coords)
except AttributeError:
pass
self.carried_particle = None
......@@ -958,11 +1027,11 @@ class Particle:
"""
if self.carried_particle is not None:
if x is not None and y is not None:
if self.world.check_coords(x=x, y=y):
if self.world.check_coords(x, y):
coords = (x, y)
if coords not in self.world.particle_map:
try: # cher: insert so to overcome the AttributeError
self.carried_particle.drop(coords)
self.carried_particle.drop_me(coords)
except AttributeError:
pass
self.carried_particle = None
......@@ -1014,7 +1083,7 @@ class Particle:
"""
coords = (0, 0)
if dir:
if dir is not None:
coords = self.world.get_coords_in_dir(self.coords, dir)
logging.info("Going to create a location in %s on position %s", str(dir), str(coords))
if self.world.add_location(coords[0], coords[1]) == True:
......@@ -1038,7 +1107,7 @@ class Particle:
"""
coords = (0, 0)
if x is not None and y is not None:
if self.world.check_coords(x=x, y=y):
if self.world.check_coords(x, y):
coords = (x, y)
logging.info("Going to create a location on position %s", str(coords))
if self.world.add_location(coords[0], coords[1]) == True:
......@@ -1050,8 +1119,23 @@ class Particle:
else:
logging.info("Not created location on coords %s", str(coords))
return False
def delete_location(self):
"""
Deletes a location on current position
def delete_location(self, location_id):
:return: True: Deleting successful; False: Deleting unsuccessful
"""
logging.info("Particle %s is", self.id)
logging.info("is going to delete a location on current position")
if self.coords in self.world.get_location_map_coords():
if self.world.remove_location_on(self.coords):
self.csv_particle_writer.write_particle(location_deleted=1)
return True
else:
logging.info("Could not delet location")
return False
def delete_location_with(self, location_id):
"""
Deletes a location with a given location-id
......@@ -1088,6 +1172,29 @@ class Particle:
else:
logging.info("Could not delet location on coords %s", str(coords))
def delete_location_on(self, x=None, y=None):
"""
Deletes a particle either on a given x,y coordinates
:param x: x coordinate
:param y: y coordinate
:return: True: Deleting successful; False: Deleting unsuccessful
"""
if x is not None and y is not None:
if self.world.check_coords(x, y):
coords = (x, y)
if self.world.remove_location_on(coords):
logging.info("Deleted location oords %s", str(coords))
self.csv_particle_writer.write_particle(location_deleted=1)
return True
else:
logging.info("Could not delet location on coords %s", str(coords))
return False
else:
return False
else:
return False
def touch(self):
"""
Tells the visualization that something has been modified and that it shoud changed it
......
"""
The particles move randomly and creating in the first half of the simulation round times tiles, particles, and locations.
And in the second round timer particles starts delete the particles, tile, and locaiton.
This scenario just scans for particles that are within 5 hops range and prints them out.
"""
import logging
from locale import str
import random
E = 0
SE = 1
SW = 2
......@@ -15,79 +16,38 @@ S = 6 # S for stop and not south
direction = [E, SE, SW, W, NW, NE]
def create(world):
copy_particles= world.particles
for particle in copy_particles:
particle.create_tile_in(dir=random.choice([0, 1, 2, 3, 4, 5]))
# particle.create_tile_in(x=random.choice([0, 1, 2, 3, 4, 5]), y=random.choice([0, 2, 4, 6, 8 , 10]))
particle.create_particle_in(dir=random.choice([0, 1, 2, 3, 4, 5]))
# particle.create_particle_in(x=random.choice([0, 1, 2, 3, 4, 5]), y=random.choice([0, 2, 4, 6, 8, 10]))
particle.create_location_in(dir=random.choice([0, 1, 2, 3, 4, 5]))
# particle.create_location_in(x=random.choice([0, 1, 2, 3, 4, 5]), y=random.choice([0, 2, 4, 6, 8 , 10]))