public static void WriteToXMLFile(string filePath, GeneticSaveData objectToWrite)
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(GeneticSaveData));

        using (var streamWriter = new StreamWriter(File.Open(filePath, FileMode.Create)))
        {
            xmlSerializer.Serialize(streamWriter, objectToWrite);
        }
    }
    // Load a population of genes
    public bool LoadGeneration(string filePath)
    {
        if (!System.IO.File.Exists(filePath))
        {
            return(false);
        }

        Save = FileReadAndWrite.ReadFromBinaryFile(filePath);

        return(true);
    }
Esempio n. 3
0
    public void SaveGeneration(string filePath)
    {
        GeneticSaveData <T> save = new GeneticSaveData <T> {
            Generation      = Generation,
            PopulationGenes = new List <T[]>(Population.Count)
        };

        for (int i = 0; i < Population.Count; i++)
        {
            save.PopulationGenes.Add(new T[dnaSize]);
            Array.Copy(Population[i].Genes, save.PopulationGenes[i], dnaSize);
        }

        FileReadWrite.WriteToBinaryFile(filePath, save);
    }
Esempio n. 4
0
    public bool LoadGeneration(string filePath)
    {
        if (!System.IO.File.Exists(filePath))
        {
            return(false);
        }
        GeneticSaveData <T> save = FileReadWrite.ReadFromBinaryFile <GeneticSaveData <T> >(filePath);

        Generation = save.Generation;
        for (int i = 0; i < save.PopulationGenes.Count; i++)
        {
            if (i >= Population.Count)
            {
                Population.Add(new DNA <T>(dnaSize, random, getRandomGene, fitnessFunction, crossoverType, shouldInitGenes: false));
            }
            Array.Copy(save.PopulationGenes[i], Population[i].Genes, dnaSize);
        }
        return(true);
    }
Esempio n. 5
0
    // [Header("How Much To Store")]
    // [SerializeField] private int storeEvery = 20;


    void Update()
    {
        ga.NewGeneration();
        // if (ga.Generation % storeEvery == 0)
        // {
        //     GeneticSaveData<char> newSave = new GeneticSaveData<char>();
        //     newSave.TakeFrom(ga);
        //     bool b = newSave.saveTo("save" + ga.Generation);
        //     Debug.Log( b? CharArrayToString(ga.Population[0].Genes) : "not saved");
        // }

        UpdateText(ga.BestGenes, ga.BestFittnes, ga.Generation, ga.Population.Count, (j) => ga.Population[j].Genes);

        if (ga.BestFittnes == 1)
        {
            GeneticSaveData <char> newSave = new GeneticSaveData <char>();
            newSave.TakeFrom(ga);
            bool b = newSave.saveTo("save" + ga.Generation);
            Debug.Log(b? CharArrayToString(ga.Population[0].Genes) : "not saved");
            this.enabled = false;
        }
    }