Skip to content

Instantly share code, notes, and snippets.

@mrpossoms
Created March 24, 2014 15:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrpossoms/9741861 to your computer and use it in GitHub Desktop.
Save mrpossoms/9741861 to your computer and use it in GitHub Desktop.
Hello genetic algorithims.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C5;
namespace GeneticHelloWorld
{
class Program
{
public static List<individual> generation;
static string target = "helloworld";
const float mutatePercentage = .1f;
static Random r;
static void Main(string[] args)
{
generation = new List<individual>();
r = new Random();
init();
individual[] best = new individual[10];
while(best[9]== null || best[9].nameString() != target)
preformSelection(best);
Console.ReadLine();
}
private static void preformSelection(individual[] best)
{
for(int j =0; j<generation.count>
{
individual i = generation[j];
i = asessFitness(i);
if (best[0] == null || i.fitness > best[0].fitness)
best[0] = i;
// if (i.fitness > 0)
// Console.WriteLine();
best = sort(best);
}
foreach (char c in best[9].name)
Console.Write(c);
Console.Write(": " + best[9].fitness);
Console.WriteLine();
//new population
generation = new List<individual>();
foreach (individual i in best)
generation.Add(i);
for (int i = 0; i < best.Length; i++)
for (int j = 0; j < 90 / best.Length; j++)
generation.Add(mutate(best[i]));
}
private static individual mutate(individual ind)
{
int[] indexes = new int[(int)(ind.name.Length * mutatePercentage)];
individual nind = new individual(target);
for(int i = 0; i < target.Length; i++)
nind.name[i] = ind.name[i];
for (int i = 0; i < indexes.Length; i++)
indexes[i] = r.Next() % ind.name.Length;
for (int i = 0; i < indexes.Length; i++)
{
nind.name[indexes[i]] = randomLetter();
}
return nind;
}
private static void init()
{
for (int i = 0; i < 100; i++)
{
individual ind = new individual(target);
for (int j = 0; j < target.Length; j++)
ind.name[j] = randomLetter();
generation.Add(ind);
}
}
public static individual[] sort(individual[] population)
{
for (int i = 0; i < population.Length; i++)
{
if(i < population.Length - 1)
if (population[i+1] == null || population[i].fitness > population[i + 1].fitness)
{
individual temp = population[i].clone();
population[i] = population[i + 1];
population[i + 1] = temp.clone();
}
}
return population;
}
public static individual asessFitness(individual ind)
{
int correct = 0;
for (int i = 0; i < ind.name.Length; i++)
if (ind.name[i] == target[i])
correct++;
ind.fitness = (float)correct / (float)target.Length;
return ind;
}
public static char randomLetter()
{
// 97 - 122
return (char)(97 + r.Next() % 25);
}
public class individual
{
public char[] name;
public float fitness;
public individual(string name)
{
this.name = new char[name.Length];
fitness = 0;
}
public string nameString()
{
string name = "";
foreach (Char c in this.name)
name += c;
return name;
}
public override string ToString()
{
string name = "";
foreach (Char c in this.name)
name += c;
return name + ": " + fitness.ToString();
}
public individual clone()
{
individual i = new individual(target);
i.fitness = fitness;
if(name!=null)
for (int c =0; c<target.length>
i.name[c] = name[c];
return i;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment