Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Genesis.Library;
- namespace Genesis
- {
- public partial class Form1 : Form
- {
- private int[] n0 = new int[] {
- 0,0,1,1,0,0,
- 0,1,0,0,1,0,
- 0,1,0,0,1,0,
- 0,1,0,0,1,0,
- 0,1,0,0,1,0,
- 0,0,1,1,0,0
- };
- private int[] n1 = new int[] {
- 0,0,0,1,0,0,
- 0,0,1,1,0,0,
- 0,0,0,1,0,0,
- 0,0,0,1,0,0,
- 0,0,0,1,0,0,
- 0,0,1,1,1,0
- };
- private int[] n2 = new int[] {
- 0,0,1,1,0,0,
- 0,1,0,0,1,0,
- 0,0,0,0,1,0,
- 0,0,0,1,0,0,
- 0,0,1,0,0,0,
- 0,1,1,1,1,0
- };
- private int[] n3 = new int[] {
- 0,0,1,1,0,0,
- 0,1,0,0,1,0,
- 0,0,0,1,0,0,
- 0,0,0,1,0,0,
- 0,1,0,0,1,0,
- 0,0,1,1,0,0
- };
- private int[] n4 = new int[] {
- 0,0,0,0,1,0,
- 0,0,0,1,1,0,
- 0,0,1,0,1,0,
- 0,1,1,1,1,1,
- 0,0,0,0,1,0,
- 0,0,0,0,1,0
- };
- private int[] n5 = new int[] {
- 0,1,1,1,1,0,
- 0,1,0,0,0,0,
- 0,1,1,1,0,0,
- 0,0,0,0,1,0,
- 0,0,0,0,1,0,
- 0,1,1,1,0,0
- };
- private int[] n6 = new int[] {
- 0,0,1,1,1,0,
- 0,1,0,0,0,0,
- 0,1,1,1,0,0,
- 0,1,0,0,1,0,
- 0,1,0,0,1,0,
- 0,0,1,1,0,0
- };
- private int[] n7 = new int[] {
- 0,1,1,1,1,0,
- 0,0,0,0,1,0,
- 0,0,0,1,0,0,
- 0,0,0,1,0,0,
- 0,0,1,0,0,0,
- 0,0,1,0,0,0
- };
- private int[] n8 = new int[] {
- 0,0,1,1,0,0,
- 0,1,0,0,1,0,
- 0,1,1,1,1,0,
- 0,1,0,0,1,0,
- 0,1,0,0,1,0,
- 0,0,1,1,0,0
- };
- private int[] n9 = new int[] {
- 0,0,1,1,0,0,
- 0,1,0,0,1,0,
- 0,1,0,0,1,0,
- 0,0,1,1,1,0,
- 0,0,0,0,1,0,
- 0,1,1,1,0,0
- };
- private class Symbol
- {
- public int[] Pattern { get; set; }
- public int Value { get; set; }
- public Symbol(int[] pattern, int value)
- {
- Pattern = pattern;
- Value = value;
- }
- }
- private List<Symbol> symbols;
- private class Bot
- {
- public FeedForward Network { get; set; }
- public float Sureness { get; set; }
- public float Successes { get; set; }
- public float Fitness { get; set; }
- }
- private List<Bot> generation { get; set; }
- private int generationsize = 100;
- private int[] layers = new int[] { 36, 50, 15, 10 };
- public Form1()
- {
- InitializeComponent();
- symbols = new List<Symbol>();
- symbols.Add(new Symbol(n0, 0));
- symbols.Add(new Symbol(n1, 1));
- symbols.Add(new Symbol(n2, 2));
- symbols.Add(new Symbol(n3, 3));
- symbols.Add(new Symbol(n4, 4));
- symbols.Add(new Symbol(n5, 5));
- symbols.Add(new Symbol(n6, 6));
- symbols.Add(new Symbol(n7, 7));
- symbols.Add(new Symbol(n8, 8));
- symbols.Add(new Symbol(n9, 9));
- generation = new List<Bot>();
- for (int i = 0; i < generationsize; i++) {
- Bot bot = new Bot();
- bot.Network = FeedForward.Random(layers);
- generation.Add(bot);
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- Random random = new Random(Guid.NewGuid().GetHashCode());
- bool searching = true;
- while (searching) {
- // Test each
- foreach (Bot bot in generation) {
- bot.Sureness = 0;
- bot.Successes = 0;
- bot.Fitness = 0;
- for (int i = 0; i < symbols.Count; i++) {
- for (int j = 0; j < 36; j++) {
- bot.Network.Layers[0][j].Input[0].Value = (float)symbols[i].Pattern[j];
- }
- bot.Network.Schedule();
- int neuron = 0;
- float score = 0;
- for (int j = 0; j < bot.Network.Layers[3].Count; j++) {
- if (bot.Network.Layers[3][j].Output > score) {
- neuron = j;
- score = bot.Network.Layers[3][j].Output;
- }
- }
- if (neuron == symbols[i].Value) {
- float sum = 0f;
- for (int j = 0; j < symbols.Count; j++) {
- float output = bot.Network.Layers[3][j].Output > 0 ? bot.Network.Layers[3][j].Output : 0;
- sum += output;
- }
- bot.Sureness += bot.Network.Layers[3][neuron].Output / sum;
- bot.Successes++;
- }
- }
- bot.Fitness = bot.Successes * 4 + bot.Sureness;
- }
- // Sort by fitness
- var queryFitness = from bot in generation
- orderby bot.Fitness descending
- select bot;
- List<Bot> botsorted = queryFitness.ToList<Bot>();
- List<Bot> newgeneration = new List<Bot>();
- for (int i = 0; i < generationsize; i++) {
- Bot child = new Bot();
- Bot mate1 = botsorted[random.Next(0, (int)(generationsize * 0.1f))];
- Bot mate2 = botsorted[random.Next(0, (int)(generationsize * 0.1f))];
- string newgenome = FeedForward.Crossover(mate1.Network, mate2.Network);
- for (int j = 0; j < 10; j++) {
- newgenome = FeedForward.Mutate(newgenome);
- }
- child.Fitness = 0;
- child.Network = new FeedForward(layers, newgenome);
- newgeneration.Add(child);
- }
- generation = newgeneration;
- Console.WriteLine(botsorted[0].Successes + " | " + botsorted[0].Fitness);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement