37
PSO – Rosenbrock(10) f or Example

PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Embed Size (px)

Citation preview

Page 1: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

PSO – Rosenbrock(10) for Example

Page 2: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User can use methods

• Init() - polymorphism– Init(int PopulationSize, int VariableDimension, double VariableLo

werbound, double VariableUpperbound, string RepeatableOption)

– Init(int PopulationSize, int VariableDimension, double[] VariableLowerbound, double[] VariableUpperbound, string RepeatableOption)

• Run()– Run(int Iteration)

• Update() - polymorphism– Update(string FirstGuidingPoint, string SecondGuidingPoint)– Update(string FirstGuidingPoint, string SecondGuidingPoint,

string ThirdGuidingPoint)

Page 3: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User can override attributes and methods

• Attribute (lSize default is 3 and RefSet default is 10)– public override int lSize { get { return 5; } }– public override int RefSetSize { get { return

20; }}

• Method– public override double Fitness(double[]

sol)

Page 4: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

PSOOption Class

• User used PSOOption when having no idea to determine 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

Page 5: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

PSOOption ClassPSOOption.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

Page 6: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Step 1 and 2

• using System;• using System.Collections.Generic;• using System.Text;• using Metaheuristic;

• namespace Testing• {• class Rosenbrock : PSO• {• • } • }

Step 1: Include Metaheuristic.

Step2: Problem class must inherit PSO.

Page 7: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Step 3 and 4• using …• namespace Testing• {• class Rosenbrock : PSO• {• static void Main(string[] args)• {• Rosenbrock bird = new Rosenbrock();• }• 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 3: Creating object.

Step 4: To override Fitness function.

Page 8: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)• {• 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 boundfor each variable.

Polymorphism

RepeatableOption has two expressions of PSOOption and string.

Page 9: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Step 6 – Simple Call• using …• namespace Testing• {• class Rosenbrock : PSO• {• static void Main(string[] args)• {• Rosenbrock bird = new Rosenbrock();• bird.Init(40, 10, -10, 10, “Repeatable”);• bird.Run(4000);• }• public override double Fitness(double[] pos)…• } • }

Default is Constriction Factor PSO

Page 10: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Step 6 – Advanced Call• using …• namespace Testing• {• class Rosenbrock : PSO• {• 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”);• 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)…• } • }

Polymorphism

FirstGuidingPoint, SecondGuidingPoint and ThirdGuidingPoint have two expressions of PSOOption and string.

If user used Lbest or RefSet as guiding point, user would override lSize and RefSetSize.

Page 11: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User requirement 1 for Rosenbrock(10)

• User requirement.– PopulationSize = 40, VariableDimension = 10, Variabl

eLowerbound = -10, VariableUpperbound = 10, RepeatableOption = “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 fitness for global).

Page 12: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 13: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 14: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 15: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User requirement 2 for Rosenbrock(10)

• User requirement.– PopulationSize = 40, VariableDimension = 10, Variabl

eLowerbound = -10, VariableUpperbound = 10, RepeatableOption = “Repeatable”.

– Iteration = 4000.– Using two guiding points of pbest and lbest.

• We will give an example to satisfy him if user has requirement above.

• User can access public attributes for Gbest (the best sol for global) and GbestFitness (the best fitness for global).

Page 16: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 17: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User requirement 3 for Rosenbrock(10)

• User requirement.– PopulationSize = 40, VariableDimension = 10, Variabl

eLowerbound = -10, VariableUpperbound = 10, RepeatableOption = “Repeatable”.

– Iteration = 4000.– Using three guiding points of pbest, gbest and RefSet.

• We will give an example to satisfy him if user has requirement above.

• User can access public attributes for Gbest (the best sol for global) and GbestFitness (the best fitness for global).

Page 18: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 19: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

PSO – TSP for Example

Page 20: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User can use methods

• Init() - polymorphism– Init(int PopulationSize, int VariableDimension, double VariableLo

werbound, double VariableUpperbound, string RepeatableOption)

– Init(int PopulationSize, int VariableDimension, double[] VariableLowerbound, double[] VariableUpperbound, string RepeatableOption)

• Run()– Run(int Iteration)

• Update() - polymorphism– Update(string FirstGuidingPoint, string SecondGuidingPoint)– Update(string FirstGuidingPoint, string SecondGuidingPoint,

string ThirdGuidingPoint)

Page 21: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User can override attributes and methods

• Attribute (lSize default is 3 and RefSet default is 10)– public override int lSize { get { return 5; } }– public override int RefSetSize { get { return

20; }}

• Method– public override double Fitness(double[]

sol)

Page 22: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

PSOOption Class

• User used PSOOption when having no idea to determine 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

Page 23: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

PSOOption ClassPSOOption.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

Page 24: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Step 1 and 2

• using System;• using System.Collections.Generic;• using System.Text;• using Metaheuristic;

• namespace Testing• {• class TSP : PSO• {• • } • }

Step 1: Include Metaheuristic.

Step2: Problem class must inherit PSO.

Page 25: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Step 3 • using …• namespace Testing• {• 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();• }• } • }

To read city distance.

Page 26: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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();• }• 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 4: Creating object.

Step 5: To override Fitness function.

Page 27: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)• {• 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 boundfor each variable.RepeatableOption has two expressions of PSOOption and string.

Polymorphism

Page 28: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 29: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

Step 7 - Advanced Call• using …• namespace Testing• {• class TSP : PSO• {• 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)• {• 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)…• } • }

Polymorphism

FirstGuidingPoint, SecondGuidingPoint and ThirdGuidingPoint have two expressions of PSOOption and string.

If user used Lbest or RefSet as guiding point, user would override lSize and RefSetSize.

Page 30: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User requirement 1 for TSP

• User requirement.– PopulationSize = 40, VariableDimension = 10, Variabl

eLowerbound = 0, VariableUpperbound = 9, RepeatableOption = “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 fitness for global).

Page 31: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 32: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 33: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 34: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User requirement 2 for TSP

• User requirement.– PopulationSize = 40, VariableDimension = 10, Variabl

eLowerbound = 0, VariableUpperbound = 9, RepeatableOption = “Nonrepeatable”.

– Iteration = 4000.– Using two guiding points of pbest and lbest.

• We will give an example to satisfy him if user has requirement above.

• User can access public attributes for Gbest (the best sol for global) and GbestFitness (the best fitness for global).

Page 35: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)…• } • }

Page 36: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

User requirement 3 for TSP

• User requirement.– PopulationSize = 40, VariableDimension = 10, Variabl

eLowerbound = 0, VariableUpperbound = 9, RepeatableOption = “Nonrepeatable”.

– Iteration = 4000.– Using three guiding points of pbest, gbest and RefSet.

• We will give an example to satisfy him if user has requirement above.

• User can access public attributes for Gbest (the best sol for global) and GbestFitness (the best fitness for global).

Page 37: PSO – Rosenbrock(10) for Example. User can use methods Init() - polymorphism –Init(int PopulationSize, int VariableDimension, double VariableLowerbound,

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)… } • }