Files
jube-fork/Accord.Genetic/Selection Algorithms/RouletteWheelSelection.cs
richard-churchman cc0a2bee21 Extensive modifications to the serialisation of Neural Network models to support Newtonsoft Json.net rather than Binary Serializer.
Included open-source Accord.net library code natively to the solution given the deprecated state of that project.  Accord.net will be supported inside Jube for own needs (such as upgrading to .Net 7+).

Accord.net libraries modified to compile to .Net 6.

Modified Accord.net libraries to better support Newtonsoft Json.net serialisation.

Included annotations in the Accord.net libraries to house important dependency statistics. Serialised json can be used independently of Jube to recall a Neural Network.

Removal of Binary Serializer totally as a precursor to the projects support of .Net 7+.

Extensive bug fixing in crucial functions of Neural Network training and recall.  While this part of the system remains in Alpha, it is substantially more fit for purpose.
2023-12-24 10:22:22 +01:00

102 lines
3.4 KiB
C#

// AForge Genetic Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2006-2011
// [email protected]
//
namespace Accord.Genetic
{
using System;
using System.Collections;
using System.Collections.Generic;
using AForge;
/// <summary>
/// Roulette wheel selection method.
/// </summary>
///
/// <remarks><para>The algorithm selects chromosomes to the new generation according to
/// their fitness values - the more fitness value chromosome has, the more chances
/// it has to become member of new generation. Each chromosome can be selected
/// several times to the new generation.</para>
///
/// <para>The "roulette's wheel" is divided into sectors, which size is proportional to
/// the fitness values of chromosomes - the size of the wheel is the sum of all fitness
/// values, size of each sector equals to fitness value of chromosome.</para>
/// </remarks>
///
public class RouletteWheelSelection : ISelectionMethod
{
/// <summary>
/// Initializes a new instance of the <see cref="RouletteWheelSelection"/> class.
/// </summary>
///
public RouletteWheelSelection() { }
/// <summary>
/// Apply selection to the specified population.
/// </summary>
///
/// <param name="chromosomes">Population, which should be filtered.</param>
/// <param name="size">The amount of chromosomes to keep.</param>
///
/// <remarks>Filters specified population keeping only those chromosomes, which
/// won "roulette" game.</remarks>
///
public void ApplySelection(List<IChromosome> chromosomes, int size)
{
var rand = Accord.Math.Random.Generator.Random;
// new population, initially empty
List<IChromosome> newPopulation = new List<IChromosome>();
// size of current population
int currentSize = chromosomes.Count;
// calculate summary fitness of current population
double fitnessSum = 0;
foreach (IChromosome c in chromosomes)
{
fitnessSum += c.Fitness;
}
// create wheel ranges
double[] rangeMax = new double[currentSize];
double s = 0;
int k = 0;
foreach (IChromosome c in chromosomes)
{
// cumulative normalized fitness
s += (c.Fitness / fitnessSum);
rangeMax[k++] = s;
}
// select chromosomes from old population to the new population
for (int j = 0; j < size; j++)
{
// get wheel value
double wheelValue = rand.NextDouble();
// find the chromosome for the wheel value
for (int i = 0; i < currentSize; i++)
{
if (wheelValue <= rangeMax[i])
{
// add the chromosome to the new population
newPopulation.Add(((IChromosome)chromosomes[i]).Clone());
break;
}
}
}
// empty current population
chromosomes.Clear();
// move elements from new to current population
chromosomes.AddRange(newPopulation);
}
}
}