Example #1
0
        /// <summary>
        /// Applies a GA object (with population) to the Api.Population object
        /// if resetState is False the ga.Population will take on the state of
        /// the Api.Population settings. If resetState is true, the Api.Populationn
        /// object will take on the state of the specified ga.Population
        /// </summary>
        /// <param name="ga">Ga.</param>
        /// <param name="resetState">If set to <c>true</c> reset state.</param>
        public void SetGeneticAlgorithm(GAF.GeneticAlgorithm ga, bool resetState)
        {
            //this constructor is used after the GA is created
            if (ga == null)
            {
                throw new ArgumentNullException("ga");
            }
            if (ga.Population == null)
            {
                throw new NullReferenceException("The value 'ga.Population' is null.");
            }
            if (ga.Population.PopulationSize != this.PopulationSize)
            {
                throw new ArgumentException("The Population being applied does not have the same PopulationSize as that specified in the Api.Population object.");
            }
            if (ga.Population.ChromosomeLength != this.ChromosomeLength)
            {
                throw new ArgumentException("The Population being applied does not have the same ChromosomeLength as that specified in the Api.Population object.");
            }

            if (resetState)
            {
                SetState(ga.Population);
            }
            else
            {
                //set the ga.Population object to the same state as the Api.Population
                ga.Population.EvaluateInParallel    = this.EvaluateInParallel;
                ga.Population.LinearlyNormalised    = this.LinearlyNormalised;
                ga.Population.ReEvaluateAll         = this.ReEvaluateAll;
                ga.Population.ParentSelectionMethod = (GAF.ParentSelectionMethod) this.ParentSelectionMethod;
            }
            _ga = ga;
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:GAF.Network.NetworkWrapper"/> class.
        /// </summary>
        /// <param name="geneticAlgorithm">Genetic algorithm.</param>
        /// <param name="serviceDiscoveryClient">Service discovery client.</param>
        /// <param name="fitnessAssemblyName">Fitness assembly name.</param>
        /// <param name="reInitialise">If set to <c>true</c> to re-initialise the server.</param>
        /// <remarks>
        /// Re-initialising the server will, if the fitness function is not defined by the server,
        /// re-transmit the the fitness function to the server.
        /// </remarks>
        public NetworkWrapper(GAF.GeneticAlgorithm geneticAlgorithm, IServiceDiscovery serviceDiscoveryClient, string fitnessAssemblyName, bool initialise)
        {
            if (geneticAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(geneticAlgorithm));
            }

            if (geneticAlgorithm.Population == null)
            {
                throw new NullReferenceException("The specified GeneticAlgorithm.Population object is null.");
            }

            if (geneticAlgorithm.Population.Solutions == null || geneticAlgorithm.Population.Solutions.Count == 0)
            {
                throw new NullReferenceException("The specified GeneticAlgorithm.PopulationSolutions object is null or empty.");
            }

            if (serviceDiscoveryClient == null)
            {
                throw new NullReferenceException("The specified IServiceDiscovery object is null");
            }

            if (string.IsNullOrEmpty(fitnessAssemblyName))
            {
                throw new NullReferenceException("The specified fitness assembly name is null or empty");
            }
            _serviceDiscoveryClient = serviceDiscoveryClient;
            _fitnessAssemblyName    = fitnessAssemblyName;

            //store the referenc to the GA and hook up to the evaluation begin class
            this.GeneticAlgorithm = geneticAlgorithm;
            this.GeneticAlgorithm.Population.OnEvaluationBegin += OnEvaluationBegin;

            //get the endpoints from consul
            Log.Info("Getting remote endpoints from Service Discovery.");
            this.EndPoints = _serviceDiscoveryClient.GetActiveServices(ServiceName);

            if (this.EndPoints.Count == 0)
            {
                throw new ServiceDiscoveryException("No server endpoints detected. Check that servers are running and registered with the appropriate IServiceDiscovery service.");
            }

            LogEndpoints(EndPoints);

            _evaluationClient = new EvaluationClient(this.EndPoints, _fitnessAssemblyName);
        }
Example #3
0
        /// <summary>
        /// Checks the specified GA to see if it is valid to run
        /// </summary>
        /// <returns><c>true</c>, if ga was validated, <c>false</c> otherwise.</returns>
        /// <param name="ga">Ga.</param>
        protected bool ValidateGa(GAF.GeneticAlgorithm ga)
        {
            //check we have an object
            if (ga == null)
            {
                RaiseExceptionEvent("ValidateGa", "The GA object is null.");
                return(false);
            }

            //check that there are some enabled operators
            if (ga.Operators.All(o => o.Enabled != true))
            {
                RaiseExceptionEvent("ValidateGa", "There are no operators defined.");
                return(false);
            }

            //check there is a valid Fitness function
            if (ga.FitnessFunction == null)
            {
                RaiseExceptionEvent("ValidateGa", "The fitness function has not been specified.");
                return(false);
            }

            if (ga.TerminateFunction == null)
            {
                RaiseExceptionEvent("ValidateGa", "The terminate function has not been specified.");
                return(false);
            }

            if (ga.Population == null)
            {
                RaiseExceptionEvent("ValidateGa", "The population has not been specified.");
                return(false);
            }

            return(true);
        }
Example #4
0
        private void CreateGeneticAlgorithm()
        {
            if (_functions == null)
            {
                RaiseExceptionEvent("CreateGeneticAlgorithm", "The consumer functions have not been loaded.");
                return;
            }

            var gafPopulation = _functions.CreatePopulation();

            gafPopulation.OnLogging += (object sender, GAF.LoggingEventArgs e) => RaiseLoggingEvent(e.Message, e.IsWarning);

            if (gafPopulation == null)
            {
                RaiseExceptionEvent("CreateGeneticAlgorithm", "The population function has not been specified.");
                return;
            }
            if (_functions.FitnessFunction == null)
            {
                RaiseExceptionEvent("CreateGeneticAlgorithm", "The fitness function has not been specified.");
                return;
            }
            if (_functions.TerminateFunction == null)
            {
                RaiseExceptionEvent("CreateGeneticAlgorithm", "The terminate function has not been specified.");
                return;
            }

            _ga = new GAF.GeneticAlgorithm(gafPopulation, _functions.FitnessFunction);
            _ga.OnRunException += (object sender, GAF.ExceptionEventArgs e) => {
                //something has gone wrong, update the local 'IsRunning'property.
                this.IsRunning = _ga.IsRunning;
                RaiseExceptionEvent("CreateGeneticAlgorithm", e.Message);
            };

            _ga.TerminateFunction     = _functions.TerminateFunction;
            _ga.OnGenerationComplete += (object sender, GaEventArgs e) => {
                var preStats = this.Stopwatch.ElapsedMilliseconds;
                if (PopulationStatistics.Enabled)
                {
                    this.PopulationStatistics.ProcessPopulation(e.Population, e.Generation);
                    this.RaisePropertyChangedEvent("PopulationStatistics");
                }
                var statsTime = this.Stopwatch.ElapsedMilliseconds - preStats;

                this.Generations    = e.Generation;
                this.Evaluations    = e.Evaluations;
                this.MaximumFitness = e.Population.MaximumFitness;
                //population will have changed
                //this.RaisePropertyChangedEvent ("Population");
                var message = _functions.CreateGenerationCompleteMessage(e.Population, e.Generation, e.Evaluations);
                this.RaiseLoggingEvent(message, false);

                System.Threading.Thread.Sleep(this.ThreadDelayMilliseconds);

                this.GenerationMilliseconds = this.Stopwatch.ElapsedMilliseconds - _tempGenerationMilliseconds - this.ThreadDelayMilliseconds - statsTime;
                _tempGenerationMilliseconds = this.Stopwatch.ElapsedMilliseconds;
                this.RunMilliseconds       += this.GenerationMilliseconds;
            };

            _ga.OnRunComplete += (object sender, GaEventArgs e) => {
                this.Stopwatch.Stop();

                IsRunning = false;

                var message = _functions.CreateRunCompleteMessage(e.Population, e.Generation, e.Evaluations);
                this.RaiseLoggingEvent(message, false);
            };

            _ga.Operators.AddRange(GeneticOperators.GafOperators);
            RaisePropertyChangedEvent("GeneticOperators");

            //add the new ga (with its population) and allow
            //it to be modified in line with the current Api.Population state.
            this.Population.SetGeneticAlgorithm(_ga, false);

            //population has changed
            RaisePropertyChangedEvent("Population");

            //reset the history
            PopulationStatistics.PopulationHistory.ClearHistory();
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:GAF.Network.NetworkWrapper"/> class.
 /// </summary>
 /// <param name="geneticAlgorithm">Genetic algorithm.</param>
 /// <param name="serviceDiscoveryClient">Service discovery client.</param>
 /// <param name="fitnessAssemblyName">Fitness assembly name.</param>
 public NetworkWrapper(GAF.GeneticAlgorithm geneticAlgorithm, IServiceDiscovery serviceDiscoveryClient, string fitnessAssemblyName)
     : this(geneticAlgorithm, serviceDiscoveryClient, fitnessAssemblyName, false)
 {
 }