From b0f0a0c452919dd0cdda2b2903f82acc9cc91676 Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:55:26 +0530 Subject: [PATCH 1/2] implementation of bee algo --- .../Bee Algorithm/beeAlgorithm.c | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 Optimization Algorithms/Bee Algorithm/beeAlgorithm.c diff --git a/Optimization Algorithms/Bee Algorithm/beeAlgorithm.c b/Optimization Algorithms/Bee Algorithm/beeAlgorithm.c new file mode 100644 index 00000000..01969260 --- /dev/null +++ b/Optimization Algorithms/Bee Algorithm/beeAlgorithm.c @@ -0,0 +1,114 @@ +#include +#include +#include +#include + +#define POP_SIZE 50 // Total number of solutions +#define MAX_ITER 1000 // Maximum number of iterations +#define DIM 2 // Dimension of the problem (2D optimization) +#define NEIGHBORS 5 // Number of neighbors per elite solution +#define ELITE_SITES 2 // Number of elite sites +#define OTHER_SITES 3 // Number of other sites +#define LOWER_BOUND -10.0 // Lower bound of the search space +#define UPPER_BOUND 10.0 // Upper bound of the search space + +// Objective function to minimize (example: sphere function) +double objective_function(double solution[DIM]) { + double result = 0.0; + for (int i = 0; i < DIM; i++) { + result += solution[i] * solution[i]; + } + return result; +} + +// Generate random solutions within the bounds +void initialize_solution(double solution[DIM]) { + for (int i = 0; i < DIM; i++) { + solution[i] = LOWER_BOUND + ((double)rand() / RAND_MAX) * (UPPER_BOUND - LOWER_BOUND); + } +} + +// Generate neighbor solutions +void generate_neighbor(double base[DIM], double neighbor[DIM]) { + for (int i = 0; i < DIM; i++) { + double perturb = ((double)rand() / RAND_MAX - 0.5) * 2.0; // Random perturbation + neighbor[i] = base[i] + perturb; + if (neighbor[i] < LOWER_BOUND) neighbor[i] = LOWER_BOUND; + if (neighbor[i] > UPPER_BOUND) neighbor[i] = UPPER_BOUND; + } +} + +// Main Bee Algorithm +void bee_algorithm() { + double population[POP_SIZE][DIM]; + double best_solution[DIM]; + double best_fitness = INFINITY; + + // Initialize population + for (int i = 0; i < POP_SIZE; i++) { + initialize_solution(population[i]); + } + + // Main loop + for (int iter = 0; iter < MAX_ITER; iter++) { + // Evaluate all solutions + for (int i = 0; i < POP_SIZE; i++) { + double fitness = objective_function(population[i]); + if (fitness < best_fitness) { + best_fitness = fitness; + for (int d = 0; d < DIM; d++) { + best_solution[d] = population[i][d]; + } + } + } + + // Elite sites neighborhood search + for (int e = 0; e < ELITE_SITES; e++) { + for (int n = 0; n < NEIGHBORS; n++) { + double neighbor[DIM]; + generate_neighbor(population[e], neighbor); + double fitness = objective_function(neighbor); + if (fitness < objective_function(population[e])) { + for (int d = 0; d < DIM; d++) { + population[e][d] = neighbor[d]; + } + } + } + } + + // Other sites neighborhood search + for (int o = ELITE_SITES; o < ELITE_SITES + OTHER_SITES; o++) { + for (int n = 0; n < NEIGHBORS; n++) { + double neighbor[DIM]; + generate_neighbor(population[o], neighbor); + double fitness = objective_function(neighbor); + if (fitness < objective_function(population[o])) { + for (int d = 0; d < DIM; d++) { + population[o][d] = neighbor[d]; + } + } + } + } + + // Scout bees - replace non-elite solutions with new random solutions + for (int i = ELITE_SITES + OTHER_SITES; i < POP_SIZE; i++) { + initialize_solution(population[i]); + } + + // Print the best solution for each iteration + printf("Iteration %d: Best Fitness = %f\n", iter + 1, best_fitness); + } + + // Print final best solution + printf("Best solution found:\n"); + for (int d = 0; d < DIM; d++) { + printf("x[%d] = %f\n", d, best_solution[d]); + } + printf("Best fitness = %f\n", best_fitness); +} + +int main() { + srand(time(0)); // Seed random number generator + bee_algorithm(); + return 0; +} From 0afafba53add079e82d184337f44d8b82a7a9b91 Mon Sep 17 00:00:00 2001 From: Abhinandan <93651229+AE-Hertz@users.noreply.github.com> Date: Mon, 4 Nov 2024 16:57:08 +0530 Subject: [PATCH 2/2] init --- .../Bee Algorithm/readme.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Optimization Algorithms/Bee Algorithm/readme.md diff --git a/Optimization Algorithms/Bee Algorithm/readme.md b/Optimization Algorithms/Bee Algorithm/readme.md new file mode 100644 index 00000000..e937aecf --- /dev/null +++ b/Optimization Algorithms/Bee Algorithm/readme.md @@ -0,0 +1,38 @@ +# Bee Algorithm in C + +## Overview + +The Bee Algorithm is an optimization technique inspired by the natural behavior of honey bees searching for food sources. This algorithm is part of swarm intelligence and is particularly useful for solving complex optimization problems where traditional methods may struggle. This repository contains a basic implementation of the Bee Algorithm in C, designed to minimize a given objective function. + +## Optimization Technique + +The Bee Algorithm simulates the foraging behavior of bees, where a colony of bees explores the environment to find the best food sources (solutions) through exploration and exploitation strategies. The main components of the algorithm include: + +1. **Initialization**: A population of candidate solutions (bees) is generated randomly within predefined bounds. +2. **Evaluation**: Each solution is evaluated using an objective function. The best solutions are identified as elite solutions. +3. **Neighborhood Search**: Elite solutions explore their neighborhoods to find better solutions by generating nearby candidates and comparing their fitness. +4. **Scouting**: Non-elite solutions are replaced with new random solutions to maintain diversity within the population and avoid local optima. +5. **Iterations**: The process repeats for a specified number of iterations, progressively improving the solutions. + +This algorithm is effective for various optimization problems, including continuous function optimization, combinatorial optimization, and multi-objective optimization. + +## How to Use + +### Prerequisites + +- A C compiler (e.g., GCC) +- Basic understanding of C programming + +### Parameters + +You can adjust the following parameters in the code to optimize the algorithm for your specific use case: + +- `POP_SIZE`: Total number of solutions (bees) +- `MAX_ITER`: Maximum number of iterations +- `DIM`: Dimension of the optimization problem +- `NEIGHBORS`: Number of neighbor solutions to evaluate per elite solution +- `ELITE_SITES`: Number of elite solutions to retain +- `OTHER_SITES`: Number of other solutions to explore +- `LOWER_BOUND` and `UPPER_BOUND`: Define the search space for the solutions + +---