Пример #1
0
    public void StartMAP2()
    {
        if (carKnowsDrag)
        {
            NUM_INPUTS = 6;
        }
        if (!randomDrag)
        {
            Trials = 1;
        }


        //TODO SetupNewExperiment skal fixes til MAP Elites
        SetupNewMAPExperiment();



        // print("Loading: " + popFileLoadPath);
        _ea       = experiment.CreateEvolutionAlgorithm(popFileSavePath);
        startTime = DateTime.Now;

        _ea.UpdateEvent += new EventHandler(ea_UpdateEventMAP);
        _ea.PausedEvent += new EventHandler(ea_PauseEvent);
        IList <NeatGenome> unTestedGenomes = null;

        if (map.Values.Count == 0)
        {
            unTestedGenomes = _ea.GenomeList;
        }
        else
        {
            unTestedGenomes = new List <NeatGenome> (map.Values);
        }

        //Loop this:
        //		while (firstRun) {
        List <NeatGenome> children = new List <NeatGenome> ();

        for (int i = 0; i < 50; i++)
        {
            NeatGenome mom   = unTestedGenomes [UnityEngine.Random.Range(0, unTestedGenomes.Count)];           // = new NeatGenome (); // Should be selected random from the map.
            NeatGenome dad   = unTestedGenomes [UnityEngine.Random.Range(0, unTestedGenomes.Count)];           // = new NeatGenome (); // Should be selected random from the map.
            NeatGenome child = mom.CreateOffspring(dad, _ea.CurrentGeneration);
            children.Add(child);
        }

        print("Test length: " + children.Count);
        _ea.GenomeList = children;
        _ea.StartContinueMAP2();
    }
 public NeatGenome MutateGenome(NeatGenome genome)
 {
     return genome.CreateOffspring(genome.BirthGeneration + 1);
 }
Пример #3
0
 public override Individual Mutate(uint gen)
 {
     return(new RobotIndividual(Genome.CreateOffspring(gen)));
 }
Пример #4
0
    void ea_UpdateEventMAP(object sender, EventArgs e)
    {
        Utility.Log(string.Format("gen={0:N0} bestFitness={1:N6}",
                                  _ea.CurrentGeneration, _ea.Statistics._maxFitness));

        Generation = _ea.CurrentGeneration;

        IList <NeatGenome> testedGenomes = _ea.GenomeList;

        print("TestedGenomes = " + testedGenomes.Count);

        foreach (NeatGenome genome in testedGenomes)
        {
            int key = convertAvgSpeedToKey(genome.EvaluationInfo.AvgSpeed);
            if (key == -1)
            {
                continue;
            }
            if (map.ContainsKey(key))
            {
                if (map [key] != null)
                {
                    if (genome.EvaluationInfo.Fitness > map [key].EvaluationInfo.Fitness)
                    {
                        Debug.Log("New Best! Key: " + key + " Old: " + map [key].EvaluationInfo.Fitness + " New: " + genome.EvaluationInfo.Fitness);
                        map [key] = genome;
                        SaveGenome(genome, key.ToString());
                    }
                }
                else
                {
                    map [key] = genome;
                    SaveGenome(genome, key.ToString());
                    Debug.Log("New Added! Key: " + key + " New: " + genome.EvaluationInfo.Fitness);
                }
            }
            else
            {
                map.Add(key, genome);
                SaveGenome(genome, key.ToString());
                Debug.Log("New Added! Key: " + key + " New: " + genome.EvaluationInfo.Fitness);
            }
        }
        Debug.Log("Map size: " + map.Values.Count);
        foreach (KeyValuePair <int, NeatGenome> value in map)
        {
            //Now you can access the key and value both separately from this attachStat as:
            Debug.Log("Key: " + value.Key + " - Fitness: " + value.Value.EvaluationInfo.Fitness);
        }

        IList <NeatGenome> unTestedGenomes = new List <NeatGenome> (map.Values);

        //Loop this:
        //		while (firstRun) {
        List <NeatGenome> children = new List <NeatGenome> ();

        for (int i = 0; i < 50; i++)
        {
            NeatGenome mom   = unTestedGenomes [UnityEngine.Random.Range(0, unTestedGenomes.Count)];           // = new NeatGenome (); // Should be selected random from the map.
            NeatGenome dad   = unTestedGenomes [UnityEngine.Random.Range(0, unTestedGenomes.Count)];           // = new NeatGenome (); // Should be selected random from the map.
            NeatGenome child = mom.CreateOffspring(dad, _ea.CurrentGeneration);
            children.Add(child);
        }

        _ea.GenomeList = children;

        Fitness     = (float)_ea.Statistics._maxFitness;
        MeanFitness = (float)_ea.Statistics._meanFitness;
        if (bestFitness < Fitness)
        {
            //map [getIndex (rain)] = _ea.CurrentChampGenome;

            XmlWriterSettings _xwSettings = new XmlWriterSettings();
            _xwSettings.Indent = true;
            // Save genomes to xml file.
            DirectoryInfo dirInf = new DirectoryInfo(Application.persistentDataPath + string.Format("/{0}", folder_prefix));
            if (!dirInf.Exists)
            {
                Debug.Log("Creating subdirectory");
                dirInf.Create();
            }

            using (XmlWriter xw = XmlWriter.Create(champFileSavePath, _xwSettings)) {
                experiment.SavePopulation(xw, new NeatGenome[] { _ea.CurrentChampGenome });
            }

            bestFitness = Fitness;
            Debug.Log("New best saved: " + bestFitness);
            TrialDuration = bestFitness * 50.0f > 70.0f ? bestFitness * 50.0f : 70.0f;
            if (TrialDuration > 450.0f)
            {
                TrialDuration = 450.0f;
            }
        }


        ChampAvgSpeed = _ea.CurrentChampGenome.EvaluationInfo.AvgSpeed;
    }