Пример #1
0
 public Genome(List <ConnectionGene> connections, List <NodeGene> nodes, GeneTracker track, int layers)
 {
     this.connections = connections;
     this.nodes       = nodes;
     this.track       = track;
     this.layers      = layers;
 }
Пример #2
0
 public Genome(GeneTracker track)
 {
     connections = new List <ConnectionGene>();
     nodes       = new List <NodeGene>();
     this.track  = track;
     layers      = 1;
     neat        = GameObject.Find("Scripts").GetComponent <Neat>();
 }
Пример #3
0
    void OnEnable()  //set up inital nodes and connections
    {
        gt      = new GeneTracker();
        genome  = new Genome(gt);
        nodes   = new List <NodeGene>();
        species = new List <Species>();
        for (int i = 0; i < inputSize; i++)
        {
            NodeGene nodeIn = new NodeGene(NodeGene.NODETYPE.INPUT, gt.NewId(), 0, false, NodeGene.ACTIVATION_FUNCTION.NULL);
            nodes.Add(nodeIn);
            genome.AddNodeGene(nodeIn.Copy());
        }
        for (int i = 0; i < outputSize; i++)
        {
            NodeGene nodeOut = new NodeGene(NodeGene.NODETYPE.OUTPUT, gt.NewId(), 1, true, outputActiationFunction);
            nodes.Add(nodeOut);
            genome.AddNodeGene(nodeOut.Copy());
        }


        connections = new List <ConnectionGene>();


        for (int i = 0; i < inputSize; i++)
        {
            for (int i2 = 0; i2 < outputSize; i2++)
            {
                //Debug.Log(genome.GetNodeGenes()[i]);
                ConnectionGene con = new ConnectionGene(genome.GetNodeGenes()[i], genome.GetNodeGenes()[inputSize + i2], gt.Innovate());
                connections.Add(con.Copy());
            }
        }
        for (int i = 0; i < connections.Count; i++)
        {
            //Debug.Log(connections[i]);
        }
    }
Пример #4
0
    public Genome CrossoverGenes(Genome genome1, Genome genome2, GeneTracker track)
    {
        Genome          child        = new Genome(track);
        List <NodeGene> newNodeGenes = new List <NodeGene>();

        //for (int i = 0; i < genome1.GetNodeGenes().Count; i++)
        //{
        //    commonNodeGenes.Add(genome1.GetNodeGenes()[i].Copy());

        //}
        //for (int i = 0; i < genome2.GetNodeGenes().Count; i++)
        //{
        //    if (!commonNodeGenes.Contains(genome2.GetNodeGenes()[i]))
        //    {
        //        commonNodeGenes.Add(genome2.GetNodeGenes()[i].Copy());

        //    }


        //}

        if (genome1.GetFitness() < genome2.GetFitness())
        {//swap so genome1 has the higher fitness
            Genome temp = genome2;
            genome2 = genome1;
            genome1 = temp;
        }
        //Debug.Log("g1 "+genome1+ "\ng2 " + genome2);

        child.SetLayers(genome1.GetLayers());

        /*
         * for (int i = 0; i < genome1.GetNodeGenes().Count; i++) {
         *  for (int i2 = 0; i2 < genome2.GetNodeGenes().Count; i2++)
         *  {
         *
         *
         *  }
         * }
         */

        for (int i = 0; i < genome1.GetNodeGenes().Count; i++)
        {
            newNodeGenes.Add(genome1.GetNodeGenes()[i].Copy());
        }

        for (int i = 0; i < genome2.GetNodeGenes().Count; i++)
        {
            if (newNodeGenes.Contains(genome2.GetNodeGenes()[i]))
            {
                if (Random.Range(0, 2) == 1)
                {
                    // newNodeGenes.RemoveAt(newNodeGenes.IndexOf(genome2.GetNodeGenes()[i]));
                    //newNodeGenes.Add(genome2.GetNodeGenes()[i].Copy());
                    newNodeGenes[newNodeGenes.IndexOf(genome2.GetNodeGenes()[i])].SetBias(genome2.GetNodeGenes()[i].GetBias());
                }
            }
        }
        for (int i = 0; i < genome1.GetNodeGenes().Count; i++)
        {
            child.AddNodeGene(newNodeGenes[i]);
        }

        List <ConnectionGene> newConnectionGenes = new List <ConnectionGene>();

        //if equal choose random
        for (int i = 0; i < genome1.GetConnectionGenes().Count; i++)  //copy all connections
        {
            newConnectionGenes.Add(genome1.GetConnectionGenes()[i].Copy());
            newConnectionGenes[i].SetInNode(child.FindNode(genome1.GetConnectionGenes()[i].GetInNode().GetId()));
            newConnectionGenes[i].SetOutNode(child.FindNode(genome1.GetConnectionGenes()[i].GetOutNode().GetId()));
        }
        for (int i = 0; i < genome2.GetConnectionGenes().Count; i++)
        {
            //Debug.Log(newConnectionGenes.Contains(genome2.GetConnectionGenes()[i]));
            if (newConnectionGenes.Contains(genome2.GetConnectionGenes()[i]))
            {
                //Debug.Log(Random.Range(0, 2) );
                if (Random.Range(0, 2) == 1)
                {
                    //newConnectionGenes.RemoveAt(newConnectionGenes.IndexOf(genome2.GetConnectionGenes()[i]));
                    //newConnectionGenes.Add(genome2.GetConnectionGenes()[i].Copy());

                    //newConnectionGenes[i].SetWeight(genome2.GetConnectionGenes()[i].GetWeight());
                    //Debug.Log(newConnectionGenes[newConnectionGenes.IndexOf(genome2.GetConnectionGenes()[i])].GetWeight());
                    newConnectionGenes[newConnectionGenes.IndexOf(genome2.GetConnectionGenes()[i])].SetWeight(genome2.GetConnectionGenes()[i].GetWeight());
                }
            }
        }
        for (int i = 0; i < genome1.GetConnectionGenes().Count; i++)
        {
            child.AddConnectionGene(newConnectionGenes[i]);
        }
        //Debug.Log("child "+child);
        return(child);
    }
Пример #5
0
 public void SetTracker(GeneTracker gt)
 {
     this.gt = gt;
 }