Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.9 kB
2
Indexable
Never
def calculate_interference(routers):
    interference = 0.0
    
    for i in range(len(routers)):
        for j in range(i + 1, len(routers)):
            router1 = routers[i]
            router2 = routers[j]
            
            # Calculate the squared Euclidean distance between routers
            distance_squared = (router1.x - router2.x)**2 + (router1.y - router2.y)**2
            
            # Calculate the inverse of squared distance as a measure of interference
            # Higher values of distance_squared result in lower interference
            # Use a constant factor to scale the value as needed
            inverse_distance_squared = 1.0 / distance_squared
            
            # Add the interference caused by this router pair to the total
            interference += inverse_distance_squared
    
    return interference












def calculate_coverage(routers, clients):
    covered_clients = 0
    for client in clients:
        for router in routers:
            distance = ((client.x - router.x)**2 + (client.y - router.y)**2)**0.5
            if distance <= router.radius:
                covered_clients += 1
                break  # This client is covered, move to the next client
    coverage = covered_clients / len(clients)
    return coverage














def calculate_load_balancing(routers):
   # Calculate the number of clients connected to each router
    clients_per_router = [0] * len(routers)
    for client in clients:
        min_distance = float('inf')
        closest_router_index = -1
        for i, router in enumerate(routers):
            distance = ((client.x - router.x)**2 + (client.y - router.y)**2)**0.5
            if distance <= router.radius and distance < min_distance:
                min_distance = distance
                closest_router_index = i
        if closest_router_index != -1:
            clients_per_router[closest_router_index] += 1

    # Calculate the standard deviation of clients per router
    mean_clients = sum(clients_per_router) / len(clients_per_router)
    load_balancing = (sum((x - mean_clients)**2 for x in clients_per_router) / len(clients_per_router))**0.5

    return load_balancing








def calculate_fitness(routers, clients, weight_interference, weight_coverage, weight_load_balancing):
    # Calculate interference based on router locations
    interference = calculate_interference(routers)
    
    # Calculate coverage and load balancing (you can implement these)
    coverage = calculate_coverage(routers, clients)
    load_balancing = calculate_load_balancing(routers)

    # Combine the components with weights to calculate overall fitness
    fitness = (
        weight_interference * interference +
        weight_coverage * coverage +
        weight_load_balancing * load_balancing
    )

    return fitness