Genetic algorithm Demo

Source: Internet
Author: User
Tags rand sin

Using System;
Using System.Collections.Generic;
Using System.Text;

Namespace Gene
{
Class Intelligenttestsystem
{
Public

static int population=100; Number of population
static int maxnumber = 2; The number of genes used per individual (chromosome)


double[,] chromosome = new double[population, MaxNumber]; The population of each individual (chromosome)

double[] Fitness = new Double[population]; Degree of adaptability

int questiontypenumber = 0;
int[] Questiontype = new Int[maxnumber];

double[] SampleX = new DOUBLE[10];

double[] Sampley = new DOUBLE[10];

public void initial ()
{
Random rand = new Random (Unchecked (5 * (int) DateTime.Now.Ticks));

for (int i = 0; i < population; i++)//randomly generated chromosomes
{
for (int j = 0; J < 2; J + +)
{
Chromosome[i, J] = (rand. Next (200)-100)/10.0;
}
}

for (int i = 0; i < x; i++)//function 10 coordinate points y=3*sin (x*x) +5*x;
{
Samplex[i] = i;
Sampley[i] = 3 * Math.sin (samplex[i] * samplex[i]) + 5 * samplex[i];
}

}

public double calculatesinglefitness (int n)
{
double[] y = new double[10];

Double A = chromosome[n, 0];

Double b = chromosome[n, 1];

Double c = 0.0; Save error sum of squares


for (int i = 0; i <; i++)
{
Y[i] = A * Math.sin (samplex[i] * Samplex[i]) + b * Samplex[i];

c = C + (Sampley[i]-y[i]) * (Sampley[i]-y[i]);
}


Fitness[n] =-C;

return fitness[n];

}

public void calculatefitness ()
{
for (int i = 0; i < population; i++) calculatesinglefitness (i);
}


public void sort ()
{
Double instead = 0;

Double ch0 = 0;

Double ch1 = 0;

Bubble sort
for (int j=0;j<population; j + +)
{
for (int i = population-1; I >0; i--)
{
if (Fitness[i] > Fitness[i-1])
{
Swap the two adjacent

Switching Fitness
instead = fitness[i-1];

FITNESS[I-1] = fitness[i];

Fitness[i] = instead;

Swap chromosomes
Ch0 = chromosome[i-1,0];

CH1 = chromosome[i-1, 1];

Chromosome[i-1, 0] = chromosome[i, 0];

Chromosome[i-1, 1] = chromosome[i, 1];

Chromosome[i, 0] = Ch0;

Chromosome[i, 1] = CH1;
}

}
}

}

public void Intercross (int father, int mother, int son, int daughter)
{
chromosome[son,0] = chromosome[father,0];
chromosome[son,1] = chromosome[mother,1];


Chromosome[daughter, 0] = 0.5 * chromosome[father, 0] + 0.5 * chromosome[mother, 0];
Chromosome[daughter, 1] = 0.5 * Chromosome[father, 1] + 0.5 * chromosome[mother, 1];

}

public void aberrance (int father, int son)
{
Random rand = new Random (Unchecked (5* (int) DateTime.Now.Ticks));

Chromosome[son, 0] = chromosome[father, 0] + (rand. Next (200)-100)/100.0;
Chromosome[son, 1] = Chromosome[father, 1] + (rand. Next (200)-100)/100.0;
}


public void Generate ()
{

Random rand = new Random (Unchecked (7 * (int) DateTime.Now.Ticks));


for (int k = +; k <; k+=2)
{
int father = Rand. Next (30);
int mother = Rand. Next (30);


int son = k;
int daughter = k+1;

Intercross (father, mother, son, daughter);
}

for (int k = Bayi; k < K + +)
{
int father = Rand. Next (30);

int son = k;

Aberrance (father, son);
}
}

}
}

Genetic algorithm Demo

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.