示例#1
0
 public void RemoteControllerFinished(RemoteCalculationsFinishedHandlerArgs args)
 {
     if (CalculationsRunning)
     {
         // evaluating function for best state of remote node - in case it's the best evaluation.
         _function.Evaluate(((ParticleState)args.Result).Location);
     }
 }
示例#2
0
        public virtual void Transpose(IFitnessFunction <double[], double[]> function)
        {
            double[] newLocation;
            var      restart = _sinceLastImprovement == _iterationsToRestart;

            if (restart)
            {
                newLocation           = RandomGenerator.GetInstance().RandomVector(CurrentState.Location.Length, Bounds);
                _sinceLastImprovement = 0;
            }
            else
            {
                newLocation = GetClampedLocation(CurrentState.Location.Select((x, i) => x + Velocity[i]).ToArray());
            }
            var newVal  = function.Evaluate(newLocation);
            var oldBest = PersonalBest;

            CurrentState = new ParticleState(newLocation, newVal);

            if (Optimization.IsBetter(newVal, PersonalBest.FitnessValue) < 0)
            {
                PersonalBest          = CurrentState;
                _sinceLastImprovement = 0;
            }
            else
            {
                _sinceLastImprovement++;
            }
        }
示例#3
0
        public static IParticle Create(PsoParticleType type,
            int locationDim,
            int fitnessDim,
            IFitnessFunction<double[], double[]> function,
            double restartEpsilon = double.MaxValue,
            int iterationsToRestart = int.MaxValue,
            DimensionBound[] bounds = null,
            double[] initVelocity = null
            )
        {
            IParticle particle = null;
            var rand = RandomGenerator.GetInstance();
            switch (type)
            {
                case PsoParticleType.Standard:
                case PsoParticleType.FullyInformed:
                    particle = new StandardParticle(restartEpsilon, iterationsToRestart);
                    break;
                case PsoParticleType.ChargedParticle:
                    particle = new ChargedParticle();
                    break;
                case PsoParticleType.DummyParticle:
                    particle = new DummyParticle();
                    break;
            }

            var x = bounds != null ? rand.RandomVector(locationDim, bounds) : rand.RandomVector(locationDim);
            particle.Init(new ParticleState(x, function.Evaluate(x)),
                initVelocity ?? rand.RandomVector(locationDim, MinInitialVelocity, MaxInitialVelocity), bounds);
            return particle;
        }
示例#4
0
        public static IParticle Create(PsoParticleType type,
                                       int locationDim,
                                       int fitnessDim,
                                       IFitnessFunction <double[], double[]> function,
                                       double restartEpsilon   = double.MaxValue,
                                       int iterationsToRestart = int.MaxValue,
                                       DimensionBound[] bounds = null,
                                       double[] initVelocity   = null
                                       )
        {
            IParticle particle = null;
            var       rand     = RandomGenerator.GetInstance();

            switch (type)
            {
            case PsoParticleType.Standard:
            case PsoParticleType.FullyInformed:
                particle = new StandardParticle(restartEpsilon, iterationsToRestart);
                break;

            case PsoParticleType.ChargedParticle:
                particle = new ChargedParticle();
                break;

            case PsoParticleType.DummyParticle:
                particle = new DummyParticle();
                break;
            }

            var x = bounds != null?rand.RandomVector(locationDim, bounds) : rand.RandomVector(locationDim);

            particle.Init(new ParticleState(x, function.Evaluate(x)),
                          initVelocity ?? rand.RandomVector(locationDim, MinInitialVelocity, MaxInitialVelocity), bounds);
            return(particle);
        }
示例#5
0
        private static void Print(IPopulation <MathProgram> pop, uint generation,
                                  IFitnessFunction <MathProgram> fitnessFunction, double diff)
        {
            var elem = pop.BestProgram;

            Console.WriteLine(
                $"Gen {generation:000}, pop: {pop.Count:000}, diff: {diff:0.00}, " +
                $"fitness: {fitnessFunction.Evaluate(elem):0.00} | {elem}");
        }
示例#6
0
        public void pruebaMutacion()
        {
            var cromosoma = new CocheCromosoma();

            cromosoma.Generate();
            Console.WriteLine(fitness.Evaluate(cromosoma));
            for (int i = 0; i < 500; i++)
            {
                cromosoma.Mutate();
                cromosoma.Evaluate(fitness);
                Console.WriteLine(cromosoma.Fitness);
            }
        }
