Commit 170a94e9 authored by Ahmad Reza's avatar Ahmad Reza
Browse files

All the dir abbrevation have been changed to direction.

The direction enum/list has been renamed to direction_list.

All the contents of str_lib of solution and scenario and the header in lib are moved to one file called swarm_sim_header.py

Some bugs has been fixed!
parent 2dac129b
......@@ -44,9 +44,9 @@ tile_mm_size = 2
##Examples##
##Moving
scenario = lonely_particle
#scenario = lonely_particle
#scenario = n_particle_in_line
solution = random_walk
#solution = random_walk
#solution = round_walk
## Creating and Deleting
......@@ -66,7 +66,7 @@ solution = random_walk
#solution= scanning_for_all_aims
## All interfaces
#scenario = test_interfaces
scenario = test_interfaces
#scenario = hexagon_border
#solution = test_all_the_interfaces
solution = test_all_the_interfaces
import math
from enum import Enum
class Colors(Enum):
black = 1
gray = 2
red = 3
green = 4
blue = 5
yellow = 6
orange = 7
cyan = 8
violett = 9
black = 1
gray = 2
red = 3
green = 4
blue = 5
yellow = 6
orange = 7
cyan = 8
violett = 9
dark_green = 10
color_map = {
black: [0.0, 0.0, 0.0],
gray: [0.3, 0.3, 0.3],
red: [0.8, 0.0, 0.0],
green: [0.0, 0.8, 0.0],
dark_green: [0.2, 1, 0.6],
blue: [0.0, 0.0, 0.8],
yellow: [0.8, 0.8, 0.0],
orange: [0.8, 0.3, 0.0],
cyan: [0.0, 0.8, 0.8],
violett: [0.8, 0.2, 0.6]
}
NE=0
E = 1
SE = 2
SW = 3
W = 4
NW = 5
direction = [NE, E, SE, SW, W, NW]
x_offset = [0.5, 1, 0.5, -0.5, -1, -0.5 ]
y_offset = [ 1, 0, -1, -1, 0, 1]
def dir_to_str(dir):
"""
:param dir: the direction that should get converted to a string
:return: the string of the direction
"""
if dir == 0:
return "NE"
elif dir == 1:
return "E"
elif dir == 2:
return "SE"
elif dir == 3:
return "SW"
elif dir == 4:
return "W"
elif dir == 5:
return "NW"
else:
return "Error"
def get_the_invert(dir):
return (dir + 3) % 6
def dir_in_range(dir):
return dir % 6
def check_coords( coords_x, coords_y):
"""
Checks if the given coordinates are matching the
hexagon coordinates
:param coords_x: proposed x coordinate
:param coords_y: proposed y coordinate
:return: True: Correct x and y coordinates; False: Incorrect coordinates
"""
if (coords_x / 0.5) % 2 == 0:
if coords_y % 2 != 0:
return False
else:
return True
else:
if coords_y % 2 == 0:
return False
else:
return True
def coords_to_sim(coords):
return coords[0], coords[1] * math.sqrt(3 / 4)
def sim_to_coords(x, y):
return x, round(y / math.sqrt(3 / 4), 0)
def scan(matter_map_coords, hop, starting_x, starting_y):
hop_list = []
if (hop / 2 + starting_x, hop + starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(hop / 2 + starting_x, hop + starting_y)])
if (hop + starting_x, starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(hop + starting_x, starting_y)])
if (hop / 2 + starting_x, -hop + starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(hop / 2 + starting_x, -hop + starting_y)])
if (-hop / 2 + starting_x, -hop + starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(-hop / 2 + starting_x, -hop + starting_y)])
if (-hop + starting_x, starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(-hop + starting_x, starting_y)])
if (-hop / 2 + starting_x, hop + starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(-hop / 2 + starting_x, hop + starting_y)])
for i in range(1, hop):
if (-hop / 2 + i + starting_x, hop + starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(-hop / 2 + i + starting_x, hop + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coords:
hop_list.append(
matter_map_coords[(hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coords:
hop_list.append(
matter_map_coords[(hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 + i + starting_x, -hop + starting_y) in matter_map_coords:
hop_list.append(matter_map_coords[(-hop / 2 + i + starting_x, -hop + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coords:
hop_list.append(
matter_map_coords[(-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coords:
hop_list.append(
matter_map_coords[(-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y)])
return hop_list
"""The marker module provides the interface to the markers. A marker is any point on
the coordinate system of the simulators world"""
from lib import matter
from lib.header import *
from lib.swarm_sim_header import *
class Marker(matter.Matter):
......
......@@ -2,7 +2,7 @@
the coordinate system of the simulators world"""
import uuid
from datetime import datetime
from lib.header import *
from lib.swarm_sim_header import *
class Matter:
......
This diff is collapsed.
import math
import random
import lib.header as header
from enum import Enum
class Colors(Enum):
black = 1
gray = 2
red = 3
green = 4
blue = 5
yellow = 6
orange = 7
cyan = 8
violett = 9
dark_green = 10
black = 1
gray = 2
red = 3
green = 4
blue = 5
yellow = 6
orange = 7
cyan = 8
violett = 9
dark_green = 10
color_map = {
black: [0.0, 0.0, 0.0],
gray: [0.3, 0.3, 0.3],
red: [0.8, 0.0, 0.0],
green: [0.0, 0.8, 0.0],
dark_green: [0.2, 1, 0.6],
blue: [0.0, 0.0, 0.8],
yellow: [0.8, 0.8, 0.0],
orange: [0.8, 0.3, 0.0],
cyan: [0.0, 0.8, 0.8],
violett: [0.8, 0.2, 0.6]
}
NE=0
E = 1
SE = 2
SW = 3
W = 4
NW = 5
direction_list = [NE, E, SE, SW, W, NW]
x_offset = [0.5, 1, 0.5, -0.5, -1, -0.5 ]
y_offset = [ 1, 0, -1, -1, 0, 1]
def direction_number_to_string(direction):
"""
:param direction: the direction that should get converted to a string
:return: the string of the direction
"""
if direction == 0:
return "NE"
elif direction == 1:
return "E"
elif direction == 2:
return "SE"
elif direction == 3:
return "SW"
elif direction == 4:
return "W"
elif direction == 5:
return "NW"
else:
return "Error"
def get_the_invert(direction):
return (direction + 3) % 6
def direction_in_range(direction):
return direction % 6
def check_coords(coords_x, coords_y):
"""
Checks if the given coordinates are matching the
hexagon coordinates
:param coords_x: proposed x coordinate
:param coords_y: proposed y coordinate
:return: True: Correct x and y coordinates; False: Incorrect coordinates
"""
if (coords_x / 0.5) % 2 == 0:
if coords_y % 2 != 0:
return False
else:
return True
else:
if coords_y % 2 == 0:
return False
else:
return True
def coords_to_sim(coords):
return coords[0], coords[1] * math.sqrt(3 / 4)
def sim_to_coords(x, y):
return x, round(y / math.sqrt(3 / 4), 0)
def get_coords_in_direction(coords, direction):
"""
Returns the coordination data of the pointed directions
:param coords: particles actual staying coordination
:param direction: The direction. Options: E, SE, SW, W, NW, or NE
:return: The coordinaiton of the pointed directions
"""
return coords[0] + x_offset[direction], coords[1] + y_offset[direction]
def global_scanning(matter_map_coords_dict, hop, starting_x, starting_y):
hop_list = []
if (hop / 2 + starting_x, hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(hop / 2 + starting_x, hop + starting_y)])
if (hop + starting_x, starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(hop + starting_x, starting_y)])
if (hop / 2 + starting_x, -hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(hop / 2 + starting_x, -hop + starting_y)])
if (-hop / 2 + starting_x, -hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + starting_x, -hop + starting_y)])
if (-hop + starting_x, starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop + starting_x, starting_y)])
if (-hop / 2 + starting_x, hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + starting_x, hop + starting_y)])
for i in range(1, hop):
if (-hop / 2 + i + starting_x, hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + i + starting_x, hop + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coords_dict:
hop_list.append(
matter_map_coords_dict[(hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y)])
if (hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coords_dict:
hop_list.append(
matter_map_coords_dict[(hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 + i + starting_x, -hop + starting_y) in matter_map_coords_dict:
hop_list.append(matter_map_coords_dict[(-hop / 2 + i + starting_x, -hop + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coords_dict:
hop_list.append(
matter_map_coords_dict[(-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y)])
if (-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coords_dict:
hop_list.append(
matter_map_coords_dict[(-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y)])
return hop_list
# Helping Methods for creating scenarios
def generating_random_spraded_particles (world, max_size_particle):
for _ in range(0, max_size_particle):
......@@ -102,3 +264,62 @@ def create_tiles_formed_as_hexagons_border(world, radius, starting_x = 0, starti
world.add_tile(-radius/2 + i + starting_x + offset_x, -radius + starting_y)
world.add_tile(-radius/2 - (0.5 * i) + starting_x + offset_x, -radius + i + starting_y)
world.add_tile(-radius/2 - (0.5 * i) + starting_x + offset_x, radius - i + starting_y)
# Helping methods for Solution
def scan_neighborhood(particle):
"""
:param particle:
:return: a dictionary with the direction and the founded matter
"""
nh_dict={}
for direction in direction_list:
nh_dict[direction] = particle.get_matter_in(direction)
def move_to_dest_in_one_rnd(particle, destiny):
if move_to_dest_step_by_step(particle, destiny):
return True
move_to_dest_in_one_rnd(particle, destiny)
def move_to_dest_step_by_step(particle, destiny):
"""
:param particle:
:param destiny:
:return: True if movement occured, False if not movment and a Matter if the next direction point has a matter on it
"""
next_dir = get_next_direction_to(particle.coords[0], particle.coords[1], destiny.coords[0], destiny.coords[1])
if particle.matter_in(next_dir):
particle.get_matter_in(next_dir)
return particle.get_matter_in(next_dir)
particle.move_to(next_dir)
print("\n P", particle.number, " moves to", direction_number_to_string(next_dir))
return False
def get_next_direction_to(src_x, src_y, dest_x, dest_y):
"""
:param src_x: x coordinate of the source
:param src_y: y coordinate of the source
:param dest_x: x coordinate of the destiny
:param dest_y: y coordinate of the destiny
:return: the next direction that brings the matter closer to the destiny
"""
next_dir = -1
if (src_x < dest_x or src_x == dest_x) and src_y < dest_y:
next_dir = NE
elif src_y < dest_y and src_x > dest_x:
next_dir = NW
elif src_y > dest_y and src_x < dest_x:
next_dir = SE
elif (src_x > dest_x or src_x == dest_x) and src_y > dest_y :
next_dir = SW
elif src_y == dest_y and src_x > dest_x:
next_dir = W
elif src_y == dest_y and src_x < dest_x:
next_dir = E
return next_dir
"""The tile module provides the interface for the tiles. A tile is a hexogon that can be taken or dropped
and be connected to each other to buld up islands"""
from lib import matter
from lib.header import *
from lib.swarm_sim_header import *
class Tile(matter.Matter):
......
......@@ -106,7 +106,7 @@ class View:
class VisWindow(pyglet.window.Window):
def __init__(self, window_size_x, window_size_y, world):
#super().__init__(world.get_sim_x_size(), world.get_sim_y_size(), resizable=window_resizable, vsync=False, caption="Simulator")
#super().__init__(world.get_world_x_size(), world.get_world_y_size(), resizable=window_resizable, vsync=False, caption="Simulator")
super().__init__(window_size_x, window_size_y , resizable=window_resizable, vsync=False, caption="Simulator")
self.window_active = True
glClearColor(0.0, 0.0, 0.0, 0.0)
......
......@@ -9,7 +9,7 @@ import random
import math
import logging
from lib import csv_generator, particle, tile, marker, vis
from lib.header import *
from lib.swarm_sim_header import *
class World:
......@@ -58,7 +58,7 @@ class World:
self.csv_round = csv_generator.CsvRoundData(scenario=config_data.scenario,
solution=config_data.solution,
seed=config_data.seed_value,
directory=config_data.dir_name)
directory=config_data.direction_name)
mod = importlib.import_module('scenario.' + self.config_data.scenario)
mod.scenario(self)
......@@ -71,7 +71,7 @@ class World:
def csv_aggregator(self):
self.csv_round.aggregate_metrics()
particle_csv = csv_generator.CsvParticleFile(self.config_data.dir_name)
particle_csv = csv_generator.CsvParticleFile(self.config_data.direction_name)
for particle in self.particles:
particle_csv.write_particle(particle)
particle_csv.csv_file.close()
......@@ -88,6 +88,14 @@ class World:
"""
return self.__round_counter
def get_max_round(self):
"""
The max round number
:return: maximum round number
"""
return self.config_data.max_round
def set_end(self):
"""
Allows to terminate before the max round is reached
......@@ -100,7 +108,7 @@ class World:
"""
return self.__end
def inc_round_cnter(self):
def inc_round_counter(self):
"""
Increases the the round counter by
......@@ -212,16 +220,6 @@ class World:
"""
return self.marker_map_id
def get_coords_in_dir(self, coords, dir):
"""
Returns the coordination data of the pointed directions
: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 get_world_x_size(self):
"""
......
......@@ -43,11 +43,11 @@ def main(argv):
elif opt in ("-n", "--maxrounds"):
max_round = int(arg)
dir = "./outputs/mulitple/" + str(n_time) + "_" + scenario_file.rsplit('.', 1)[0] + "_" + \
direction = "./outputs/mulitple/" + str(n_time) + "_" + scenario_file.rsplit('.', 1)[0] + "_" + \
solution_file.rsplit('.', 1)[0]
if not os.path.exists(dir):
os.makedirs(dir)
out = open(dir + "/multiprocess.txt", "w")
if not os.path.exists(direction):
os.makedirs(direction)
out = open(direction + "/multiprocess.txt", "w")
child_processes = []
process_cnt=0
for seed in range(seed_start, seed_end+1):
......@@ -64,9 +64,9 @@ def main(argv):
for cp in child_processes:
cp.wait()
fout = open(dir+"/all_aggregates.csv","w+")
fout = open(direction+"/all_aggregates.csv","w+")
for seed in range(seed_start, seed_end+1):
f = open(dir+"/"+str(seed)+"/aggregate_rounds.csv")
f = open(direction+"/"+str(seed)+"/aggregate_rounds.csv")
f.__next__() # skip the header
for line in f:
fout.write(line)
......
import scenario.std_lib as std
import lib.header as header
import lib.swarm_sim_header as header
def scenario(world):
world.add_particle(0,0)
std.add_tiles_as_hexagon(world, 10, color=header.dark_green)
......@@ -2,8 +2,7 @@ import random
#Standard Lib that has to be in each solution
from solution.std_lib import *
def solution(world):
if world.get_actual_round() % 1 == 0:
for particle in world.get_particle_list():
particle.move_to(random.choice(direction))
\ No newline at end of file
def solution(world):
for particle in world.get_particle_list():
particle.move_to(random.choice(direction))
\ No newline at end of file
......@@ -7,28 +7,28 @@ def solution(world):
for particle in world.get_particle_list():
global ttl
global max
global dir
global direction
if world.get_actual_round() == 1:
max = 0
ttl = 0
dir = NE
direction = NE
if (ttl==0 and (dir==NE or dir==SW)):
if (ttl==0 and (direction==NE or direction==SW)):
max = max+1
if ttl==0:
print("Round ", world.get_actual_round())
ttl=max
if dir==NE:
dir=NW
elif dir==NW:
dir=SW
elif dir==SW:
dir=SE
elif dir==SE:
dir=NE
if direction==NE:
direction=NW
elif direction==NW:
direction=SW
elif direction==SW:
direction=SE
elif direction==SE:
direction=NE
particle.create_marker()
particle.move_to(dir)
particle.move_to(direction)
ttl = ttl - 1
\ No newline at end of file
#standard libaries
from lib.header import *
def scan_neighborhood(particle):
"""
:param particle:
:return: a dictionary with the direction and the founded matter
"""
nh_dict={}
for dir in direction:
nh_dict[dir] = particle.get_matter_in(dir)