Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace GENETIW_ALGRIW
- {
- class Program
- {
- static void Main(string[] args)
- {
- long start = 0;
- int generations = 5000;
- Random rnd = new Random();
- 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", "å", "ä", "ö" };
- int populationSize = 1000;
- int mutationStrength = 1;
- 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();
- Pop[] population = new Pop[populationSize];
- // Generate random genetic start material.
- string temp;
- for(int i = 0; i < populationSize; i++)
- {
- temp = "";
- for(int j = 0; j < text.Length; j++)
- {
- temp += alphabet[rnd.Next(0, alphabet.Length)];
- }
- population[i] = new Pop(temp);
- }
- start = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
- int score;
- // Main loop.
- for (int gen = 0; gen < generations; gen++)
- {
- // Calculate fitness. Each pop
- for (int i = 0; i < populationSize; i++)
- {
- score = 0;
- // Each char
- for (int j = 0; j < text.Length; j++)
- {
- if (population[i].Text[j] == text[j])
- {
- score += 3;
- }
- else if (text.Contains(population[i].Text[j]))
- {
- score += 1;
- }
- population[i].Fitness = score;
- }
- }
- // Order by fitness.
- population = population.OrderByDescending(p => p.Fitness).ToArray();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write("Generation: ");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(gen + 1);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write(" Fitness: ");
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write(population[0].Fitness);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.Write(" Text: ");
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine(population[0].Text);
- //Create crossover / Mutation
- Pop[] tempPop = new Pop[populationSize];
- char[] t;
- for (int i = 0; i < populationSize; i++)
- {
- tempPop[i] = new Pop(population[0].Text);
- for (int j = 0; j < text.Length; j++)
- {
- t = tempPop[i].Text.ToCharArray();
- if (rnd.Next(0, 100) < mutationStrength)
- {
- t[j] = alphabet[rnd.Next(0, alphabet.Length)][0];
- string s = "";
- for (int k = 0; k < text.Length; k++)
- {
- s += t[k].ToString();
- }
- tempPop[i].Text = s;
- break;
- }
- }
- }
- population = tempPop;
- if (population[0].Text == text)
- break;
- }
- Console.WriteLine("Total time: " + ((DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond) - start).ToString() + " ms.");
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement