Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSO.Algorithm
{
    public class Particle
    {
        #region Fields
        static Random rnd = new Random();
        #endregion

        #region Properties
        public double[] Position { get; private set; }
        public double Fitness { get; private set; }
        public double[] BestPosition { get; private set; }
        public double BestFitness { get; private set; }
        public double[] Velocity { get; private set; }
        #endregion

        #region Ctor
        public Particle(double[] position, double[] velocity)
        {
            Position = position;
            Velocity = velocity;
            BestFitness = Double.MaxValue;
            BestPosition = new double[Position.Length];
        }
        #endregion

        #region Methods
        public void UpdateBestPosition()
        {
            if (Fitness < BestFitness)
            {
                BestFitness = Fitness;
                Position.CopyTo(BestPosition, 0);
            }
        }

        public void UpdateFitness(double fitness)
        {
            Fitness = fitness;
        }

        public void UpdateVelocity(double[] velocity)
        {
            velocity.CopyTo(Velocity, 0);
        }

        public void UpdatePosition(double[] position)
        {
            position.CopyTo(Position, 0);
        }

        #endregion
    }

    namespace PSO
    {
        public class ParticleSwarm
        {
            #region Fields
            static Random rnd = new Random();
            #endregion

            #region Properties
            public bool UseTightness { get; private set; }
            public double COneValue { get; private set; }
            public double CTwoValue { get; private set; }
            public double? Tightness { get; private set; }
            public double Inertia { get; private set; }
            public double BestFitness { get; private set; }
            public double[] BestPosition { get; private set; }
            public List<particle> Particles { get; private set; }
            public Function OptimizationFunction { get; private set; }
            public int SwarmSize { get; private set; }
            public int Dimension { get; private set; }
            public double Vmax { get; private set; }
            #endregion

            #region Ctor
            public ParticleSwarm(int swarmSize, Function function, double inertia, double cOneValue, double cTwoValue, double vmax, bool useTightness, double? tigthness = null)
            {
                SwarmSize = swarmSize;
                Inertia = inertia;
                COneValue = cOneValue;
                CTwoValue = cTwoValue;
                Vmax = vmax;
                OptimizationFunction = function;
                UseTightness = useTightness;
                Tightness = tigthness;

                Dimension = 3;
                BestFitness = Double.MaxValue;
                BestPosition = new double[Dimension];

                InitializeParticles();
            }
            #endregion Ctor

            #region Private Methods
            private void InitializeParticles()
            {
                Particles = new List<particle>();

                for (int i = 0; i < SwarmSize; i++)
                {
                    double[] randomParticlePosition = new double[Dimension];
                    for (int j = 0; j < Dimension; j++)
                    {
                        randomParticlePosition[j] = NextDouble(OptimizationFunction.LowerBound, OptimizationFunction.UpperBound);
                    }

                    double[] randomParticleVelocity = new double[Dimension];

                    for (int k = 0; k < Dimension; k++)
                    {
                        if (UseTightness)
                        {
                            randomParticleVelocity[k] = 0;
                            continue;
                        }

                        double minVelocity = (-1 * Vmax) / 3;
                        double maxVelocity = Vmax / 3;

                        randomParticleVelocity[k] = NextDouble(minVelocity, maxVelocity);
                    }

                    Particles.Add(new Particle(randomParticlePosition, randomParticleVelocity));

                }
            }

            private double NextDouble(double min, double max)
            {
                return rnd.NextDouble() * (Math.Abs(max - min)) + min;
            }

            private double CorrectVelocityToVmax(double velocity)
            {
                if (velocity < -1 * Vmax)
                {
                    return -1 * Vmax;
                }
                else if (velocity > Vmax)
                {
                    return Vmax;
                }
                else
                {
                    return velocity;
                }
            }

            private double CorrectPositionToDomain(double x)
            {
                if (x < OptimizationFunction.LowerBound)
                {
                    return OptimizationFunction.LowerBound;
                }
                else if (x > OptimizationFunction.UpperBound)
                {
                    return OptimizationFunction.UpperBound;
                }
                else
                {
                    return x;
                }
            }

            private void UpdateParticleBestPosition(Particle p)
            {
                double particleFitness = OptimizationFunction.Evaluate(p.Position);
                p.UpdateFitness(particleFitness);
                p.UpdateBestPosition();
            }

            private void UpdateGlobalBestPosition(Particle p)
            {
                if (p.BestFitness < this.BestFitness)
                {
                    this.BestFitness = p.BestFitness;
                    p.Position.CopyTo(this.BestPosition, 0);
                }
            }

            #endregion

            #region Public Methods
            public void Iteration()
            {
                foreach (Particle p in Particles)
                {
                    UpdateParticleBestPosition(p);
                    UpdateGlobalBestPosition(p);
                }

                foreach (Particle p in Particles)
                {
                    double[] newVelocity = new double[Dimension];
                    double[] newPosition = new double[Dimension];

                    for (int i = 0; i < Dimension; i++)
                    {
                        double firstRnd = NextDouble(0, 1);
                        double secondRnd = NextDouble(0, 1);

                        // Scisk

                        if (UseTightness)
                        {
                            double cognitiveWeight = COneValue * firstRnd * (p.BestPosition[i] - p.Position[i]);
                            double socialWeight = CTwoValue * secondRnd * (this.BestPosition[i] - p.Position[i]);

                            newVelocity[i] = (double)Tightness * p.Velocity[i] + cognitiveWeight + socialWeight;
                        }

                        // Bez scisku - inercja + vmax
                        else
                        {
                            double cognitiveWeight = COneValue * firstRnd * (p.BestPosition[i] - p.Position[i]);
                            double socialWeight = CTwoValue * secondRnd * (this.BestPosition[i] - p.Position[i]);

                            newVelocity[i] = Inertia * p.Velocity[i] + cognitiveWeight + socialWeight;

                            // ograniczenie predkosci do Vmax

                            newVelocity[i] = CorrectVelocityToVmax(newVelocity[i]);
                        }

                        newPosition[i] = p.Position[i] + newVelocity[i];

                        // ograniczenie pozycji do zakresu domeny

                        newPosition[i] = CorrectPositionToDomain(newPosition[i]);
                    }

                    p.UpdateVelocity(newVelocity);
                    p.UpdatePosition(newPosition);

                }
            }
            #endregion
        }
    }
}
writes as an error:

