pso – rosenbrock(10) for example. user can use methods init() - polymorphism –init(int...
Post on 02-Apr-2015
216 Views
Preview:
TRANSCRIPT
PSO – Rosenbrock(10) for Example
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)
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)
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
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
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.
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.
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.
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
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.
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).
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, 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).
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, 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).
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 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)
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)
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
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
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.
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.
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.
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
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• {• 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.
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).
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, 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).
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, 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).
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)… } • }
top related