Untitled

 avatar
unknown
plain_text
5 months ago
1.7 kB
3
Indexable
# Define the functions with adjustments to find the optimal control center location

def calculate_distance(x1, y1, x2, y2):
    """Calculates the Manhattan distance between two points."""
    return abs(x1 - x2) + abs(y1 - y2)

def calculate_total_cost(wind_farms, control_center):
    """Calculates the total cost of connecting all wind farms to a control center."""
    total_cost = 0
    for farm in wind_farms:
        distance = calculate_distance(control_center[0], control_center[1], farm[0], farm[1])
        cost = distance * farm[2]
        total_cost += cost
    return total_cost

# Optimal control center finder using the weighted median method
def find_optimal_control_center(wind_farms):
    # Separate x, y, and weights (premium)
    x_coords = [farm[0] for farm in wind_farms]
    y_coords = [farm[1] for farm in wind_farms]
    weights = [farm[2] for farm in wind_farms]
    
    # Helper function to find the weighted median
    def weighted_median(coordinates, weights):
        data = sorted(zip(coordinates, weights))
        total_weight = sum(weights)
        cumulative_weight = 0
        for coord, weight in data:
            cumulative_weight += weight
            if cumulative_weight * 2 >= total_weight:
                return coord
        return data[-1][0]

    # Find weighted medians for x and y coordinates
    optimal_x = weighted_median(x_coords, weights)
    optimal_y = weighted_median(y_coords, weights)
    
    # Calculate the total cost with the optimal control center
    optimal_control_center = (optimal_x, optimal_y)
    total_cost = calculate_total_cost(wind_farms, optimal_control_center)
    
    return total_cost
Editor is loading...
Leave a Comment