Skip to content

Commit b6d1a2b

Browse files
committed
test: add verification script for MOEA/D variants
1 parent 8955146 commit b6d1a2b

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

scripts/verify_variants.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import os
2+
import sys
3+
import numpy as np
4+
from datetime import datetime
5+
6+
# Add project root to sys.path
7+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8+
9+
from src.portfolio import (
10+
calculate_expected_returns_and_cov,
11+
MOEADOptimizer,
12+
MOEADDRAOptimizer,
13+
MOEADAWAOptimizer,
14+
PortfolioProblem,
15+
ReturnObjective,
16+
RiskObjective,
17+
)
18+
19+
def run_test():
20+
print("Starting MOEA/D Variants Verification Test...")
21+
22+
# Mock data for testing
23+
num_assets = 5
24+
expected_returns = np.array([0.1, 0.15, 0.12, 0.08, 0.11])
25+
cov_matrix = np.eye(num_assets) * 0.05
26+
cov_matrix[0, 1] = cov_matrix[1, 0] = 0.01
27+
28+
data_kwargs = {
29+
"expected_returns": expected_returns,
30+
"cov_matrix": cov_matrix
31+
}
32+
33+
problem = PortfolioProblem([ReturnObjective(), RiskObjective()])
34+
35+
optimizers = {
36+
"Standard MOEA/D": MOEADOptimizer(problem=problem),
37+
"MOEA/D-DRA": MOEADDRAOptimizer(problem=problem),
38+
"MOEA/D-AWA": MOEADAWAOptimizer(problem=problem)
39+
}
40+
41+
for name, opt in optimizers.items():
42+
print(f"\nRunning {name}...")
43+
try:
44+
metrics, weights, w_vectors = opt.generate_pareto_front(
45+
num_points=50,
46+
generations=100,
47+
verbose=False,
48+
**data_kwargs
49+
)
50+
print(f" {name} completed successfully.")
51+
print(f" Points found: {len(weights)}")
52+
print(f" Return range: [{np.min(metrics['Return']):.4f}, {np.max(metrics['Return']):.4f}]")
53+
print(f" Risk range: [{np.min(metrics['Risk']):.4f}, {np.max(metrics['Risk']):.4f}]")
54+
except Exception as e:
55+
print(f" {name} FAILED with error: {e}")
56+
import traceback
57+
traceback.print_exc()
58+
59+
if __name__ == "__main__":
60+
run_test()

0 commit comments

Comments
 (0)