本文整理汇总了C#中IChromosome类的典型用法代码示例。如果您正苦于以下问题:C# IChromosome类的具体用法?C# IChromosome怎么用?C# IChromosome使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
IChromosome类属于命名空间,在下文中一共展示了IChromosome类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Translate
public object Translate(IChromosome chromosome)
{
// Конструираме невронна мрежа и изчисляваме резултата
DoubleArrayChromosome dac = (DoubleArrayChromosome)chromosome;
ActivationNetwork Network = new ActivationNetwork(
new BipolarSigmoidFunction(sigmoidAlphaValue),
mArchitecture[0], mArchitecture[1], mArchitecture[2]);
int current = 0;
int i = 0;
// Тегла на скрит слой
for (i = 0; i < mArchitecture[1]; i++)
{
for (int j = 0; j < mArchitecture[0]; j++)
{
Network[0][i][j] = dac.Value[current++];
}
}
// Тегла на изходен слой
for (i = 0; i < mArchitecture[2]; i++)
{
for (int j = 0; j < mArchitecture[1]; j++)
{
Network[1][i][j] = dac.Value[current++];
}
}
return Network;
}
示例2: QueuePopulation
/// <summary>
/// Initializes a new instance of the <see cref="Population"/> class.
/// </summary>
///
/// <param name="size">Initial size of population.</param>
/// <param name="ancestor">Ancestor chromosome to use for population creatioin.</param>
/// <param name="fitnessFunction">Fitness function to use for calculating
/// chromosome's fitness values.</param>
/// <param name="selectionMethod">Selection algorithm to use for selection
/// chromosome's to new generation.</param>
///
/// <remarks>Creates new population of specified size. The specified ancestor
/// becomes first member of the population and is used to create other members
/// with same parameters, which were used for ancestor's creation.</remarks>
///
/// <exception cref="ArgumentException">Too small population's size was specified. The
/// exception is thrown in the case if <paramref name="size"/> is smaller than 2.</exception>
///
public QueuePopulation(int size,
IChromosome ancestor,
IFitnessFunction fitnessFunction,
ISelectionMethod selectionMethod)
{
if (size < 2)
throw new ArgumentException("Too small population's size was specified.");
this.fitnessFunction = fitnessFunction;
this.selectionMethod = selectionMethod;
this.size = size;
// add ancestor to the population
ancestor.Evaluate(fitnessFunction);
population.Add(ancestor.Clone());
// add more chromosomes to the population
for (int i = 1; i < size; i++)
{
// create new chromosome
IChromosome c = ancestor.CreateNew();
// calculate it's fitness
c.Evaluate(fitnessFunction);
// add it to population
population.Add(c);
}
}
示例3: evolve
public IChromosome[] evolve(IChromosome alpha, IChromosome beta)
{
IChromosome[] offspring = new IChromosome[2];
offspring[0] = new Chromosome(0, new char[alpha.Alleles.Length]);
offspring[1] = new Chromosome(0, new char[alpha.Alleles.Length]);
int half = alpha.Alleles.Length/2;
int count = 0;
for (int i = 0; i < half; i++)
{
offspring[0].Alleles[i] = alpha.Alleles[i];
count++;
}
for (int j = count; j < beta.Alleles.Length - 1; j++)
{
offspring[0].Alleles[j] = beta.Alleles[j];
}
for (int i = 0; i < half; i++)
{
offspring[1].Alleles[i] = beta.Alleles[i];
count++;
}
for (int j = count; j < beta.Alleles.Length - 1; j++)
{
offspring[1].Alleles[j] = alpha.Alleles[j];
}
return offspring;
}
开发者ID:keym,项目名称:Monoalphabetic-Cipher-Solving-Genetic-Algorithm,代码行数:33,代码来源:SinglePointCrossover.cs
示例4: Roulette
public void Roulette()
{
IChromosome<int>[] a = new IChromosome<int>[] { new DigitalChromosome().GenerateFromArray(new int[] { 9, 9, 9 }),
new DigitalChromosome().GenerateFromArray(new int[] { 7, 7, 7 }) };
RouletteSelection<int> selection = new RouletteSelection<int>();
IChromosome<int>[] res = selection.Select(a, x => x.ToArray().Sum(), 1);
}
示例5: Evaluate
//retorna a quantidade de rainhas a salvo
public double Evaluate(IChromosome chromosome)
{
this.individo = (ShortArrayChromosome)chromosome;
nCasas = individo.Length;
double ret = 0;
int[,] tabuleiro = new int[nCasas, nCasas];
// primeiro representamos o tabuleiro com 0 e 1
gerarTabuleiro(tabuleiro);
// agora verificamos quantas rainhas estao a salvo, este será o nosso
// retorno da função fitness
for (int i = 0; i < nCasas; i++)
{
for (int j = 0; j < nCasas; j++)
{
if (tabuleiro[i, j] == 1)
{
if (!temAtacante(tabuleiro, i, j))
{
ret++;
}
}
}
}
return ret;
}
示例6: Draw
/// <summary>
/// Draws the specified best chromosome.
/// </summary>
/// <param name="bestChromosome">The best chromosome.</param>
public override void Draw(IChromosome bestChromosome)
{
var best = bestChromosome as EquationChromosome;
var genes = best.GetGenes();
Console.WriteLine("Equation: {0} + 2*{1} + 3*{2} + 4*{3} = {4}", genes[0], genes[1], genes[2], genes[3], EqualityFitness.GetEquationResult(best));
}
示例7: PerformMutate
/// <summary>
/// Mutate the specified chromosome.
/// </summary>
/// <param name="chromosome">The chromosome.</param>
/// <param name="probability">The probability to mutate each chromosome.</param>
protected override void PerformMutate(IChromosome chromosome, float probability)
{
ExceptionHelper.ThrowIfNull("chromosome", chromosome);
var genesLength = chromosome.Length;
if (m_mutableGenesIndexes == null || m_mutableGenesIndexes.Length == 0)
{
if (m_allGenesMutable)
{
m_mutableGenesIndexes = Enumerable.Range(0, genesLength).ToArray();
}
else
{
m_mutableGenesIndexes = RandomizationProvider.Current.GetInts(1, 0, genesLength);
}
}
foreach (var i in m_mutableGenesIndexes)
{
if (i >= genesLength)
{
throw new MutationException(this, "The chromosome has no gene on index {0}. The chromosome genes length is {1}.".With(i, genesLength));
}
if (RandomizationProvider.Current.GetDouble() <= probability)
{
chromosome.ReplaceGene(i, chromosome.GenerateGene(i));
}
}
}
示例8: Select
/// <summary>
/// Realizuje algorytm selekcji.
/// </summary>
/// <param name="population">Populacja poddawana selekcji.</param>
/// <returns>Zbiór osobników populacji, wybranych w wyniku selekcji do następnej generacji.</returns>
public IChromosome[] Select(IChromosome[] population)
{
if (population.Length < 2)
{
return population;
}
IChromosome[] subpopulation = population.Where(ch => ch.Evaluate() > 0).ToArray();
Double totalFitness = subpopulation.Sum(ch => ch.Evaluate());
Int32 newPopulationSize = (Int32)PopulationSize.ComputeSize(subpopulation);
IChromosome[] result = new IChromosome[newPopulationSize];
for (Int32 i = 0; i < newPopulationSize; i++)
{
Double ptr = RandomGenerator.NextDouble();
Double sum = 0.0;
for (Int32 j = 0; j < subpopulation.Length; ++j)
{
sum += subpopulation[j].Evaluate() / totalFitness;
if (sum > ptr)
{
result[i] = subpopulation[j];
break;
}
}
}
return result;
}
示例9: Select
/// <summary>
/// Realizuje algorytm selekcji.
/// </summary>
/// <param name="population">Populacja poddawana selekcji.</param>
/// <returns>Zbiór osobników populacji, wybranych w wyniku selekcji do następnej generacji.</returns>
public IChromosome[] Select(IChromosome[] population)
{
if (population.Length < 2)
{
return population;
}
IChromosome[] subpopulation = population.Where(ch => ch.Evaluate() > 0).ToArray();
Double totalFitness = subpopulation.Sum(ch => ch.Evaluate());
Int32 newPopulationSize = (Int32)PopulationSize.ComputeSize(subpopulation);
IChromosome[] result = new IChromosome[newPopulationSize];
Double ptrstep = 1.0 / newPopulationSize;
Double ptr = RandomGenerator.NextDouble() * ptrstep;
Int32 pos = 0;
Double sum = 0.0;
for (Int32 i = 0; i < subpopulation.Length; i++)
{
for (sum += subpopulation[i].Evaluate() / totalFitness; sum > ptr; ptr += ptrstep)
{
result[pos++] = subpopulation[i];
if (pos == newPopulationSize)
{
return result;
}
}
}
// it shouldn't happen
Debug.Assert(false);
return result;
}
示例10: Evaluate
/// <summary>
/// Performs the evaluation against the specified chromosome.
/// </summary>
/// <param name="chromosome">The chromosome to be evaluated.</param>
/// <returns>The fitness of the chromosome.</returns>
public double Evaluate(IChromosome chromosome)
{
var c = chromosome as GhostwriterChromosome;
var text = c.BuildText();
return EvaluateFunc(text);
}
示例11: Evaluate
/// <summary>
/// Evaluates chromosome.
/// </summary>
///
/// <param name="chromosome">Chromosome to evaluate.</param>
///
/// <returns>Returns chromosome's fitness value.</returns>
///
/// <remarks>The method calculates fitness value of the specified
/// chromosome.</remarks>
///
public double Evaluate( IChromosome chromosome )
{
// get function in polish notation
string function = chromosome.ToString( );
// go through all the data
double error = 0.0;
for ( int i = 0, n = data.GetLength( 0 ); i < n; i++ )
{
// put next X value to variables list
variables[0] = data[i, 0];
// avoid evaluation errors
try
{
// evalue the function
double y = PolishExpression.Evaluate( function, variables );
// check for correct numeric value
if ( double.IsNaN( y ) )
return 0;
// get the difference between evaluated Y and real Y
// and sum error
error += Math.Abs( y - data[i, 1] );
}
catch
{
return 0;
}
}
// return optimization function value
return 100.0 / ( error + 1 );
}
示例12: Draw
/// <summary>
/// Draws the sample;
/// </summary>
public void Draw(IChromosome bestChromosome)
{
var c = bestChromosome as TspChromosome;
Console.WriteLine("Distance: {0:n2}", c.Distance);
//var cities = bestChromosome.GetGenes ().Select (g => g.Value.ToString ()).ToArray ();
//Console.WriteLine("City tour: {0}", String.Join(", ", cities));
}
示例13: Evaluate
/// <summary>
/// Performs the evaluation against the specified chromosome.
/// </summary>
/// <param name="chromosome">The chromosome to be evaluated.</param>
/// <returns>The fitness of the chromosome.</returns>
public double Evaluate(IChromosome chromosome)
{
var equalityChromosome = chromosome as EquationChromosome;
var fitness = Math.Abs(m_getEquationResult(equalityChromosome.GetGenes()) - m_expectedResult);
return fitness * -1;
}
示例14: Organism
public Organism ( IChromosome cs , float fitness )
{
GeneticUtil.CHECKNULLARG( cs );
this.m_chromosome = cs;
this.Fitness = fitness;
this.ID = Organism.generator.GetUniqueID();
this.GenerationID = -1;
}
示例15: OnePointCrossoverTest
public void OnePointCrossoverTest()
{
IChromosome<int> a = new DigitalChromosome().GenerateFromArray(new int[] { 1, 2, 3, 4 });
IChromosome<int> b = new DigitalChromosome().GenerateFromArray(new int[] { 4, 3, 2, 1 });
OnePointCrossover<int> cross = new OnePointCrossover<int>(2);
IChromosome<int>[] res = cross.Crossover(a, b);
IChromosome<int>[] exp = new IChromosome<int>[2] { new DigitalChromosome().GenerateFromArray(new int[] { 1, 2, 2, 1 }), new DigitalChromosome().GenerateFromArray(new int[] { 4, 3, 3, 4 }) };
CollectionAssert.AreEqual(res, exp);
}