Exemple #1
0
        public void InitializeAI()
        {
            _gaPopulation = new List<global::AIGame.AI.GA.GenomePair>();
            _gaGenethicAlgorithm = new global::AIGame.AI.GA.GeneticAlgorithm(
                    _aiobjects.Count,
                    AIParams.MutationRate,
                    AIParams.CrossoverRate,
                    _aiobjects[0].AINeuralBrain.GetNumberOfWeights(),
                    _aiobjects[0].AINeuralBrain.CalculateSplitPoints()
                );

            #region read from dump file
            try
            {
                if (AIParams.CONFIG_GET_GENERATION_FROM_FILE)
                {
                    if (!File.Exists(AIParams.CONFIG_GET_GENERATION_FROM_FILE_PATH))
                        throw new Exception("Attempt to read start generation from file failed! File does not exist!");

                    //// set the temporary culture for importing
                    //string temp_culture = Thread.CurrentThread.CurrentCulture.Name;
                    //Thread.CurrentThread.CurrentCulture
                    //    = new CultureInfo("en-US");

                    _gaGenethicAlgorithm.Population.Clear();

                    TextReader tr = new StreamReader(AIParams.CONFIG_GET_GENERATION_FROM_FILE_PATH);
                    List<double> weights = new List<double>();
                    string line = tr.ReadLine();
                    double fitness = 0;
                    while (line != null)
                    {
                        if (line.Contains("Generation:"))
                        {
                            string[] temp_gen = line.Split(' ');
                            _generation = int.Parse(temp_gen[1]);
                            _gaGenethicAlgorithm.BestFitness = double.Parse(temp_gen[3]);
                            _gaGenethicAlgorithm.AverageFitness = double.Parse(temp_gen[5]);
                        }
                        if (line.Contains("Fitness:"))
                        {
                            fitness = double.Parse(line.Split(' ')[1]);
                        }
                        if (line.Contains("Weights:"))
                        {
                            weights.Clear();
                            line = tr.ReadLine().TrimStart().TrimEnd();
                            string[] s_w = line.Split(' ');
                            foreach (string s in s_w)
                                weights.Add(double.Parse(s));

                            double[] temp = weights.ToArray();
                            _gaGenethicAlgorithm.Population.Add(new GA.GenomePair("",new GA.Genome(new List<double>(temp), fitness)));
                        }
                        line = tr.ReadLine();
                    }

                    if (AIParams.CONFIG_GET_GENERATION_FROM_FILE_ONLY_ELITE)
                    {
                        _gaGenethicAlgorithm.Population.Sort(GA.GenomePair.GenomePairComparison);
                        _gaGenethicAlgorithm.Population.Reverse();
                        //GA.GenomePair[] temp = new global::AIGame.AI.GA.Genome[_gaGenethicAlgorithm.Population.Count];
                        //_gaGenethicAlgorithm.Population.CopyTo(temp);
                        int e = 0;
                        for (int i = 0; i < _gaGenethicAlgorithm.Population.Count; i++)
                        {
                            if (i < AIParams.NumElite * AIParams.NumCopiesElite)
                                continue;
                            _gaGenethicAlgorithm.Population[i] = _gaGenethicAlgorithm.Population[e];
                            e++;
                        }
                    }

                    //// restore current culture after importing
                    //Thread.CurrentThread.CurrentCulture = new CultureInfo(temp_culture);
                }
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error reading first generation from file! Exception: " + ex.Message);
            }
            #endregion

            int j = 0;
            foreach (AIObject aiobj in _aiobjects)
            {
                _gaGenethicAlgorithm.Population[j].Genome.Fitness = 0.0;
                aiobj.AINeuralBrain.PutWeights(_gaGenethicAlgorithm.Population[j].Genome.Weights);
                _gaPopulation.Add(_gaGenethicAlgorithm.Population[j]);
                j++;
            }
        }