示例#7
0
        public void Run(PsoParameters psoParameters, IParticle[] proxyParticleServices = null)
        {
            _function = FunctionFactory.GetFitnessFunction(psoParameters.FunctionParameters);

            var useGpu = psoParameters.GpuParameters.UseGpu && GpuController.AnySupportedGpu();

            CudaParticle cudaParticle = null;

            if (useGpu)
            {
                cudaParticle = PrepareCudaAlgorithm(psoParameters);
            }

            var particles = PrepareParticles(psoParameters, proxyParticleServices, cudaParticle);

            RunningParameters = psoParameters;
            _algorithm        = new PsoAlgorithm(psoParameters, _function, particles.ToArray());

            _cudaReadyLock = new AutoResetEvent(false);

            if (useGpu)
            {
                RunningCudaAlgorithm = Task <ParticleState> .Factory.StartNew(() =>
                {
                    _cudaAlgorithm.Initialize();
                    _cudaReadyLock.Set();
                    var result = _cudaAlgorithm.Run(_cudaTokenSource.Token);
                    _function.Evaluate(result.Location);
                    _cudaAlgorithm.Dispose();
                    return(result);
                }, _cudaTokenSource.Token);
            }
            else
            {
                _cudaReadyLock.Set();
            }

            RunningAlgorithm = Task <ParticleState> .Factory.StartNew(delegate
            {
                _cudaReadyLock.WaitOne();
                _cudaReadyLock.Dispose();
                return(StartAlgorithm(_tokenSource.Token));
            }, _tokenSource.Token);
        }
示例#8
0
 /// <summary>
 /// Calculates the Fitness this genome based in the FitnessFunction implements
 /// </summary>
 /// <returns>Value of the Fitness Genome</returns>
 public double Evaluate()
 {
     return(fitnessFunction.Evaluate(this));
 }
 /// <summary>
 /// Evaluate chromosome with specified fitness function.
 /// </summary>
 /// 
 /// <param name="function">Fitness function to use for evaluation of the chromosome.</param>
 /// 
 /// <remarks><para>Calculates chromosome's fitness using the specifed fitness function.</para></remarks>
 ///
 public void Evaluate( IFitnessFunction function )
 {
     fitness = function.Evaluate( this );
 }
 /// <summary>
 /// Evaluate chromosome with specified fitness function.
 /// </summary>
 /// <param name="function">Fitness function to use for evaluation of the chromosome.</param>
 /// <remarks><para>Calculates chromosome's fitness using the specifed fitness function.</para></remarks>
 public void Evaluate(IFitnessFunction function)
 {
     this.fitness = function.Evaluate((IChromosome)this);
 }
示例#11
0
        public void Run(PsoParameters psoParameters, IParticle[] proxyParticleServices = null)
        {
            _function = FunctionFactory.GetFitnessFunction(psoParameters.FunctionParameters);

            var useGpu = psoParameters.GpuParameters.UseGpu && GpuController.AnySupportedGpu();

            CudaParticle cudaParticle = null;

            if (useGpu)
                cudaParticle = PrepareCudaAlgorithm(psoParameters);

            var particles = PrepareParticles(psoParameters,proxyParticleServices,cudaParticle);
            RunningParameters = psoParameters;
            _algorithm = new PsoAlgorithm(psoParameters, _function, particles.ToArray());

            _cudaReadyLock = new AutoResetEvent(false);

            if (useGpu)
                RunningCudaAlgorithm = Task<ParticleState>.Factory.StartNew(() =>
                {
                    _cudaAlgorithm.Initialize();
                    _cudaReadyLock.Set();
                    var result = _cudaAlgorithm.Run(_cudaTokenSource.Token);
                    _function.Evaluate(result.Location);
                    _cudaAlgorithm.Dispose();
                    return result;

                }, _cudaTokenSource.Token);
            else
                _cudaReadyLock.Set();

            RunningAlgorithm = Task<ParticleState>.Factory.StartNew(delegate
            {
                _cudaReadyLock.WaitOne();
                _cudaReadyLock.Dispose();
                return StartAlgorithm(_tokenSource.Token);
            }, _tokenSource.Token);
        }
示例#12
0
 public void Evaluate(IFitnessFunction function)
 {
     m_fitness      = function.Evaluate(this);
     GenesEvaluated = this.Genes.ToList();
 }
