Skip to content

Commit

Permalink
changed cand to candidate, ignore md files in pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
danyoungday committed May 14, 2024
1 parent b561ae7 commit 91d774c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
2 changes: 2 additions & 0 deletions use_cases/eluc/.pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[MASTER]
ignore=demo, prescriptors/esp

ignore-patterns=*.md

recursive=y

fail-under=9.0
Expand Down
2 changes: 1 addition & 1 deletion use_cases/eluc/prescriptors/nsga2/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, in_size: int, hidden_size: int, out_size: int,
self.parents = parents

@classmethod
def from_crossover(cls, parent1, parent2, p_mutation: float, gen: int, cand_id: int):
def from_crossover(cls, parent1, parent2, p_mutation: float, gen: int, cand_id: int) -> "Candidate":
"""
Crossover two parents to create a child.
Take a random 50/50 choice of either parent's weights
Expand Down
2 changes: 1 addition & 1 deletion use_cases/eluc/prescriptors/nsga2/nsga2_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def calculate_crowding_distance(front):
n_objectives = len(front[0].metrics)
distances = [0 for _ in range(len(front))]
for m in range(n_objectives):
front.sort(key=lambda cand: cand.metrics[m])
front.sort(key=lambda candidate: candidate.metrics[m])
obj_min = front[0].metrics[m]
obj_max = front[-1].metrics[m]
distances[0] = float('inf')
Expand Down
12 changes: 6 additions & 6 deletions use_cases/eluc/prescriptors/nsga2/torch_prescriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def _select_parents(self, candidates: list[Candidate], n_parents: int) -> list[C
for candidate, distance in zip(front, crowding_distance):
candidate.distance = distance
if len(parents) + len(front) > n_parents: # If adding this front exceeds num_parents
front = sorted(front, key=lambda cand: cand.distance, reverse=True)
front = sorted(front, key=lambda candidate: candidate.distance, reverse=True)
parents += front[:n_parents - len(parents)]
break
parents += front
Expand All @@ -169,7 +169,7 @@ def _make_new_pop(self, parents: list[Candidate], pop_size: int, gen:int) -> lis
Makes new population by creating children from parents.
We use tournament selection to select parents for crossover.
"""
sorted_parents = sorted(parents, key=lambda cand: (cand.rank, -cand.distance))
sorted_parents = sorted(parents, key=lambda candidate: (candidate.rank, -candidate.distance))
children = []
for i in range(pop_size):
parent1, parent2 = self._tournament_selection(sorted_parents)
Expand Down Expand Up @@ -226,15 +226,15 @@ def _record_gen_results(self, gen: int, candidates: list[Candidate], save_path:
Save the pareto front to disk.
"""
# Save statistics of candidates
gen_results = [cand.record_state() for cand in candidates]
gen_results = [candidate.record_state() for candidate in candidates]
gen_results_df = pd.DataFrame(gen_results)
gen_results_df.to_csv(save_path / f"{gen}.csv", index=False)

# Save rank 1 candidate state dicts
(save_path / f"{gen}").mkdir(parents=True, exist_ok=True)
pareto_candidates = [cand for cand in candidates if cand.rank == 1]
for cand in pareto_candidates:
torch.save(cand.state_dict(), save_path / f"{gen}" / f"{cand.gen}_{cand.cand_id}.pt")
pareto_candidates = [candidate for candidate in candidates if candidate.rank == 1]
for candidate in pareto_candidates:
torch.save(candidate.state_dict(), save_path / f"{gen}" / f"{candidate.gen}_{candidate.cand_id}.pt")

def _record_candidate_avgs(self, gen: int, candidates: list[Candidate]) -> dict:
"""
Expand Down

0 comments on commit 91d774c

Please sign in to comment.