File size: 10,801 Bytes
53b198d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
486c364
53b198d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e5057f3
53b198d
e5057f3
 
 
53b198d
e5057f3
 
 
9ef4d00
e5057f3
9ef4d00
e5057f3
9ef4d00
 
 
 
e5057f3
9ef4d00
e5057f3
69e6e87
e5057f3
9ef4d00
 
e5057f3
 
 
9ef4d00
e5057f3
 
 
8097d14
 
 
 
e5057f3
 
53b198d
e5057f3
53b198d
 
 
e5057f3
53b198d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18e2593
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from .population import Population
from .molecule import Molecule
from core.predictors.pure_component.property_predictor import PropertyPredictor
from core.config import EvolutionConfig
from crem.crem import mutate_mol
from rdkit import Chem
import pandas as pd
import numpy as np
import random
from typing import List, Tuple
from core.data_prep import df  # Initial dataset for sampling
from pathlib import Path

class MolecularEvolution:
    """Main evolutionary algorithm coordinator."""
    BASE_DIR = Path(__file__).resolve().parent.parent.parent
    REP_DB_PATH = BASE_DIR / "data" / "fragments" / "diesel_fragments.db"

    def __init__(self, config: EvolutionConfig):
        self.config = config
        self.predictor = PropertyPredictor(config)
        self.population = Population(config)
        self.history = []
    
    def _mutate_molecule(self, mol: Chem.Mol) -> List[str]:
        """Generate mutations for a molecule using CREM."""
        try:
            mutants = list(mutate_mol(
                mol,
                db_name=str(self.REP_DB_PATH),
                max_size=2,
                return_mol=False
            ))
            return [m for m in mutants if m and m not in self.population.seen_smiles]

        except SystemExit:
        # CREM can call sys.exit(1) internally; this prevents Gunicorn worker crash
            return []
        
        except Exception:
            return []
    
    def _create_molecules(self, smiles_list: List[str]) -> List[Molecule]:
        """Create Molecule objects from SMILES with predictions (OPTIMIZED)."""
        if not smiles_list:
            return []
        
        # OPTIMIZATION: Single featurization + all predictions
        predictions = self.predictor.predict_all_properties(smiles_list)
        
        molecules = []
        for i, smiles in enumerate(smiles_list):
            # Extract predictions for this molecule
            props = {k: v[i] for k, v in predictions.items()}
            
            # Validate required properties
            if props.get('cn') is None:
                continue
            if self.config.minimize_ysi and props.get('ysi') is None:
                continue
            
            # Validate filtered properties
            if not all(self.predictor.is_valid(k, props.get(k)) for k in ['bp', 'density', 'lhv', 'dynamic_viscosity']):
                continue
            
            molecules.append(Molecule(
                smiles=smiles,
                cn=props['cn'],
                cn_error=abs(props['cn'] - self.config.target_cn),
                cn_score=props['cn'],  # For maximize mode
                bp=props.get('bp'),
                ysi=props.get('ysi'),
                density=props.get('density'),
                lhv=props.get('lhv'),
                dynamic_viscosity=props.get('dynamic_viscosity')
            ))
        
        return molecules
    
    def initialize_population(self, initial_smiles: List[str]) -> int:
        """Initialize the population from initial SMILES."""
        print("Predicting properties for initial population...")
        molecules = self._create_molecules(initial_smiles)
        return self.population.add_molecules(molecules)
    
    def _log_generation_stats(self, generation: int):
        """Log statistics for the current generation."""
        mols = self.population.molecules
        
        if self.config.maximize_cn:
            best_cn = max(mols, key=lambda m: m.cn)
            avg_cn = np.mean([m.cn for m in mols])
            
            print_msg = (f"Gen {generation}/{self.config.generations} | "
                        f"Pop {len(mols)} | "
                        f"Best CN: {best_cn.cn:.3f} | "
                        f"Avg CN: {avg_cn:.3f}")
        else:
            best_cn = min(mols, key=lambda m: m.cn_error)
            avg_cn_err = np.mean([m.cn_error for m in mols])
            
            print_msg = (f"Gen {generation}/{self.config.generations} | "
                        f"Pop {len(mols)} | "
                        f"Best CN err: {best_cn.cn_error:.3f} | "
                        f"Avg CN err: {avg_cn_err:.3f}")
        
        if self.config.minimize_ysi:
            front = self.population.pareto_front()
            best_ysi = min(mols, key=lambda m: m.ysi)
            avg_ysi = np.mean([m.ysi for m in mols])
            
            print_msg += (f" | Best YSI: {best_ysi.ysi:.3f} | "
                         f"Avg YSI: {avg_ysi:.3f} | "
                         f"Pareto: {len(front)}")
        
        print(print_msg)
    
    def _generate_offspring(self, survivors: List[Molecule]) -> List[Molecule]:
        """Generates offspring from survivors."""
        target_count = self.config.population_size - len(survivors)
        max_attempts = target_count * self.config.max_offspring_attempts
        
        all_children = []
        new_molecules = []
        
        print(f"  β†’ Generating offspring (target: {target_count})...")
        
        for attempt in range(max_attempts):
            if len(new_molecules) >= target_count:
                break
            
            # Generate mutations
            parent = random.choice(survivors)
            mol = Chem.MolFromSmiles(parent.smiles)
            if mol is None:
                continue
            
            children = self._mutate_molecule(mol)
            all_children.extend(children[:self.config.mutations_per_parent])
            
            # Process in larger batches (single featurization per batch)
            if len(all_children) >= self.config.batch_size:
                print(f"  β†’ Evaluating batch of {len(all_children)} (featurizing once)...")
                new_molecules.extend(self._create_molecules(all_children))
                all_children = []
        
        # Process remaining children
        if all_children:
            print(f"  β†’ Evaluating final batch of {len(all_children)}...")
            new_molecules.extend(self._create_molecules(all_children))
        
        print(f"  βœ“ Generated {len(new_molecules)} valid offspring")
        return new_molecules
    
    def _run_evolution_loop(self):
        """Run the main evolution loop."""
        for gen in range(1, self.config.generations + 1):
    
            self._log_generation_stats(gen)
    
            # current population
            population = self.population.molecules
            survivors = self.population.get_survivors()
    
            sample_smiles = []
    
            is_final_gen = (gen == self.config.generations)
    
            if is_final_gen:
                # FINAL generation β†’ top 5 survivors
                if self.config.maximize_cn:
                    sorted_survivors = sorted(survivors, key=lambda m: m.cn, reverse=True)
                else:
                    sorted_survivors = sorted(survivors, key=lambda m: m.cn_error)
    
                chosen = sorted_survivors[:5]
    
            else:
                # EARLY generations β†’ rejected molecules
                survivor_smiles = set(m.smiles for m in survivors)
                rejected = [m for m in population if m.smiles not in survivor_smiles]
    
                chosen = rejected[:5]
    
            sample_smiles = [m.smiles for m in chosen]
            sample_smiles = list(dict.fromkeys(sample_smiles))
    
            # βœ… APPEND HISTORY FOR EVERY GENERATION
            self.history.append({
                "generation": gen,
                "smiles": sample_smiles
            })
    
            # produce next generation
            offspring = self._generate_offspring(survivors)
    
            new_pop = Population(self.config)
            new_pop.add_molecules(survivors + offspring)
            self.population = new_pop

    
    def _generate_results(self) -> Tuple[pd.DataFrame, pd.DataFrame]:
        """Generate final results DataFrames."""
        final_df = self.population.to_dataframe()

        # Apply different filtering based on mode
        if self.config.maximize_cn:
            if self.config.minimize_ysi and "ysi" in final_df.columns:
                # Maximize CN + minimize YSI: keep high CN, low YSI
                final_df = final_df[
                    (final_df["cn"] > 50) &
                    (final_df["ysi"] < 50)
                ].sort_values(["cn", "ysi"], ascending=[False, True])
            else:
                # Maximize CN only: just keep high CN
                final_df = final_df[final_df["cn"] > 50].sort_values("cn", ascending=False)
        else:
            if self.config.minimize_ysi and "ysi" in final_df.columns:
                # Target CN + minimize YSI: keep low error, low YSI
                final_df = final_df[
                    (final_df["cn_error"] < 5) &
                    (final_df["ysi"] < 50)
                ].sort_values(["cn_error", "ysi"], ascending=True)
            else:
                # Target CN only: just keep low error
                final_df = final_df[final_df["cn_error"] < 5].sort_values("cn_error", ascending=True)
        
        # Overwrite rank safely
        final_df["rank"] = range(1, len(final_df) + 1)
        
        if self.config.minimize_ysi:
            pareto_mols = self.population.pareto_front()
            pareto_df = pd.DataFrame([m.to_dict() for m in pareto_mols])
            
            if not pareto_df.empty:
                if self.config.maximize_cn:
                    pareto_df = pareto_df[
                        (pareto_df['cn'] > 50) & (pareto_df['ysi'] < 50)
                    ].sort_values(["cn", "ysi"], ascending=[False, True])
                else:
                    pareto_df = pareto_df[
                        (pareto_df['cn_error'] < 5) & (pareto_df['ysi'] < 50)
                    ].sort_values(["cn_error", "ysi"], ascending=True)
                
                pareto_df.insert(0, 'rank', range(1, len(pareto_df) + 1))
        else:
            pareto_df = pd.DataFrame()
        
        return final_df, pareto_df
    

    def evolve(self) -> Tuple[pd.DataFrame, pd.DataFrame]:
        """Run the evolutionary algorithm."""
        # Initialize
        df_bins = pd.qcut(df["cn"], q=30)
        initial_smiles = (
            df.groupby(df_bins, observed=False)
            .apply(lambda x: x.sample(20, random_state=42))
            .reset_index(drop=True)["SMILES"]
            .tolist()
        )
        init_count = self.initialize_population(initial_smiles)

        if init_count == 0:
            print("No valid initial molecules")
            return pd.DataFrame(), pd.DataFrame()
        
        print(f"βœ“ Initial population size: {init_count}\n")
        
        # Evolution
        self._run_evolution_loop()
        
        # Results
        return self._generate_results()