Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Population {
  4.  
  5.     Point[] points;
  6.  
  7.  
  8.     int population_size;
  9.     int max = 10;
  10.     int min = -10;
  11.     int tournament_size = 5;
  12.  
  13.     int point_count = 0;
  14.  
  15.     public Population(int population_size) {
  16.         points = new Point[population_size];
  17.         for (int i = 0; i < population_size; i++) {
  18.             points[i] = new Point();
  19.         }
  20.  
  21.  
  22.         this.population_size = population_size;
  23.  
  24.  
  25.         randomizePopulation();
  26.     }
  27.  
  28.  
  29.     public void randomizePopulation() {
  30.         for (int i = 0; i < population_size; i++) {
  31.             points[i].x = min + Math.random() * (max - min);
  32.             points[i].y = min + Math.random() * (max - min);
  33.         }
  34.     }
  35.  
  36.     public Population evolution() {
  37.         //print(this,this.population_size);
  38.         double[] results = new double[this.population_size];
  39.         for (int i = 0; i < population_size; i++) {
  40.             results[i] = GeneticAlgorithm.function.calculate(points[i].x, points[i].y);
  41.  
  42.         }
  43.  
  44.         Point best = getFittest();
  45.         System.out.println("Najlepsza wartosc " + calculate_fittness(best));
  46.        
  47.  
  48.         Population newpopulation = new Population(this.population_size);
  49.  
  50.  
  51.  
  52.  
  53.         for (int j = 0; j < this.population_size; j++) {
  54.  
  55.             Point parent1 =  tournament(this);
  56.             Point parent2 =  tournament(this);
  57.  
  58.             crossover(parent1, parent2, newpopulation);
  59.  
  60.             if (this.population_size == this.point_count) break;
  61.  
  62.         }
  63.  
  64.         this.point_count = 0;
  65.  
  66.  
  67.  
  68.  
  69.         return newpopulation;
  70.     }
  71.  
  72.     public void crossover(Point parent1 , Point parent2,Population newpopulation)
  73.     {
  74.         newpopulation.points[point_count].x=parent1.x;
  75.         newpopulation.points[point_count].y=parent2.x;
  76.  
  77.         point_count+=1;
  78.  
  79.         newpopulation.points[point_count].x=parent1.y;
  80.         newpopulation.points[point_count].y=parent2.y;
  81.  
  82.         point_count+=1;
  83.     }
  84.  
  85.     public Point tournament(Population population)
  86.     {
  87.         Population tournament = new Population(this.tournament_size);
  88.  
  89.         for(int i = 0 ; i< this.tournament_size ; i++)
  90.         {
  91.             int index = (int) (Math.random() * this.population_size);
  92.             tournament.points[i] = population.points[index];
  93.         }
  94.  
  95.  
  96.         Point parent = tournament.getFittest();
  97.  
  98.  
  99.         return parent;
  100.     }
  101.  
  102.     public Point getFittest()
  103.     {
  104.         double fittest = calculate_fittness(points[0]);
  105.         Point x = this.points[0];
  106.         for(int i=1;i<population_size;i++)
  107.         {
  108.            if(fittest> calculate_fittness(points[i]))
  109.            {
  110.                fittest=calculate_fittness(points[i]);
  111.                x=this.points[i];
  112.            }
  113.         }
  114.  
  115.         return x;
  116.     }
  117.  
  118.  
  119.     public double calculate_fittness(Point p)
  120.     {
  121.  
  122.         return GeneticAlgorithm.function.calculate(p.x,p.y);
  123.     }
  124.  
  125.     public void print(Population population, int size)
  126.     {
  127.         for(int i=0;i<size;i++)
  128.         {
  129.             //System.out.println(population.points[i].x +" "+population.points[i].y);
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement