0% found this document useful (0 votes)
36 views

PSO - Rosenbrock (10) For Ex Ample

This document discusses how to implement a particle swarm optimization (PSO) algorithm for solving problems. It describes the PSO class and methods that can be used, including Init() to initialize the swarm, Run() to perform iterations, and Update() to update particle positions. It also discusses overriding attributes and methods, and using the PSOOption class to specify algorithm parameters like guiding points. Examples are provided of implementing PSO for the Rosenbrock test function, including initializing particles and calling methods over multiple iterations.

Uploaded by

surjyo_8
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

PSO - Rosenbrock (10) For Ex Ample

This document discusses how to implement a particle swarm optimization (PSO) algorithm for solving problems. It describes the PSO class and methods that can be used, including Init() to initialize the swarm, Run() to perform iterations, and Update() to update particle positions. It also discusses overriding attributes and methods, and using the PSOOption class to specify algorithm parameters like guiding points. Examples are provided of implementing PSO for the Rosenbrock test function, including initializing particles and calling methods over multiple iterations.

Uploaded by

surjyo_8
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

PSO – Rosenbrock(10) for Ex

ample
User can use methods
• Init() - polymorphism
– Init(int PopulationSize, int VariableDimension, double VariableLo
werbound, double VariableUpperbound, string RepeatableOptio
n)
– Init(int PopulationSize, int VariableDimension, double[] VariableL
owerbound, double[] VariableUpperbound, string RepeatableOpt
ion)
• Run()
– Run(int Iteration)
• Update() - polymorphism
– Update(string FirstGuidingPoint, string SecondGuidingPoint)
– Update(string FirstGuidingPoint, string SecondGuidingPoint,
string ThirdGuidingPoint)
User can override attributes and
methods
• Attribute (lSize default is 3 and RefSet def
ault is 10)
– public override int lSize { get { return 5; } }
– public override int RefSetSize { get { return
20; }}
• Method
– public override double Fitness(double[]
sol)
PSOOption Class
• User used PSOOption when having no idea to d
etermine problem options or strategy options.
• PSOOption
– RepeatableOption
• Repeatable, Nonrepeatable
– FirstGuidingPoint
• Pbest, Lbest, Gbest, Random, RefSet
– SecondGuidingPoint
• Pbest, Lbest, Gbest, Random, RefSet
– ThirdGuidingPoint
• Pbest, Lbest, Gbest, Random, RefSet
PSOOption Class
PSOOption.RepeatableOption.Repeatable

PSOOption.RepeatableOption.Nonrepeatable

PSOOption.FirstGuidingPoint.Pbset
PSOOption.FirstGuidingPoint.Lbset

PSOOption.FirstGuidingPoint.Gbset
PSOOption.FirstGuidingPoint.RefSet

PSOOption.FirstGuidingPoint.Random

PSOOption.SecondGuidingPoint.Pbset
PSOOption.SecondGuidingPoint.Lbset

PSOOption.SecondGuidingPoint.Gbset
PSOOption.SecondGuidingPoint.RefSet

PSOOption.SecondGuidingPoint.Random

PSOOption.ThirdGuidingPoint.Pbset
PSOOption.ThirdGuidingPoint.Lbset

PSOOption.ThirdGuidingPoint.Gbset
PSOOption.ThirdGuidingPoint.RefSet

PSOOption.ThirdGuidingPoint.Random
Step 1 and 2
• using System;
• using System.Collections.Generic;
• using System.Text;
• using Metaheuristic; Step 1: Include Metaheuristic.

• namespace Testing
• {
• class Rosenbrock : PSO
• {
• Step2: Problem class must inherit PSO.
• }
• }
Step 3 and 4
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• static void Main(string[] args)
• {
• Rosenbrock bird = new Rosenbrock(); Step 3: Creating object.
• }
• public override double Fitness(double[] pos)
• {
• double fitness = 0;
• for (int j = 0; j < pos.Length - 1; j++)
• fitness = fitness + 100 * Math.Pow(pos[j + 1] - Math.Pow(pos[j], 2), 2) + Math.Pow(pos[j] - 1, 2);
• return fitness;
• }
• }
• }
Step 4: To override Fitness function.
Step 5
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• double[] Low = new double[10] { -10, -10, … , -10 };
• double[] Up = new double[10] { 10, 10, … , 10 };
• static void Main(string[] args)
• { Polymorphism
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, PSOOption.RepeatableOption.Repeatable);
• //bird.Init(40, 10, Low, Up, “Repeatable”);
• }
• public override double Fitness(double[] pos)…
• } User can use arrays to input lower bound and upper bound
• }
for each variable.
RepeatableOption has two expressions of PSOOption and string.
Step 6 – Simple Call
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• static void Main(string[] args) Default is Constriction Factor PSO
• {
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, “Repeatable”);
• bird.Run(4000);
• }
• public override double Fitness(double[] pos)…
• }
• }
Step 6 – Advanced Call
• using …
• namespace Testing
• { If user used Lbest or RefSet as guiding point, user
• class Rosenbrock : PSO
• {
would override lSize and RefSetSize.
• static void Main(string[] args)
• {
• //public override int lSize { get { return 5; } }
• //public override int RefSetSize { get { return 20; }}
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, “Repeatable”); Polymorphism
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update();
• //bird.Update(“Pbest”, PSOOption.SecondGuidingPoint.Lbest);
• //bird.Update(PSOOption.FirstGuidingPoint.Pbest, “Gbest", “RefSet");
• }
• public override double Fitness(double[] pos)…
• }
• } FirstGuidingPoint, SecondGuidingPoint and ThirdGuidingPoint have two
expressions of PSOOption and string.
User requirement 1 for Rosenbrock
(10)
• User requirement.
– PopulationSize = 40, VariableDimension = 10, Variabl
eLowerbound = -10, VariableUpperbound = 10, Repe
atableOption = “Repeatable”.
– Iteration = 4000.
– Using two guiding points of pbest and gbest.
• We will give three examples to satisfy him if user
has requirement above.
• User can access public attributes for Gbest (the
best sol for global) and GbestFitness (the best fit
ness for global).
Example 1 – Simple Call
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• static void Main(string[] args)
• {
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, PSOOption.RepeatableOption.Repeatable);
• bird.Run(4000);
• }
• public override double Fitness(double[] pos)…
• }
• }
Example 2 – Advanced Call
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• static void Main(string[] args)
• {
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, PSOOption.RepeatableOption.Repeatable);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update();
• }
• public override double Fitness(double[] pos)…
• }
• }
Example 3 – Advanced Call
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• static void Main(string[] args)
• {
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, PSOOption.RepeatableOption.Repeatable);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update(PSOOption.FirstGuidingPoint.Pbest,
• PSOOption.SecondGuidingPoint.Gbest);
• }
• public override double Fitness(double[] pos)…
• }
• }
User requirement 2 for Rosenbrock
(10)
• User requirement.
– PopulationSize = 40, VariableDimension = 10, Variabl
eLowerbound = -10, VariableUpperbound = 10, Repe
atableOption = “Repeatable”.
– Iteration = 4000.
– Using two guiding points of pbest and lbest.
• We will give an example to satisfy him if user ha
s requirement above.
• User can access public attributes for Gbest (the
best sol for global) and GbestFitness (the best fit
ness for global).
Example 1 – Advanced Call
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• public override int lSize { get { return 5; } }
• static void Main(string[] args)
• {
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, PSOOption.RepeatableOption.Repeatable);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update(PSOOption.FirstGuidingPoint.Pbest,
• PSOOption.SecondGuidingPoint.Lbest);
• }
• public override double Fitness(double[] pos)…
• }
• }
User requirement 3 for Rosenbrock
(10)
• User requirement.
– PopulationSize = 40, VariableDimension = 10, Variabl
eLowerbound = -10, VariableUpperbound = 10, Repe
atableOption = “Repeatable”.
– Iteration = 4000.
– Using three guiding points of pbest, gbest and RefSet.
• We will give an example to satisfy him if user ha
s requirement above.
• User can access public attributes for Gbest (the
best sol for global) and GbestFitness (the best fit
ness for global).
Example 1 – Advanced Call
• using …
• namespace Testing
• {
• class Rosenbrock : PSO
• {
• public override int RefSetSize { get { return 20; } }
• static void Main(string[] args)
• {
• Rosenbrock bird = new Rosenbrock();
• bird.Init(40, 10, -10, 10, PSOOption.RepeatableOption.Repeatable);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update(PSOOption.FirstGuidingPoint.Pbest,
• PSOOption.SecondGuidingPoint.Gbest
• PSOOption.ThirdGuidingPoint.RefSet);
• }
• public override double Fitness(double[] pos)…
• }
• }
PSO – TSP for Example
User can use methods
• Init() - polymorphism
– Init(int PopulationSize, int VariableDimension, double VariableLo
werbound, double VariableUpperbound, string RepeatableOptio
n)
– Init(int PopulationSize, int VariableDimension, double[] VariableL
owerbound, double[] VariableUpperbound, string RepeatableOpt
ion)
• Run()
– Run(int Iteration)
• Update() - polymorphism
– Update(string FirstGuidingPoint, string SecondGuidingPoint)
– Update(string FirstGuidingPoint, string SecondGuidingPoint,
string ThirdGuidingPoint)
User can override attributes and
methods
• Attribute (lSize default is 3 and RefSet def
ault is 10)
– public override int lSize { get { return 5; } }
– public override int RefSetSize { get { return
20; }}
• Method
– public override double Fitness(double[]
sol)
PSOOption Class
• User used PSOOption when having no idea to d
etermine problem options or strategy options.
• PSOOption
– RepeatableOption
• Repeatable, Nonrepeatable
– FirstGuidingPoint
• Pbest, Lbest, Gbest, Random, RefSet
– SecondGuidingPoint
• Pbest, Lbest, Gbest, Random, RefSet
– ThirdGuidingPoint
• Pbest, Lbest, Gbest, Random, RefSet
PSOOption Class
PSOOption.RepeatableOption.Repeatable

PSOOption.RepeatableOption.Nonrepeatable

PSOOption.FirstGuidingPoint.Pbset
PSOOption.FirstGuidingPoint.Lbset

PSOOption.FirstGuidingPoint.Gbset
PSOOption.FirstGuidingPoint.RefSet

PSOOption.FirstGuidingPoint.Random

PSOOption.SecondGuidingPoint.Pbset
PSOOption.SecondGuidingPoint.Lbset

PSOOption.SecondGuidingPoint.Gbset
PSOOption.SecondGuidingPoint.RefSet

PSOOption.SecondGuidingPoint.Random

PSOOption.ThirdGuidingPoint.Pbset
PSOOption.ThirdGuidingPoint.Lbset

PSOOption.ThirdGuidingPoint.Gbset
PSOOption.ThirdGuidingPoint.RefSet

PSOOption.ThirdGuidingPoint.Random
Step 1 and 2
• using System;
• using System.Collections.Generic;
• using System.Text;
• using Metaheuristic; Step 1: Include Metaheuristic.

• namespace Testing
• {
• class TSP : PSO
• {
• Step2: Problem class must inherit PSO.
• }
• }
Step 3
• using …
• namespace Testing
• { To read city distance.
• class TSP : PSO
• {
• double[,] distance = new double[10, 10];
• static void Main(string[] args)
• {}
• public void Read()
• {
• StreamReader sr = new StreamReader(@” tsp01.txt”);
• string line = sr.ReadToEnd();
• string[] AllLine = line.Split(',', '\n');

• for (int i = 0; i < distance.GetLength(0); i++)


• for (int j = 0; j < distance.GetLength(1); j++)
• distance[i, j] = double.Parse(AllLine[i * (distance.GetLength(1)) + j]);
• sr.Close();
• }
• }
• }
Step 4 and 5
• using …
• namespace Testing
• {
• class TSP : ACO
• {
• double[,] distance = new double[10, 10];
• static void Main(string[] args)
• {
• TSP bird = new TSP(); Step 4: Creating object.
• }
• public void Read()…
• public override double Fitness(double[] sol)
• {
• double sum = 0;
• for (int j = 0; j < sol.GetLength(0) - 1; j++)
• sum = sum + distance[(int)Math.Round(sol[j]), (int)Math.Round(sol[j + 1])];
• sum = sum + distance[(int)Math.Round(sol[sol.GetLength(0) - 1]), (int)Math.Round(sol[0])];
• return sum;
• }
• }
• }
Step 5: To override Fitness function.
Step 6
• using …
• namespace Testing
• {
• class TSP : PSO
• {
• double[,] distance = new double[10, 10];
• double[] Low = new double[10] { 0, 0, … , 0 };
• double[] Up = new double[10] { 9, 9, … , 9 };
• static void Main(string[] args)
• { Polymorphism
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, PSOOption.RepeatableOption.Nonrepeatable);
• //bird.Init(40, 10, Low, Up, “Nonrepeatable”);
• }
• public void Read()…
• public override double Fitness(int[] sol)…
• } User can use arrays to input lower bound and upper bound
• } for each variable.
RepeatableOption has two expressions of PSOOption and string.
Step 7 - Simple Call
• using …
• namespace Testing
• {
• class TSP : PSO
• {
• double[,] distance = new double[10, 10];
• static void Main(string[] args)
• {
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, PSOOption.RepeatableOption.Nonrepeatable);
• bird.Run(4000);
• }
• public void Read()…
• public override double Fitness(int[] sol)…
• }
• }
Step 7 - Advanced Call
• using …
• namespace Testing
• { If user used Lbest or RefSet as guiding point, user
• class TSP : PSO
• {
would override lSize and RefSetSize.
• double[,] distance = new double[10, 10];
• //public override int lSize { get { return 5; } }
• //public override int RefSetSize { get { return 20; }}
• static void Main(string[] args)
• { Polymorphism
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, PSOOption.RepeatableOption.Nonrepeatable);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update();
• //bird.Update(“Pbest”, PSOOption.SecondGuidingPoint.Lbest);
• //bird.Update(PSOOption.FirstGuidingPoint.Pbest, “Gbest", “RefSet");
• }
• public void Read()…
• public override double Fitness(int[] sol)…
• }
• }
FirstGuidingPoint, SecondGuidingPoint and ThirdGuidingPoint have two
expressions of PSOOption and string.
User requirement 1 for TSP
• User requirement.
– PopulationSize = 40, VariableDimension = 10, Variabl
eLowerbound = 0, VariableUpperbound = 9, Repeata
bleOption = “Nonrepeatable”.
– Iteration = 4000.
– Using two guiding points of pbest and gbest.
• We will give three examples to satisfy him if user
has requirement above.
• User can access public attributes for Gbest (the
best sol for global) and GbestFitness (the best fit
ness for global).
Example 1 – Simple Call
• using …
• namespace Testing
• {
• class TSP : PSO
• {
• double[,] distance = new double[10, 10];
• static void Main(string[] args)
• {
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, “Nonrepeatable”);
• bird.Run(4000);
• }
• public void Read()…
• public override double Fitness(int[] sol)…
• }
• }
Example 2 – Advanced Call
• using …
• namespace Testing
• {
• class TSP : PSO
• {
• double[,] distance = new double[10, 10];
• static void Main(string[] args)
• {
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, “Nonrepeatable”);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update();
• }
• public void Read()…
• public override double Fitness(int[] sol)…
• }
• }
Example 3 – Advanced Call
• using …
• namespace Testing
• {
• class TSP : PSO
• {
• double[,] distance = new double[10, 10];
• static void Main(string[] args)
• {
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, “Nonrepeatable”);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update(“Pbest”, “Gbest”);
• }
• public void Read()…
• public override double Fitness(int[] sol)…
• }
• }
User requirement 2 for TSP
• User requirement.
– PopulationSize = 40, VariableDimension = 10, Variabl
eLowerbound = 0, VariableUpperbound = 9, Repeata
bleOption = “Nonrepeatable”.
– Iteration = 4000.
– Using two guiding points of pbest and lbest.
• We will give an example to satisfy him if user ha
s requirement above.
• User can access public attributes for Gbest (the
best sol for global) and GbestFitness (the best fit
ness for global).
Example 1 – Advanced Call
• using …
• namespace Testing
• {
• class TSP : PSO
• {
• public override int lSize { get { return 5; } }
• static void Main(string[] args)
• {
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, “Nonrepeatable”);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update(“Pbest”, “Lbest”);
• }
• public void Read()…
• public override double Fitness(int[] sol)…
• }
• }
User requirement 3 for TSP
• User requirement.
– PopulationSize = 40, VariableDimension = 10, Variabl
eLowerbound = 0, VariableUpperbound = 9, Repeata
bleOption = “Nonrepeatable”.
– Iteration = 4000.
– Using three guiding points of pbest, gbest and RefSet.
• We will give an example to satisfy him if user ha
s requirement above.
• User can access public attributes for Gbest (the
best sol for global) and GbestFitness (the best fit
ness for global).
Example 1 – Advanced Call
• using …
• namespace Testing
• {
• class TSP : PSO
• {
• public override int RefSetSize { get { return 20; } }
• static void Main(string[] args)
• {
• TSP bird = new TSP();
• bird.Init(40, 10, 0, 9, “Nonrepeatable”);
• for (int iter = 1; iter <= 4000; iter++)
• bird.Update(“Pbest”, “Gbest”, “RefSet”);
• public void Read()…
• public override double Fitness(int[] sol)… }
• }

You might also like