示例#1
0
    public void CleanUp()
    {
        head  = family[Random.Range(0, family.Count)];
        score = 0;

        family.Clear();

        head.fitnessScore = 0;
    }
示例#2
0
    private void PopulateSimulations()
    {
        unisignedNet = new List <GNNNet>();
        foreach (GNNSpiecies spiecie in spiecies)
        {
            int keep = (int)(spiecie.family.Count * 0.2f);
            if (keep == 0)
            {
                keep = 1;
            }

            for (int i = 0; i < keep; i++)
            {
                unisignedNet.Add(spiecie.family[i]);
            }
        }

        GNNNet[] parents = new GNNNet[2];
        GNNNet   child;

        int freeSpaces = CONFIG.POPULATION - unisignedNet.Count;

        for (int i = 0; i < freeSpaces; i++)
        {
            GNNSpiecies spiecie = GetRandomSpiecieByFitness();

            parents[0] = spiecie.GetRandomNetByFitness();
            parents[1] = spiecie.GetRandomNetByFitness();

            if (parents[0].fitnessScore < parents[1].fitnessScore)
            {
                GNNNet tmp = parents[0];
                parents[0] = parents[1];
                parents[1] = tmp;
            }

            child = parents[0].Breed(parents[1]);

            if (Random.Range(0, 101) < 50)
            {
                child.Mutation();
            }
            if (Random.Range(0, 101) < 10)
            {
                child.SafeMutation("node");
            }
            if (Random.Range(0, 101) < 10)
            {
                child.SafeMutation("link");
            }

            child.connections = child.connections.OrderBy(x => x.innov).ToList();
            unisignedNet.Add(child);
        }
    }
示例#3
0
文件: GNNNet.cs 项目: riha112/GNNNeat
    // -- END OF: Feed forward --

    // HOW BREEDING WORKS:
    // 1. Two parents
    // 2. If both have connection then random
    // 3. If one have then take from strongest
    public GNNNet Breed(GNNNet parthner)
    {
        GNNNet child = new GNNNet();

        int[] range   = UTYL.InnovRange(connections, parthner.connections);
        bool  isAsFit = fitnessScore == parthner.fitnessScore;

        GNNNet[] parents = new GNNNet[] { this, parthner };
        Connect?[,] table = new Connect?[2, range[1] - range[0]];

        for (int i = 0; i < 2; i++)
        {
            for (int c = 0; c < parents[i].connections.Count; c++)
            {
                table[i, parents[i].connections[c].innov - range[0]] = parents[i].connections[c];
            }
        }

        for (int c = 0; c < range[1] - range[0]; c++)
        {
            // If both cells are empty, or parhner is less fit then skip connection
            if ((table[0, c] == null && table[1, c] == null) || (!isAsFit && table[0, c] == null))
            {
                continue;
            }

            // If both have same fitness adds parthners connection
            if (table[0, c] == null)
            {
                child.connections.Add(table[1, c].Value);
            }
            // Adds my connection if prev is empty
            else if (table[1, c] == null)
            {
                child.connections.Add(table[0, c].Value);
            }
            // If both have connections select random
            else
            {
                child.connections.Add(table[Random.Range(0, 2), c].Value);
            }
        }

        child.BuildNodes();
        return(child);
    }
示例#4
0
    public GNNNet GetRandomNetByFitness()
    {
        GNNNet net = family[0];

        float random = Random.Range(0, 1.0f);

        double cumulative = 0;

        for (int i = 0; i < family.Count && i < 50; i++)
        {
            cumulative += propobilty[i];
            if (random < cumulative)
            {
                net = family[i];
                break;
            }
        }
        return(net);
    }
示例#5
0
    private void Start()
    {
        UTYL.agentPrefab = Resources.Load("prefabs/AI") as GameObject;
        UTYL.foodPrefab  = Resources.Load("prefabs/Food") as GameObject;
        UTYL.boxPrefab   = Resources.Load("prefabs/Box") as GameObject;

        unisignedNet = new List <GNNNet>();
        SaveObject so = SaveLoadManager.Load();

        if (so == null) // No save file new sim
        {
            DB.SendData("clean_up", new Dictionary <string, string>());

            for (int i = 0; i < CONFIG.POPULATION; i++)
            {
                GNNNet net = new GNNNet();

                for (ushort o = 0; o < 4; o++)
                {
                    net.MutateLink();
                }

                net.connections = net.connections.OrderBy(x => x.innov).ToList();
                unisignedNet.Add(net);
            }
        }
        else
        {
            unisignedNet.AddRange(so.networks);
            GEN = so.GEN;
            InnovController.innovations = new List <Innovation>(so.innovations);
            unisignedNet.RemoveAt(0);
            InnovController.innov = so.innovID;
        }

        UTYL.InitFood();
        InitGeneration();
    }
示例#6
0
 public GNNSpiecies(GNNNet head)
 {
     family = new List <GNNNet>();
     family.Add(head);
     this.head = head;
 }
示例#7
0
文件: UTYL.cs 项目: riha112/GNNNeat
    // δ = E/N + D/N + W/M | + U/N, where U - is count of difrence in disabled
    public static double GetDistance(GNNNet a, GNNNet b)
    {
        if (a.connections.Count == 0 && b.connections.Count == 0)
        {
            return(0);
        }

        double W;
        int    M, D, E, N, U;

        M = 1;
        W = D = E = U = 0;

        N = a.connections.Count;
        if (N < b.connections.Count)
        {
            N = b.connections.Count;
        }

        if (N < 10)
        {
            N = 3;
        }

        GNNNet[] nets  = new GNNNet[] { a, b };
        int[]    range = UTYL.InnovRange(a.connections, b.connections);
        Connect?[,] table = new Connect?[2, range[1] - range[0]];

        for (int i = 0; i < 2; i++)
        {
            for (int c = 0; c < nets[i].connections.Count; c++)
            {
                table[i, nets[i].connections[c].innov - range[0]] = nets[i].connections[c];
            }
        }


        bool isDisjoint = true;

        for (int c = range[1] - range[0] - 1; c >= 0; c--)
        {
            // End of
            if (table[0, c] != null || table[1, c] != null)
            {
                isDisjoint = false;
            }

            if (isDisjoint)
            {
                D++;
                continue;
            }

            if (table[0, c] == null && table[1, c] != null || table[0, c] != null && table[1, c] == null)
            {
                E++;
                continue;
            }

            if (table[0, c] != null && table[1, c] != null)
            {
                if (table[0, c].Value.isDisabled != table[1, c].Value.isDisabled)
                {
                    U++;
                }

                M++;
                W += System.Math.Abs(table[0, c].Value.weight - table[1, c].Value.weight);
            }
        }
        return((double)E / (double)N +
               (double)D / (double)N +
               (double)U / (double)N +
               W / M);
    }