Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace GENETIW_ALGRIW
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. long start = 0;
  14. int generations = 5000;
  15.  
  16. Random rnd = new Random();
  17. string[] alphabet = { " ", ",", ".", "!", "(", ")", ":", "”", "?", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "å", "ä", "ö" };
  18.  
  19. int populationSize = 1000;
  20. int mutationStrength = 1;
  21.  
  22. string text = "För att en mening ska vara fullständig ska den innehålla en eller flera satser (en sats ska innehålla subjekt och predikat, se satsdelar). En mening behöver inte vara lång för att kallas fullständig, till exempel: ”Han är trött.”, är en fullständig mening. Däremot är inte ”Han är.” en fullständig mening då den inte är nog begriplig.".ToLower();
  23.  
  24. Pop[] population = new Pop[populationSize];
  25.  
  26. // Generate random genetic start material.
  27. string temp;
  28. for(int i = 0; i < populationSize; i++)
  29. {
  30. temp = "";
  31. for(int j = 0; j < text.Length; j++)
  32. {
  33. temp += alphabet[rnd.Next(0, alphabet.Length)];
  34. }
  35. population[i] = new Pop(temp);
  36. }
  37.  
  38. start = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  39.  
  40. int score;
  41.  
  42. // Main loop.
  43. for (int gen = 0; gen < generations; gen++)
  44. {
  45. // Calculate fitness. Each pop
  46. for (int i = 0; i < populationSize; i++)
  47. {
  48. score = 0;
  49. // Each char
  50. for (int j = 0; j < text.Length; j++)
  51. {
  52. if (population[i].Text[j] == text[j])
  53. {
  54. score += 3;
  55. }
  56. else if (text.Contains(population[i].Text[j]))
  57. {
  58. score += 1;
  59. }
  60. population[i].Fitness = score;
  61. }
  62. }
  63.  
  64. // Order by fitness.
  65. population = population.OrderByDescending(p => p.Fitness).ToArray();
  66. Console.ForegroundColor = ConsoleColor.Yellow;
  67. Console.Write("Generation: ");
  68. Console.ForegroundColor = ConsoleColor.White;
  69. Console.Write(gen + 1);
  70. Console.ForegroundColor = ConsoleColor.Yellow;
  71. Console.Write(" Fitness: ");
  72. Console.ForegroundColor = ConsoleColor.White;
  73. Console.Write(population[0].Fitness);
  74. Console.ForegroundColor = ConsoleColor.Yellow;
  75. Console.Write(" Text: ");
  76. Console.ForegroundColor = ConsoleColor.White;
  77. Console.WriteLine(population[0].Text);
  78.  
  79. //Create crossover / Mutation
  80. Pop[] tempPop = new Pop[populationSize];
  81. char[] t;
  82.  
  83. for (int i = 0; i < populationSize; i++)
  84. {
  85. tempPop[i] = new Pop(population[0].Text);
  86.  
  87. for (int j = 0; j < text.Length; j++)
  88. {
  89. t = tempPop[i].Text.ToCharArray();
  90. if (rnd.Next(0, 100) < mutationStrength)
  91. {
  92. t[j] = alphabet[rnd.Next(0, alphabet.Length)][0];
  93. string s = "";
  94.  
  95. for (int k = 0; k < text.Length; k++)
  96. {
  97. s += t[k].ToString();
  98. }
  99. tempPop[i].Text = s;
  100. break;
  101. }
  102. }
  103. }
  104. population = tempPop;
  105.  
  106. if (population[0].Text == text)
  107. break;
  108. }
  109.  
  110. Console.WriteLine("Total time: " + ((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - start).ToString() + " ms.");
  111. Console.ReadKey();
  112. }
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement