Ejemplo n.º 1
0
        public void Run(int evaluations, double C_t, double N_max, double V_f, double D_max)
        {
            var               scaleVector       = CalculateScaleVector(C_t, UB_vector, LB_vector);
            InducedMotion     inducedMotion     = new InducedMotion(KrillPopulation, FitnessFunction, evaluations, N_max);
            ForagingMotion    foragingMotion    = new ForagingMotion(KrillPopulation, FitnessFunction, evaluations, V_f);
            VirtualFood       virtualFood       = new VirtualFood(KrillPopulation, FitnessFunction, UB_vector, LB_vector);
            PhysicalDiffusion physicalDiffusion = new PhysicalDiffusion(D_max, evaluations, scaleVector.Count);

            // We evaluate each krill in the population
            KrillPopulation.EvaluatePopulation(FitnessFunction);

            for (int i = 1; i <= evaluations; i++)
            {
                var vf_position = virtualFood.CreateVirtualFood();

                foreach (var krill in KrillPopulation.Population)
                {
                    Vector <double> N_i = inducedMotion.GetInducedMotion(krill, i);
                    Vector <double> F_i = foragingMotion.GetForagingMotion(krill, i, vf_position);
                    Vector <double> D_i = physicalDiffusion.GetPhysicalDiffusion(i);

                    Vector <double> X_i = (F_i + N_i).Add(D_i); // EQUATION 1
                    X_i = X_i.PointwiseMultiply(scaleVector);
                    var newPosition = krill.Coordinates + X_i;

                    var bestKrillPosition = KrillPopulation.GetBestKrill().Coordinates;
                    krill.Coordinates = MathHelpers.FindLimits(newPosition, bestKrillPosition, LB_vector, UB_vector);
                }

                // We evaluate each krill in the population
                KrillPopulation.EvaluatePopulation(FitnessFunction);

                OnGenerationComplete.Invoke(KrillPopulation, new RunEventArgs(i));
            }

            OnRunComplete.Invoke(KrillPopulation, new RunEventArgs(evaluations));
        }
Ejemplo n.º 2
0
        private void RunNewQueue()
        {
            lock (_Queues)//Prioritize
            {
                foreach (var q in _Queues.Where(x => x.IsPrioritize))
                {
                    StartQueue(q);
                }
            }

            if (_Queues.Count == 0 && _Runnings.Count == 0)
            {
                try
                {
                    OnRunComplete?.Invoke(_ReQueues.Count > 0);//on completed
                }
                finally
                {
                    if (_ReQueues.Count > 0)
                    {
                        _Queues.AddRange(_ReQueues);
                        _ReQueues.Clear();
                        RunNewQueue();
                    }
                    else
                    {
                        manualResetEvent.Set();
                    }
                }
                return;
            }
            else
            {
                manualResetEvent.Reset();
            }

            if (_Runnings.Count >= MaxRun)
            {
                return;                     //other
            }
            else
            {
                lock (_Queues)
                {
                    T queue;
                    if (RunRandom)
                    {
                        queue = _Queues.OrderBy(x => Guid.NewGuid()).FirstOrDefault();
                    }
                    else
                    {
                        queue = _Queues.FirstOrDefault();
                    }
                    StartQueue(queue);
                }
                if (_Queues.Count > 0 && _Runnings.Count < MaxRun)
                {
                    RunNewQueue();
                }
            }
        }
Ejemplo n.º 3
0
        public void Run(int maxEvaluations, double CR, double F)
        {
            Population.Evaluate(Parameters.FitnessFunction);

            while (maxEvaluations > 0)
            {
                List <Individual> newGeneration = new List <Individual>();

                foreach (var orginal in Population.Solutions)
                {
                    // generate unique random numbers
                    List <int> randomValues = RandomGenerator.GenerateRandom(3, 0, Population.Solutions.Count);
                    int        a            = randomValues[0];
                    int        b            = randomValues[1];
                    int        c            = randomValues[2];

                    // choose random individuals (agents) from population
                    Individual individual1 = Population.Solutions[a];
                    Individual individual2 = Population.Solutions[b];
                    Individual individual3 = Population.Solutions[c];

                    int        i         = 0;
                    int        R         = RandomGenerator.Instance.Random.Next(Population.Solutions.Count);
                    Individual candidate = new Individual();
                    foreach (var orginalElement in orginal.Elements)
                    {
                        double ri = RandomGenerator.Instance.Random.NextDouble();
                        if (ri < CR || i == R)
                        {
                            // simple mutation
                            double newElement = individual1.Elements[i] + F * (individual2.Elements[i] - individual3.Elements[i]);

                            if (CheckIfWithinDomain(newElement, Parameters))
                            {
                                candidate.Elements.Add(newElement);
                            }
                            else
                            {
                                candidate.Elements.Add(orginalElement);
                            }
                        }
                        else
                        {
                            candidate.Elements.Add(orginalElement);
                        }

                        i++;
                    }

                    if (candidate.Evaluate(Parameters.FitnessFunction) < orginal.Evaluate(Parameters.FitnessFunction))
                    {
                        newGeneration.Add(candidate);
                    }
                    else
                    {
                        newGeneration.Add(orginal);
                    }
                }

                // switch populations
                Population.Solutions = newGeneration;
                maxEvaluations--;

                OnGenerationComplete.Invoke(Population, new EventArgs());
            }

            OnRunComplete.Invoke(Population, new EventArgs());
        }