Beispiel #1
0
    NeatGenome LoadChampion()
    {
        NeatGenome genome = null;

        // Try to load the genome from the XML document.
        try
        {
            // It would make more sense to use LoadGenome instead of ReadCompleteGenomeList
            using (XmlReader xr = XmlReader.Create(champFileSavePath))
                genome = NeatGenomeXmlIO.ReadCompleteGenomeList(
                    xr, false, (NeatGenomeFactory)experiment.CreateGenomeFactory())[0];
        }
        catch (Exception e1)
        {
        }
        return(genome);
    }
Beispiel #2
0
    public void RunBest()
    {
        Time.timeScale = 1;

        NeatGenome genome = null;


        // Try to load the genome from the XML document.
        try
        {
            using (XmlReader xr = XmlReader.Create(champFileSavePath))
                genome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, (NeatGenomeFactory)experiment.CreateGenomeFactory())[0];
        }
        catch (Exception e1)
        {
            // print(champFileLoadPath + " Error loading genome from file!\nLoading aborted.\n"
            //						  + e1.Message + "\nJoe: " + champFileLoadPath);
            return;
        }

        // Get a genome decoder that can convert genomes to phenomes.
        var genomeDecoder = experiment.CreateGenomeDecoder();

        // Decode the genome into a phenome (neural network).
        var phenome = genomeDecoder.Decode(genome);

        GameObject     obj        = Instantiate(Unit, Unit.transform.position, Unit.transform.rotation) as GameObject;
        UnitController controller = obj.GetComponent <UnitController>();

        ControllerMap.Add(phenome, controller);

        controller.Activate(phenome);
    }
    public void RunBest()
    {
        timeScale = 1;

        NeatGenome genome = null;


        // Try to load the genome from the XML document.
        try {
            TextAsset popTxtAsset = (TextAsset)Resources.Load(champFileSavePath);
            string    stream      = popTxtAsset.text;

            string      xrString = stream;
            XmlDocument xdoc     = new XmlDocument();
            xdoc.LoadXml(xrString);

            using (XmlReader xr = new XmlNodeReader(xdoc))
                genome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, (NeatGenomeFactory)experiment.CreateGenomeFactory())[0];
        } catch (Exception e1) {
            Debug.LogError(" Error loading genome from file!\nLoading aborted.\n" + e1.Message + "\nin: " + champFileSavePath);
            return;
        }

        // Get a genome decoder that can convert genomes to phenomes.
        var genomeDecoder = experiment.CreateGenomeDecoder();

        // Decode the genome into a phenome (neural network).
        var phenome = genomeDecoder.Decode(genome);

        GameObject     obj        = Instantiate(Unit, Unit.transform.position, Unit.transform.rotation) as GameObject;
        UnitController controller = obj.GetComponent <UnitController>();

        ControllerMap.Add(phenome, controller);

        controller.Activate(phenome);
    }
Beispiel #4
0
    private void ActivateFromFile()
    {
        NeatGenome       genome     = null;
        SimpleExperiment experiment = new SimpleExperiment();
        XmlDocument      xmlConfig  = new XmlDocument();
        TextAsset        textAsset  = (TextAsset)Resources.Load("experiment.config");

        xmlConfig.LoadXml(textAsset.text);

        experiment.Initialize("Tmp Experiment", xmlConfig.DocumentElement, 8 + 2, 1);

        string champFileSavePath = Application.persistentDataPath + "/car_movement.champ.xml";

        using (XmlReader xr = XmlReader.Create(champFileSavePath))
            genome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, (NeatGenomeFactory)experiment.CreateGenomeFactory())[0];

        var genomeDecoder = experiment.CreateGenomeDecoder();

        box = genomeDecoder.Decode(genome);
    }
Beispiel #5
0
    void ea_UpdateEvent(object sender, EventArgs e)
    {
        Utility.Log(string.Format("gen={0:N0} bestFitness={1:N6}",
                                  _ea.CurrentGeneration, _ea.Statistics._maxFitness));



        Fitness = _ea.Statistics._maxFitness;

        Generation = _ea.CurrentGeneration;

        if (MaxAllFitness < Fitness)
        {
            MaxAllFitness = (float)Fitness;

            float timeTemp = Time.timeScale;
            Time.timeScale    = 0;
            AllGenerationBest = _ea.CurrentChampGenome;

            XmlWriterSettings _xwSettings = new XmlWriterSettings();
            _xwSettings.Indent = true;

            NeatGenome genome = null;

            bool fileAlreadyExist = File.Exists(champFileSavePath2);

            try
            {
                using (XmlReader xr = XmlReader.Create(champFileSavePath2)) {
                    genome = NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, (NeatGenomeFactory)experiment.CreateGenomeFactory()) [0];
                }
            }catch (Exception ex) {
            }

            using (XmlWriter xw = XmlWriter.Create(champFileSavePath2, _xwSettings))
            {
                if ((fileAlreadyExist && genome.EvaluationInfo.Fitness < MaxAllFitness) || !fileAlreadyExist)
                {
                    experiment.SavePopulation(xw, new NeatGenome[] { _ea.CurrentChampGenome });
                    Debug.Log("saved new best: " + AllGenerationBest.EvaluationInfo.Fitness);
                }
            }
            Time.timeScale = timeTemp;


//			Debug.Log(_ea.CurrentChampGenome.EvaluationInfo.Fitness+" vs "+AllGenerationBest.EvaluationInfo.Fitness);
        }
    }
Beispiel #6
0
    void loadMap()
    {
        string path = Application.persistentDataPath + string.Format("/{0}/map/", folder_prefix);

        DirectoryInfo dir = new DirectoryInfo(path);

        if (dir.Exists)
        {
            FileInfo[] info = dir.GetFiles("*.xml");

            foreach (FileInfo f in info)
            {
                int key = int.Parse(f.Name.Substring(0, f.Name.Length - 4));

                try {
                    using (XmlReader xr = XmlReader.Create(path + f.Name))
                        map.Add(key, NeatGenomeXmlIO.ReadCompleteGenomeList(xr, false, (NeatGenomeFactory)experiment.CreateGenomeFactory()) [0]);
                } catch (Exception e1) {
                    print(" Error loading genome from file!\nLoading aborted.\n" + e1.Message);
                    continue;
                }
            }
        }
    }