def evolve(self, portfolio: Portfolio) -> None: """Осуществляет эволюции. При необходимости создается начальная популяция из организмов по умолчанию. """ self._setup() tickers = tuple(portfolio.index[:-2]) end = portfolio.date scale = 1.0 step = 0 while True: step += 1 print(f"***{end.date()}: Шаг эволюции — {step}***") population.print_stat() print(f"Фактор - {scale:.2%}\n") parent = population.get_parent() print("Переоцениваю родителя:") if _eval_and_print(parent, tickers, end) is None: scale *= SCALE_DOWN continue child = parent.make_child(scale) print("Потомок:") if _eval_and_print(child, tickers, end) is None: scale *= SCALE_DOWN continue if population.count() > self._max_population: _kill_weakest(child)
def _eval_exiting(self, portfolio: Portfolio) -> None: """Оценивает существующих.""" tickers = tuple(portfolio.index[:-2]) end = portfolio.date count = population.count() for step, parent in enumerate(population.get_all_organisms(), 1): print(f"***{end.date()}: Шаг переоценки существующих организмов - {step}/{count}***") population.print_stat() print() print("Родитель:") parent_fitness = self._eval_and_print(parent, tickers, end) if parent_fitness is None: continue
def evolve(self) -> None: """Осуществляет эволюции. При необходимости создается начальная популяция из случайных организмов по умолчанию. """ step = 0 current = None while _check_time_range(): step, current = self._step_setup(step, current) date = self._end.date() self._logger.info(f"***{date}: Шаг эволюции — {step}***") population.print_stat() scale = 1 / self._scale self._logger.info(f"Scale - {scale:.2%}\n") # noqa: WPS221 current = self._maybe_clear(current) current = self._step(current)
def evolve(self) -> None: """Осуществляет эволюции. При необходимости создается начальная популяция из случайных организмов по умолчанию. """ step = 0 org = None while _check_time_range(): step = self._step_setup(step) date = self._end.date() self._logger.info(f"***{date}: Шаг эволюции — {step}***") population.print_stat() self._logger.info(f"Scale - {self._scale}\n") if org is None: org = population.get_next_one( self._end) or population.get_next_one(None) org = self._step(org)
def evolve(self, portfolio: Portfolio) -> NoReturn: """Осуществляет одну эпоху эволюции. При необходимости создается начальная популяция из организмов по умолчанию. """ tickers = tuple(portfolio.index[:-2]) end = portfolio.date self._setup() count = population.count() for step, parent in enumerate(population.get_all_organisms(), 1): print(f"***{end.date()}: Шаг эпохи - {step}/{count}***") population.print_stat() print() print("Родитель:") parent_fitness = self._eval_and_print(parent, tickers, end) if parent_fitness is None: continue child = parent.make_child() print("Потомок:") child_fitness = self._eval_and_print(child, tickers, end) if child_fitness is None: continue if population.count() <= self._max_population: continue weakest = parent if parent_fitness > child_fitness: weakest = child weakest = weakest.find_weaker() print("Более слабый и наиболее медленный - удаляю:") self._eval_and_print(weakest, tickers, end) weakest.die()
def evolve(self, portfolio: Portfolio) -> NoReturn: """Осуществляет одну эпоху эволюции. При необходимости создается начальная популяция из организмов по умолчанию. """ tickers = tuple(portfolio.index[:-2]) end = portfolio.date self._setup() self._eval_exiting(portfolio) count = population.count() factor = MUTATION_FACTOR for step, parent in enumerate(population.get_all_organisms(), 1): print(f"***{end.date()}: Шаг размножения - {step}/{count}***") population.print_stat() print(f"Фактор - {factor:.2%}") print() child = parent.make_child(factor) print("Потомок:") child_fitness = self._eval_and_print(child, tickers, end) if child_fitness is None: factor *= MUTATION_FACTOR continue if population.count() <= self._max_population: continue weakest = child.find_weaker() print("Наиболее слабый - удаляю:") self._eval_and_print(weakest, tickers, end) weakest.die()
def test_print_stat(capsys): population.print_stat() captured = capsys.readouterr() assert "LLH" in captured.out assert "Максимум побед" in captured.out
def test_print_stat(caplog): with caplog.at_level(logging.INFO): population.print_stat() assert "LLH" in caplog.records[0].msg assert "Максимум оценок" in caplog.records[2].msg