Fix substation coordinate handling for cable crossing detection
This commit is contained in:
38
mip.py
38
mip.py
@@ -404,11 +404,43 @@ def check_cable_crossings(connections, turbines, substation):
|
|||||||
def get_turbine_coord(connection_part):
|
def get_turbine_coord(connection_part):
|
||||||
"""Get coordinates from connection part (turbine_# or substation)."""
|
"""Get coordinates from connection part (turbine_# or substation)."""
|
||||||
if connection_part == "substation":
|
if connection_part == "substation":
|
||||||
# Ensure substation is returned as a proper tuple for unpacking
|
# Handle different substation formats robustly
|
||||||
if isinstance(substation, (list, np.ndarray)):
|
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])
|
return (substation[0], substation[1])
|
||||||
else:
|
else:
|
||||||
return (substation[0], substation[1])
|
return (float("inf"), float("inf"))
|
||||||
|
else:
|
||||||
|
# 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:
|
else:
|
||||||
turbine_idx = int(connection_part.split("_")[1])
|
turbine_idx = int(connection_part.split("_")[1])
|
||||||
return (
|
return (
|
||||||
|
|||||||
Reference in New Issue
Block a user