forked from hyperactive-project/Hyperactive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdifferential_evolution_example.py
More file actions
59 lines (49 loc) · 2.18 KB
/
Copy pathdifferential_evolution_example.py
File metadata and controls
59 lines (49 loc) · 2.18 KB
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
"""
Differential Evolution Example - Vector-Based Evolutionary Algorithm
Differential Evolution is a population-based evolutionary algorithm that uses
difference vectors between population members to generate new candidate solutions.
It's particularly effective for continuous optimization problems and has robust
convergence properties across various problem types.
Characteristics:
- Population-based with vector difference operations
- Self-adapting through mutation and crossover
- Robust convergence across different problem types
- Effective for continuous and mixed optimization
- Simple parameter control with good default behavior
"""
import numpy as np
from sklearn.datasets import load_wine
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_val_score
from hyperactive.experiment.integrations import SklearnCvExperiment
from hyperactive.opt.gfo import DifferentialEvolution
# Load dataset
X, y = load_wine(return_X_y=True)
print(f"Dataset: Wine classification ({X.shape[0]} samples, {X.shape[1]} features)")
# Create experiment
estimator = RandomForestClassifier(random_state=42)
experiment = SklearnCvExperiment(estimator=estimator, X=X, y=y, cv=3)
# Define search space
search_space = {
"n_estimators": list(range(10, 201, 5)), # Discrete integer values (broader range)
"max_depth": list(range(1, 21)), # Discrete integer values
"min_samples_split": list(range(2, 21)), # Discrete integer values
"min_samples_leaf": list(range(1, 11)), # Discrete integer values
}
# Configure Differential Evolution
optimizer = DifferentialEvolution(
search_space=search_space,
n_iter=50,
random_state=42,
experiment=experiment
)
# Run optimization
# Differential evolution uses difference vectors between population members
# For each target vector, it creates a mutant vector using weighted differences
# from other population members, then applies crossover to generate trial vector
# The trial vector replaces the target if it has better fitness
best_params = optimizer.solve()
# Results
print("\n=== Results ===")
print(f"Best parameters: {best_params}")
print("Differential evolution optimization completed successfully")