Exemplo n.º 1
0
    public void Load()
    {
        if (neat.speciation)
        {
            string info = PlayerPrefs.GetString("Genomes");
            //species.Clear();
            SpeciesWrapper sw = JsonUtility.FromJson <SpeciesWrapper>(info);
            Debug.Log(sw.species.Count);
            neat.SetSpecies(sw.species);
            neat.SetGeneration(sw.generation);
            neat.SetTracker(sw.gt);
            List <Genome> genomes = new List <Genome>();
            int           k       = 0;
            for (int i = 0; i < sw.species.Count; i++)
            {
                for (int j = 0; j < sw.species[i].GetGenomes().Count; j++)
                {
                    //Debug.Log(species[i].GetGenomes()[j].GetSpeciesId());
                    // newGenomes.Add(species[i].GetGenomes()[j]);
                    impf.SetGenome(agents[k], sw.species[i].GetGenomes()[j]);
                    impf.ResetAgent(agents[k], k);

                    neat.GetGenomes()[k] = sw.species[i].GetGenomes()[j];
                    impf.SetSpecies(agents[k]);
                    sw.species[i].GetGenomes()[j].SetNeat();

                    for (int l = 0; l < sw.species[i].GetGenomes()[j].GetConnectionGenes().Count; l++)//sample n connections
                    {
                        int inId  = sw.species[i].GetGenomes()[j].GetConnectionGenes()[l].GetInNode().GetId();
                        int outId = sw.species[i].GetGenomes()[j].GetConnectionGenes()[l].GetOutNode().GetId();
                        for (int l2 = 0; l2 < sw.species[i].GetGenomes()[j].GetNodeGenes().Count; l2++)
                        {
                            if (inId == sw.species[i].GetGenomes()[j].GetNodeGenes()[l2].GetId())
                            {
                                sw.species[i].GetGenomes()[j].GetConnectionGenes()[l].SetInNode(sw.species[i].GetGenomes()[j].GetNodeGenes()[l2]);
                            }
                            if (outId == sw.species[i].GetGenomes()[j].GetNodeGenes()[l2].GetId())
                            {
                                sw.species[i].GetGenomes()[j].GetConnectionGenes()[l].SetOutNode(sw.species[i].GetGenomes()[j].GetNodeGenes()[l2]);
                            }
                        }
                    }

                    k++;
                }
            }
            Debug.Log(neat.GetGenomes().Count);
            Debug.Log(neat.GetGenomes()[0]);
            impf.StatsSpecies(sw.species.Count);
        }
        else
        {
            string info = PlayerPrefs.GetString("Genomes");
            Debug.Log(info);
            //genomes.Clear();
            GenomesWrapper gw = JsonUtility.FromJson <GenomesWrapper>(info);
            Debug.Log(gw.genomes.Count);
            neat.SetGenomes(gw.genomes);
            neat.SetGeneration(gw.generation);
            neat.SetTracker(gw.gt);
            for (int i = 0; i < agents.Count; i++)
            {
                //agents[i].GetComponent<Brain>().SetGenome(gw.genomes[i]);
                gw.genomes[i].SetNeat();
                impf.SetGenome(agents[i], gw.genomes[i].Copy());
                impf.ResetAgent(agents[i], i);
            }
        }
        // impf.OnLoad();
        neat.ResetTimer();
        GameObject.Find("Main Camera").GetComponent <CameraScript>().ResetPos();
    }