Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. public static class Program
  2.     {
  3.         static int populationCount = 100;
  4.  
  5.         static int width = 400;
  6.         static int height = 400;
  7.        
  8.         static Random r = new Random();
  9.        
  10.         static float tx = 50;
  11.         //static float tx = (float)Math.Round((double)r.Next(-width, width)/10, 2);
  12.         //static float ty = (float)Math.Round((double)r.Next(-height, height)/10, 2);
  13.        
  14.         static Creature[] population = new Creature[populationCount];
  15.        
  16.         public static void Main()
  17.         {
  18.             Generations(5);
  19.         }
  20.        
  21.         static void Generations(int n)
  22.         {
  23.             List<Creature> matingpool = new List<Creature>(new Creature[populationCount]);
  24.             population = CreatePopulation(population);
  25.            
  26.             for(int _ = 0; _ < n; _++)
  27.             {
  28.                 Console.WriteLine("\nGeneration: {0}", _);
  29.  
  30.                 for(int i = 0; i < population.Length; i++)
  31.                 {
  32.                     Creature c = population[i];
  33.                    
  34.                     c.Live();
  35.                    
  36.                     float cfit = Math.Abs(1/CheckDist(c.x, 0/*c.y*/));
  37.                    
  38.                     AddToMP(cfit, matingpool, c.dna, null);
  39.                 }
  40.                
  41.                 Creature bestCreature = GetNearestCreature(population);
  42.                
  43.                 foreach(Creature c in population)
  44.                     //Console.WriteLine(c.x);
  45.                
  46.                 AddToMP(null, matingpool, null, bestCreature.dna);
  47.                
  48.                 Console.WriteLine("Best Creature position: x={0}, y={1}", bestCreature.x, 0/*bestCreature.y*/);
  49.                 Console.WriteLine("Target position: x={0}, y={1}", tx, 0/*ty*/);
  50.                
  51.                 population = Reproduce(matingpool, population);
  52.             }
  53.         }
  54.        
  55.         static void AddToMP(float? chance, List<Creature> mp, List<float> cdna, List<float> bdna)
  56.         {
  57.             if(bdna == null)
  58.             {
  59.                 chance = (int) chance * 20;
  60.                 for(int i = 0; i < chance && mp.Count < population.Length; i++)
  61.                 {
  62.                     mp[i] = new Creature(cdna);
  63.                 }
  64.             }
  65.             else
  66.             {
  67.                 mp[0] = new Creature(bdna);
  68.                 while(mp.Count < population.Length)
  69.                 {
  70.                     mp.Add(new Creature(null));
  71.                 }
  72.             }
  73.         }
  74.        
  75.         static Creature[] Reproduce(List<Creature> mp, Creature[] popul)
  76.         {
  77.             for(int i = 0; i < mp.Count; i++)
  78.                 popul[i] = mp[i];
  79.             return popul;
  80.         }
  81.        
  82.         static Creature[] CreatePopulation(Creature[] pop)
  83.         {
  84.             for(int i = 0; i < populationCount; i++)
  85.             {
  86.                 pop[i] = new Creature(null);
  87.             }
  88.             return pop;
  89.         }
  90.        
  91.         static Creature GetNearestCreature(Creature[] popul)
  92.         {
  93.             float recordDist = 10000;
  94.             int recordCreatureIndex = 0;
  95.             for(int i = 0; i < popul.Length; i++)
  96.             {
  97.                 float distance = CheckDist(popul[i].x, popul[i].y);
  98.                 if(distance < recordDist)
  99.                 {
  100.                     recordDist = distance;
  101.                     recordCreatureIndex = i;
  102.                 }
  103.             }
  104.             return popul[recordCreatureIndex];
  105.         }
  106.        
  107.         static float CheckDist(float x, float y)
  108.         {
  109.             float dx = tx - x;
  110.             return dx;
  111.             //float dy = ty - y;
  112.             //float dist = (dx + dy) / 2;
  113.             //return dist;
  114.         }
  115.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement