Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
rpreen committed May 19, 2024
1 parent 098709b commit 82504b8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions nkcs/ea.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def run_initial(self, nkcs: NKCS) -> None:
for s in range(Cons.S):
for p in range(Cons.P):
team[s] = self.pop[s][p]
genomes = np.asarray([ind.genome for ind in team])
genomes = [ind.genome for ind in team]
team[s].fitness = nkcs.calc_team_fit(genomes)
self.__update_archive(s, team[s])
self.evals += 1
Expand All @@ -103,7 +103,7 @@ def __eval_team(self, nkcs: NKCS, team: list[Ind]) -> None:
Assign fitness to each individual if it's the best seen.
"""
genomes = np.asarray([ind.genome for ind in team])
genomes = [ind.genome for ind in team]
team_fit: Final[float] = nkcs.calc_team_fit(genomes)
for s in range(Cons.S):
team[s].fitness = max(team[s].fitness, team_fit)
Expand All @@ -126,7 +126,7 @@ def __update_archive(self, s: int, p: Ind) -> None:
self.archive[s].pop(0)

def update_perf(
self, evals: list[int], perf_best: np.ndarray, perf_avg: np.ndarray
self, evals: np.ndarray, perf_best: np.ndarray, perf_avg: np.ndarray
) -> None:
"""Update current performance tracking."""
if self.evals % (Cons.P * Cons.S) == 0:
Expand Down Expand Up @@ -180,7 +180,9 @@ def get_best_fit(self, s: int) -> float:

def get_avg_fit(self, s: int) -> float:
"""Return the average fitness of a given species."""
return np.mean([p.fitness for p in self.pop[s]])
fitnesses: list[float] = [p.fitness for p in self.pop[s]]
mean = np.mean(fitnesses)
return float(mean)

def print_archive(self, s: int) -> None:
"""Print the archived individuals."""
Expand Down
2 changes: 1 addition & 1 deletion nkcs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
ea[r].run_ea(nkcs[f], evals[r], perf_best[r], perf_avg[r])
else:
ea[r].run_sea(nkcs[f], evals[r], perf_best[r], perf_avg[r])
best_fit: Final[float] = ea[r].get_best_fit(0)
best_fit: float = ea[r].get_best_fit(0)
status = f"nkcs {f} experiment {e} complete: {best_fit:.5f}"
r += 1
bar.set_description(status)
Expand Down
2 changes: 1 addition & 1 deletion nkcs/surrogate.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def model_mlp(seed: Optional[int]) -> MLPRegressor:


def fit_model(
x: np.ndarray, y: np.ndarray, seed: int = None
x: np.ndarray, y: np.ndarray, seed: int | None = None
) -> Union[GaussianProcessRegressor, MLPRegressor]:
"""Train a surrogate model."""
if Cons.MODEL == "gp":
Expand Down

0 comments on commit 82504b8

Please sign in to comment.