Beispiel #1
0
 void Restart()
 {
     isRestart            = true;
     timer1.Enabled       = false;
     timerdanbay.Enabled  = false;
     timerbongbay.Enabled = false;
     superfood            = new SuperFood();
     s        = 2;
     tatspace = true;
     dan1     = new Dan1();
     dan2     = new Dan2();
     dan3     = new Dan3();
     sodan    = 2; dem = 0;
     diemn    = score; //if (diemcao < diemn) { diemcao = diemn;CustomMsgBox.Show("Có muốn lưu danh sử sách hay không? :)))","Bạn Đạt Điểm Cao", "Ok", "Để lần sau"); toolStripStatusLabelpro.Text = caothu; }
     //MessageBox.Show("Thua rồi :)) choi lai de");
     start.Text = "Bấm phím cách để bắt đầu trò chơi";
     score      = 0;
     chanbay    = new ChanBay();
     bongbay    = new BongBay();
     bong       = new Bong();
     van        = new Van();
     diem.Text  = "Điểm:";
     network.breedNetworks(gameIndex);
     while (true)
     {
         if (DataGame.IsAllGameFailed())
         {
             Thread.Sleep(500);
             DataGame.ResetGameState();
             for (int i = 0; i < 5; i++)
             {
                 if (gameIndex == i)
                 {
                     Thread.Sleep(i * 200);
                 }
             }
             watch          = Stopwatch.StartNew();
             timer1.Enabled = true;
             left           = false; right = false;
             start.Text     = "";
             tatspace       = false;
             //SendKey("B");
             break;
         }
         Thread.Sleep(50);
     }
 }
Beispiel #2
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            int populationSize = 10;

            DataGame.InnitGameState(populationSize);
            DataGame[] lstDataGame   = new DataGame[populationSize];
            ANN        NeuralNetwork = new ANN(ref lstDataGame, populationSize);

            NeuralNetwork.createFirstGeneration();
            for (int i = 0; i < lstDataGame.Length - 1; i++)
            {
                Task.Run(() =>
                {
                    App(ref NeuralNetwork, i);
                });
                Thread.Sleep(1000);
            }
            Application.Run(new Form1(ref NeuralNetwork, lstDataGame.Length - 1));
        }
Beispiel #3
0
        public void breedNetworks(int gameIndex)
        {
            // updates the score
            weightsList[gameIndex].fitness = lstgame[gameIndex].GameTime + lstgame[gameIndex].Score;

            averageFitness += weightsList[gameIndex].fitness;
            maxFitness      = maxFitness > weightsList[gameIndex].fitness ? maxFitness : weightsList[gameIndex].fitness;


            if (DataGame.IsAllGameFailed())
            {
                generation++;

                Debug.WriteLine("GEN: " + generation + " | AVG: " + averageFitness / (float)POPULATION_SIZE + " | MAX: " + maxFitness);
                averageFitness = 0;
                maxFitness     = 0;


                weightsList = weightsList.OrderByDescending(wi => wi.fitness).ToList();

                // starting with a large mutation rate so there's will be more solutions to choose from
                if (weightsList[0].fitness < 10)
                {
                    MUTATION_RATE = 0.9;
                }
                else if (weightsList[0].fitness < 50 && weightsList[0].fitness >= 10)
                {
                    MUTATION_RATE = 0.2;
                }
                else
                {
                    MUTATION_RATE = 0.05;
                }
                // adding better solutions from the other instances (if any)
                if (nextWeightsList.Count + 3 <= POPULATION_SIZE)
                {
                    // the whole synchronization thingy
                    try
                    {
                        if (mmfMutex == null)
                        {
                            mmfMutex = Mutex.OpenExisting("mmfMutex");
                        }

                        if (mmfMutex.WaitOne())
                        {
                            WeightsInfo wi = new WeightsInfo();
                            wi = dataSharer.getFromMemoryMap();

                            if (wi == null || wi.fitness < weightsList[0].fitness)
                            {
                                if (wi == null)
                                {
                                    Debug.WriteLine("Updated - NULL -> " + weightsList[0].fitness);
                                }

                                if (wi != null)
                                {
                                    Debug.WriteLine("Updated - " + wi.fitness + " -> " + weightsList[0].fitness);
                                }

                                dataSharer.writeToMemoryMap(weightsList[0]);
                            }

                            if (wi != null && wi.fitness > weightsList[0].fitness)
                            {
                                nextWeightsList.Add(wi);
                            }

                            mmfMutex.ReleaseMutex();
                        }
                    }
                    catch (WaitHandleCannotBeOpenedException ex)
                    {
                        mmfMutex = new Mutex(true, "mmfMutex");
                        nextWeightsList.AddRange(weightsList.Take(4));
                        mmfMutex.ReleaseMutex();
                    }

                    // adding elites to the next generation
                    nextWeightsList.AddRange(weightsList.Take(3));
                }

                // creating a new generation
                while (nextWeightsList.Count < POPULATION_SIZE)
                {
                    WeightsInfo w1 = select();
                    WeightsInfo w2 = select();


                    while (w1 == w2)
                    {
                        w1 = select();
                        w2 = select();
                    }

                    List <double> gene1 = new List <double>();
                    List <double> gene2 = new List <double>();

                    encode(w1, gene1);
                    encode(w2, gene2);


                    crossover(gene1, gene2);

                    if (mutate(gene1))
                    {
                        w1 = new WeightsInfo();
                    }

                    if (mutate(gene2))
                    {
                        w2 = new WeightsInfo();
                    }

                    decode(w1, gene1);
                    decode(w2, gene2);

                    if (!nextWeightsList.Contains(w1))
                    {
                        nextWeightsList.Add(w1);
                    }

                    if (!nextWeightsList.Contains(w2))
                    {
                        nextWeightsList.Add(w2);
                    }
                }

                weightsList.Clear();
                nextWeightsList = nextWeightsList.OrderByDescending(wi => wi.fitness).ToList();


                weightsList.AddRange(nextWeightsList);

                nextWeightsList.Clear();
            }
        }