public void SetNeat() { neat = GameObject.Find("Scripts").GetComponent <Neat>(); }
private void window_keydown(object sender, KeyEventArgs e) { if (keydown.Contains(e.KeyCode) || e.KeyCode == Keys.NumLock || e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu) { return; } else { keydown.Add(e.KeyCode); } Keys ekey = e.KeyCode; if (e.KeyCode == Keys.Delete) { ekey = Keys.Decimal; } else if (e.KeyCode == Keys.Insert) { ekey = Keys.NumPad0; } else if (e.KeyCode == Keys.Clear) { ekey = Keys.NumPad5; } else if (e.KeyCode == Keys.F2) { ContrastMode = !ContrastMode; return; } else if (e.KeyCode == Keys.F3) { if (state == States.NEWGAME && Neat.All.Count < 24) { Neat n = new Neat(); n.SetupGenZero(); n.AddKey(); IKey.UpdateAll(); Map.SetMaxPoints(); } return; } else if (e.KeyCode == Keys.F1) { if (state != States.NEWGAME) { return; } if (ActiveKeys.Count > 0) { new AI(ActiveKeys[ActiveKeys.Count - 1]); } MVP.Flash($"AI added to {HEADS[ActiveKeys[ActiveKeys.Count - 1]].DisplayKey}"); return; } else if (e.KeyCode == Keys.F4 && !e.Alt) { if (state == States.NEWGAME) { if (Neat.All.Count > 0) { Neat toRemove = Neat.All.Last(); toRemove.Remove(); IKey.UpdateAll(); } Map.SetMaxPoints(); } return; } else if (e.KeyCode == Keys.F9 && e.Alt) { if (ActiveKeys.Count == 0 || InactiveKeys.Count == 0) { return; } HEADS[InactiveKeys[0]].Reward(0, ActiveKeys[0]); HEADS[ActiveKeys[0]].Die(); return; } else if (e.KeyCode == Keys.F10) { if (SyncUpdate) { SyncUpdate = false; UpdateThread.Stop(); NeuralThread.Stop(); StartAsyncUpdate(); } else { SyncUpdate = true; UpdateThread.Start(); NeuralThread.Start(); } return; } else if (e.KeyCode == Keys.F7) { SlowMo = true; return; } else if (e.KeyCode == Keys.F8) { SpeedMo = true; return; } else if (e.KeyCode == Keys.F11) { FullScreen = !FullScreen; if (FullScreen) { Width = Screen.PrimaryScreen.Bounds.Width; FormBorderStyle = FormBorderStyle.None; WindowState = FormWindowState.Maximized; Cursor.Hide(); } else { Width = 1024; Height = 600; FormBorderStyle = FormBorderStyle.Sizable; WindowState = FormWindowState.Normal; Cursor.Show(); } return; } else if (e.KeyCode == Keys.F12) { if (!e.Alt) { if (map is BotArena) { List <byte> bytes = new List <byte>(); foreach (Neat bot in ((BotArena)map).bots) { bytes.AddRange(Neat.compile(bot)); } SaveFileDialog dialog = new SaveFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { File.WriteAllBytes(dialog.FileName, bytes.ToArray()); } } else if (ActiveKeys.Count == 0) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Filter = "Bot files (*.bot)|*.bot|All files (*.*)|*.*"; dialog.RestoreDirectory = true; if (dialog.ShowDialog() == DialogResult.OK) { List <byte> bytes = new List <byte>(File.ReadAllBytes(dialog.FileName)); BotArena arena = new BotArena(BotArena.Type.MAX_POINTS); map = arena; while (bytes.Count > 0) { arena.bots.Add(Neat.decompile(bytes)); } arena.AddBots(false); Map.StartGame(); Ingame = true; state = States.INGAME; } } } else if (state == States.NEWGAME && !TutorialActive) { if (ActiveKeys.Count != 1) { return; } TUTO = new Tutorial(Map); TutorialActive = true; Map.StartGame(); if (SyncUpdate) { NeuralThread.Start(); } } return; // trigger tutorial } else if (e.KeyCode == Keys.F6) { if (ActiveKeys.Count != 0) { return; } BotArena arena = new BotArena(7, BotArena.Type.MAX_POINTS); /// < - - - - - - - - - - - - - - - - map = arena; arena.AddBots(false); Map.StartGame(); Ingame = true; state = States.INGAME; return; // trigger bot arena } else if (e.KeyCode == Keys.F5) { if (state != States.NEWGAME) { return; } switch (Gamemode) { case Gamemodes.CLASSIC: MVP.SetText("Red chaos"); Gamemode = Gamemodes.CHAOS_RED; foreach (Head head in HEADS.Values) { head.NewColor(true); } break; case Gamemodes.CHAOS_RED: MVP.SetText("Rainbow chaos"); Gamemode = Gamemodes.CHAOS_RAINBOW; foreach (Head head in HEADS.Values) { head.NewColor(); } break; case Gamemodes.CHAOS_RAINBOW: MVP.SetText("King of the hill"); foreach (Head head in HEADS.Values) { head.NewColor(false); } Map.MaxOrbs = 7; Gamemode = Gamemodes.KING_OF_THE_HILL; break; case Gamemodes.KING_OF_THE_HILL: MVP.SetText("Yeet mode"); Map.MaxOrbs = 8; Gamemode = Gamemodes.YEET_MODE; break; case Gamemodes.YEET_MODE: MVP.SetText("Classic"); Map.MaxOrbs = 255; Gamemode = Gamemodes.CLASSIC; break; } UpdateColors?.Invoke(); return; } switch (state) { case States.NEWGAME: //add new player if (e.KeyCode == Keys.Enter && ActiveKeys.Count > 0) // (re)start game { if (e.Shift) { Map.ResumeGame(); } else { Map.StartGame(); } if (SyncUpdate) { NeuralThread.Start(); } Ingame = true; state = States.INGAME; } else if (e.KeyCode == Keys.Escape) // clear all keys { HashSet <Neat> NeatCopy = new HashSet <Neat>(Neat.All); foreach (Neat neat in NeatCopy) { neat.Remove(); } HashSet <Head> HeadCopy = new HashSet <Head>(HEADS.Values); foreach (Head head in HeadCopy) { head.Remove(); } Map.MaxPoints = 0; Leader = Keys.None; } else if (0 <= e.KeyValue && e.KeyValue < 256) //add key { lock (ActiveLock) { if (ActiveKeys.Contains(ekey)) { HEADS[ekey].Remove(); } else { if (HEADSOnPause.ContainsKey(ekey)) { HEADS.Add(ekey, new Head(HEADSOnPause[ekey])); } else { HEADS.Add(ekey, new Head(ekey)); } ActiveKeys.Add(ekey); Map.SetMaxPoints(); } } IKey.UpdateAll(); } break; case States.INGAME: if (e.KeyCode == Keys.Escape) //pause game { ApplicationPause(); } else if (ActiveKeys.Contains(ekey)) //default action { HEADS[ekey].Action(); } break; case States.PAUSED: if (e.KeyCode == Keys.Escape) // unpause { state = States.INGAME; Ingame = true; if (SyncUpdate) { UpdateThread.Start(); NeuralThread.Start(); } else { StartAsyncUpdate(); } VisualThread.Start(); //ForcePaused = false; } else if (e.KeyCode == Keys.Enter) // let players join { HEADSOnPause.Clear(); foreach (KeyValuePair <Keys, Head> player in HEADS) { HEADSOnPause.Add(player.Key, player.Value); } Map.EndGame(); Map.Clear(); Map.phase = Phases.NONE; state = States.NEWGAME; MVP.Show("Prepare!"); lock (ActiveLock) foreach (Keys key in ActiveKeys) { HEADS[key].v = IVector.Up; } Ingame = false; VisualThread.Start(); if (SyncUpdate) { UpdateThread.Start(); } else { StartAsyncUpdate(); } } break; } }
private void ExecuteNeat() { bool wait = false; int inputCount = 2; int outputCount = 1; int delay = 150; Dispatcher.Invoke(() => { controlsToDisable.ForEach(x => x.IsEnabled = false); controlsToEnable.ForEach(x => x.IsEnabled = true); if (CbAnimate.IsChecked == true) { wait = true; } inputCount = int.TryParse(TbInputs.Text, out inputCount) ? inputCount : 2; outputCount = int.TryParse(TbOutputs.Text, out outputCount) ? outputCount : 1; delay = int.TryParse(TbDelay.Text, out delay) ? delay : 0; }); Neat neat = new Neat(); neat.OnGenerationEnd += (a) => { Dispatcher.Invoke(() => { TBSynapseData.Clear(); a.synapses.ForEach(x => { TBSynapseData.Text += "FromLayer: " + x.FromLayer + "\n"; TBSynapseData.Text += "Fromneuron: " + x.FromNeuron + "\n"; TBSynapseData.Text += "ToLayer: " + x.ToLayer + "\n"; TBSynapseData.Text += "ToNeuron: " + x.ToNeuron + "\n"; TBSynapseData.Text += "Weight: " + x.Weight + "\n"; TBSynapseData.Text += "\n"; }); TBNeuronData.Clear(); a.hiddenNeurons.ForEach(x => { TBNeuronData.Text += "Layer: " + x.Layer + "\n"; TBNeuronData.Text += "NeuronPosition: " + x.NeuronPosition + "\n"; TBNeuronData.Text += "Bias: " + x.Bias + "\n"; TBNeuronData.Text += "\n"; }); TBOutputNeurons.Clear(); a.outputNeurons.ToList().ForEach(x => { TBOutputNeurons.Text += "Value: " + x.Value + "\n"; TBOutputNeurons.Text += "\n"; }); }); if (wait) { DrawAnn(a); Thread.Sleep(delay); } while (pause) { } neat.LowerWaitFlag(); }; string[] inputColumns = new string[] { //"movie_title", "genres", "director_name", "budget", }; string[] expectedOutputColumns = new string[] { "gross", }; NormalizedDataAndDictionaries inputData = GetDataFromDatabase(inputColumns); NormalizedDataAndDictionaries outputData = GetDataFromDatabase(expectedOutputColumns); //Ann ann = neat.Train(10000, 100, 0.1, 3, 30, wait, inputs, expectedOutputs, Crossover.TwoPointCrossover, ActivationFunction.Identity); //Ann ann = neat.Train(1000, 100, 0.03, 3, 30, false, inputs, expectedOutputs, Crossover.TwoPointCrossover, ActivationFunction.Identity); //Ann ann = neat.Train(1000, 100, 0.03, 3, 30, false, inputData.NormalizedData, outputData.NormalizedData, Crossover.BestParentClone, ActivationFunction.Sigmoid); Ann ann = neat.Train(1000, 100, 0.03, 3, 30, false, inputData.NormalizedData, outputData.NormalizedData, Crossover.BestParentClone, ActivationFunction.Identity); Dispatcher.Invoke(() => { controlsToDisable.ForEach(x => x.IsEnabled = true); controlsToEnable.ForEach(x => x.IsEnabled = false); SVCanvas.Background = new SolidColorBrush(Colors.Gray); }); if (!wait) { DrawAnn(ann); } }
public static void ProperTestRun() { EvolutionaryAlgorithm neat = new Neat(); neat.InitializePopulation(); bool solvedit = false; for (int i = 0; i < EAParameters.MaximumRuns; i++) { Console.WriteLine("generation:" + i); neat.EvaluatePopulation(i); if (neat.IsDeadRun()) { Console.WriteLine("death"); break; } if (neat.SolvedIt()) { Console.WriteLine("obtained maximum fitness"); solvedit = true; break; } neat.NextGeneration(); } Console.WriteLine("best individual in existence:"); var best = ((Neat)neat).bestAchieved; Console.WriteLine("individualID: {0} generation:{1}", best.individualID, ((Neat)neat).generationOfBest); Console.WriteLine("\tFitness:{0}", best.Fitness); Console.WriteLine("\tstats:"); Console.WriteLine("\t\tnodes:{0}", best.genome.nodeGenes.Count); Console.Write("\t\tConnections:{0}", best.genome.connectionGenes.Count); int count = 0; foreach (var gene in best.genome.connectionGenes) { if (gene.isEnabled) { count++; } } Console.WriteLine(" enabled:{0}", count); Console.WriteLine("biggest individual in existence:"); var biggest = ((Neat)neat).biggestAchieved; Console.WriteLine("individualID: {0} generation:{1}", biggest.individualID, ((Neat)neat).generationOfBiggest); Console.WriteLine("\tFitness:{0}", biggest.Fitness); Console.WriteLine("\tstats:"); Console.WriteLine("\t\tnodes:{0}", biggest.genome.nodeGenes.Count); Console.Write("\t\tConnections:{0}", biggest.genome.connectionGenes.Count); count = 0; foreach (var gene in biggest.genome.connectionGenes) { if (gene.isEnabled) { count++; } } Console.WriteLine(" enabled:{0}", count); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("so what the best do?"); Console.WriteLine("wanted_gotten rule-results:"); foreach (var rule in LambdaExperiment.ruleSet) { Console.WriteLine("rule:{0} _ best:{1}", rule.Result, best.network.GetNextState(rule)); ((CPPNetwork)(best.network)).ResetNetwork(); } Console.WriteLine(); Console.WriteLine("so what the biggest do?"); Console.WriteLine("wanted_gotten rule-results:"); foreach (var rule in LambdaExperiment.ruleSet) { Console.WriteLine("rule:{0} _ best:{1}", rule.Result, best.network.GetNextState(rule)); ((CPPNetwork)(biggest.network)).ResetNetwork(); } Console.WriteLine(); if (solvedit) { Console.WriteLine("your perfect genome is ready for you in {0}", @"C:/genes"); } }
public void SetGeneration(List <byte> list) { generation = BitConverter.ToInt64(Neat.remove(list, 8), 0); }
public static void Main() { Console.WriteLine("Initializing"); Neat.Initialize(); Console.WriteLine("Initialization Complete"); for (;;) { Neat.DoGeneration(); Console.Read(); } /* * Console.WriteLine("Creating new genome"); * Genome genome01 = new Genome(3, 5); * Console.WriteLine("Populating genome"); * uint[] geneList = new uint[10]{9,18,14,19,20,26,27,42,43,52}; * for(int i = 0; i < geneList.Length; i++) * { * genome01.contents.Add(geneList[i], 1); * } * Console.WriteLine(genome01); * Console.WriteLine("Compiling network"); * Network net01 = genome01.Compile(); * Console.WriteLine("Network compilation complete"); * * Console.WriteLine("Inspecting network"); * foreach(KeyValuePair<uint, Neuron> node in net01.neurons) * { * Console.WriteLine(" NodeID={0}", node.Key); * Console.WriteLine(" Edges={0}", node.Value.from.Length); * for(int i = 0; i < node.Value.from.Length; i++) * { * Console.WriteLine(" Weight={0}", node.Value.weight[i]); * } * } * * Console.WriteLine("Beginning neuron test cases"); * float[][] testInput01 = new float[8][]; * testInput01[0] = new float[3]{0,0,0}; * testInput01[1] = new float[3]{0,0,1}; * testInput01[2] = new float[3]{0,1,0}; * testInput01[3] = new float[3]{0,1,1}; * testInput01[4] = new float[3]{1,0,0}; * testInput01[5] = new float[3]{1,0,1}; * testInput01[6] = new float[3]{1,1,0}; * testInput01[7] = new float[3]{1,1,1}; * for(int i = 0; i < testInput01.Length; i++) * { * //Console.WriteLine("Flushing network"); * //net01.FlushStates(); * Console.WriteLine("Setting test input {0}", i); * net01.SetInputs(testInput01[i]); * Console.WriteLine("Calculating network state"); * net01.Iterate(); * Console.WriteLine("Reading network output"); * float[] outputs = net01.GetOutputs(); * for(int j = 0; j < 5; j++) * { * Console.Write(",{0}", outputs[j]); * } * Console.WriteLine(); * } * //*/ /* * uint from; * uint to; * for(;;) * { * Console.Write("From: "); * from = uint.Parse(Console.ReadLine()); * Console.Write("To: "); * to = uint.Parse(Console.ReadLine()); * Console.WriteLine("Pair Index: {0}", Genome.Pair(from, to)); * } * //*/ }