def prepare(self, kwargs): """ Prepare the initial state of the arena and start the game loop :param kwargs: Keyword arguments containing the configuration of the Scene. {"config": {1: ROBOT1_TYPE, 2: ROBOT2_TYPE}} """ self.config = kwargs['config'] self.width = 600 self.height = 600 self.turn_start = None self.resetting = False self.delay_ms = 500 self.world = World(self.config) self.canvas = Canvas(self.master, bg="white", width=self.width, height=self.height) self.canvas.pack() self.world_view = WorldView(self.world, self.canvas, self.width, self.height) # Bind to key events self.canvas.focus_set() self.canvas.bind("<Key>", self.on_key) # Start the game loop self.render() self.after_id = self.after(1000, self.game_loop)
def train_q_learning(games): print "Starting Q-learning training for %d games" % (games) world = World({ 1: STRATEGIES.Q_LEARNING, 2: STRATEGIES.VALUE_ITERATION }, False) world.epsilon = 0.5 world.reset_game() # tell world that we are in training mode world.training = True player1_wins = 0 player2_wins = 0 while (player1_wins + player2_wins) < games: sys.stdout.write("\r%f%%" % ((player1_wins + player2_wins) / float(games) * 100)) if world.game_over: if (world.winner == 1): player1_wins += 1 else: player2_wins += 1 world.reset_game() world.update() world.update() print "" # Clears the line total_games = player1_wins + player2_wins print "Player 1 won %s out of %s [%f]" % ( player1_wins, total_games, float(player1_wins) / total_games * 100.0) old = world.bot1.strategy.Q.values world.bot1.strategy.save_to_store() # Check for read/write errors. world.bot1.strategy.Q.values = None world.bot1.strategy.load_from_store() new = world.bot1.strategy.Q.values for key in old: if old[key] != new[key]: print "Difference between read/write" return for key in new: if old[key] != new[key]: print "Difference between read/write" return print "Read/Write to store was performed correctly for q-learning."
def __init__(self): self.PPM = PPM self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.WORLD_WIDTH = self.SCREEN_WIDTH / self.PPM self.WORLD_HEIGHT = self.SCREEN_HEIGHT / self.PPM self.TIME_STEP = TIME_STEP self.world = World().get_world() self.__initialize_game_borders() self._initialize_car((self.WORLD_WIDTH / 2, self.WORLD_HEIGHT / 2)) self.areas = deserialize_areas() self.obstacles = deserialize_obstacles() self.default_terrain = 'AsphaltTerrain' self.terrain = TerrainFactory.get_terrain(self.default_terrain) self.__build_obstacles()
def train_q_learning(games): print "Starting Q-learning training for %d games" % (games) world = World({1: STRATEGIES.Q_LEARNING, 2: STRATEGIES.VALUE_ITERATION}, False) world.epsilon = 0.5 world.reset_game() # tell world that we are in training mode world.training = True player1_wins = 0 player2_wins = 0 while (player1_wins + player2_wins) < games: sys.stdout.write("\r%f%%" % ((player1_wins + player2_wins) / float(games) * 100)) if world.game_over: if (world.winner == 1): player1_wins += 1 else: player2_wins += 1 world.reset_game() world.update() world.update() print "" # Clears the line total_games = player1_wins + player2_wins print "Player 1 won %s out of %s [%f]" % (player1_wins, total_games, float(player1_wins) / total_games * 100.0) old = world.bot1.strategy.Q.values world.bot1.strategy.save_to_store() # Check for read/write errors. world.bot1.strategy.Q.values = None world.bot1.strategy.load_from_store() new = world.bot1.strategy.Q.values for key in old: if old[key] != new[key]: print "Difference between read/write" return for key in new: if old[key] != new[key]: print "Difference between read/write" return print "Read/Write to store was performed correctly for q-learning."
def draw_map(): world = World() world_db = WorldDB(WORLD_DB_PATH) world.build_from_db(world_db) world_db.close() print(world.vertices) point2 = Point(50, 0, 0) point3 = Point(50, 50, 0) road = Road() road._segments.append(RoadSegment(point2, point3)) print(road.to_json()) map_data = { "fields": [parcel.to_json() for parcel in world.parcels.values()], "roads": [road.to_json()] } return render_template("map.jinja", map_data=map_data)
def setup(coordinates_interval): with open(path_join(BASEPATH, 'input/input.json')) as json_file: settings = json_load(json_file) logging.log_event('Starting the setup', 'main') # Log the start up to the loggin file wsd = {'radius': 6371000, 'wattPerSquareMetre': 1368} country_list = [ Country(c, GHG, data_instance.get_data(c)) for c in data_instance.get_country_names() ] ghg_instance = GHG() data = dict( time=Time(settings['start_year']), albedo=Albedo(), ghg=ghg_instance, countries=country_list, coordinates=create_list_coordinates( coordinates_interval, { 'albedo': Albedo, 'ghg': ghg_instance, 'country_names': data_instance.get_country_with_location( ), # {'country': [long, lat], ...} 'country_instances': country_list, })) earth = World(data, wsd) for c_x in earth.coordinates: for c_y in c_x: c_y.world_instance = earth c_y.calculate_current_temperature() mapping = Mapping(BASEPATH, settings['start_year'], data['time'].time_interval) return earth, data['time'], mapping
def train_value_iteration(): world = World( { 1: STRATEGIES.VALUE_ITERATION, 2: STRATEGIES.VALUE_ITERATION }, False) # Check for read/write errors. old = world.bot1.strategy.U world.bot1.strategy.U = None world.bot1.strategy.load_from_store() new = world.bot1.strategy.U for key in old: if old[key] != new[key]: print "Difference between read/write" return for key in new: if old[key] != new[key]: print "Difference between read/write" return print "Read/Write to store was performed correctly for value iteration."
class ArenaScene(Scene): """ ArenaScene is the Scene in which the robots actually fight. """ def prepare(self, kwargs): """ Prepare the initial state of the arena and start the game loop :param kwargs: Keyword arguments containing the configuration of the Scene. {"config": {1: ROBOT1_TYPE, 2: ROBOT2_TYPE}} """ self.config = kwargs['config'] self.width = 600 self.height = 600 self.turn_start = None self.resetting = False self.delay_ms = 500 self.world = World(self.config) self.canvas = Canvas(self.master, bg="white", width=self.width, height=self.height) self.canvas.pack() self.world_view = WorldView(self.world, self.canvas, self.width, self.height) # Bind to key events self.canvas.focus_set() self.canvas.bind("<Key>", self.on_key) # Start the game loop self.render() self.after_id = self.after(1000, self.game_loop) def game_loop(self): """ Update the world and render. The update may not complete due to other """ if self.resetting: # Make sure that no old "after" calls run while resetting. return if self.turn_start is None: self.turn_start = datetime.now() update_successful = self.update() self.render() if update_successful: # If turn took longer than 1 second, repeat immediately. # Else, repeat after 1 second has passed since the start of the turn. turn_finish = datetime.now() dt = turn_finish - self.turn_start self.turn_start = None dt_ms = int(dt.total_seconds() * 1000) if self.world.state == WORLD_STATES.COUNT_DOWN: delay = 1000 else: delay = self.delay_ms after_ms = max(100, delay - dt_ms) self.after(after_ms, self.game_loop) else: # update was not completed. Allow Tk mainloop to get events. self.after_id = self.after(0, self.game_loop) def render(self): """ Render the world. """ self.world_view.render() def update(self): """ Update this Scene :return: True if the update completed, else False """ return self.world.update() def cleanup(self): """ Destroy the canvas to clean up before destruction. """ self.resetting = True self.after_cancel(self.after_id) self.canvas.destroy() def on_key(self, event): """ Keypress callback Need to handle keypress up, down, left, right :param event: The Key Event """ if event.keysym == "d": self.world.debug = not self.world.debug elif event.keysym == "m": self.master.set_scene(SCENES.TITLE,config={1:None, 2: None}) return elif event.keysym == "r": self.master.set_scene(SCENES.ARENA, config=self.config) return elif event.keysym == 'minus': self.delay_ms = max(0, self.delay_ms - 50) elif event.keysym == 'equal': self.delay_ms = max(0, self.delay_ms + 50) else: self.world.key_event = event self.world_view.render()
class GameController(metaclass=Singleton): def __init__(self): self.PPM = PPM self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.WORLD_WIDTH = self.SCREEN_WIDTH / self.PPM self.WORLD_HEIGHT = self.SCREEN_HEIGHT / self.PPM self.TIME_STEP = TIME_STEP self.world = World().get_world() self.__initialize_game_borders() self._initialize_car((self.WORLD_WIDTH / 2, self.WORLD_HEIGHT / 2)) self.areas = deserialize_areas() self.obstacles = deserialize_obstacles() self.default_terrain = 'AsphaltTerrain' self.terrain = TerrainFactory.get_terrain(self.default_terrain) self.__build_obstacles() def __initialize_game_borders(self): """ Builds borders for cointaning the game environment """ horizontal_width = self.WORLD_WIDTH + 1 vertical_height = self.WORLD_HEIGHT + 1 # Ground ground = self.world.CreateStaticBody( position=(0, -1), shapes=polygonShape(box=(horizontal_width, 1))) # Left Wall left_wall = self.world.CreateStaticBody( position=(-1, 0), shapes=polygonShape(box=(1, vertical_height))) # Ceilling ceilling = self.world.CreateStaticBody( position=(0, vertical_height), shapes=polygonShape(box=(horizontal_width, 1))) # Right Wall right_wall = self.world.CreateStaticBody( position=(horizontal_width, 0), shapes=polygonShape(box=(1, vertical_height))) def _initialize_car(self, car_pos): """ Initializes the car """ self.car = self.world.CreateDynamicBody(position=(car_pos[0], car_pos[1])) self.car.angularDamping = 0.1 self.car_box = self.car.CreatePolygonFixture(box=(2, 1), density=1, friction=0.3) def update(self): """ Updates internal state of the game """ # Check terrain of the area self.terrain = TerrainFactory.get_terrain(self.default_terrain) for a in self.areas: if a.contains_point(self.car.worldCenter): self.terrain = a.terrain self.terrain.apply_ordinary_forces(self.car) def handle_event(self, event): """ Handles input commands """ self.terrain.handle_impulse_event(self.car, EventFactory.create(event)) def apply_acceleration(self, ac): self.car.ApplyForce(force=ac * self.car.GetWorldVector(Axis.VERTICAL.value), point=self.car.worldCenter, wake=True) def apply_torque(self, tor): self.car.ApplyTorque(tor, wake=True) def get_default_terrain_color(self): """ Returns the default terrain colour """ return TerrainFactory.get_terrain(self.default_terrain).COLOR def get_terrains(self): """ Returns the areas of different terrains and their colors for the UI to render (the returned value is specially adapted for the pygame engine) The returned value is a list of tuples of the type [(colour, [rect_information])] """ render_areas = [] for a in self.areas: a_color = a.terrain.COLOR rect_width = (a.upper_x - a.lower_x) * self.PPM rect_height = (a.upper_y - a.lower_y) * self.PPM lower_x = a.lower_x * self.PPM lower_y = self.SCREEN_HEIGHT - a.lower_y * self.PPM - rect_height render_areas.append({ "COLOR": a_color, "RECT": [lower_x, lower_y, rect_width, rect_height] }) return render_areas def get_bodies(self): """ Returns the objects and their coordinates """ fixtures = [] for body in self.world.bodies: for fixture in body.fixtures: shape = fixture.shape vertices = [(body.transform * v) * self.PPM for v in shape.vertices] vertices = [(v[0], self.SCREEN_HEIGHT - v[1]) for v in vertices] fixtures.append({ "BODY": Body(body.type), "VERTICES": vertices }) return fixtures def step(self): self.world.Step(self.TIME_STEP, 5, 5) self.world.ClearForces() def __build_obstacles(self): """ Builds the obstacles for the game """ for o_vertices in self.obstacles: self.world.CreateStaticBody(shapes=polygonShape( vertices=o_vertices)) def get_car_state(self): """ Returns the car state, ie, its x and y position, its velocity and torque """ x, y = self.car.position angle = self.car.angle vel_x, vel_y = self.car.linearVelocity ang_vel = self.car.angularVelocity return [x, y, angle, vel_x, vel_y, ang_vel] def _reset_car(self, car_pos): """ Resets car """ self.world.DestroyBody(self.car) self._initialize_car(car_pos)
class ArenaScene(Scene): """ ArenaScene is the Scene in which the robots actually fight. """ def prepare(self, kwargs): """ Prepare the initial state of the arena and start the game loop :param kwargs: Keyword arguments containing the configuration of the Scene. {"config": {1: ROBOT1_TYPE, 2: ROBOT2_TYPE}} """ self.config = kwargs['config'] self.width = 600 self.height = 600 self.turn_start = None self.resetting = False self.delay_ms = 500 self.world = World(self.config) self.canvas = Canvas(self.master, bg="white", width=self.width, height=self.height) self.canvas.pack() self.world_view = WorldView(self.world, self.canvas, self.width, self.height) # Bind to key events self.canvas.focus_set() self.canvas.bind("<Key>", self.on_key) # Start the game loop self.render() self.after_id = self.after(1000, self.game_loop) def game_loop(self): """ Update the world and render. The update may not complete due to other """ if self.resetting: # Make sure that no old "after" calls run while resetting. return if self.turn_start is None: self.turn_start = datetime.now() update_successful = self.update() self.render() if update_successful: # If turn took longer than 1 second, repeat immediately. # Else, repeat after 1 second has passed since the start of the turn. turn_finish = datetime.now() dt = turn_finish - self.turn_start self.turn_start = None dt_ms = int(dt.total_seconds() * 1000) if self.world.state == WORLD_STATES.COUNT_DOWN: delay = 1000 else: delay = self.delay_ms after_ms = max(100, delay - dt_ms) self.after(after_ms, self.game_loop) else: # update was not completed. Allow Tk mainloop to get events. self.after_id = self.after(0, self.game_loop) def render(self): """ Render the world. """ self.world_view.render() def update(self): """ Update this Scene :return: True if the update completed, else False """ return self.world.update() def cleanup(self): """ Destroy the canvas to clean up before destruction. """ self.resetting = True self.after_cancel(self.after_id) self.canvas.destroy() def on_key(self, event): """ Keypress callback Need to handle keypress up, down, left, right :param event: The Key Event """ if event.keysym == "d": self.world.debug = not self.world.debug elif event.keysym == "m": self.master.set_scene(SCENES.TITLE, config={1: None, 2: None}) return elif event.keysym == "r": self.master.set_scene(SCENES.ARENA, config=self.config) return elif event.keysym == 'minus': self.delay_ms = max(0, self.delay_ms - 50) elif event.keysym == 'equal': self.delay_ms = max(0, self.delay_ms + 50) else: self.world.key_event = event self.world_view.render()