Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix mypy errors#3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
SSteve wants to merge1 commit intodavecom:master
base:master
Choose a base branch
Loading
fromSSteve:fix-mypy-error
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletionsChapter5/genetic_algorithm.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,11 +38,11 @@ def __init__(self, initial_population: List[C], threshold: float, max_generation

# Use the probability distribution wheel to pick 2 parents
# Note: will not work with negative fitness results
def _pick_roulette(self, wheel: List[float]) -> Tuple[C,C]:
def _pick_roulette(self, wheel: List[float]) -> Tuple[C,...]:
return tuple(choices(self._population, weights=wheel, k=2))

# Choose num_participants at random and take the best 2
def _pick_tournament(self, num_participants: int) -> Tuple[C,C]:
def _pick_tournament(self, num_participants: int) -> Tuple[C,...]:
participants: List[C] = choices(self._population, k=num_participants)
return tuple(nlargest(2, participants, key=self._fitness_key))

Expand All@@ -53,7 +53,7 @@ def _reproduce_and_replace(self) -> None:
while len(new_population) < len(self._population):
# pick the 2 parents
if self._selection_type == GeneticAlgorithm.SelectionType.ROULETTE:
parents: Tuple[C,C] = self._pick_roulette([x.fitness() for x in self._population])
parents: Tuple[C,...] = self._pick_roulette([x.fitness() for x in self._population])
else:
parents = self._pick_tournament(len(self._population) // 2)
# potentially crossover the 2 parents
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp