Wednesday, March 1, 2023

 The  FOA with constraints program generated by ChatGPT


import random

import numpy as np


# Define the optimization problem with constraints

def objective_function(x):

    # objective function: x1^2 + x2^2 + x3^2

    # constraint: 2*x1 + 3*x2 + 4*x3 <= 5

    f = x[0]**2 + x[1]**2 + x[2]**2

    g = 2*x[0] + 3*x[1] + 4*x[2] - 5

    if g > 0:

        # if constraint violated, add a penalty to the objective function

        f += g**2

    return f


# Define the FOA algorithm

def foa(n, lb, ub, max_iter):

    # n: number of fruit flies (solutions)

    # lb: lower bound of decision variables

    # ub: upper bound of decision variables

    # max_iter: maximum number of iterations

    

    # Initialize the positions of the fruit flies

    positions = np.zeros((n, 3))

    for i in range(n):

        positions[i] = [random.uniform(lb[0], ub[0]), 

                        random.uniform(lb[1], ub[1]), 

                        random.uniform(lb[2], ub[2])]

    

    # Evaluate the objective function at the initial positions

    fitness = np.zeros(n)

    for i in range(n):

        fitness[i] = objective_function(positions[i])

    

    # Initialize the best solution and its fitness value

    best_fitness = np.min(fitness)

    best_solution = positions[np.argmin(fitness)]

    

    # Main loop of the FOA algorithm

    for t in range(max_iter):

        # Compute the distance between each pair of fruit flies

        distances = np.zeros((n, n))

        for i in range(n):

            for j in range(n):

                distances[i][j] = np.linalg.norm(positions[i] - positions[j])

        

        # Compute the attraction and repulsion forces between each pair of fruit flies

        forces = np.zeros((n, 3))

        for i in range(n):

            for j in range(n):

                if i != j:

                    if fitness[j] < fitness[i]:

                        f = fitness[j] / (distances[i][j] + 1e-10)

                        forces[i] += f * (positions[j] - positions[i])

                    else:

                        f = fitness[i] / (distances[i][j] + 1e-10)

                        forces[i] -= f * (positions[j] - positions[i])

        

        # Compute the new positions of the fruit flies

        new_positions = np.zeros((n, 3))

        for i in range(n):

            new_positions[i] = positions[i] + forces[i]

            # Apply boundary constraints

            new_positions[i] = np.clip(new_positions[i], lb, ub)

        

        # Evaluate the objective function at the new positions

        new_fitness = np.zeros(n)

        for i in range(n):

            new_fitness[i] = objective_function(new_positions[i])

        

        # Update the best solution and its fitness value

        if np.min(new_fitness) < best_fitness:

            best_fitness = np.min(new_fitness)

            best_solution = new_positions[np.argmin(new_fitness)]

        

        # Update the positions and fitness values of the fruit flies

        positions = new_positions

        fitness = new_fitness


    

    return best_solution, best_fitness


# Test the FOA algorithm with the given problem and constraints


best_solution, best_fitness = foa(100, [-5,-5,-5][5,5,5], 1000)

print(f"best_solution: {best_solution}")

print(f"best_fitness: {best_fitness}")


"""

Output:


best_solution: [ 0.49919158  0.12151386 -0.61439392]

best_fitness: 0.6414377366696835


"""





0 comments:

Post a Comment