[Python] handle equality constraints in a Differential Evolution

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm implementing a Differential Evolution (DE) algorithm to solve an optimization problem that includes both inequality and equality constraints.

    import numpy as np
    import funciones

    # Differential Evolution algorithm parameters
    NP = 50 # Population size
    F = 0.3 # Mutation factor
    CR = 0.7 # Crossover rate
    GEN = 500 # Number of generations

    # Function to check if a solution is feasible
    def is_feasible(x, problem_id=4):
    return all(funciones.restrictions(x, problem_id) <= 1)

    # Population initialization
    def initialize_population(D, lower_limits, upper_limits):
    .
    .
    .
    return population



    # Mutation of the population
    def mutation(population, lower_limits, upper_limits):
    .
    .
    .
    return apply_limits(mutated_vectors, lower_limits, upper_limits)

    # Crossover of the population
    def crossover(population, mutated_vectors):
    .
    .
    .
    return trial

    # Selection with constraint handling
    def selection(population, trial, problem_id=4):
    .
    .
    .
    return new_population

    # Main function to run the Differential Evolution algorithm
    def run_de(problem_id=4):
    np.random.seed(15)
    D, lower_limits, upper_limits = funciones.get_problem_dimensions(problem_id)
    population = initialize_population(D, lower_limits, upper_limits)
    for gen in range(GEN):
    mutated = mutation(population, lower_limits, upper_limits)
    crossed = crossover(population, mutated)
    population = selection(population, crossed, problem_id)
    feasible_solutions = [population for i in range(NP) if is_feasible(population, problem_id)]
    if feasible_solutions:
    values = [funciones.objective_function(x, problem_id) for x in feasible_solutions]
    best_index = np.argmin(values)
    best_solution = feasible_solutions[best_index]
    return best_solution, funciones.objective_function(best_solution, problem_id)
    else:
    return None, None

    best_solution, best_value = run_de(problem_id=4)

    if best_solution is not None:
    print("Best solution found:", best_solution)
    print("Objective value:", best_value)
    else:
    print("No feasible solutions found")



    My problem is handling the equalities. I tried adding them like this, but when i try the code, it give me "No feasible solutions found":

    elif problema_id == 4:

    g = np.zeros(5)
    g[0] = -x[3] + x[2] - 0.55
    g[1] = -x[2] + x[3] - 0.55
    h3 = 1000 * np.sin(-x[2] - 0.25) + 1000 * np.sin(-x[3] - 0.25) + 894.8 - x[0]
    g[2] = np.abs(h3)
    h4 = 1000 * np.sin(x[2] - 0.25) + 1000 * np.sin(x[2] - x[3] - 0.25) + 894.8 - x[1]
    g[3] = np.abs(h4)
    h5 = 1000 * np.sin(x[3] - 0.25) + 1000 * np.sin(x[3] - x[2] - 0.25) + 1294.8
    g[4] = np.abs(h5)

    return g


    However, I'm not sure if I'm handling these equalities correctly, since even when I change the parameters, I always get the error

    Continue reading...

Compartilhe esta Página