Home > Back-end >  Colony algorithm to solve function optimization problems
Colony algorithm to solve function optimization problems

Time:11-04

This from online algorithm (English comments originally, but I change to Chinese annotation, there may be not the exact place) have few places to see don't understand, ask a great god!!!!! Is that several functions see don't understand, ask god for help, and I need to apply the letter group algorithm is to the logistics distribution vehicle scheduling, so have a great god can give me talk about the effects of several functions! Pray god help
Typedef double FunctionCallback (*) (double sol [D]);
Benchmark test functions/* */
Double sphere (double sol [D]);
Double Rosenbrock (double sol [D]);
Double Griewank (double sol [D]);
Double Rastrigin (double sol [D]);

Code:
/* colony algorithm for function optimization
*/
#include
#include
#include
#include
#include

# define M_PI 3.1415926
/* control parameters of the ABC algorithm */
100/* # define NP swarm size (lead + follow bee bee) the number of */
# define FoodNumber NP/2/* the number of food sources, and leads the number of bees */
2500/* # define limit if a food source in a given limit limiting step in fitness has not been improved, is to give up the food source */
# define maxCycle 2000/* */stop condition

The specific parameters *//* function
# define D/* 50 the number of function arguments */
# define lb - 5.12/* function lower parameters */
5.12/* # define ub function parameter, the upper bound, lower bound parameters can be defined as array for its parameters with different boundary problems */


30/* # define the runtime run number */


Double Foods [FoodNumber] [D];/* food each row of the matrix is a vector to optimize parameters. The number of rows of the matrix and food source consistent */
Double f [FoodNumber];/* is a objective function values related to the food source */
Double fitness [FoodNumber]; The quality of the food source (fitness) */*/
Double trial [FoodNumber];/* initializes the variable number (local search) */
Double prob [FoodNumber];/* */food source selected probability
Double solution [D];/* create new solution solution */
Double ObjValSol; The new objective function values */*/
Double FitnessSol;/* the new fitness (fitness) */
Int neighbour, param2change;/* param2change=j, neighbour=k, v_ {ij}=x_ + phi_ {ij} {the ij} * (x_ {kj} - x_ {ij}) */
Double GlobalMin;/* algorithm to get the optimal solution */
Double GlobalParams [D];/* to save the optimal food source solution set */
Double GlobalMins [runtime];/* run times */
Double r; Random number between/* [0, 1) */

/* a function pointer returning double and taking a D - dimensional array as the argument */
/* If your function takes additional arguments then change the function pointer definition and lines calling "...=function (solution);" In the code */
Typedef double FunctionCallback (*) (double sol [D]);

Benchmark test functions/* */
Double sphere (double sol [D]);
Double Rosenbrock (double sol [D]);
Double Griewank (double sol [D]);
Double Rastrigin (double sol [D]);

Home/* Write your own objective function name sphere */
FunctionCallback function=& amp; Rastrigin;

/* through food source and calculate the fitness of the objective function */
Double CalculateFitness (double fun)
{
Double the result=0;
If (fun>=0)
{
Result=1/(fun + 1);
}
The else
{
Result=1 + fabs (fun);
}
return result;
}

So far the best food source */* memory/
Void MemorizeBestSource ()
{
int i,j;

for(i=0; i{
If (f [I] {
GlobalMin=f [I];//to find the optimal food source and replace the current
for(j=0; jGlobalParams [j]=Foods [I] [j];//will be the best food source row of preserved
}
}
}

/* Variables are initialized in the range [lb, ub]. If the each parameter from the company's range, use the arrays lb [j], ub home [j] lb and ub */
Initialize Counters/* food source of the count of the food sources are also an initialized in this function */
Void init (int index)
{
int j;
For (j=0; j{
R=((double) rand ()/((double) (RAND_MAX) + (double) (1)));
Foods [index] [j]=r * (ub lb) + lb.
Solution [j]=Foods [index] [j];
}
F [index]=function (solution);
Fitness [index]=CalculateFitness (f [index]);
Trial [index]=0;
}

/* all of the food source is initialized */
Void initial ()
{
int i;
for(i=0; i{
Init (I);
}
GlobalMin=f [0];
for(i=0; iGlobalParams [I]=Foods [0] [I];


}

Void SendEmployedBees ()
{
int i,j;
/* lead bee */
for (i=0; i{
/* random change parameter */
R=((double) rand ()/((double) (RAND_MAX) + (double) (1)));
Param2change=(int) (r * D);

/* A randomly feature solution is, informs the in producing A mutant solution of the solution I */
R=((double) rand ()/((double) (RAND_MAX) + (double) (1)));
Neighbour=(int) (r * FoodNumber);

/* randomly selected solution cannot be the same as the solution I */
While (neighbour==I)
{
R=((double) rand ()/((double) (RAND_MAX) + (double) (1)));
Neighbour=(int) (r * FoodNumber);
}
for(j=0; jSolution [j]=Foods [I] [j];

/* v_ {ij}=x_ + phi_ {ij} {the ij} * (x_ {kj} - x_ {ij}) */
R=((double) rand ()/((double) (RAND_MAX) + (double) (1)));
Solution [param2change]=Foods [I] [param2change] + (Foods [I] [param2change] - Foods [neighbour] [param2change]) * (r - 0.5) * 2;

/* if the parameter is beyond the boundary, then use border instead of */
If (solution [param2change] If (solution [param2change] & gt; Ub) solution [param2change]=ub;
ObjValSol=function (solution);
FitnessSol=CalculateFitness (ObjValSol);

/* between the I and the solution in the current solution greedy choice */
If (FitnessSol> Fitness [I])
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
Prev:Perl
  • Related