def __init__(self): IPMaker.__init__(self) World.__init__(self) PropagationModel.__init__(self) self.nodes = [] self.nodes.append(gateway(2, 2, self.get_address())) self.transactionQue = [] self.transaction_log = []
def create_double_rect_world(self, random_robot=True): outer_walls = create_rect_walls(self.width / 2, self.height / 2, self.width, self.height) inner_walls = create_rect_walls(self.width / 2, self.height / 2, self.width / 2, self.height / 2) walls = [*outer_walls, *inner_walls] world = World(walls, self.width, self.height, self.scenario) x_left = self.width / 2 - self.width * 3 / 8 x_right = self.width / 2 + self.width * 3 / 8 y_up = self.height / 2 + self.height * 3 / 8 y_down = self.height / 2 - self.height * 3 / 8 robot_start_loc = [] x_options = np.linspace(x_left, x_right, 100) for x in x_options: robot_start_loc.append((x, y_up, np.random.uniform(0, 2 * np.pi))) robot_start_loc.append((x, y_down, np.random.uniform(0, 2 * np.pi))) y_options = np.linspace(y_down, y_up, 100) for y in y_options: robot_start_loc.append((x_left, y, np.random.uniform(0, 2 * np.pi))) robot_start_loc.append( (x_right, y, np.random.uniform(0, 2 * np.pi))) robot_start_loc = random.choice(robot_start_loc) robot = self.__add_robot__(world, random_robot=random_robot, robot_start_loc=robot_start_loc) return world, robot
def create_world(self, dimensions=WORLD_DIMENSION): """construct world, create board""" if self.world: raise Exception("world exists") self.world = World(dimensions, mode="hex") self.board_display = WorldBoard(self, self.world.dimensions, mode="hex") self.world.generate() self.regen_btn["command"] = self.regen_world BoardStylist(self.world.factory.organisms) self.update_board() self.place()
def load_world(files, ia, sa): names = files.split() if len(names) > 1: world = World.load_map(names[0]) world.load_agents(names[1]) else: world = World.from_file(names[0], load_agents=True) world.clear_agents() for i in range(1, ia + 1): world.add_agent(agents.PathfindingIntruder) for s in range(1, sa + 1): world.add_agent(agents.CameraGuard) for s in range(sa, 5): world.add_agent(agents.PatrollingGuard) return world
def create_localization_maze(self, random_robot=False): border_buffer = 10 effective_width = self.width - border_buffer*2 effective_height = self.height - border_buffer*2 border = create_rect_walls(self.width / 2, self.height / 2, effective_width, effective_height) internal_walls, internal_beacons = create_localization_maze_walls_and_beacons(effective_width, effective_height, border_buffer) walls = [*border, *internal_walls] beacons = [*internal_beacons] world = World(walls, self.width, self.height, self.scenario, beacons=beacons) robot_start_loc = ((effective_width/6) + border_buffer, (effective_height/10) + border_buffer, 0) robot = self.__add_robot__(world, random_robot=random_robot, robot_start_loc=robot_start_loc) return world, robot
def create_rect_world(self, random_robot=True): walls = create_rect_walls(self.width / 2, self.height / 2, self.width, self.height) world = World(walls, self.width, self.height, self.scenario) min_x = self.robot_radius max_x = self.width - self.robot_radius min_y = self.robot_radius max_y = self.height - self.robot_radius margin = 20 x = random.uniform(min_x + margin, max_x - margin) y = random.uniform(min_y + margin, max_y - margin) robot_start_loc = (x, y, np.random.uniform(0, 2 * np.pi)) robot = self.__add_robot__(world, random_robot=random_robot, robot_start_loc=robot_start_loc) return world, robot
def create_star_world(self, random_robot=True): border = create_rect_walls(self.width / 2, self.height / 2, self.width, self.height) star = create_star_walls(self.width / 2, self.height / 2, self.height / 4, self.height / 2) walls = [*border, *star] world = World(walls, self.width, self.height, self.scenario) radius = min(self.width / 6, self.height / 6) radius = random.random() * radius angle = random.random() * 2 * math.pi robot_start_loc = ( int(self.width / 2 + radius * math.cos(angle)), int(self.height / 2 + radius * math.sin(angle)), np.random.uniform(0, 2 * np.pi)) robot = self.__add_robot__(world, random_robot=random_robot, robot_start_loc=robot_start_loc) return world, robot
def create_trapezoid_world(self, random_robot=True): border = create_rect_walls(self.width / 2, self.height / 2, self.width, self.height) trapezoid = create_trapezoid_walls(self.width / 2, self.height / 2, self.height, self.width, self.width / 2) walls = [*border, *trapezoid] world = World(walls, self.width, self.height, self.scenario) min_x = self.robot_radius max_x = self.width - self.robot_radius min_y = self.robot_radius max_y = self.height - self.robot_radius margin_x = self.width / 3 margin_y = 20 x = random.uniform(min_x + margin_x, max_x - margin_x) y = random.uniform(min_y + margin_y, max_y - margin_y) robot_start_loc = (x, y, np.random.uniform(0, 2 * np.pi)) robot = self.__add_robot__(world, random_robot=random_robot, robot_start_loc=robot_start_loc) return world, robot
import arcade from gui import renderer from simulation.world import World from simulation.environment import MapGenerator from ai import agents if __name__ == '__main__': i = input('Load save? ') # load from file if i: names = i.split() if len(names) > 1: world = World.load_map(names[0]) world.load_agents(names[1]) else: world = World.from_file(names[0], load_agents=True) # or build a new map else: m = MapGenerator.random(size=(51, 51)) world = World(m) world.add_agent(agents.CameraGuard) world.add_agent(agents.CameraGuard) world.add_agent(agents.CameraGuard) world.add_agent(agents.CameraGuard) world.add_agent(agents.CameraGuard) world.add_agent(agents.PathfindingIntruder) world.add_agent(agents.PathfindingIntruder)
class Application(tk.Frame): def __init__(self, master=None): super(Application, self).__init__(master) self.world = None self.autoplay = tk.IntVar() self.pack() self.create_widgets() def create_widgets(self): """create and bind control buttons and place them""" self.turn_btn = ttk.Button(self, text="TURN", command=self.turn, style="TButton") self.quit_btn = ttk.Button(self, text="QUIT", command=root.destroy, style="TButton") self.regen_btn = ttk.Button(self, text="REGENERATE", style="TButton") self.play_btn = tk.Checkbutton(self, text="PLAY", variable=self.autoplay, command=self.autoplay_turn) self.logging_stext = tkst.ScrolledText(self, state="disabled") self.logging_stext.configure(font="TkDefaultFont") self.logging_stext.tag_config("INFO", foreground="black") self.logging_stext.tag_config("DEBUG", foreground="gray") self.logging_stext.tag_config("WARNING", foreground="orange") self.logging_stext.tag_config("ERROR", foreground="red") self.logging_stext.tag_config("CRITICAL", foreground="red", underline=1) def create_world(self, dimensions=WORLD_DIMENSION): """construct world, create board""" if self.world: raise Exception("world exists") self.world = World(dimensions, mode="hex") self.board_display = WorldBoard(self, self.world.dimensions, mode="hex") self.world.generate() self.regen_btn["command"] = self.regen_world BoardStylist(self.world.factory.organisms) self.update_board() self.place() def place(self): self.board_display.pack(side="right", padx=10, pady=10) self.logging_stext.pack(side="top") self.turn_btn.pack(side="left", padx=5) self.play_btn.pack(side="left", padx=5) self.quit_btn.pack(side="left", padx=5) self.regen_btn.pack(side="left", padx=5) def turn(self): """Will execute world turn and update UI board with data from world""" self.world.turn() self.turn_btn["text"] = "TURN {}".format(self.world.turn_count) self.update_board() def update_board(self): self.board_display.update_with(self.world.board) def regen_world(self): self.world.generate() self.world.turn_count = 0 self.update_board() self.logging_stext.configure(state="normal") self.logging_stext.delete(1.0, tk.END) self.logging_stext.configure(state="disabled") def autoplay_turn(self): if self.autoplay: self.turn() self.after(200, self.autoplay_turn)
import logging from simulation.world import World from simulation.life import * logging.basicConfig(level=logging.ERROR) logger = logging.getLogger(__name__) WORLD_DIMENSION = (20, 20) TURNS = 20 if __name__ == "__main__": world = World(WORLD_DIMENSION) world.generate() for turn in range(TURNS): world.turn() world.print_board()