From 2b3ab83d913ada9a2a09cb60ca958ee9fa2312cb Mon Sep 17 00:00:00 2001 From: dmy Date: Thu, 8 Jan 2026 16:05:06 +0800 Subject: [PATCH] Fix substation coordinate handling for cable crossing detection --- mip.py | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/mip.py b/mip.py index 26a661e..cc1e37f 100644 --- a/mip.py +++ b/mip.py @@ -404,11 +404,43 @@ def check_cable_crossings(connections, turbines, substation): def get_turbine_coord(connection_part): """Get coordinates from connection part (turbine_# or substation).""" if connection_part == "substation": - # Ensure substation is returned as a proper tuple for unpacking - if isinstance(substation, (list, np.ndarray)): - return (substation[0], substation[1]) + # Handle different substation formats robustly + if isinstance(substation, np.ndarray): + if substation.ndim == 1: + # 1D array [x, y] + return (substation[0], substation[1]) + elif substation.ndim == 2: + # 2D array [[x, y]] or shape (n, 2) + if substation.shape[0] == 1: + return (substation[0, 0], substation[0, 1]) + else: + # Multiple points, use first one + return (substation[0, 0], substation[0, 1]) + else: + # Unexpected dimension, try fallback + return (substation.flat[0], substation.flat[1]) + elif isinstance(substation, (list, tuple)): + # List or tuple format + # Handle nested lists like [[x, y]] + if ( + isinstance(substation[0], (list, tuple, np.ndarray)) + and len(substation[0]) >= 2 + ): + return (substation[0][0], substation[0][1]) + elif len(substation) >= 2: + return (substation[0], substation[1]) + else: + return (float("inf"), float("inf")) else: - return (substation[0], substation[1]) + # Unexpected format, try to convert + try: + sub_array = np.array(substation) + if sub_array.ndim == 1: + return (sub_array[0], sub_array[1]) + else: + return (sub_array.flat[0], sub_array.flat[1]) + except: + return (float("inf"), float("inf")) else: turbine_idx = int(connection_part.split("_")[1]) return (