Spaces:
Sleeping
Sleeping
Update core/evolution/evolution.py
Browse files- core/evolution/evolution.py +22 -20
core/evolution/evolution.py
CHANGED
|
@@ -155,47 +155,49 @@ class MolecularEvolution:
|
|
| 155 |
def _run_evolution_loop(self):
|
| 156 |
"""Run the main evolution loop."""
|
| 157 |
for gen in range(1, self.config.generations + 1):
|
|
|
|
| 158 |
self._log_generation_stats(gen)
|
| 159 |
-
|
|
|
|
|
|
|
| 160 |
survivors = self.population.get_survivors()
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
if survivors:
|
| 166 |
is_final_gen = (gen == self.config.generations)
|
| 167 |
-
|
| 168 |
if is_final_gen:
|
| 169 |
-
# FINAL generation →
|
| 170 |
if self.config.maximize_cn:
|
| 171 |
sorted_survivors = sorted(survivors, key=lambda m: m.cn, reverse=True)
|
| 172 |
else:
|
| 173 |
sorted_survivors = sorted(survivors, key=lambda m: m.cn_error)
|
| 174 |
-
|
| 175 |
chosen = sorted_survivors[:5]
|
| 176 |
-
|
| 177 |
else:
|
| 178 |
-
# EARLY generations →
|
| 179 |
survivor_smiles = set(m.smiles for m in survivors)
|
| 180 |
rejected = [m for m in population if m.smiles not in survivor_smiles]
|
| 181 |
-
|
| 182 |
-
chosen = rejected[:5]
|
| 183 |
-
|
| 184 |
sample_smiles = [m.smiles for m in chosen]
|
| 185 |
-
sample_smiles = list(dict.fromkeys(sample_smiles))
|
| 186 |
-
|
|
|
|
| 187 |
self.history.append({
|
| 188 |
"generation": gen,
|
| 189 |
"smiles": sample_smiles
|
| 190 |
})
|
| 191 |
-
|
| 192 |
-
|
| 193 |
offspring = self._generate_offspring(survivors)
|
| 194 |
-
|
| 195 |
-
# Create new population
|
| 196 |
new_pop = Population(self.config)
|
| 197 |
new_pop.add_molecules(survivors + offspring)
|
| 198 |
self.population = new_pop
|
|
|
|
| 199 |
|
| 200 |
def _generate_results(self) -> Tuple[pd.DataFrame, pd.DataFrame]:
|
| 201 |
"""Generate final results DataFrames."""
|
|
|
|
| 155 |
def _run_evolution_loop(self):
|
| 156 |
"""Run the main evolution loop."""
|
| 157 |
for gen in range(1, self.config.generations + 1):
|
| 158 |
+
|
| 159 |
self._log_generation_stats(gen)
|
| 160 |
+
|
| 161 |
+
# current population
|
| 162 |
+
population = self.population.molecules
|
| 163 |
survivors = self.population.get_survivors()
|
| 164 |
+
|
| 165 |
+
sample_smiles = []
|
| 166 |
+
|
|
|
|
|
|
|
| 167 |
is_final_gen = (gen == self.config.generations)
|
| 168 |
+
|
| 169 |
if is_final_gen:
|
| 170 |
+
# FINAL generation → top 5 survivors
|
| 171 |
if self.config.maximize_cn:
|
| 172 |
sorted_survivors = sorted(survivors, key=lambda m: m.cn, reverse=True)
|
| 173 |
else:
|
| 174 |
sorted_survivors = sorted(survivors, key=lambda m: m.cn_error)
|
| 175 |
+
|
| 176 |
chosen = sorted_survivors[:5]
|
| 177 |
+
|
| 178 |
else:
|
| 179 |
+
# EARLY generations → rejected molecules
|
| 180 |
survivor_smiles = set(m.smiles for m in survivors)
|
| 181 |
rejected = [m for m in population if m.smiles not in survivor_smiles]
|
| 182 |
+
|
| 183 |
+
chosen = rejected[:5]
|
| 184 |
+
|
| 185 |
sample_smiles = [m.smiles for m in chosen]
|
| 186 |
+
sample_smiles = list(dict.fromkeys(sample_smiles))
|
| 187 |
+
|
| 188 |
+
# ✅ APPEND HISTORY FOR EVERY GENERATION
|
| 189 |
self.history.append({
|
| 190 |
"generation": gen,
|
| 191 |
"smiles": sample_smiles
|
| 192 |
})
|
| 193 |
+
|
| 194 |
+
# produce next generation
|
| 195 |
offspring = self._generate_offspring(survivors)
|
| 196 |
+
|
|
|
|
| 197 |
new_pop = Population(self.config)
|
| 198 |
new_pop.add_molecules(survivors + offspring)
|
| 199 |
self.population = new_pop
|
| 200 |
+
|
| 201 |
|
| 202 |
def _generate_results(self) -> Tuple[pd.DataFrame, pd.DataFrame]:
|
| 203 |
"""Generate final results DataFrames."""
|