Exemple #2
0
        public void InitializeAI()
        {
            _gaPopulation        = new List <global::AIGame.AI.GA.GenomePair>();
            _gaGenethicAlgorithm = new global::AIGame.AI.GA.GeneticAlgorithm(
                _aiobjects.Count,
                AIParams.MutationRate,
                AIParams.CrossoverRate,
                _aiobjects[0].AINeuralBrain.GetNumberOfWeights(),
                _aiobjects[0].AINeuralBrain.CalculateSplitPoints()
                );

            #region read from dump file
            try
            {
                if (AIParams.CONFIG_GET_GENERATION_FROM_FILE)
                {
                    if (!File.Exists(AIParams.CONFIG_GET_GENERATION_FROM_FILE_PATH))
                    {
                        throw new Exception("Attempt to read start generation from file failed! File does not exist!");
                    }

                    //// set the temporary culture for importing
                    //string temp_culture = Thread.CurrentThread.CurrentCulture.Name;
                    //Thread.CurrentThread.CurrentCulture
                    //    = new CultureInfo("en-US");

                    _gaGenethicAlgorithm.Population.Clear();

                    TextReader    tr      = new StreamReader(AIParams.CONFIG_GET_GENERATION_FROM_FILE_PATH);
                    List <double> weights = new List <double>();
                    string        line    = tr.ReadLine();
                    double        fitness = 0;
                    while (line != null)
                    {
                        if (line.Contains("Generation:"))
                        {
                            string[] temp_gen = line.Split(' ');
                            _generation = int.Parse(temp_gen[1]);
                            _gaGenethicAlgorithm.BestFitness    = double.Parse(temp_gen[3]);
                            _gaGenethicAlgorithm.AverageFitness = double.Parse(temp_gen[5]);
                        }
                        if (line.Contains("Fitness:"))
                        {
                            fitness = double.Parse(line.Split(' ')[1]);
                        }
                        if (line.Contains("Weights:"))
                        {
                            weights.Clear();
                            line = tr.ReadLine().TrimStart().TrimEnd();
                            string[] s_w = line.Split(' ');
                            foreach (string s in s_w)
                            {
                                weights.Add(double.Parse(s));
                            }

                            double[] temp = weights.ToArray();
                            _gaGenethicAlgorithm.Population.Add(new GA.GenomePair("", new GA.Genome(new List <double>(temp), fitness)));
                        }
                        line = tr.ReadLine();
                    }

                    if (AIParams.CONFIG_GET_GENERATION_FROM_FILE_ONLY_ELITE)
                    {
                        _gaGenethicAlgorithm.Population.Sort(GA.GenomePair.GenomePairComparison);
                        _gaGenethicAlgorithm.Population.Reverse();
                        //GA.GenomePair[] temp = new global::AIGame.AI.GA.Genome[_gaGenethicAlgorithm.Population.Count];
                        //_gaGenethicAlgorithm.Population.CopyTo(temp);
                        int e = 0;
                        for (int i = 0; i < _gaGenethicAlgorithm.Population.Count; i++)
                        {
                            if (i < AIParams.NumElite * AIParams.NumCopiesElite)
                            {
                                continue;
                            }
                            _gaGenethicAlgorithm.Population[i] = _gaGenethicAlgorithm.Population[e];
                            e++;
                        }
                    }


                    //// restore current culture after importing
                    //Thread.CurrentThread.CurrentCulture = new CultureInfo(temp_culture);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Error reading first generation from file! Exception: " + ex.Message);
            }
            #endregion

            int j = 0;
            foreach (AIObject aiobj in _aiobjects)
            {
                _gaGenethicAlgorithm.Population[j].Genome.Fitness = 0.0;
                aiobj.AINeuralBrain.PutWeights(_gaGenethicAlgorithm.Population[j].Genome.Weights);
                _gaPopulation.Add(_gaGenethicAlgorithm.Population[j]);
                j++;
            }
        }