def __init__(self, track: 'Track', evaluate: bool = True): """ This class creates a new racing simulation and implements the controller of a car. :param track: The racing track object to be used in the simulation :param evaluate: Sets if GUI is visible or not """ simulator.evaluate = evaluate self.track = track self.sensors = [] self.episode = 1 self.track_name = track self.episode_length = track.episode_length self.game_state = simulator.Simulation(track) self.best_score = -float('inf') self.best_features = [] pass
def comp(a_track: 'Cave', weights_1: numpy.ndarray, weights_2: numpy.ndarray, sub1_points: int, sub2_points: int) -> (int, int): """ Run competition safely :param weights_1: weights from controller 1 :param weights_2: weights from controller 2 :param sub1_points: controller1's score :param sub2_points: controller2's score :return: None """ ctrl1 = Controller(chosen_cave, evaluate=False) ctrl2 = Controller2(chosen_cave, evaluate=False) simulator.evaluate = True simulation = simulator.Simulation(a_cave, player2=True) simulation.frame_step(2) frame_current = 0 episode_length = 1058 while frame_current <= 1058 and simulation.sub1.ontrack == True and simulation.sub1.oxigenio > 0 and simulation.sub2.ontrack == True and simulation.sub2.oxigenio > 0: ctrl1.sensors = simulation.sub1.sensors ctrl2.sensors = simulation.sub2.sensors simulation.sub1.sub_step(ctrl1.take_action(weights_1)) simulation.sub2.sub_step(ctrl2.take_action(weights_2)) simulation.comp_frame_step() frame_current += 1 if simulation.sub1.ontrack == False and simulation.sub2.ontrack == False: print("Empate. Player 1 e Player 2 -> Recebem 1 ponto") sub2_points += 1 sub1_points += 1 elif simulation.sub1.oxigenio <= 0 and simulation.sub2.oxigenio <= 0: print("Empate. Player 1 e Player 2 -> Recebem 1 ponto") sub2_points += 1 sub1_points += 1 elif simulation.sub1.ontrack == False or simulation.sub1.oxigenio <= 0: print("Ganhador: Player 2 -> Recebe 3 pontos") sub2_points += 3 elif simulation.sub2.ontrack == False or simulation.sub2.oxigenio <= 0: print("Ganhador: Player 1 -> Recebe 3 Pontos") sub2_points += 3 return sub1_points, sub2_points
def comp(a_track: 'Track', weights_1: numpy.ndarray, weights_2: numpy.ndarray, car1_points: int, car2_points: int) -> (int, int): """ Run competition safely :param weights_1: weights from controller 1 :param weights_2: weights from controller 2 :param car1_points: controller1's score :param car2_points: controller2's score :return: None """ ctrl1 = Controller1(chosen_track, evaluate=False) ctrl2 = Controller2(chosen_track, evaluate=False) simulator.evaluate = True simulation = simulator.Simulation(a_track, 'player2') simulation.frame_step(4) frame_current = 0 episode_length = 500 while frame_current <= episode_length: ctrl1.sensors = simulation.car1.sensors ctrl2.sensors = simulation.car_bot.sensors simulation.car1.car_step(ctrl1.take_action(weights_1)) simulation.car_bot.car_step(ctrl2.take_action(weights_2)) simulation.comp_frame_step() frame_current += 1 print("Player 1: " + str(simulation.car1.score)) print("Player 2: " + str(simulation.car_bot.score)) if simulation.car1.score > simulation.car_bot.score: print("Winner Player 1 received 3pts") car1_points += 3 elif simulation.car1.score == simulation.car_bot.score: print("Both Players received 1pt") car2_points += 1 car1_points += 1 else: print("Winner Player 2 received 3pts") car2_points += 3 return car1_points, car2_points
def run_sim(grid, program, max_steps, max_program_size, use_compile=False): grid = grid.splitlines() s = odo.Simulation(odo.DEFAULT_GRID_SIDE, debug=False, max_steps=max_steps) odo.load_grid(grid, s.grid) try: s.load_program(program.splitlines()) except odo.ParserException as e: return (False, "Could not parse source code:\n%s" % (e)) s.execute_program(compilation=use_compile) if s.instruction_num > max_program_size: return (False, "Maximum program size exceeded") if s.killed: if s.step_num >= max_program_size: return (False, "Program killed") else: return (False, "Maximum execution length exceeded") return (True, s)
exit(-1) simscale = 2 simulation_config = simulator.Simulation.Configuration() simulation_config.numClouds = 15 * simscale simulation_config.numUsers = 20 * simscale simulation_config.numServices = 20 * simscale #25*simscale simulation_config.numInternalNodes = 15 * simscale simulation_config.numBaseStations = 10 * simscale simulation_config.migration_algorithm = migration_algorithm simulation_config.service_cost_function = basicCostFunctions.ComplexCostFunctionNoActivation( ) #ComplexCostFunction() simulation_config.movement_model = 'linear' sim = simulator.Simulation(simulation_config, rng) if algorithm_choice == 8: log_file_name = input("log(model) filename: ") with open(log_file_name) as log_file: data = json.load(log_file) migration_algorithm.shared_agent.deserialize_model( [np.asarray(mat) for mat in data['model_weights']]) #global migration baseline strategies might need the entire simulation if hasattr(migration_algorithm, 'set_simulation'): migration_algorithm.set_simulation(sim) enable_visualization = True if enable_visualization: simPltPygame = SimPlotPygame(sim)
def run_experiment(id, numUsers, numEpochs, seed, target_model_update_frequency, network_depth, service_cost_function, migration_algorithm): rng = random.Random() rng.seed(seed) date_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") filename = "simulation_output/sim_target_and_depth_test_" + date_time + "_" + str( id) + ".txt" hparam = Learning.DQNMigrationAlgorithm.Hyperparameters() hparam.batch_fraction_of_replay_memory = 1 hparam.max_replay_memory_size = 1000 hparam.episode_length = 1000 hparam.num_epochs = 50 hparam.network_depth = network_depth hparam.network_width = 20 hparam.max_num_services = 3 hparam.initial_exploration_boost = 0 hparam.target_model_update_frequency = target_model_update_frequency #migration_algorithm = Learning.DQNMigrationAlgorithm(hparam, rng,True) #migration_algorithm = Learning.DQNMigrationAlgorithm(hparam, rng) simulation_config = simulator.Simulation.Configuration() simulation_config.numClouds = 15 * simscale simulation_config.numUsers = numUsers simulation_config.numServices = numUsers simulation_config.numInternalNodes = 15 * simscale simulation_config.numBaseStations = 10 * simscale simulation_config.migration_algorithm = migration_algorithm simulation_config.service_cost_function = service_cost_function #basicCostFunctions.ComplexCostFunctionNoActivation() simulation_config.movement_model = 'linear' sim = simulator.Simulation(simulation_config, rng) progress(0, num_simulation_steps) #simulate for i in range(num_simulation_steps): sim.step() if i % 500 == 0: progress(i, num_simulation_steps) # converte data to JSON data = {} data['num_simulation_steps'] = i data['random_seed'] = seed data['statistics'] = sim.statistics.getSerializableDict() data['avgRewards'] = migration_algorithm.shared_agent.avgRewards data['configuration'] = simulation_config.get_serializable_dict() data['hyperparameters'] = hparam.get_serializable_dict() if hasattr(migration_algorithm, 'shared_agent'): if hasattr(migration_algorithm.shared_agent, 'serialize_model'): data['model_weights'] = [ x.tolist() for x in migration_algorithm.shared_agent.serialize_model() ] json_str = json.dumps(data, indent=4) # create directory if not existent if not os.path.exists(os.path.dirname(filename)): try: os.makedirs(os.path.dirname(filename)) except OSError as exc: # Guard against race condition if exc.errno != errno.EEXIST: raise # write json f = open(filename, "w+") f.write(json_str) f.close() progress(num_simulation_steps, num_simulation_steps)