public void loadWeightsFromFile()
    {
        List <float> weightAverages = CreatureAveragesIO.loadAverages();
        int          avgsIndex      = 0;

        // for every node
        for (int j = 0; j < creature.networks.Count; j++)
        {
            foreach (string key in creature.networks[j].Keys)
            {
                for (int k = 0; k < creature.networks[j][key].net.Count; k++)
                {
                    for (int l = 0; l < creature.networks[j][key].net[k].Count; l++)
                    {
                        bool         castWorked = true;
                        NonInputNode node       = null;
                        // get node and convert it to Non-Input node if possible
                        try
                        {
                            node = (NonInputNode)creature.networks[j][key].net[k][l];
                        }
                        catch (InvalidCastException e)
                        {
                            castWorked = false;
                        }
                        // if it is a non-input node
                        if (castWorked)
                        {
                            for (int i = 0; i < node.weights.Count; i++)
                            {
                                try
                                {
                                    node.weights[i] = weightAverages[avgsIndex];
                                    avgsIndex++;
                                }
                                catch (ArgumentOutOfRangeException e)
                                {
                                    Debug.LogError("Tried to read weight from file, but weight array length didn't match current creature.");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
Пример #2
0
    public void calculateWeightStats()
    {
        updateWeightsByCreature();

        // NOTE: only works if all creatures in the population have the same number of weights
        // calculate averages
        List <float> averages = new List <float>();

        int count;

        // assuming any creatures were left
        if (weightsByCreature.Count > 0)
        {
            // for every weight
            for (int i = 0; i < weightsByCreature[0].Count; i++)
            {
                count = 0;
                float sum = 0;
                // for every creature
                for (int j = 0; j < weightsByCreature.Count; j++)
                {
                    // TODO: fix this: Probably relates to creature removal from list?
                    try
                    {
                        count++;
                        sum += weightsByCreature[j][i]; // add the weight for that creature to the sum for that weight
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        //Debug.Log("Out of range: " + j + " " + i);
                    }
                }
                averages.Add(sum / count);
            }

            // calculate variances and standard deviations
            List <float> variances = new List <float>();
            List <float> sDs       = new List <float>();


            for (int i = 0; i < weightsByCreature[0].Count; i++)
            {
                count = 0;
                float sumSquaredDiff = 0;
                // for every creature
                for (int j = 0; j < weightsByCreature.Count; j++)
                {
                    // TODO: fix this
                    try
                    {
                        count++;
                        sumSquaredDiff += (float)Math.Pow(weightsByCreature[j][i] - averages[i], 2.0); // add the weight for that creature to the sum for that weight
                    }
                    catch (ArgumentOutOfRangeException e)
                    {
                        //Debug.Log("Out of range: " + j + " " + i);
                    }
                }
                variances.Add(sumSquaredDiff / count);
                sDs.Add((float)Math.Sqrt((double)sumSquaredDiff / count));
            }


            // set instance variables
            weightAverages = averages;
            weightSDs      = sDs;

            // calculate measure of overall variability


            float varianceSum = 0;
            for (int i = 0; i < variances.Count; i++)
            {
                varianceSum += variances[i];
            }
            overallVariability = (float)Math.Sqrt((double)(varianceSum / variances.Count));
        }


        CreatureAveragesIO.saveAverages(weightAverages);
    }