Exemplo n.º 1
0
        public double[] CreateTrial(double[,] Bounds, ParameterTypes[] VariableType, int i)
        {
            // Select the 3 mutation agents and a recombination index.
            double[] randoms = MutationRNG.Next();
            for (int j = 0; j < 3; ++j)
            {
                randoms[j] = Math.Floor(randoms[j] * PopulationSize);
            }

            randoms[3] = Math.Floor(randoms[3] * Dimensionality);

            // Randoms for creating the trial.
            double[] recombinationRandoms = RecombinationRNG.Next();

            // Mutate and create the donor agent.
            double[] donor = new double[Dimensionality];
            for (int j = 0; j < Dimensionality; ++j)
            {
                donor[j] = Agents[(int)randoms[0], j] + DifferentialWeight * (Agents[(int)randoms[1], j] - Agents[(int)randoms[2], j]);
                donor[j] = Math.Min(Math.Max(donor[j], Bounds[j, 0]), Bounds[j, 1]);
            }

            // Recombine with the original agent to get the new trial.
            double[] trial = new double[Dimensionality];
            for (int j = 0; j < Dimensionality; ++j)
            {
                trial[j] = AdjustValueForVariableType(VariableType[j], ((recombinationRandoms[j] < CrossoverProbability || j == randoms[3]) ? donor[j] : Agents[i, j]));
            }

            return(trial);
        }
Exemplo n.º 2
0
        void InitialiseAgents(int MinMaxFlag, double[,] Bounds, ParameterTypes[] VariableType)
        {
            Agents = new double[PopulationSize, Dimensionality];
            Scores = new double[PopulationSize];

            SobolRNG initialisationRNG = new SobolRNG(Dimensionality, 777);

            double[] randoms;
            for (int i = 0; i < PopulationSize; ++i)
            {
                while (true)
                {
                    randoms = initialisationRNG.Next();

                    for (int j = 0; j < Dimensionality; ++j)
                    {
                        Agents[i, j] = AdjustValueForVariableType(VariableType[j], randoms[j] * Bounds[j, 0] + (1 - randoms[j]) * Bounds[j, 1]);
                    }

                    if (!PreviousParameterSets.ContainsKey(Agents.Row(i)))
                    {
                        PreviousParameterSets.Add(Agents.Row(i), true);
                        break;
                    }
                }
            }

            Scores = ScoreGeneration(MinMaxFlag, Agents);

            for (int i = 0; i < PopulationSize; ++i)
            {
                if (double.IsNaN(Scores[i]))
                {
                    StringBuilder sb = new StringBuilder("Error initialising differential evolution! (");
                    for (int j = 0; j < Dimensionality - 1; ++j)
                    {
                        sb.Append(Agents[i, j] + ",");
                    }

                    sb.Append(Agents[i, Dimensionality - 1] + ")");

                    throw new Exception(sb.ToString());
                }
            }
        }