Пример #1
0
    public void LoadGenome(string filename, bool all)
    {
        string path = filename;

        Genome genome = new Genome();

        using (StreamReader reader = new StreamReader(path))
        {
            bool   node       = false;
            bool   connection = false;
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                if (line.Equals("Nodes"))
                {
                    node       = true;
                    connection = false;
                }
                else if (line.Equals("Connections"))
                {
                    node       = false;
                    connection = true;
                }
                else
                {
                    string[] info = line.Split(',');

                    if (node)
                    {
                        int                    id         = int.Parse(info[0]);
                        NodeGene.Type          type       = (NodeGene.Type)System.Enum.Parse(typeof(NodeGene.Type), info[1]);
                        GenomeUtils.Activation activation = (GenomeUtils.Activation)System.Enum.Parse(typeof(GenomeUtils.Activation), info[2]);
                        genome.AddNodeGene(new NodeGene(type, id, activation));
                        Counter.SetNodeCounter(id);
                    }
                    else if (connection)
                    {
                        int   innovation = int.Parse(info[0]);
                        bool  expressed  = bool.Parse(info[1]);
                        int   inNode     = int.Parse(info[2]);
                        int   outNode    = int.Parse(info[3]);
                        float weight     = float.Parse(info[4]);
                        genome.AddConnectionGene(new ConnectionGene(inNode, outNode, weight, expressed, innovation));
                        Counter.SetConnectionCounter(innovation);
                    }
                    else
                    {
                        Debug.LogError("Invalid genome file");
                    }
                }
            }
        }

        genomes.Clear();
        genomes.Add(genome);

        if (all)
        {
            for (int i = 1; i < GenomeUtils.POP_SIZE; i++)
            {
                genomes.Add(GenomeUtils.Clone(genome));
            }
        }
        else
        {
            for (int i = 1; i < GenomeUtils.POP_SIZE; i++)
            {
                genomes.Add(new Genome(inputNodes, outputNodes));
            }
        }

        SetSpecies();
        MakeNNets();
    }
Пример #2
0
 public NodeGene(Type type, int id, GenomeUtils.Activation activation)
 {
     this.type       = type;
     this.id         = id;
     this.activation = activation;
 }