public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
public class Chromosome : IComparable { public double [] gens; static Random random = new Random (); public Chromosome() { this .gens = new double [3]; for ( int i = 0; i < 3; i++) { this .gens[i] = random.NextDouble() * 100; } this .fit = fitness( this ); } public int CompareTo( object obj) } * This source code was highlighted with Source Code Highlighter .
* This source code was highlighted with Source Code Highlighter .
- public class Population
- {
- Random random = new Random ();
- public List <Chromosome> population;
- public int count;
- public double crossProbability;
- public double MutationProbability;
- public int age;
- public Population ()
- {
- for ( int i = 0; i <5000; i ++)
- {
- this .population.Add ( new Chromosome ());
- }
- }
- public void GoToNextGeneration ()
- {
- Chromosome chromosome;
- for ( int i = 0; i <count; i ++)
- {
- chromosome = population [i];
- if (random.NextDouble () <crossProbability)
- {
- population.Add (Cross (chromosome, population [random.Next (count)]));
- }
- }
- for ( int i = 0; i <population.Count; i ++)
- {
- if (random.NextDouble () <MutationProbability)
- {
- population.Add (Mutate (population [i]));
- }
- }
- population.Sort ();
- population.RemoveRange (count, population.Count-count);
- age ++;
- }
- Chromosome Cross (Chromosome a, Chromosome b)
- Chromosome Mutate (Chromosome a)
- double Fitness (Chromosome a)
- }
* This source code was highlighted with Source Code Highlighter .
- Chromosome Cross (Chromosome a, Chromosome b)
- {
- double [] pair = new double [3];
- for (int i = 0; i <3; i ++)
- {
- if ((random. Next ()% 2) == 0)
- {
- pair [i] = a.Gens [i] + (b.Gens [i] - a.Gens [i]) * 0.1;
- }
- else
- {
- pair [i] = b.Gens [i] - (b.Gens [i] - a.Gens [i]) * 0.1;
- }
- }
- Chromosome result = new Chromosome (pair);
- return result;
- }
* This source code was highlighted with Source Code Highlighter .
- Chromosome Mutate (Chromosome a)
- {
- double [] pair = ( double []) a.Gens.Clone ();
- int geneNum = random.Next (3);
- pair [geneNum] = random.NextDouble () * 100;
- return new Chromosome (pair, a.chromosomeSettings);
- }
* This source code was highlighted with Source Code Highlighter .
- double GetFitness (Chromosome a)
- {
- double result = 0;
- double temp;
- for ( int i = 0; i <funcLength; i ++)
- {
- temp = Math .Abs (Func (i, a.Gens) - funcArray [i]);
- result + = temp;
- }
- return result;
- }
- double Func ( double x, double [] gens)
- {
- double result = 0;
- for ( int i = 2; i> = 0; i--)
- {
- result + = gens [2-i] * Math .Pow (x, i);
- }
- return result;
- }
Source: https://habr.com/ru/post/66121/
All Articles