#!/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}")