def test_default_proxy_is_progress_proxy(self): driver_factory, _ = prepare("NSGAII", "ZDT1") driver = driver_factory() for _ in range(3): with self.subTest(): proxy = driver.next_step() self.assertIsInstance(proxy, ProgressMessage) self.assertEqual(proxy.step_no, driver.step_no - 1) self.assertEqual(proxy.cost, driver.cost)
def run(self): log_helper.init() logger = logging.getLogger(__name__) logger.info( "Starting the worker. PID: %d, simulation case: %s", os.getpid(), self.simulation, ) if self.simulation.renice and os.name == "posix": logger.debug("Renice the process PID:%s by %s", os.getpid(), self.simulation) os.nice(int(self.simulation.renice)) # pylint: disable=no-member self._init_random_seed(logger) driver = None try: final_driver, problem_mod = factory.prepare( self.simulation.algorithm_name, self.simulation.problem_name) logger.debug("Creating the driver used to perform computation") driver = final_driver() proc_time = [] logger.debug("Beginning processing of %s, simulation: %s", driver, self.simulation) with log_time( process_time, logger, "Processing done in {time_res}s CPU time", out=proc_time, ): results = self.run_driver(driver, problem_mod, logger) return results, proc_time[-1], self.simulation_no except NotViableConfiguration as e: reason = inspect.trace()[-1] logger.info( "Configuration disabled by %s:%d:%s. simulation case:%s", reason[1], reason[2], reason[3], self.simulation, ) logger.debug("Configuration disabled args:%s. Stack:", exc_info=e) except Exception as e: logger.exception("Some error", exc_info=e) finally: if driver: driver.shutdown() logger.debug("Finished processing. simulation case:%s", self.simulation)
def test_steps(self): final_driver, problem_mod = prepare("HGS+NSGAII", "ZDT1") print(final_driver()) hgs = final_driver() steps = [0, 1, 3] for steps_no in steps: with self.subTest(steps_no=steps_no): steps_run = StepsRun(steps_no) results = [] steps_run.create_job(hgs).subscribe( lambda result: results.append(result)) self.assertEqual(len(results), steps_no)
def test_results_number_is_correct(self): steps = [0, 1, 3] for steps_no in steps: with self.subTest(steps_no=steps_no): final_driver, problem_mod = prepare("IMGA+NSGAII", "ZDT1") print(final_driver()) imga = final_driver() steps_run = StepsRun(steps_no) results = [] steps_run.create_job(imga).subscribe( lambda proxy: results.append(proxy)) self.assertEqual(len(results), steps_no)
def test_create_and_run_all_supported_algorithms(self): sys = ActorSystem("multiprocTCPBase", logDefs=log_helper.EVOGIL_LOG_CONFIG) test_cases = run_config.algorithms for test_case in test_cases: with self.subTest(algorithm=test_case): algo_factory, _ = prepare(test_case, "ZDT1") algorithm = algo_factory() simple_simulation = StepsRun(1) result = list( simple_simulation.create_job(algorithm) .pipe(ops.subscribe_on(NewThreadScheduler()), ops.to_iterable()) .run() ) self.assertEqual(1, len(result)) self.assertIsInstance(result[0], ProgressMessage) sys.shutdown()
def test_imga_cost_calculation(self): final_driver, problem_mod = prepare("IMGA+NSGAII", "ZDT1") imga = final_driver() steps_run = StepsRun(4) total_costs = [] islands_costs = [] def on_imga_result(result): total_costs.append(result.cost), islands_costs.append( sum([island.driver.cost for island in imga.islands])) steps_run.create_job(imga).pipe( ops.subscribe_on(NewThreadScheduler()), ops.do_action(on_next=on_imga_result), ).run() self.assertListEqual(total_costs, islands_costs)