示例#13
0
        public virtual void Transpose(IFitnessFunction<double[], double[]> function)
        {
            double[] newLocation;
            var restart = _sinceLastImprovement == _iterationsToRestart;
            if (restart)
            {
                newLocation = RandomGenerator.GetInstance().RandomVector(CurrentState.Location.Length, Bounds);
                _sinceLastImprovement = 0;
            }
            else
            {
                newLocation = GetClampedLocation(CurrentState.Location.Select((x, i) => x + Velocity[i]).ToArray());
            }
            var newVal = function.Evaluate(newLocation);
            var oldBest = PersonalBest;
            CurrentState = new ParticleState(newLocation, newVal);

            if (Optimization.IsBetter(newVal, PersonalBest.FitnessValue) < 0)
            {
                PersonalBest = CurrentState;
                _sinceLastImprovement = 0;
            }
            else
            {
                _sinceLastImprovement++;
            }
        }
示例#14
0
        /// <summary>
        ///     Simplifies the expression of the given <see cref="MathProgram" /> by returning a sub-combination whose fitness is
        ///     approximately equal to that of the original program by a margin of <paramref name="margin" />, and whose
        ///     expression is naturally simpler, i.e., has fewer sub-programs.
        /// </summary>
        /// <returns>An program corresponding to a simplification of the given program.</returns>
        /// <param name="program">The program we want to simplify.</param>
        /// <param name="fitnessFunction">The fitness function used to determine equivalence.</param>
        /// <param name="margin">
        ///     The acceptable difference between the fitness of the given program and that of a simplified program for them to be
        ///     considered equivalent.
        /// </param>
        public static ITreeProgram <double> Simplify(
            this MathProgram program, IFitnessFunction <MathProgram> fitnessFunction, double margin = DEFAULT_MARGIN)
        {
            // gets original program's fitness and count
            var simplified = program;
            var fitness    = fitnessFunction.Evaluate(program);
            var length     = program.Length;

            // first replaces all sub-programs by infinity and NaN recursively
            var consts = new HashSet <Constant>
            {
                new Constant(double.NegativeInfinity),
                new Constant(double.NaN),
                new Constant(double.PositiveInfinity)
            };
            bool simplificationFound;

            do
            {
                simplificationFound = false;
                var simpLength = simplified.Length;
                for (var i = 0u; i < simpLength && !simplificationFound; i++)
                {
                    if (consts.Contains(simplified.ProgramAt(i)))
                    {
                        continue;
                    }
                    foreach (var constant in consts)
                    {
                        var prog       = (MathProgram)simplified.Replace(i, constant);
                        var fit        = fitnessFunction.Evaluate(prog);
                        var progLength = prog.Length;
                        if (Math.Abs(fit - fitness) < margin && progLength <= length)
                        {
                            simplified          = prog;
                            length              = progLength;
                            simplificationFound = true;
                            break;
                        }
                    }
                }
            } while (simplificationFound);

            // then gets all sub-combinations of the simplified program
            var alternatives = simplified.GetSubCombinations();

            foreach (MathProgram subComb in alternatives)
            {
                var subFit        = fitnessFunction.Evaluate(subComb);
                var subCombLength = subComb.Length;

                //checks their fitness and count
                if (Math.Abs(subFit - fitness) < margin && subCombLength < length)
                {
                    simplified = subComb;
                    length     = subCombLength;
                }
            }

            return(simplified);
        }
示例#15
0
 public void Evaluate(IFitnessFunction function, int objectiveIndex)
 {
     _objectives[objectiveIndex] = function.Evaluate(this);
 }
示例#16
0
 public void EvaluateChromosome(GPChromosome c)
 {
     c.fitness = fitnessFunction.Evaluate(c.expressionTree, functionSet);
 }
示例#17
0
 /// <summary>
 /// Evaluate chromosome with specified fitness function.
 /// </summary>
 ///
 /// <param name="function">Fitness function to use for evaluation of the chromosome.</param>
 ///
 /// <remarks><para>Calculates chromosome's fitness using the specifed fitness function.</para></remarks>
 ///
 public void Evaluate(IFitnessFunction function)
 {
     _objectives[0] = function.Evaluate(this);
 }
 /// <summary>
 /// Evaluate chromosome with specified fitness function.
 /// </summary>
 /// 
 /// <param name="function">Fitness function to use for evaluation of the chromosome.</param>
 /// 
 /// <remarks><para>Calculates chromosome's fitness using the specifed fitness function.</para></remarks>
 ///
 public void Evaluate( IFitnessFunction function )
 {
     fitness = function.Evaluate( this );
 }
示例#19
0
 public virtual void Evaluate(IFitnessFunction function)
 {
     Fitness = function.Evaluate(this);
 }