Beispiel #1
0
        public NeuralNet Load(string file)
        {
            NeuralNet     ret       = new NeuralNet();
            XmlSerializer formatter = new XmlSerializer(ret.GetType());
            FileStream    nFile     = new FileStream(file, FileMode.Open);

            byte[] buffer = new byte[nFile.Length];
            nFile.Read(buffer, 0, (int)nFile.Length);
            MemoryStream stream = new MemoryStream(buffer);

            return((NeuralNet)formatter.Deserialize(stream));
        }
Beispiel #2
0
        private void update()
        {
            //restart
            if (Global.restartBots == true)
            {
                restartBots(bots);
            }

            //find bestC
            Cop bestC;
            int best = FindIndexOfMostAdvancedPos();

            bestC = bots[best];
            if (lastBestBot != null && bestC.Position.X > lastBestBot.Position.X)
            {
                lastBestBot = bestC;
            }
            else
            {
                if (lastBestBot != null)
                {
                    lastBestBot.Position = bestC.Position;
                    bestC = lastBestBot;
                    bestC.nn.mutate();
                }
                ;
            }
            if (lastBestBot == null)
            {
                lastBestBot = bestC;
            }


            if (allDead())
            {
                for (int i = 0; i < bots.Count; i++)
                {
                    bots[i].Position = new Vector2(0, -100);
                    bots[i].nn       = new NeuralNet(lastBestBot.nn);
                    bots[i].nn.mutate();
                    bots[i].dead           = false;
                    Global.copList[i].dead = false;
                    Tools.pm.changeSpeed(bots[i].ID, Vector2.Zero);
                    lastBestBot.Position = new Vector2(-999999);
                }
                return;
            }


            Global.cam = bestC.cam;
            for (int i = 0; i < bots.Count; i++)
            {
                if (bots[i].dead == true)
                {
                    bots[i].Position = new Vector2(bestC.Position.X, bestC.Position.Y);
                    NeuralNet nnT = new NeuralNet(bestC.nn);
                    nnT.mutate();
                    bots[i].nn = nnT;
                    Tools.pm.changeSpeed(bots[i].ID, Tools.pm.getEngine(bestC.ID).velocity);
                }
            }

            if (cntRound == amount_of_runs)
            {
                cntRound = 0;//restart counter

                //List<int> worstI = Find_index_of_worst(5);
                //for (int i = 0; i < worstI.Count; i++)
                //{
                //    bots[worstI[i]].kill();
                //    Tools.pm.createNewEngine(bots[worstI[i]].ID, Tools.pm.getEngine(bestC.ID));
                //    bots[worstI[i]] = new Cop(bestC.nn, "masks/policeCarM", copArray, new BotKeys(), bots[worstI[i]].ID, null,
                //copF, new Vector2(bestC.Position.X, bestC.Position.Y), null, Color.White, 0, new Vector2(730, 1440),
                //       new Vector2(0.1f), 0, 0.1f, Tools.pm, 1.2f);
                //}
                //File.WriteAllText("saveNN.txt", bestC.nn.ToString());
                using (StreamWriter sw = File.AppendText("saveNN.txt"))
                {
                    sw.WriteLine(bestC.nn.ToString());
                }
            }

            else
            {
                cntRound++;
            }
        }