Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogbuagu committed Nov 13, 2014
1 parent 672df36 commit ebb556c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Model/GeneticAlgorithm/Population.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Population
{
int xsomeLength;
int populationSize;
int fitnessSum;
double crossProbability;
double mutationProbability;
List<Chromosome> population;
Expand All @@ -31,6 +32,7 @@ public Population(int length ,int size, double cross_prob, double mut_prob)
this.populationSize = size;
this.crossProbability = cross_prob;
this.mutationProbability = mut_prob;
this.fitnessSum =0;
population = new ArrayList<>();
}

Expand Down Expand Up @@ -69,7 +71,7 @@ private double function(double x)

public void fitness()
{
int fitnessSum =0;
fitnessSum =0;
for (Chromosome x: population)
{
x.fitness = function(decimal(x));
Expand All @@ -88,7 +90,7 @@ public int getParent()
{
for (Chromosome x: population)
{
if (rand.nextDouble() < x.fitnessRatio)
if ((Math.random() * fitnessSum) < x.fitnessRatio)
return population.indexOf(x);
}
return rand.nextInt((population.size()-1 -0)+1) +0;
Expand All @@ -102,7 +104,7 @@ public Chromosome[] crossOver(Chromosome father, Chromosome mother)
Chromosome child1 = new Chromosome(father.length);
Chromosome child2 = new Chromosome(father.length);

if(rand.nextDouble() <= crossProbability)
if(Math.random() < crossProbability)
{
for (int i =0; i< crossingPoint; i++)
child1.genes.set(i, father.genes.get(i));
Expand Down Expand Up @@ -132,7 +134,7 @@ public Chromosome mutate(Chromosome chrome)
{
for (int i=0; i< chrome.length; i++)
{
if (rand.nextDouble() < mutationProbability)
if (Math.random() <= mutationProbability)
{
if (chrome.genes.get(i) == false)
chrome.genes.set(i, true);
Expand All @@ -148,7 +150,7 @@ public double geneticAlgorithm()
{
initialPopulation();
List<Chromosome> newPopulation = new ArrayList<>();
for (int j = 0; j < 100; j++)
for (int j = 0; j < 1000; j++)
{
fitness();
// List<Chromosome> newPopulation = new ArrayList<>();
Expand Down Expand Up @@ -182,7 +184,7 @@ public double geneticAlgorithm()

public static void main(String[] args)
{
Population p = new Population (4, 10, 0.7, 0.001);
Population p = new Population (4, 10, 0.7, 0.005);
System.out.println(p.geneticAlgorithm());
}

Expand Down

0 comments on commit ebb556c

Please sign in to comment.