Skip to content

Commit

Permalink
Merge pull request #1619 from AE-Hertz/branch6
Browse files Browse the repository at this point in the history
 [NEW ALGORITHM] Bee Algorithm #1614
  • Loading branch information
pankaj-bind authored Nov 4, 2024
2 parents ab30415 + 0afafba commit 394ef86
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
114 changes: 114 additions & 0 deletions Optimization Algorithms/Bee Algorithm/beeAlgorithm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#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;
}
38 changes: 38 additions & 0 deletions Optimization Algorithms/Bee Algorithm/readme.md
Original file line number Diff line number Diff line change
@@ -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

---

0 comments on commit 394ef86

Please sign in to comment.