Ejemplo n.º 1
0
        public Result Optimize(ArrayList Elements)
        {
            var Individuals = new ArrayList();
            var Fitnesses   = new List <Double>();

            for (int i = (int)LowerParamBounds[0]; i < (int)UpperParamBounds[0]; i++)
            {
                for (int j = (int)LowerParamBounds[1]; j < (int)UpperParamBounds[1]; j++)
                {
                    Individuals.Add(new BaseElement(FitnessFunction, new ArrayList {
                        (double)i, (double)j
                    }, FitnessEvaluated));
                    Fitnesses.Add(FitnessFunction(new ArrayList {
                        (double)i, (double)j
                    }));
                }
            }
            GenerationCreated?.Invoke(this, Individuals, Fitnesses.ToArray());
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method responsible for the optimization.
        /// </summary>
        /// <returns>An object containing the optimized values of the parameters as well as other characteristics of the optimization process.</returns>
        public Result Optimize(ArrayList ElementsTrans)
        {
            bool genRand = ElementsTrans == null;

            // Create a list object for elementes.
            Elements = new ArrayList();
            if (!genRand)
            {
                // Transfer elements importing
                foreach (BaseElement Element in ElementsTrans)
                {
                    Elements.Add(GetNewElement(FitnessFunction, Element.Position));
                }
            }
            // Create a random number generator object.
            RNG = new Random();
            // The ordinal number of the actual generation.
            Generation = 1;
            // Define a logical variable that keeps track of the current state.
            Stop = false;
            // Start execturion timer
            var timer = new System.Diagnostics.Stopwatch();

            timer.Reset();
            timer.Start();
            if (genRand)
            {
                // Put the first element into the pool.
                Elements.Add(GetNewElement(FitnessFunction, InitialParameters));
            }
            // Initializing the first element takes an evaluation, so we initialize to one
            Evaluation = 1;
            // Check if the stopping condition was met
            if (StopHandler != null)
            {
                Stop = StopHandler.Invoke(this, Elements);
            }
            else
            {
                CheckStop();
            }
            // Create an object for the returned information.
            Info = new Dictionary <InfoTypes, object>();
            // Add the affinity (performance) corresponding to the initial parameter values.
            Info.Add(InfoTypes.InitialFitness, ((BaseElement)Elements[0]).Fitness);
            if (genRand)
            {
                // Create the rest of the initial pool (first generation) of elementes creating elementes at random positions.
                CreateRandomElements(NumberOfElements - 1);
            }
            // Raise GenerationCreated event if there are any subscribers.
            GenerationCreated?.Invoke(this, Elements, GetFitnesses());
            ArrayList BestAffinities = new ArrayList();

            BestAffinities.Add(((BaseElement)Elements[0]).Fitness);
            if (StopHandler != null)
            {
                Stop = StopHandler.Invoke(this, Elements);
            }
            else
            {
                CheckStop();
            }

            //Begin the optimization process
            while (!Stop)
            {
                if (Slow)
                {
                    System.Threading.Thread.Sleep(100);
                }
                Generation++;
                // Create next generation.
                CreateNextGeneration();
                BestAffinities.Add(((BaseElement)Elements[0]).Fitness);
                // Raise GenerationCreated event if there are any subscribers.
                GenerationCreated?.Invoke(this, Elements, GetFitnesses());
                // Stop if the stopping criteria is the number of generations and the number of allowed generations is reached
                if (StopHandler != null)
                {
                    Stop = StopHandler.Invoke(this, Elements);
                }
                else
                {
                    CheckStop();
                }
            }
            // Stop timer
            timer.Stop();
            // Create an array for the parameters (position) of the best antibody.
            var Best = new ArrayList();

            for (int dim = 0; dim < InitialParameters.Count; dim++)
            {
                Best.Add(((BaseElement)Elements[0])[dim]);
            }
            // Add the affinity (performance) value of the best antibody to the returned information.
            Info.Add(InfoTypes.FinalFitness, ((BaseElement)Elements[0]).Fitness);
            // Add munber of generations.
            Info.Add(InfoTypes.Generations, Generation);
            // Add number of affinity evaluations.
            Info.Add(InfoTypes.Evaluations, Evaluation);
            // Define returned values.
            Info.Add(InfoTypes.Affinities, BestAffinities);
            // Add execution time to result info
            Info.Add(InfoTypes.ExecutionTime, timer.ElapsedMilliseconds);
            var res = new Result(Best, Info);

            return(res);
        }