Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using Genesis.Library;
  11.  
  12. namespace Genesis
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private int[] n0 = new int[] {
  17.             0,0,1,1,0,0,
  18.             0,1,0,0,1,0,
  19.             0,1,0,0,1,0,
  20.             0,1,0,0,1,0,
  21.             0,1,0,0,1,0,
  22.             0,0,1,1,0,0
  23.         };
  24.         private int[] n1 = new int[] {
  25.             0,0,0,1,0,0,
  26.             0,0,1,1,0,0,
  27.             0,0,0,1,0,0,
  28.             0,0,0,1,0,0,
  29.             0,0,0,1,0,0,
  30.             0,0,1,1,1,0
  31.         };
  32.         private int[] n2 = new int[] {
  33.             0,0,1,1,0,0,
  34.             0,1,0,0,1,0,
  35.             0,0,0,0,1,0,
  36.             0,0,0,1,0,0,
  37.             0,0,1,0,0,0,
  38.             0,1,1,1,1,0
  39.         };
  40.         private int[] n3 = new int[] {
  41.             0,0,1,1,0,0,
  42.             0,1,0,0,1,0,
  43.             0,0,0,1,0,0,
  44.             0,0,0,1,0,0,
  45.             0,1,0,0,1,0,
  46.             0,0,1,1,0,0
  47.         };
  48.         private int[] n4 = new int[] {
  49.             0,0,0,0,1,0,
  50.             0,0,0,1,1,0,
  51.             0,0,1,0,1,0,
  52.             0,1,1,1,1,1,
  53.             0,0,0,0,1,0,
  54.             0,0,0,0,1,0
  55.         };
  56.         private int[] n5 = new int[] {
  57.             0,1,1,1,1,0,
  58.             0,1,0,0,0,0,
  59.             0,1,1,1,0,0,
  60.             0,0,0,0,1,0,
  61.             0,0,0,0,1,0,
  62.             0,1,1,1,0,0
  63.         };
  64.         private int[] n6 = new int[] {
  65.             0,0,1,1,1,0,
  66.             0,1,0,0,0,0,
  67.             0,1,1,1,0,0,
  68.             0,1,0,0,1,0,
  69.             0,1,0,0,1,0,
  70.             0,0,1,1,0,0
  71.         };
  72.         private int[] n7 = new int[] {
  73.             0,1,1,1,1,0,
  74.             0,0,0,0,1,0,
  75.             0,0,0,1,0,0,
  76.             0,0,0,1,0,0,
  77.             0,0,1,0,0,0,
  78.             0,0,1,0,0,0
  79.         };
  80.         private int[] n8 = new int[] {
  81.             0,0,1,1,0,0,
  82.             0,1,0,0,1,0,
  83.             0,1,1,1,1,0,
  84.             0,1,0,0,1,0,
  85.             0,1,0,0,1,0,
  86.             0,0,1,1,0,0
  87.         };
  88.         private int[] n9 = new int[] {
  89.             0,0,1,1,0,0,
  90.             0,1,0,0,1,0,
  91.             0,1,0,0,1,0,
  92.             0,0,1,1,1,0,
  93.             0,0,0,0,1,0,
  94.             0,1,1,1,0,0
  95.         };
  96.  
  97.         private class Symbol
  98.         {
  99.             public int[] Pattern { get; set; }
  100.             public int Value { get; set; }
  101.             public Symbol(int[] pattern, int value)
  102.             {
  103.                 Pattern = pattern;
  104.                 Value = value;
  105.             }
  106.         }
  107.  
  108.         private List<Symbol> symbols;
  109.  
  110.         private class Bot
  111.         {
  112.             public FeedForward Network { get; set; }
  113.             public float Sureness { get; set; }
  114.             public float Successes { get; set; }
  115.             public float Fitness { get; set; }
  116.         }
  117.  
  118.         private List<Bot> generation { get; set; }
  119.         private int generationsize = 100;
  120.         private int[] layers = new int[] { 36, 50, 15, 10 };
  121.  
  122.         public Form1()
  123.         {
  124.             InitializeComponent();
  125.             symbols = new List<Symbol>();
  126.             symbols.Add(new Symbol(n0, 0));
  127.             symbols.Add(new Symbol(n1, 1));
  128.             symbols.Add(new Symbol(n2, 2));
  129.             symbols.Add(new Symbol(n3, 3));
  130.             symbols.Add(new Symbol(n4, 4));
  131.             symbols.Add(new Symbol(n5, 5));
  132.             symbols.Add(new Symbol(n6, 6));
  133.             symbols.Add(new Symbol(n7, 7));
  134.             symbols.Add(new Symbol(n8, 8));
  135.             symbols.Add(new Symbol(n9, 9));
  136.             generation = new List<Bot>();
  137.             for (int i = 0; i < generationsize; i++) {
  138.                 Bot bot = new Bot();
  139.                 bot.Network = FeedForward.Random(layers);
  140.                 generation.Add(bot);
  141.             }
  142.         }
  143.  
  144.         private void Form1_Load(object sender, EventArgs e)
  145.         {
  146.             Random random = new Random(Guid.NewGuid().GetHashCode());
  147.             bool searching = true;
  148.             while (searching) {
  149.                 // Test each
  150.                 foreach (Bot bot in generation) {
  151.                     bot.Sureness = 0;
  152.                     bot.Successes = 0;
  153.                     bot.Fitness = 0;
  154.                     for (int i = 0; i < symbols.Count; i++) {
  155.                         for (int j = 0; j < 36; j++) {
  156.                             bot.Network.Layers[0][j].Input[0].Value = (float)symbols[i].Pattern[j];
  157.                         }
  158.                         bot.Network.Schedule();
  159.                         int neuron = 0;
  160.                         float score = 0;
  161.                         for (int j = 0; j < bot.Network.Layers[3].Count; j++) {
  162.                             if (bot.Network.Layers[3][j].Output > score) {
  163.                                 neuron = j;
  164.                                 score = bot.Network.Layers[3][j].Output;
  165.                             }
  166.                         }
  167.                         if (neuron == symbols[i].Value) {
  168.                             float sum = 0f;
  169.                             for (int j = 0; j < symbols.Count; j++) {
  170.                                 float output = bot.Network.Layers[3][j].Output > 0 ? bot.Network.Layers[3][j].Output : 0;
  171.                                 sum += output;
  172.                             }
  173.                             bot.Sureness += bot.Network.Layers[3][neuron].Output / sum;
  174.                             bot.Successes++;
  175.                         }
  176.                     }
  177.                     bot.Fitness = bot.Successes * 4 + bot.Sureness;
  178.                 }
  179.                 // Sort by fitness
  180.                 var queryFitness = from bot in generation
  181.                                    orderby bot.Fitness descending
  182.                                    select bot;
  183.                 List<Bot> botsorted = queryFitness.ToList<Bot>();
  184.                 List<Bot> newgeneration = new List<Bot>();
  185.                 for (int i = 0; i < generationsize; i++) {
  186.                     Bot child = new Bot();
  187.                     Bot mate1 = botsorted[random.Next(0, (int)(generationsize * 0.1f))];
  188.                     Bot mate2 = botsorted[random.Next(0, (int)(generationsize * 0.1f))];
  189.                     string newgenome = FeedForward.Crossover(mate1.Network, mate2.Network);
  190.                     for (int j = 0; j < 10; j++) {
  191.                         newgenome = FeedForward.Mutate(newgenome);
  192.                     }
  193.                     child.Fitness = 0;
  194.                     child.Network = new FeedForward(layers, newgenome);
  195.                     newgeneration.Add(child);
  196.                 }
  197.                 generation = newgeneration;
  198.                 Console.WriteLine(botsorted[0].Successes + " | " + botsorted[0].Fitness);
  199.             }
  200.         }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement