~ruther/ctu-fee-eoa

ref: c67cbe05b263f3d2f59d9217d0e112ff23b43d74 ctu-fee-eoa/codes/py_plotter/test_target_proximity.py -rw-r--r-- 2.2 KiB
c67cbe05 — Rutherther Finish hw02 5 days ago
                                                                                
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
#!/usr/bin/env python3

import json
import csv
import numpy as np
from pathlib import Path

# Simple test to verify the target proximity logic works
config = {
    "data_path": "../constr_hw02/solutions",
    "targets": [5.0, 10.0],
    "problem_groups": [["g05", "g06"], ["g09", "g11"]],
}

objectives = {
    "g04": -30665.53867178333,
    "g05": 5126.4967140071,
    "g06": -6961.8137558015,
    "g08": -0.0958250414180359,
    "g09": 680.6300573745,
    "g11": 0.7499,
    "g21": 193.724510070035,
    "g24": -5.50801327159536
}

def calculate_percentage_deviation(values, instance_name):
    """Calculate percentage deviation from optimal value"""
    if instance_name not in objectives:
        raise ValueError(f"No objective value found for instance {instance_name}")

    optimal_value = objectives[instance_name]
    
    new_optimal_value = optimal_value
    if optimal_value < 0:
        new_optimal_value = -optimal_value
        values = values + 2*new_optimal_value

    percentage_deviations = (values - new_optimal_value) / new_optimal_value * 100
    return percentage_deviations

# Test with a sample from nsga/g05
data_path = Path(config["data_path"])
sample_file = data_path / "nsga" / "g05" / "best_candidates_20251206_184606.csv"

if sample_file.exists():
    print(f"Testing with file: {sample_file}")
    
    with open(sample_file, 'r') as f:
        reader = csv.DictReader(f)
        evaluations = []
        for row in reader:
            evaluations.append(float(row['evaluation']))
    
    print(f"Found {len(evaluations)} evaluations")
    print(f"First few values: {evaluations[:5]}")
    
    # Calculate percentage deviations
    values = np.array(evaluations)
    deviations = calculate_percentage_deviation(values, "g05")
    
    print(f"Percentage deviations: {deviations[:5]}...{deviations[-5:]}")
    print(f"Final deviation: {deviations[-1]:.2f}%")
    
    # Test target proximity
    for target in [5.0, 10.0]:
        within_target = deviations[-1] <= target
        print(f"Within {target}% target: {within_target}")
    
else:
    print(f"Sample file not found: {sample_file}")
    print("Available files:")
    if (data_path / "nsga" / "g05").exists():
        for f in (data_path / "nsga" / "g05").glob("*.csv"):
            print(f"  {f.name}")