private bool AddParameterSet(MutationSet s, string[] parameters)
        {
            List <uint> indices = new List <uint>();

            foreach (string name in parameters)
            {
                string nm  = name.Trim();
                int    idx = ParameterFromName(nm);

                if (idx < 0)
                {
                    Console.Error.WriteLine("Could not find parameter `{0}' for mutation set", nm);
                    return(false);
                }

                indices.Add((uint)idx);
            }

            if (indices.Count != 0)
            {
                s.Add(indices.ToArray());
            }

            return(true);
        }
        private void ParseMutationSet(XmlNode node)
        {
            MutationSet s = new MutationSet();

            XmlNodeList parameters = node.SelectNodes("parameters");
            XmlNodeList parameter  = node.SelectNodes("parameter");

            if (parameters.Count == 0 && parameter.Count == 0)
            {
                if (!AddParameterSet(s, ParameterSetNames(node)))
                {
                    return;
                }
            }

            foreach (XmlNode p in parameter)
            {
                if (!AddParameterSet(s, ParameterSetNames(p)))
                {
                    return;
                }
            }

            foreach (XmlNode p in parameters)
            {
                List <string> names = new List <string>();

                foreach (XmlNode c in p.SelectNodes("parameter"))
                {
                    names.AddRange(ParameterSetNames(c));
                }

                if (!AddParameterSet(s, names.ToArray()))
                {
                    return;
                }
            }

            if (s.Count == 1)
            {
                // Add empty set
                s.Insert(0, new uint[] {});
            }

            if (s.Count != 0)
            {
                State.MutationSets.Add(s);
            }
            else
            {
                Console.Error.WriteLine("Ignoring empty mutation set");
            }
        }
        private bool Mutate(int mutationIndex, Probabilities probabilities, MutationSet s, Particle gbest)
        {
            // Calculate mutation probability
            double[] mutprob     = CalculateMutationProbabilities(mutationIndex, probabilities, s, gbest);
            double   r           = d_state.Random.NextDouble();
            double   accumulated = 0;

            for (int i = 0; i < mutprob.Length; ++i)
            {
                accumulated += mutprob[i];

                if (r < accumulated)
                {
                    foreach (Particle particle in this)
                    {
                        particle.ActiveSet[mutationIndex] = (uint)i;
                    }

                    return(true);
                }
            }

            return(false);
        }
        public void Initialize()
        {
            Optimization.Random r         = d_state.Random;
            List <uint>         activeSet = new List <uint>();
            List <MutationSet>  sets      = d_state.MutationSets;

            // Initialize active parameter set randomly
            for (int i = 0; i < sets.Count; ++i)
            {
                MutationSet s = sets[i];

                int num = s.Count - 1;
                int idx = (int)System.Math.Round(r.Range(0, num));

                activeSet.Add((uint)idx);
            }

            foreach (Particle particle in this)
            {
                particle.ActiveSet.Clear();
                particle.ActiveSet.AddRange(activeSet);
                particle.UpdateParameterSet();
            }
        }
Exemple #5
0
        public void UpdateParameterSet()
        {
            List <MutationSet> sets          = MutationSets;
            List <int>         removeIndices = new List <int>();

            d_activeIndices.Clear();

            for (int i = 0; i < d_activeSet.Count; ++i)
            {
                MutationSet s             = sets[i];
                uint        activeIndex   = d_activeSet[i];
                uint[]      activeIndices = s[(int)activeIndex];

                for (int j = 0; j < s.Count; ++j)
                {
                    if (j == activeIndex)
                    {
                        continue;
                    }

                    uint[] indices = Difference(s[j], activeIndices);

                    for (int k = 0; k < indices.Length; ++k)
                    {
                        removeIndices.Add((int)indices[k]);
                    }
                }
            }

            Parameters.Clear();

            List <double> vel = new List <double>();

            for (int i = 0; i < d_allParameters.Count; ++i)
            {
                if (!removeIndices.Contains(i))
                {
                    Parameters.Add(d_allParameters[i]);
                    vel.Add(d_allVelocities[i]);

                    d_activeIndices.Add((uint)i);
                }
            }

            Velocity = vel.ToArray();

            RecalculateHash();

            // Update the standard PSO personal best from our cache of personal
            // best for each configuration
            Particle best;

            if (d_personalBests.TryGetValue(d_hash, out best))
            {
                base.PersonalBest = (Particle)best.Clone();
            }
            else
            {
                base.PersonalBest = null;
            }
        }
        private double[] CalculateMutationProbabilities(int mutationIndex, Probabilities probabilities, MutationSet s, Particle gbest)
        {
            double[] ret = new double[s.Count];

            for (int i = 0; i < ret.Length; ++i)
            {
                ret[i] = 1;
            }

            List <uint> activeSet = this[0].ActiveSet;

            for (int i = 0; i < s.Count; ++i)
            {
                // Don't care about non-mutation, since it's just the inverse
                // of the mutation probability
                if (activeSet[mutationIndex] == i)
                {
                    continue;
                }

                // Exploration mutation probability
                ret[i] *= 1 - probabilities.Exploration;

                // Super local best mutation probability
                if (d_best != null && d_best.ActiveSet[mutationIndex] == i)
                {
                    ret[i] *= 1 - probabilities.Cognitive;
                }

                // Super global best mutation probability
                if (gbest != null && gbest.ActiveSet[mutationIndex] == i)
                {
                    ret[i] *= 1 - probabilities.Social;
                }
            }

            for (int i = 0; i < ret.Length; ++i)
            {
                ret[i] = 1 - ret[i];
            }

            return(ret);
        }