CS0246 The type or namespace name 'Function' could not be found (are you missing a using directive or an assembly reference?)

CS0246 The type or namespace name 'Function' could not be found (are you missing a using directive or an assembly reference?)


I would be glad if you help. Thank you.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSO.Algorithm
{
    public class Particle
    {
        #region Fields
        static Random rnd = new Random();
        #endregion

        #region Properties
        public double[] Position { get; private set; }
        public double Fitness { get; private set; }
        public double[] BestPosition { get; private set; }
        public double BestFitness { get; private set; }
        public double[] Velocity { get; private set; }
        #endregion

        #region Ctor
        public Particle(double[] position, double[] velocity)
        {
            Position = position;
            Velocity = velocity;
            BestFitness = Double.MaxValue;
            BestPosition = new double[Position.Length];
        }
        #endregion

        #region Methods
        public void UpdateBestPosition()
        {
            if (Fitness < BestFitness)
            {
                BestFitness = Fitness;
                Position.CopyTo(BestPosition, 0);
            }
        }

        public void UpdateFitness(double fitness)
        {
            Fitness = fitness;
        }

        public void UpdateVelocity(double[] velocity)
        {
            velocity.CopyTo(Velocity, 0);
        }

        public void UpdatePosition(double[] position)
        {
            position.CopyTo(Position, 0);
        }

        #endregion
    }

    namespace PSO
    {
        public class ParticleSwarm
        {
            #region Fields
            static Random rnd = new Random();
            #endregion

            #region Properties
            public bool UseTightness { get; private set; }
            public double COneValue { get; private set; }
            public double CTwoValue { get; private set; }
            public double? Tightness { get; private set; }
            public double Inertia { get; private set; }
            public double BestFitness { get; private set; }
            public double[] BestPosition { get; private set; }
            public List<particle> Particles { get; private set; }
            public Function OptimizationFunction { get; private set; }
            public int SwarmSize { get; private set; }
            public int Dimension { get; private set; }
            public double Vmax { get; private set; }
            #endregion

            #region Ctor
            public ParticleSwarm(int swarmSize, Function function, double inertia, double cOneValue, double cTwoValue, double vmax, bool useTightness, double? tigthness = null)
            {
                SwarmSize = swarmSize;
                Inertia = inertia;
                COneValue = cOneValue;
                CTwoValue = cTwoValue;
                Vmax = vmax;
                OptimizationFunction = function;
                UseTightness = useTightness;
                Tightness = tigthness;

                Dimension = 3;
                BestFitness = Double.MaxValue;
                BestPosition = new double[Dimension];

                InitializeParticles();
            }
            #endregion Ctor

            #region Private Methods
            private void InitializeParticles()
            {
                Particles = new List<particle>();

                for (int i = 0; i < SwarmSize; i++)
                {
                    double[] randomParticlePosition = new double[Dimension];
                    for (int j = 0; j < Dimension; j++)
                    {
                        randomParticlePosition[j] = NextDouble(OptimizationFunction.LowerBound, OptimizationFunction.UpperBound);
                    }

                    double[] randomParticleVelocity = new double[Dimension];

                    for (int k = 0; k < Dimension; k++)
                    {
                        if (UseTightness)
                        {
                            randomParticleVelocity[k] = 0;
                            continue;
                        }

                        double minVelocity = (-1 * Vmax) / 3;
                        double maxVelocity = Vmax / 3;

                        randomParticleVelocity[k] = NextDouble(minVelocity, maxVelocity);
                    }

                    Particles.Add(new Particle(randomParticlePosition, randomParticleVelocity));

                }
            }

            private double NextDouble(double min, double max)
            {
                return rnd.NextDouble() * (Math.Abs(max - min)) + min;
            }

            private double CorrectVelocityToVmax(double velocity)
            {
                if (velocity < -1 * Vmax)
                {
                    return -1 * Vmax;
                }
                else if (velocity > Vmax)
                {
                    return Vmax;
                }
                else
                {
                    return velocity;
                }
            }

            private double CorrectPositionToDomain(double x)
            {
                if (x < OptimizationFunction.LowerBound)
                {
                    return OptimizationFunction.LowerBound;
                }
                else if (x > OptimizationFunction.UpperBound)
                {
                    return OptimizationFunction.UpperBound;
                }
                else
                {
                    return x;
                }
            }

            private void UpdateParticleBestPosition(Particle p)
            {
                double particleFitness = OptimizationFunction.Evaluate(p.Position);
                p.UpdateFitness(particleFitness);
                p.UpdateBestPosition();
            }

            private void UpdateGlobalBestPosition(Particle p)
            {
                if (p.BestFitness < this.BestFitness)
                {
                    this.BestFitness = p.BestFitness;
                    p.Position.CopyTo(this.BestPosition, 0);
                }
            }

            #endregion

            #region Public Methods
            public void Iteration()
            {
                foreach (Particle p in Particles)
                {
                    UpdateParticleBestPosition(p);
                    UpdateGlobalBestPosition(p);
                }

                foreach (Particle p in Particles)
                {
                    double[] newVelocity = new double[Dimension];
                    double[] newPosition = new double[Dimension];

                    for (int i = 0; i < Dimension; i++)
                    {
                        double firstRnd = NextDouble(0, 1);
                        double secondRnd = NextDouble(0, 1);

                        // Scisk

                        if (UseTightness)
                        {
                            double cognitiveWeight = COneValue * firstRnd * (p.BestPosition[i] - p.Position[i]);
                            double socialWeight = CTwoValue * secondRnd * (this.BestPosition[i] - p.Position[i]);

                            newVelocity[i] = (double)Tightness * p.Velocity[i] + cognitiveWeight + socialWeight;
                        }

                        // Bez scisku - inercja + vmax
                        else
                        {
                            double cognitiveWeight = COneValue * firstRnd * (p.BestPosition[i] - p.Position[i]);
                            double socialWeight = CTwoValue * secondRnd * (this.BestPosition[i] - p.Position[i]);

                            newVelocity[i] = Inertia * p.Velocity[i] + cognitiveWeight + socialWeight;

                            // ograniczenie predkosci do Vmax

                            newVelocity[i] = CorrectVelocityToVmax(newVelocity[i]);
                        }

                        newPosition[i] = p.Position[i] + newVelocity[i];

                        // ograniczenie pozycji do zakresu domeny

                        newPosition[i] = CorrectPositionToDomain(newPosition[i]);
                    }

                    p.UpdateVelocity(newVelocity);
                    p.UpdatePosition(newPosition);

                }
            }
            #endregion
        }
    }
}
writes as an error:

CS0246 The type or namespace name 'Function' could not be found (are you missing a using directive or an assembly reference?)

CS0246 The type or namespace name 'Function' could not be found (are you missing a using directive or an assembly reference?)
I would be glad if you help. Thank you.
Posted
Updated 21-Apr-20 16:33pm
v4
Comments
BillWoodruff 23-Apr-20 3:02am    
You are "drowning" in code you didn't write and which you don't have the tools to understand. You need a basic education in the C# language. Either you are in the wrong course, or you have the wrong teacher.

1 solution

The error message is telling you exactly what the problem is: Function is not defined anywhere. I'd guess it is a class since you try to access the LowerBound and UpperBound properties and call the Evaluate method. You are missing the source code that defines that class.
 
Share this answer
 
Comments
Member 14806007 22-Apr-20 6:37am    
I am very new in programming. What do I do about this problem will be improved?
MadMyche 22-Apr-20 7:34am    
You are going to have to go to the original source of the code you are working with, and find the classes that are being referenced in them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900