C # Implementation of genetic algorithm

Source: Internet
Author: User
Tags tostring

Genetic algorithm is an evolutionary algorithm for data optimization by simulating biological evolution. The main process is the initial population generation, selection, crossover, mutation, cyclic iteration, until the optimal solution appears. This program consists of two main classes, population classes and individual classes. Define the evolutionary strategy interface and compute the adaptive policy interface. The evolutionary strategy interface implements three behaviors, crossover, mutation, and selection, in which an evolutionary strategy interface can add its own implementation. Generally implemented as follows:

//
Class Population
//
Using System;
Using System.Collections.Generic;
Using System.Collections;
Using System.Text;
Namespace SGA. Singlega
{
/*
Group class, the realization of population initialization, there are crossover, mutation behavior, selection and other behaviors. At the same time, the average fitness of the population, the maximum fitness, and the degree of adaptability are also analyzed.
*/
public class Population:collectionbase,icloneable,idisposable
{
Private Igeneticstrategy _igeneticstrategy;
Public Population (Igeneticstrategy igeneticstrategy) {this._igeneticstrategy = Igeneticstrategy;}
Public Population () {}
public void Init (int iMax)
{
Random rd = new Random ();
for (int i = 0; i < IMax; i++)
{
StringBuilder sb = new StringBuilder ();
for (int j = 0; J < J + +)
{
Sb. Append (Rd. Next (0,2));
}
This. List.add (The new individual (sb.) ToString ()));
}
}
Public double maxfitness
{
Get
{
Double DMax = Double. MinValue;
for (int i = 0; i < List.count; i++)
{
if ((List[i] as individual). Fitness > DMax) DMax = (List[i] as individual). Fitness;
}
return dmax;
}
}
Public double averagefitness
{
Get
{
return sumfitness/list.count;
}
}
Public double sumfitness
{
Get
{
Double dSum = 0;
for (int i = 0; i < List.count; i++)
{
DSum = = (List[i] as individual). Fitness;
}
return to DSum;
}
}
Public Igeneticstrategy Geneticstrategy
{
get {return this._igeneticstrategy;}
set {this._igeneticstrategy = value;}
}
Public Population Select ()
{
if (_igeneticstrategy = null) return null;
Return _igeneticstrategy.select (this);
}
public void Crossover ()
{
if (_igeneticstrategy = null) return;
_igeneticstrategy.crossover (this);
}
public void Mutation ()
{
if (_igeneticstrategy = null) return;
_igeneticstrategy.mutation (this);
}
Public individual This[int index]
{
get {return (individual) this. List[index]; }
set {this. List[index] = value; }
}
public void ADD (individual ind)
{
This. List.add (Ind);
}
public int Indexof (individual ind)
{
return this. List.indexof (Ind);
}
public void Print ()
{
for (int i = 0; i < List.count; i++)
{
Console.WriteLine ("{0} individual fit={2} {1}", i.ToString (), (List[i] as individual). Variable.tostring (), (List[i] as individual). Fitness);
}
}
#region ICloneable Members
public Object Clone ()
{
Population pop = new Population (this. Geneticstrategy);
for (int i = 0; I < this. List.count; i++)
Pop. List.add (this. List[i]);
return pop;
}
#endregion
#region IDisposable Members
public void Dispose ()
{
_igeneticstrategy = null;
}
#endregion
}
}
//
Individual
//
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace SGA. Singlega
{
public class Individual:icloneable
{
private string _gene;
Private double _fitness = double. MinValue;
public static ifitness _calfit = new onedfitness ();
public string Gene
{
get {return _gene;}
Set
{
_gene = value;
_fitness = _calfit.fitness (Variable);
}
}
Public double Fitness
{
get {return _fitness;}
}
Public double Variable
{
get {return coder.toreal (_gene,-1.0, 2.0);}
}
Public individual ()
{
}
Public individual (string sgene)
{
Gene = Sgene;
}
Public individual (string sgene,ifitness calfit)
{
_calfit = Calfit;
This. Gene = Sgene;
}
Public Ifitness Calfit
//{
get {return this._calfit;}
Set
{
This._calfit = value;
_fitness = _calfit.fitness (Coder.toreal (_gene,-1.0, 2.0));
}
//}
public int tometrication ()
{
Return Convert.ToInt32 (Gene, 2);
}
public static int operator * (Individual ind1,individual Ind2)
{
Random rd = new Random ();
int iStart = Rd. Next (0, Ind1. GENE.LENGTH-2);
int ilast = Rd. Next (Istart+1,ind1. GENE.LENGTH-1);
while (Ind1. Gene.substring (IStart, ilast-istart) = = Ind2. Gene.substring (IStart, Ilast-istart))
{
IStart = Rd. Next (0, Ind1. GENE.LENGTH-2);
ilast = Rd. Next (IStart + 1, ind1. GENE.LENGTH-1);
}
StringBuilder sbGene1 = new StringBuilder ();
Sbgene1.append (Ind1. Gene.substring (0, IStart));
Sbgene1.append (Ind2. Gene.substring (IStart, Ilast-istart));
Sbgene1.append (Ind1. Gene.substring (Ilast));
StringBuilder sbGene2 = new StringBuilder ();
Sbgene2.append (Ind2. Gene.substring (0, IStart));
Sbgene2.append (Ind1. Gene.substring (Istart,ilast-istart));
Sbgene2.append (Ind2. Gene.substring (Ilast));
Ind1. Gene = Sbgene1.tostring ();
Ind2. Gene = Sbgene2.tostring ();
return ilast-istart;
}
public int Crossover (individual ind)
{
return this * IND;
}
public int Mutation ()
{
Random rd = new Random ();
int iPos = Rd. Next (0, this.) GENE.LENGTH-1);
StringBuilder sb = new StringBuilder (this. Gene);
Sb[ipos] = sb[ipos] = = ' 0 '? ' 1 ': ' 0 ';
This. Gene = sb. ToString ();
return iPos;
}
public override string ToString ()
{
return this. Gene;
}
public override bool Equals (object obj)
{
Return base. Equals (obj);
}
public override int GetHashCode ()
{
Return base. GetHashCode ();
}
#region ICloneable Members
public Object Clone ()
{
Return to new individual (this. Gene);
}
#endregion
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.