Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mcychan authored Jun 14, 2024
1 parent 7a3c6b7 commit 74ec4a4
Show file tree
Hide file tree
Showing 4 changed files with 314 additions and 70 deletions.
75 changes: 8 additions & 67 deletions GaSchedule.Algorithm/Cso.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public class Cso<T> : NsgaIII<T> where T : Chromosome<T>
{
private int _max_iterations = 5000;
private int _chromlen;
private double _pa, _beta, _σu, _σv;
private float[] _gBestScore = null;
private double _pa;
private float[] _gBest = null;
private float[][] _current_position = null;
private LévyFlights<T> _lf;

// Initializes Cuckoo Search Optimization
public Cso(T prototype, int numberOfCrossoverPoints = 2, int mutationSize = 2, float crossoverProbability = 80, float mutationProbability = 3) : base(prototype, numberOfCrossoverPoints, mutationSize, crossoverProbability, mutationProbability)
Expand All @@ -29,12 +30,6 @@ public Cso(T prototype, int numberOfCrossoverPoints = 2, int mutationSize = 2, f
_populationSize = 5;

_pa = .25;
_beta = 1.5;

var num = Gamma(1 + _beta) * Math.Sin(Math.PI * _beta / 2);
var den = Gamma((1 + _beta) / 2) * _beta * Math.Pow(2, (_beta - 1) / 2);
_σu = Math.Pow(num / den, 1 / _beta);
_σv = 1;
}

static E[][] CreateArray<E>(int rows, int cols)
Expand All @@ -45,25 +40,6 @@ static E[][] CreateArray<E>(int rows, int cols)

return array;
}

static double Gamma(double z)
{
if (z < 0.5)
return Math.PI / Math.Sin(Math.PI * z) / Gamma(1.0 - z);

// Lanczos approximation g=5, n=7
var coef = new double[7] { 1.000000000190015, 76.18009172947146, -86.50532032941677,
24.01409824083091, -1.231739572450155, 0.1208650973866179e-2, -0.5395239384953e-5 };

var zz = z - 1.0;
var b = zz + 5.5; // g + 0.5
var sum = coef[0];
for (int i = 1; i < coef.Length; ++i)
sum += coef[i] / (zz + i);

var LogSqrtTwoPi = 0.91893853320467274178;
return Math.Exp(LogSqrtTwoPi + Math.Log(sum) - b + Math.Log(b) * (zz + 0.5));
}

protected override void Initialize(List<T> population)
{
Expand All @@ -76,48 +52,13 @@ protected override void Initialize(List<T> population)
if(i < 1) {
_chromlen = positions.Count;
_current_position = CreateArray<float>(_populationSize, _chromlen);
_lf = new LévyFlights<T>(_chromlen);
}
}
}

private float[] Optimum(float[] localVal, T chromosome)
{
var localBest = _prototype.MakeEmptyFromPrototype();
localBest.UpdatePositions(localVal);

if(localBest.Dominates(chromosome)) {
chromosome.UpdatePositions(localVal);
return localVal;
}

var positions = new float[_chromlen];
chromosome.ExtractPositions(positions);
return positions;
}

private void UpdatePosition1(List<T> population)
{
var current_position = _current_position.ToArray();
for(int i = 0; i < _populationSize; ++i) {
double u = Configuration.NextGaussian() * _σu;
double v = Configuration.NextGaussian() * _σv;
double S = u / Math.Pow(Math.Abs(v), 1 / _beta);

if(_gBestScore == null) {
_gBestScore = new float[_chromlen];
population[i].ExtractPositions(_gBestScore);
}
else
_gBestScore = Optimum(_gBestScore, population[i]);

for(int j = 0; j < _chromlen; ++j)
_current_position[i][j] += (float) (Configuration.NextGaussian() * 0.01 * S * (current_position[i][j] - _gBestScore[j]));

_current_position[i] = Optimum(_current_position[i], population[i]);
}
}

