async def run(): logger.info('Hello World!') settings = parser.parse_args() # Start Simulator if settings.simulator_cmd != 'debug': simulator_supervisor = DynamicSimSupervisor( world_file=settings.world, simulator_cmd=settings.simulator_cmd, simulator_args=["--verbose"], plugins_dir_path=os.path.join('.', 'build', 'lib'), models_dir_path=os.path.join('.', 'models'), simulator_name='gazebo' ) await simulator_supervisor.launch_simulator(port=settings.port_start) await asyncio.sleep(0.1) connection = await World.create() if connection: logger.info("Connected to the simulator world.") await connection.pause(True) while True: await asyncio.sleep(10.0)
async def _evaluate_robot(self, simulator_connection, robot, conf): if robot.failed_eval_attempt_count == 3: logger.info( f'Robot {robot.phenotype.id} evaluation failed (reached max attempt of 3), fitness set to None.' ) robot_fitness = None return robot_fitness, None else: # Change this `max_age` from the command line parameters (--evalution-time) max_age = conf.evaluation_time robot_manager = await simulator_connection.insert_robot( robot.phenotype, Vector3(0, 0, self._settings.z_start), max_age) start = time.time() # Start a run loop to do some stuff while not robot_manager.dead: # robot_manager.age() < max_age: await asyncio.sleep(1.0 / 2) # 5= state_update_frequency end = time.time() elapsed = end - start logger.info(f'Time taken: {elapsed}') robot_fitness = conf.fitness_function(robot_manager, robot) simulator_connection.unregister_robot(robot_manager) # await simulator_connection.delete_all_robots() # await simulator_connection.delete_robot(robot_manager) # await simulator_connection.pause(True) await simulator_connection.reset(rall=True, time_only=True, model_only=False) return robot_fitness, measures.BehaviouralMeasurements( robot_manager, robot)
async def _simulator_queue_worker(self, i): try: self._free_simulator[i] = True while True: logger.info(f"simulator {i} waiting for robot") (robot, future, conf) = await self._robot_queue.get() self._free_simulator[i] = False logger.info( f"Picking up robot {robot.phenotype.id} into simulator {i}" ) success = await self._worker_evaluate_robot( self._connections[i], robot, future, conf) if success: if robot.failed_eval_attempt_count == 3: logger.info( "Robot failed to be evaluated 3 times. Saving robot to failed_eval file" ) conf.experiment_management.export_failed_eval_robot( robot) robot.failed_eval_attempt_count = 0 logger.info( f"simulator {i} finished robot {robot.phenotype.id}") else: # restart of the simulator happened robot.failed_eval_attempt_count += 1 logger.info( f"Robot {robot.phenotype.id} current failed attempt: {robot.failed_eval_attempt_count}" ) await self._robot_queue.put((robot, future, conf)) await self._restart_simulator(i) self._robot_queue.task_done() self._free_simulator[i] = True except Exception: logger.exception(f"Exception occurred for Simulator worker {i}")
def export_snapshots(self, individuals, gen_num): if self.settings.recovery_enabled: for environment in self.environments: path = os.path.join(self._experiment_folder()+'/selectedpop_'+environment, f'selectedpop_{gen_num}') if os.path.exists(path): shutil.rmtree(path) os.mkdir(path) for ind in individuals: self.export_phenotype_images('selectedpop_'+environment+'/'+f'selectedpop_{str(gen_num)}', ind[environment]) logger.info(f'Exported snapshot {str(gen_num)} with {str(len(individuals))} individuals')
async def _evaluate_robot(simulator_connection, robot, conf): await simulator_connection.pause(True) robot_manager = await simulator_connection.insert_robot( robot, Vector3(0, 0, 0.25)) await simulator_connection.pause(False) start = time.time() # Start a run loop to do some stuff max_age = conf.evaluation_time while robot_manager.age() < max_age: await asyncio.sleep(1.0 / 5) # 5= state_update_frequency end = time.time() elapsed = end - start logger.info(f'Time taken: {elapsed}') robot_fitness = conf.fitness_function(robot_manager) await simulator_connection.pause(True) delete_future = await simulator_connection.delete_all_robots( ) # robot_manager await delete_future return robot_fitness
async def _simulator_queue_worker(self, i): self._free_simulator[i] = True while True: logger.info(f"simulator {i} waiting for robot") (robot, future, conf) = await self._robot_queue.get() logger.info(f"Picking up robot {robot.id} into simulator {i}") self._free_simulator[i] = False start = time.time() evaluation_future = asyncio.create_task( self._evaluate_robot(self._connections[i], robot, conf)) broken = False while not evaluation_future.done(): elapsed = time.time() - start if elapsed > 5: await self._robot_queue.put((robot, future, conf)) await self._restart_simulator(i) broken = True break await asyncio.sleep(0.1) if not broken: await evaluation_future robot_fitness = evaluation_future.result() future.set_result(robot_fitness) self._robot_queue.task_done() self._free_simulator[i] = True logger.info(f"simulator {i} finished robot {robot.id}")
async def _worker_evaluate_robot(self, connection, robot, future, conf): await asyncio.sleep(0.01) start = time.time() try: timeout = self.EVALUATION_TIMEOUT # seconds result = await asyncio.wait_for(self._evaluate_robot( connection, robot, conf), timeout=timeout) except asyncio.TimeoutError: # WAITED TO MUCH, RESTART SIMULATOR elapsed = time.time() - start logger.error(f"Simulator restarted after {elapsed}") return False except Exception: logger.exception(f"Exception running robot {robot.phenotype}") return False elapsed = time.time() - start logger.info(f"time taken to do a simulation {elapsed}") robot.failed_eval_attempt_count = 0 future.set_result(result) return True
async def run(): """ The main coroutine, which is started below. """ # experiment params # num_generations = 100 population_size = 100 offspring_size = 50 genotype_conf = PlasticodingConfig( max_structural_modules=100, ) mutation_conf = MutationConfig( mutation_prob=0.8, genotype_conf=genotype_conf, ) crossover_conf = CrossoverConfig( crossover_prob=0.8, ) # experiment params # # Parse command line / file input arguments settings = parser.parse_args() experiment_management = ExperimentManagement(settings) do_recovery = settings.recovery_enabled and not experiment_management.experiment_is_new() logger.info('Activated run '+settings.run+' of experiment '+settings.experiment_name) if do_recovery: gen_num, has_offspring, next_robot_id = experiment_management.read_recovery_state(population_size, offspring_size) if gen_num == num_generations-1: logger.info('Experiment is already complete.') return else: gen_num = 0 next_robot_id = 1 def fitness_function(robot_manager, robot): contacts = measures.contacts(robot_manager, robot) assert(contacts != 0) return fitness.displacement_velocity_hill(robot_manager, robot) population_conf = PopulationConfig( population_size=population_size, genotype_constructor=random_initialization, genotype_conf=genotype_conf, fitness_function=fitness_function, mutation_operator=standard_mutation, mutation_conf=mutation_conf, crossover_operator=standard_crossover, crossover_conf=crossover_conf, selection=lambda individuals: tournament_selection(individuals, 2), parent_selection=lambda individuals: multiple_selection(individuals, 2, tournament_selection), population_management=steady_state_population_management, population_management_selector=tournament_selection, evaluation_time=settings.evaluation_time, offspring_size=offspring_size, experiment_name=settings.experiment_name, experiment_management=experiment_management, ) settings = parser.parse_args() simulator_queue = SimulatorQueue(settings.n_cores, settings, settings.port_start) await simulator_queue.start() population = Population(population_conf, simulator_queue, next_robot_id) if do_recovery: # loading a previous state of the experiment await population.load_snapshot(gen_num) if gen_num >= 0: logger.info('Recovered snapshot '+str(gen_num)+', pop with ' + str(len(population.individuals))+' individuals') if has_offspring: individuals = await population.load_offspring(gen_num, population_size, offspring_size, next_robot_id) gen_num += 1 logger.info('Recovered unfinished offspring '+str(gen_num)) if gen_num == 0: await population.init_pop(individuals) else: population = await population.next_gen(gen_num, individuals) experiment_management.export_snapshots(population.individuals, gen_num) else: # starting a new experiment experiment_management.create_exp_folders() await population.init_pop() experiment_management.export_snapshots(population.individuals, gen_num) while gen_num < num_generations-1: gen_num += 1 population = await population.next_gen(gen_num) experiment_management.export_snapshots(population.individuals, gen_num)
async def run(): """ The main coroutine, which is started below. """ # experiment params # num_generations = 200 population_size = 100 offspring_size = 100 front = 'slaves' # environment world and z-start environments = {'plane': 0.03, 'tilted5': 0.1 } genotype_conf = PlasticodingConfig( max_structural_modules=15, plastic=True, ) mutation_conf = MutationConfig( mutation_prob=0.8, genotype_conf=genotype_conf, ) crossover_conf = CrossoverConfig( crossover_prob=0.8, ) # experiment params # # Parse command line / file input arguments settings = parser.parse_args() experiment_management = ExperimentManagement(settings, environments) do_recovery = settings.recovery_enabled and not experiment_management.experiment_is_new() logger.info('Activated run '+settings.run+' of experiment '+settings.experiment_name) if do_recovery: gen_num, has_offspring, next_robot_id = experiment_management.read_recovery_state(population_size, offspring_size) if gen_num == num_generations-1: logger.info('Experiment is already complete.') return else: gen_num = 0 next_robot_id = 1 def fitness_function_plane(robot_manager, robot): return fitness.displacement_velocity_hill(robot_manager, robot, False) fitness_function = {'plane': fitness_function_plane, 'tilted5': fitness_function_plane} population_conf = PopulationConfig( population_size=population_size, genotype_constructor=random_initialization, genotype_conf=genotype_conf, fitness_function=fitness_function, mutation_operator=standard_mutation, mutation_conf=mutation_conf, crossover_operator=standard_crossover, crossover_conf=crossover_conf, selection=lambda individuals: tournament_selection(individuals, environments, 2), parent_selection=lambda individuals: multiple_selection(individuals, 2, tournament_selection, environments), population_management=steady_state_population_management, population_management_selector=tournament_selection, evaluation_time=settings.evaluation_time, offspring_size=offspring_size, experiment_name=settings.experiment_name, experiment_management=experiment_management, environments=environments, front=front ) settings = parser.parse_args() simulator_queue = {} analyzer_queue = None previous_port = None for environment in environments: settings.world = environment settings.z_start = environments[environment] if previous_port is None: port = settings.port_start previous_port = port else: port = previous_port+settings.n_cores previous_port = port simulator_queue[environment] = SimulatorQueue(settings.n_cores, settings, port) await simulator_queue[environment].start() analyzer_queue = AnalyzerQueue(1, settings, port+settings.n_cores) await analyzer_queue.start() population = Population(population_conf, simulator_queue, analyzer_queue, next_robot_id) if do_recovery: if gen_num >= 0: # loading a previous state of the experiment await population.load_snapshot(gen_num) logger.info('Recovered snapshot '+str(gen_num)+', pop with ' + str(len(population.individuals))+' individuals') if has_offspring: individuals = await population.load_offspring(gen_num, population_size, offspring_size, next_robot_id) gen_num += 1 logger.info('Recovered unfinished offspring '+str(gen_num)) if gen_num == 0: await population.init_pop(individuals) else: population = await population.next_gen(gen_num, individuals) experiment_management.export_snapshots(population.individuals, gen_num) else: # starting a new experiment experiment_management.create_exp_folders() await population.init_pop() experiment_management.export_snapshots(population.individuals, gen_num) while gen_num < num_generations-1: gen_num += 1 population = await population.next_gen(gen_num) experiment_management.export_snapshots(population.individuals, gen_num)
async def run(): """ The main coroutine, which is started below. """ # experiment params # num_generations = 100 population_size = 100 offspring_size = 50 front = 'slaves' # environment world and z-start environments = {'plane': 0.03, 'tilted5': 0.1} genotype_conf = PlasticodingConfig( max_structural_modules=15, plastic=True, ) mutation_conf = MutationConfig( mutation_prob=0.8, genotype_conf=genotype_conf, ) crossover_conf = CrossoverConfig(crossover_prob=0.8, ) # experiment params #load_individual # Parse command line / file input arguments settings = parser.parse_args() experiment_management = ExperimentManagement(settings, environments) do_recovery = settings.recovery_enabled and not experiment_management.experiment_is_new( ) logger.info('Activated run ' + settings.run + ' of experiment ' + settings.experiment_name) def fitness_function(robot_manager, robot): #contacts = measures.contacts(robot_manager, robot) #assert(contacts != 0) return fitness.displacement_velocity_hill(robot_manager, robot, False) population_conf = PopulationConfig( population_size=population_size, genotype_constructor=random_initialization, genotype_conf=genotype_conf, fitness_function=fitness_function, mutation_operator=standard_mutation, mutation_conf=mutation_conf, crossover_operator=standard_crossover, crossover_conf=crossover_conf, selection=lambda individuals: tournament_selection( individuals, environments, 2), parent_selection=lambda individuals: multiple_selection( individuals, 2, tournament_selection, environments), population_management=steady_state_population_management, population_management_selector=tournament_selection, evaluation_time=settings.evaluation_time, offspring_size=offspring_size, experiment_name=settings.experiment_name, experiment_management=experiment_management, environments=environments, front=front) settings = parser.parse_args() simulator_queue = {} analyzer_queue = None previous_port = None for environment in environments: settings.world = environment settings.z_start = environments[environment] if previous_port is None: port = settings.port_start previous_port = port else: port = previous_port + settings.n_cores previous_port = port simulator_queue[environment] = SimulatorQueue(settings.n_cores, settings, port) await simulator_queue[environment].start() analyzer_queue = AnalyzerQueue(1, settings, port + settings.n_cores) await analyzer_queue.start() population = Population(population_conf, simulator_queue, analyzer_queue, 1) # choose a snapshot here. and the maximum best individuals you wish to watch generation = 61 #99 max_best = 3 #10 await population.load_snapshot(generation) values = [] for ind in population.individuals: # define a criteria here for environment in environments: ind[environment].evaluated = False values.append(ind[list(environments.keys())[-1]].consolidated_fitness) #values.append(ind['plane'].phenotype._behavioural_measurements.displacement_velocity_hill) values = np.array(values) ini = len(population.individuals) - max_best fin = len(population.individuals) population.individuals = np.array(population.individuals) # highest population.individuals = population.individuals[np.argsort(values) [ini:fin]] # lowest #population.individuals = population.individuals[np.argsort(values)[0:max_best]] for ind in population.individuals: print(ind[list(environments.keys())[-1]].phenotype.id) print('consolidated_fitness', ind[list(environments.keys())[-1]].consolidated_fitness) for environment in environments: print(environment) await population.evaluate(new_individuals=population.individuals, gen_num=generation, environment=environment, type_simulation='watch')
async def run(): logger.info('Hello World!')