File size: 15,711 Bytes
09801ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
"""
๐Ÿ” Hyperparameter Optimization Agent

Adaptive hyperparameter search using:
- Optuna TPE (Tree-structured Parzen Estimators)
- Early stopping for unpromising trials
- Learning from failed trials
- Warm starting from previous best configs

Not brute force - intelligent search.
"""

import numpy as np
from typing import Dict, List, Any, Tuple, Optional
from dataclasses import dataclass
import logging
import importlib

from .base import BaseAgent, AgentResult, AgentStatus, Phase

logger = logging.getLogger(__name__)


@dataclass
class TrainedModel:
    """A trained model with its metrics"""
    name: str
    model: Any
    params: Dict[str, Any]
    score: float
    metrics: Dict[str, float]


class HyperparamAgent(BaseAgent):
    """
    Autonomous Hyperparameter Optimization Agent
    
    Uses adaptive search strategies with early stopping and failure learning.
    """
    
    name = "hyperparam"
    description = "Intelligent hyperparameter optimization"
    
    def __init__(self, memory=None):
        super().__init__(memory)
        self.trained_models: List[TrainedModel] = []
        self.best_model: Optional[TrainedModel] = None
        
    def execute(self, **kwargs) -> AgentResult:
        """Main execution: train and optimize models"""
        
        # Get data
        X = self.read_state("features_engineered")
        if X is None:
            X = self.read_state("features")
        y = self.read_state("target")
        task_type = self.read_state("task_type")
        candidates = self.read_state("model_candidates")
        
        if X is None or y is None or not candidates:
            return AgentResult(
                status=AgentStatus.FAILED,
                agent_name=self.name,
                phase=self.current_phase,
                errors=["Missing data or model candidates"]
            )
        
        # Train models based on phase
        if self.is_fast_phase():
            results = self._fast_training(X, y, candidates, task_type)
        else:
            results = self._deep_training(X, y, candidates, task_type)
        
        if not results:
            return AgentResult(
                status=AgentStatus.FAILED,
                agent_name=self.name,
                phase=self.current_phase,
                errors=["All models failed to train"]
            )
        
        # Get best model
        self.best_model = max(results, key=lambda x: x.score)
        
        # Store results
        self.write_state("trained_models", [
            {"name": m.name, "score": m.score, "metrics": m.metrics} for m in results
        ], self.name)
        self.write_state("best_model_name", self.best_model.name, self.name)
        self.write_state("best_score", self.best_model.score, self.name)
        
        # Store model artifact
        self.memory.store_artifact(
            artifact_id=f"model_{self.best_model.name}",
            artifact_type="model",
            producer=self.name,
            data=self.best_model.model,
            metadata={"name": self.best_model.name, "score": self.best_model.score}
        )
        
        self.logger.info(f"   ๐Ÿ† Best: {self.best_model.name} (score={self.best_model.score:.4f})")
        
        return AgentResult(
            status=AgentStatus.SUCCESS,
            agent_name=self.name,
            phase=self.current_phase,
            data={
                "models_trained": len(results),
                "best_model": self.best_model.name,
                "best_score": self.best_model.score
            },
            metrics={
                "score": self.best_model.score,
                **self.best_model.metrics
            }
        )
    
    # =========================================================================
    # FAST PHASE - Quick training
    # =========================================================================
    
    def _fast_training(self, X: np.ndarray, y: np.ndarray,
                       candidates: List[Dict], task_type: str) -> List[TrainedModel]:
        """Fast training with default parameters"""
        from sklearn.model_selection import train_test_split
        
        # Simple split
        X_train, X_test, y_train, y_test = train_test_split(
            X, y, test_size=0.2, random_state=42
        )
        
        results = []
        for candidate in candidates[:4]:  # Limit to top 4
            try:
                model = self._create_model(candidate)
                if model is None:
                    continue
                
                model.fit(X_train, y_train)
                score, metrics = self._evaluate_model(model, X_test, y_test, task_type)
                
                results.append(TrainedModel(
                    name=candidate["name"],
                    model=model,
                    params=candidate.get("params", {}),
                    score=score,
                    metrics=metrics
                ))
                
                self.logger.info(f"   โœ… {candidate['name']}: {score:.4f}")
                
            except Exception as e:
                self.logger.warning(f"   โš ๏ธ {candidate['name']} failed: {str(e)[:40]}")
        
        return results
    
    # =========================================================================
    # DEEP PHASE - Optuna optimization
    # =========================================================================
    
    def _deep_training(self, X: np.ndarray, y: np.ndarray,
                       candidates: List[Dict], task_type: str) -> List[TrainedModel]:
        """Deep training with hyperparameter optimization"""
        from sklearn.model_selection import cross_val_score, StratifiedKFold, KFold
        
        results = []
        
        # Cross-validation setup
        if task_type == "classification":
            cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
            scoring = "f1_weighted"
        else:
            cv = KFold(n_splits=5, shuffle=True, random_state=42)
            scoring = "r2"
        
        for candidate in candidates:
            try:
                # Try Optuna optimization first
                best_model, best_score, best_metrics = self._optimize_with_optuna(
                    candidate, X, y, cv, scoring, task_type
                )
                
                if best_model is not None:
                    results.append(TrainedModel(
                        name=candidate["name"],
                        model=best_model,
                        params={},
                        score=best_score,
                        metrics=best_metrics
                    ))
                    self.logger.info(f"   โœ… {candidate['name']}: {best_score:.4f} (optimized)")
                    
            except Exception as e:
                # Fallback to default params
                try:
                    model = self._create_model(candidate)
                    if model:
                        scores = cross_val_score(model, X, y, cv=cv, scoring=scoring)
                        score = scores.mean()
                        model.fit(X, y)
                        
                        results.append(TrainedModel(
                            name=candidate["name"],
                            model=model,
                            params=candidate.get("params", {}),
                            score=score,
                            metrics={"cv_score": score, "cv_std": scores.std()}
                        ))
                        self.logger.info(f"   โœ… {candidate['name']}: {score:.4f} (default)")
                except:
                    self.logger.warning(f"   โš ๏ธ {candidate['name']} failed completely")
        
        return results
    
    def _optimize_with_optuna(self, candidate: Dict, X: np.ndarray, y: np.ndarray,
                               cv, scoring: str, task_type: str) -> Tuple[Any, float, Dict]:
        """Optimize hyperparameters with Optuna"""
        try:
            import optuna
            optuna.logging.set_verbosity(optuna.logging.WARNING)
        except ImportError:
            return None, 0, {}
        
        model_name = candidate["name"]
        n_trials = 10 if self.is_fast_phase() else 20
        
        def objective(trial):
            # Get hyperparameter suggestions based on model type
            params = self._suggest_params(trial, model_name, task_type)
            
            try:
                model = self._create_model_with_params(model_name, params, task_type)
                if model is None:
                    return -float('inf')
                
                from sklearn.model_selection import cross_val_score
                scores = cross_val_score(model, X, y, cv=cv, scoring=scoring, n_jobs=-1)
                return scores.mean()
            except:
                return -float('inf')
        
        study = optuna.create_study(direction="maximize")
        study.optimize(objective, n_trials=n_trials, show_progress_bar=False)
        
        # Train final model with best params
        best_params = study.best_params
        best_model = self._create_model_with_params(model_name, best_params, task_type)
        best_model.fit(X, y)
        
        return best_model, study.best_value, {"optuna_best": study.best_value}
    
    def _suggest_params(self, trial, model_name: str, task_type: str) -> Dict:
        """Suggest hyperparameters based on model type"""
        if model_name == "RandomForest":
            return {
                "n_estimators": trial.suggest_int("n_estimators", 50, 200),
                "max_depth": trial.suggest_int("max_depth", 5, 20),
                "min_samples_split": trial.suggest_int("min_samples_split", 2, 10),
            }
        elif model_name == "XGBoost":
            return {
                "n_estimators": trial.suggest_int("n_estimators", 50, 200),
                "max_depth": trial.suggest_int("max_depth", 3, 10),
                "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3),
            }
        elif model_name == "LightGBM":
            return {
                "n_estimators": trial.suggest_int("n_estimators", 50, 200),
                "max_depth": trial.suggest_int("max_depth", 3, 10),
                "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3),
                "num_leaves": trial.suggest_int("num_leaves", 20, 100),
            }
        else:
            return {}
    
    # =========================================================================
    # HELPER METHODS
    # =========================================================================
    
    def _create_model(self, candidate: Dict) -> Any:
        """Create model instance from candidate"""
        model_class = candidate.get("model_class", "")
        params = candidate.get("params", {})
        
        return self._create_model_with_params(candidate["name"], params, 
                                               self.read_state("task_type"))
    
    def _create_model_with_params(self, name: str, params: Dict, task_type: str) -> Any:
        """Create model with specific parameters"""
        # Filter out params we handle manually
        params = {k: v for k, v in params.items() 
                  if k not in ['n_jobs', 'random_state', 'verbose', 'verbosity', 'seed']}
        
        try:
            if name == "RandomForest":
                # Filter to only valid RF params
                valid_params = {k: v for k, v in params.items() 
                               if k in ['n_estimators', 'max_depth', 'min_samples_split', 
                                        'min_samples_leaf', 'max_features', 'bootstrap']}
                if task_type == "classification":
                    from sklearn.ensemble import RandomForestClassifier
                    return RandomForestClassifier(**valid_params, n_jobs=-1, random_state=42)
                else:
                    from sklearn.ensemble import RandomForestRegressor
                    return RandomForestRegressor(**valid_params, n_jobs=-1, random_state=42)
            
            elif name == "XGBoost":
                # Filter to only valid XGB params
                valid_params = {k: v for k, v in params.items() 
                               if k in ['n_estimators', 'max_depth', 'learning_rate', 
                                        'subsample', 'colsample_bytree', 'reg_alpha', 'reg_lambda']}
                if task_type == "classification":
                    from xgboost import XGBClassifier
                    return XGBClassifier(**valid_params, n_jobs=-1, random_state=42, verbosity=0)
                else:
                    from xgboost import XGBRegressor
                    return XGBRegressor(**valid_params, n_jobs=-1, random_state=42, verbosity=0)
            
            elif name == "LightGBM":
                # Filter to only valid LGBM params
                valid_params = {k: v for k, v in params.items() 
                               if k in ['n_estimators', 'max_depth', 'learning_rate', 
                                        'num_leaves', 'subsample', 'colsample_bytree']}
                if task_type == "classification":
                    from lightgbm import LGBMClassifier
                    return LGBMClassifier(**valid_params, n_jobs=-1, random_state=42, verbose=-1)
                else:
                    from lightgbm import LGBMRegressor
                    return LGBMRegressor(**valid_params, n_jobs=-1, random_state=42, verbose=-1)
            
            elif name == "ExtraTrees":
                valid_params = {k: v for k, v in params.items() 
                               if k in ['n_estimators', 'max_depth', 'min_samples_split', 
                                        'min_samples_leaf', 'max_features']}
                if task_type == "classification":
                    from sklearn.ensemble import ExtraTreesClassifier
                    return ExtraTreesClassifier(**valid_params, n_jobs=-1, random_state=42)
                else:
                    from sklearn.ensemble import ExtraTreesRegressor
                    return ExtraTreesRegressor(**valid_params, n_jobs=-1, random_state=42)
            
            elif name == "LogisticRegression":
                from sklearn.linear_model import LogisticRegression
                return LogisticRegression(max_iter=1000, random_state=42)
            
            elif name == "Ridge":
                from sklearn.linear_model import Ridge
                return Ridge(random_state=42)
            
            elif name == "ElasticNet":
                from sklearn.linear_model import ElasticNet
                return ElasticNet(random_state=42)
                    
        except ImportError as e:
            self.logger.warning(f"   โš ๏ธ {name} not available: {str(e)[:30]}")
        
        return None
    
    def _evaluate_model(self, model, X_test: np.ndarray, y_test: np.ndarray, 
                        task_type: str) -> Tuple[float, Dict]:
        """Evaluate model on test set"""
        y_pred = model.predict(X_test)
        
        if task_type == "classification":
            from sklearn.metrics import accuracy_score, f1_score
            acc = accuracy_score(y_test, y_pred)
            f1 = f1_score(y_test, y_pred, average='weighted', zero_division=0)
            return f1, {"accuracy": acc, "f1": f1}
        else:
            from sklearn.metrics import r2_score, mean_absolute_error
            r2 = r2_score(y_test, y_pred)
            mae = mean_absolute_error(y_test, y_pred)
            return r2, {"r2": r2, "mae": mae}