private void UpdatePosition2(List<T> population)
private void UpdateVelocities(List<T> population)
{
var current_position = _current_position.ToArray();
for (int i = 0; i < _populationSize; ++i) {
Expand All @@ -136,7 +77,7 @@ private void UpdatePosition2(List<T> population)
}

if(changed)
_current_position[i] = Optimum(_current_position[i], population[i]);
_current_position[i] = _lf.Optimum(_current_position[i], population[i]);
}
}

Expand All @@ -151,8 +92,8 @@ protected override void Reform()

protected override List<T> Replacement(List<T> population)
{
UpdatePosition1(population);
UpdatePosition2(population);
_gBest = _lf.UpdateVelocities(population, _populationSize, _current_position, _gBest);
UpdateVelocities(population);

for (int i = 0; i < _populationSize; ++i) {
var chromosome = _prototype.MakeEmptyFromPrototype();
Expand Down
222 changes: 222 additions & 0 deletions GaSchedule.Algorithm/Dlba.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GaSchedule.Model;

/*
* Xie, Jian & Chen, Huan. (2013).
* A Novel Bat Algorithm Based on Differential Operator and Lévy Flights Trajectory.
* Computational intelligence and neuroscience. 2013. 453812. 10.1155/2013/453812.
* Copyright (c) 2024 Miller Cy Chan
*/

namespace GaSchedule.Algorithm
{
public class Dlba<T> : NsgaIII<T> where T : Chromosome<T>
{
private int _currentGeneration, _max_iterations = 5000;

Check warning on line 17 in GaSchedule.Algorithm/Dlba.cs

View workflow job for this annotation

GitHub Actions / build

Field 'Dlba<T>._currentGeneration' is never assigned to, and will always have its default value 0

Check warning on line 17 in GaSchedule.Algorithm/Dlba.cs

View workflow job for this annotation

GitHub Actions / build

Field 'Dlba<T>._currentGeneration' is never assigned to, and will always have its default value 0

private int _chromlen, _minValue = 0;

private double _alpha, _pa;

private float[] _frequency, _rate, _loudness;

private float[] _gBest = null;
private float[][] _position = null;
private float[][] _velocity = null;

private List<int> _maxValues;
private LévyFlights<T> _lf;

// Initializes Bat algorithm
public Dlba(T prototype, int numberOfCrossoverPoints = 2, int mutationSize = 2, float crossoverProbability = 80, float mutationProbability = 3) : base(prototype, numberOfCrossoverPoints, mutationSize, crossoverProbability, mutationProbability)
{
_alpha = 0.9;
_pa = .25;
}

static E[][] CreateArray<E>(int rows, int cols)
{
E[][] array = new E[rows][];
for (int i = 0; i < array.GetLength(0); i++)
array[i] = new E[cols];

return array;
}

protected override void Initialize(List<T> population)
{
_maxValues = new();
_prototype.MakeEmptyFromPrototype(_maxValues);

for (int i = 0; i < _populationSize; ++i) {
List<float> positions = new();

// initialize new population with chromosomes randomly built using prototype
population.Add(_prototype.MakeNewFromPrototype(positions));

if(i < 1) {
_chromlen = positions.Count;
_frequency = new float[_chromlen];
_rate = new float[_populationSize];
_loudness = new float[_populationSize];
_position = CreateArray<float>(_populationSize, _chromlen);
_velocity = CreateArray<float>(_populationSize, _chromlen);
_lf = new LévyFlights<T>(_chromlen);
}

_rate[i] = (float) Configuration.Random();
_loudness[i] = (float) Configuration.Random() + 1;
}
}

protected override void Reform()
{
Configuration.Seed();
if (_crossoverProbability < 95)
_crossoverProbability += 1.0f;
else if (_pa < .5)
_pa += .01;
}

private void UpdateVelocities(List<T> population)
{
var mean = _loudness.Average();

var globalBest = _prototype.MakeEmptyFromPrototype();
globalBest.UpdatePositions(_gBest);
var localBest = _prototype.MakeNewFromPrototype();

for (int i = 0; i < _populationSize; ++i) {
var beta = (float) Configuration.Random();
var rand = Configuration.Random();
var n = Configuration.Rand(-1.0, 1.0);

int dim = _velocity[i].Length;
for(int j = 0; j < dim; ++j) {
_frequency[j] = ((_maxValues[j] - _minValue) * _currentGeneration / (float) n + _minValue) * beta;
_velocity[i][j] += (_position[i][j] - _gBest[j]) * _frequency[j];

if (rand > _rate[i]) {
_position[i][j] += _velocity[i][j];
if(_position[i][j] > _maxValues[j]) {
_position[i][j] = _maxValues[j];
_velocity[i][j] = _minValue;
}
else if(_position[i][j] < _minValue)
_position[i][j] = _velocity[i][j] = _minValue;
}
}

var localTemp = _prototype.MakeEmptyFromPrototype();
localTemp.UpdatePositions(_position[i]);
if (localTemp.Dominates(localBest))
localBest = localTemp;
}

for (int i = 0; i < _populationSize; ++i) {
var positionTemp = _position.ToArray();
var rand = Configuration.Random();
if (rand < _loudness[i]) {
var n = Configuration.Rand(-1.0, 1.0);
int dim = _velocity[i].Length;
for(int j = 0; j < dim; ++j) {
positionTemp[i][j] = _gBest[j] + (float) n * mean;
if(positionTemp[i][j] > _maxValues[j]) {
positionTemp[i][j] = _maxValues[j];
_velocity[i][j] = _minValue;
}
else if(positionTemp[i][j] < _minValue)
positionTemp[i][j] = _velocity[i][j] = _minValue;
}

if (globalBest.Dominates(localBest)) {
_position[i] = positionTemp[i];
_rate[i] *= (float) Math.Pow(_currentGeneration / n, 3);
_loudness[i] *= (float) _alpha;
}
}

_position[i] = _lf.Optimum(_position[i], population[i]);
}
}

protected override List<T> Replacement(List<T> population)
{
_gBest = _lf.UpdateVelocities(population, _populationSize, _position, _gBest);
UpdateVelocities(population);

for (int i = 0; i < _populationSize; ++i) {
var chromosome = _prototype.MakeEmptyFromPrototype();
chromosome.UpdatePositions(_position[i]);
population[i] = chromosome;
}

return base.Replacement(population);
}

// Starts and executes algorithm
public override void Run(int maxRepeat = 9999, double minFitness = 0.999)
{
if (_prototype == null)
return;

var pop = new List<T>[2];
pop[0] = new List<T>();
Initialize(pop[0]);

// Current generation
int currentGeneration = 0;
int bestNotEnhance = 0;
double lastBestFit = 0.0;

int cur = 0, next = 1;
while(currentGeneration < _max_iterations)
{
var best = Result;
if (currentGeneration > 0)
{
var status = string.Format("\rFitness: {0:F6}\t Generation: {1}", best.Fitness, currentGeneration);
Console.Write(status);

// algorithm has reached criteria?
if (best.Fitness > minFitness)
break;

var difference = Math.Abs(best.Fitness - lastBestFit);
if (difference <= 1e-6)
++bestNotEnhance;
else {
lastBestFit = best.Fitness;
bestNotEnhance = 0;
}

if (bestNotEnhance > (maxRepeat / 100))
Reform();
}

/******************* crossover *****************/
var offspring = Crossing(pop[cur]);

/******************* mutation *****************/
foreach (var child in offspring)
child.Mutation(_mutationSize, _mutationProbability);

pop[cur].AddRange(offspring);

/******************* replacement *****************/
pop[next] = Replacement(pop[cur]);
_best = pop[next][0].Dominates(pop[cur][0]) ? pop[next][0] : pop[cur][0];

(cur, next) = (next, cur);
++currentGeneration;
}
}

public override string ToString()
{
return "Bat algorithm with differential operator and Levy flights trajectory (DLBA)";
}
}
}
Loading

0 comments on commit 74ec4a4

Please sign in to comment.