def setUp(self): ''' Create three grids system with different grid size ''' self.grid_four_equal_width_height = Grid(24, 24, grid_size=4) self.grid_four_unequal_width_height = Grid(24, 16, grid_size=4) self.grid_five = Grid(20, 20, grid_size=5)
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(MoveTowardsModel, self).__init__(seed=None) else: super(MoveTowardsModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.target = Sites(id=1, location=(45, 45), radius=5, q_value=0.5) self.grid.add_object_to_grid(self.target.location, self.target) for i in range(self.num_agents): a = SwarmMoveTowards(i, self) self.schedule.add(a) x = -45 y = -45 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agent = a
def __init__(self, N, width, height, grid=10, iter=100000, seed=None): """Initialize the attributes.""" if seed is None: super(EnvironmentModel, self).__init__(seed=None) else: super(EnvironmentModel, self).__init__(seed) self.runid = datetime.datetime.now().strftime("%s") + str( self.random.randint(1, 1000, 1)[0]) self.pname = os.getcwd() + '/' + self.runid + "SForagingComm" self.stepcnt = 1 self.iter = iter self.top = None # Create db connection connect = Connect('swarm', 'swarm', 'swarm', 'localhost') self.connect = connect.tns_connect() # Fill out the experiment table self.experiment = Experiment(self.connect, self.runid, N, seed, 'SForaging Communication', iter, width, height, grid) self.experiment.insert_experiment() self.sn = self.experiment.sn # Create a folder to store results os.mkdir(self.pname) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) # self.site = Sites(id=1, location=(5, 5), radius=11, q_value=0.5) # self.grid.add_object_to_grid(self.site.location, self.site) # self.hub = Hub(id=1, location=(0, 0), radius=11) # self.grid.add_object_to_grid(self.hub.location, self.hub) self.agents = [] # Create agents for i in range(self.num_agents): a = SwarmAgent(i, self) self.schedule.add(a) # Add the agent to a random grid cell x = self.random.randint(-self.grid.width / 2, self.grid.width / 2) # x = 0 y = self.random.randint(-self.grid.height / 2, self.grid.height / 2) # y = 0 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10 self.agents.append(a)
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(MultipleCarryModel, self).__init__(seed=None) else: super(MultipleCarryModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.thing = Food(id=1, location=(30, 30), radius=40) self.grid.add_object_to_grid(self.thing.location, self.thing) self.thing1 = Food(id=1, location=(30, 30), radius=40) self.grid.add_object_to_grid(self.thing1.location, self.thing1) self.site = Sites(id=1, location=(30, 30), radius=5, q_value=0.5) self.grid.add_object_to_grid(self.site.location, self.site) self.hub = Hub(id=1, location=(0, 0), radius=11) self.grid.add_object_to_grid(self.hub.location, self.hub) self.agent = [] for i in range(self.num_agents): a = SwarmMultipleCarry(i, self) self.schedule.add(a) x = 30 y = 30 a.location = (x, y) a.direction = 2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agent.append(a)
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(GEEnvironmentModel, self).__init__(seed=None) else: super(GEEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) for i in range(self.num_agents): a = GEBTAgent(i, self) self.schedule.add(a) # Add the agent to a random grid cell # x = self.random.randint( # -self.grid.width / 2, self.grid.width / 2) x = 0 # y = self.random.randint( # -self.grid.height / 2, self.grid.height / 2) y = 0 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(MultipleCarryModel, self).__init__(seed=None) else: super(MultipleCarryModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.thing = Debris(id=1, location=(0, 0), radius=40) self.grid.add_object_to_grid(self.thing.location, self.thing) self.agent = [] for i in range(self.num_agents): a = SwarmMultipleCarry(i, self) self.schedule.add(a) x = 1 y = 1 a.location = (x, y) a.direction = 1.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agent.append(a)
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(SingleCarryFoodModel, self).__init__(seed=None) else: super(SingleCarryFoodModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) for a in range(5): self.thing = Food(id=a, location=(0, 0), radius=16) self.grid.add_object_to_grid(self.thing.location, self.thing) for i in range(self.num_agents): a = SwarmSingleCarryFood(i, self) self.schedule.add(a) x = 0 y = 0 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agent = a
def __init__(self, N, width, height, grid=10, seed=None, viewer=False): """Initialize the environment methods.""" if seed is None: super(RandomWalkSwarmEnvironmentModel, self).__init__(seed=None) else: super(RandomWalkSwarmEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.viewer = viewer self.schedule = SimultaneousActivation(self) self.hub = Hub(id=2, location=(0, 0), radius=5) self.grid.add_object_to_grid(self.hub.location, self.hub) self.site = Sites(id=2, location=(25, 25), radius=5) self.grid.add_object_to_grid(self.site.location, self.site) self.agents = [] for i in range(self.num_agents): a = SwarmAgentRandomWalk(i, self) self.schedule.add(a) x = 0 y = 0 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agents.append(a) if self.viewer: self.ui = UI((width, height), [self.hub], self.agents, [self.site])
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(SenseHubSiteSwarmEnvironmentModel, self).__init__(seed=None) else: super(SenseHubSiteSwarmEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.target = Sites(id=1, location=(10, 10), radius=11, q_value=0.5) self.grid.add_object_to_grid(self.target.location, self.target) self.hub = Hub(id=1, location=(-45, -45), radius=11) self.grid.add_object_to_grid(self.hub.location, self.hub) for i in range(self.num_agents): a = SwarmAgentSenseHubSite(i, self) self.schedule.add(a) x = 45 y = 45 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agent = a
def __init__( self, N, width, height, grid=10, iter=100000, seed=None, name='CTForaging', viewer=False, parent=None, ratio=1.0): """Initialize the attributes.""" if seed is None: super(CTModel, self).__init__(seed=None) else: super(CTModel, self).__init__(seed) # Create a unique experiment id self.runid = datetime.datetime.now().timestamp() self.runid = str(self.runid).replace('.', '') # Create the experiment folder # If parent folder exits create inside it if parent is not None and pathlib.Path(parent).is_dir(): self.pname = parent + '/' + self.runid + '-' + str(ratio) + name else: self.pname = '/'.join( os.getcwd().split('/')[:-2] ) + '/results/' + self.runid + '-' + str(iter) + name # Define some parameters to count the step self.stepcnt = 1 self.iter = iter # UI self.viewer = viewer # Create db connection connect = Connect('swarm', 'swarm', 'swarm', 'localhost') self.connect = connect.tns_connect() # Fill out the experiment table self.experiment = Experiment( self.connect, self.runid, N, seed, name, iter, width, height, grid) self.experiment.insert_experiment() # Get the primary key of the experiment table for future use self.sn = self.experiment.sn # Create a folder to store results os.mkdir(self.pname) # Number of agents self.num_agents = N # Environmental grid size self.grid = Grid(width, height, grid) # Schedular to active agents self.schedule = SimultaneousActivation(self) # Empty list of hold the agents self.agents = []
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(NMModel, self).__init__(seed=None) else: super(NMModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.hub = Hub(id=1, location=(0, 0), radius=11) self.grid.add_object_to_grid(self.hub.location, self.hub) # Create a place for the agents to drop the derbis self.obstacles = [] try: for i in range(4): dx, dy = self.random.randint(5, 10, 2) dx = self.hub.location[0] + 25 + dx dy = self.hub.location[1] + 25 + dy o = Obstacles(id=i, location=(dx, dy), radius=10) self.grid.add_object_to_grid(o.location, o) self.obstacles.append(o) except AttributeError: pass self.agent = [] for i in range(self.num_agents): a = NM(i, self) self.schedule.add(a) x = 19 y = 10 a.location = (x, y) a.direction = 2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agent.append(a) self.debris = [] try: for i in range(self.num_agents * 10): dx, dy = self.random.randint(10, 20, 2) dx = self.hub.location[0] + dx dy = self.hub.location[1] + dy d = Debris( i, location=(dx, dy), radius=5) # print (i, dx, dy) d.agent_name = None self.grid.add_object_to_grid(d.location, d) self.debris.append(d) except KeyError: pass
def __init__(self, N, width, height, grid=10, seed=None, viewer=False): if seed is None: super(SingleCarryDropReturnSwarmEnvironmentModel, self).__init__(seed=None) else: super(SingleCarryDropReturnSwarmEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.viewer = viewer self.schedule = SimultaneousActivation(self) self.hub = Hub(id=2, location=(0, 0), radius=5) self.grid.add_object_to_grid(self.hub.location, self.hub) self.site = Sites(id=3, location=(-35, -5), radius=5) self.grid.add_object_to_grid(self.site.location, self.site) self.agents = [] self.foods = [] for i in range(self.num_agents): f = Food(i, location=(-35, -5), radius=5) self.grid.add_object_to_grid(f.location, f) self.foods.append(f) for i in range(self.num_agents): # a = SwarmAgentRandomSingleCarryDropReturn(i, self) a = SwarmAgentHandCodedForaging(i, self) self.schedule.add(a) x = -45 y = -45 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agents.append(a) if self.viewer: self.ui = UI((width, height), [self.hub], self.agents, [self.site], food=self.foods)
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(RandomWalkSwarmEnvironmentModel, self).__init__(seed=None) else: super(RandomWalkSwarmEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) for i in range(self.num_agents): a = SwarmAgentRandomWalk(i, self) self.schedule.add(a) x = 0 y = 0 a.location = (x, y) a.direction = -2.3561944901923448 self.grid.add_object_to_grid((x, y), a) self.agent = a
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(WealthEnvironmentModel, self).__init__(seed=None) else: super(WealthEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) for i in range(self.num_agents): a = SwarmAgent(i, self) self.schedule.add(a) # Add the agent to a random grid cell x = self.random.randint(-self.grid.width / 2, self.grid.width / 2) y = self.random.randint(-self.grid.height / 2, self.grid.height / 2) a.location = (x, y) self.grid.add_object_to_grid((x, y), a)
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(XMLEnvironmentModel, self).__init__(seed=None) else: super(XMLEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.site = Sites(id=1, location=(45, 45), radius=5, q_value=0.5) self.grid.add_object_to_grid(self.site.location, self.site) self.hub = Hub(id=1, location=(0, 0), radius=11) self.grid.add_object_to_grid(self.hub.location, self.hub) self.agents = [] # Create agents for i in range(self.num_agents): a = XMLTestAgent(i, self) self.schedule.add(a) x = 45 y = 45 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10 self.agents.append(a) # Add equal number of food source for i in range(5): # self.num_agents * 2): f = Food(i, location=(45, 45), radius=3) f.agent_name = None self.grid.add_object_to_grid(f.location, f)
def __init__(self, N, width, height, grid=10, seed=None): if seed is None: super(GEEnvironmentModel, self).__init__(seed=None) else: super(GEEnvironmentModel, self).__init__(seed) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.agents = [] # Create agents for i in range(self.num_agents): a = GEBTAgent(i, self) self.schedule.add(a) x = 0 y = 0 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10 self.agents.append(a)
def __init__( self, N, width, height, grid=10, iter=100000, xmlstrings=None, seed=None, viewer=False, pname=None, expname='MSFSimulation', agent='SimAgent'): """Initialize the attributes.""" if seed is None: super(SimModel, self).__init__(seed=None) else: super(SimModel, self).__init__(seed) self.runid = datetime.datetime.now().strftime( "%s") + str(self.random.randint(1, 1000, 1)[0]) if pname is None: self.pname = os.getcwd() + '/' + self.runid + expname else: self.pname = pname + '/' + self.runid + expname self.width = width self.height = height self.stepcnt = 1 self.iter = iter self.xmlstrings = xmlstrings self.viewer = viewer # Create db connection connect = Connect('swarm', 'swarm', 'swarm', 'localhost') self.connect = connect.tns_connect() # Fill out the experiment table self.experiment = Experiment( self.connect, self.runid, N, seed, expname, iter, width, height, grid, phenotype=xmlstrings[0]) self.experiment.insert_experiment_simulation() self.sn = self.experiment.sn # Create a folder to store results os.mkdir(self.pname) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.agents = [] bound = np.ceil((self.num_agents * 1.0) / len(self.xmlstrings)) j = 0 # Create agents for i in range(self.num_agents): # print (i, j, self.xmlstrings[j]) a = eval(agent)(i, self, xmlstring=self.xmlstrings[j]) self.schedule.add(a) # Add the agent to a random grid cell # x = self.random.randint( # -self.grid.width / 2, self.grid.width / 2) x = 0 # y = self.random.randint( # -self.grid.height / 2, self.grid.height / 2) y = 0 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10 self.agents.append(a) if (i + 1) % bound == 0: j += 1
def __init__(self, N, width, height, grid=10, iter=100000, seed=None, expname='COT', agent='EvolAgent', parm='swarm_mcarry.txt'): """Initialize the attributes.""" if seed is None: super(EvolModel, self).__init__(seed=None) else: super(EvolModel, self).__init__(seed) self.runid = datetime.datetime.now().strftime("%s") + str( self.random.randint(1, 1000, 1)[0]) self.pname = '/'.join(os.getcwd().split('/')[:-2]) + '/results/' \ + self.runid + expname self.stepcnt = 1 self.iter = iter self.top = None # Create db connection connect = Connect('swarm', 'swarm', 'swarm', 'localhost') self.connect = connect.tns_connect() # Fill out the experiment table self.experiment = Experiment(self.connect, self.runid, N, seed, expname, iter, width, height, grid) self.experiment.insert_experiment() self.sn = self.experiment.sn # Create a folder to store results os.mkdir(self.pname) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) self.agents = [] self.parm = parm # Create agents for i in range(self.num_agents): a = eval(agent)(i, self) self.schedule.add(a) # Add the agent to a random grid cell x = self.random.randint(-self.grid.width / 2, self.grid.width / 2) # x = 0 y = self.random.randint(-self.grid.height / 2, self.grid.height / 2) # y = 0 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10 self.agents.append(a)
def __init__(self, N, width, height, grid=10, iter=100000, xmlstrings=None, seed=None, viewer=False, pname=None): """Initialize the attributes.""" if seed is None: super(SimModelComm, self).__init__(seed=None) else: super(SimModelComm, self).__init__(seed) self.runid = datetime.datetime.now().timestamp() self.runid = str(self.runid).replace( '.', '') + '-' + str(prob).replace('.', '') #self.runid = datetime.datetime.now().strftime( # "%s") + str(self.random.randint(1, 1000, 1)[0]) if pname is None: self.pname = os.getcwd() + '/' + self.runid + "SForagingSimulation" else: self.pname = pname + '/' + self.runid + "SForagingSimulation" # self.pname = os.getcwd() + '/' + self.runid + "SFCommSimulation" self.width = width self.height = height self.stepcnt = 1 self.iter = iter self.xmlstrings = xmlstrings self.viewer = viewer # Create db connection connect = Connect('swarm', 'swarm', 'swarm', 'localhost') self.connect = connect.tns_connect() # Fill out the experiment table self.experiment = Experiment(self.connect, self.runid, N, seed, 'Simuation SFComm', iter, width, height, grid, phenotype=xmlstrings[0]) self.experiment.insert_experiment_simulation() self.sn = self.experiment.sn # Create a folder to store results os.mkdir(self.pname) self.num_agents = N self.grid = Grid(width, height, grid) self.schedule = SimultaneousActivation(self) # self.site = Sites(id=1, location=(5, 5), radius=11, q_value=0.5) # self.grid.add_object_to_grid(self.site.location, self.site) # self.hub = Hub(id=1, location=(0, 0), radius=11) # self.grid.add_object_to_grid(self.hub.location, self.hub) self.agents = [] bound = np.ceil((self.num_agents * 1.0) / len(self.xmlstrings)) j = 0 # Create agents for i in range(self.num_agents): # print (i, j, self.xmlstrings[j]) a = SimAgent(i, self, xmlstring=self.xmlstrings[j]) self.schedule.add(a) # Add the agent to a random grid cell # x = self.random.randint( # -self.grid.width / 2, self.grid.width / 2) x = 0 # y = self.random.randint( # -self.grid.height / 2, self.grid.height / 2) y = 0 a.location = (x, y) self.grid.add_object_to_grid((x, y), a) a.operation_threshold = 2 # self.num_agents // 10 self.agents.append(a) if (i + 1) % bound == 0: j += 1