GA – Rosenbrock (10) for Example
description
Transcript of GA – Rosenbrock (10) for Example
![Page 1: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/1.jpg)
GA – Rosenbrock(10) for Example
![Page 2: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/2.jpg)
Public methods (for general user)
• Init() – (4 polymorphism)– Init (int PopulationSize, int VariableDimension, double VariableUpbound, double Varia
bleLowbound, string EncodeType, string RepeatableOption, bool Minima) – Init (int PopulationSize, int VariableDimension, double VariableUpbound, double Varia
bleLowbound, string EncodeType, string RepeatableOption)– Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] V
ariableLowbound, string EncodeType, string RepeatableOption, bool Minima)– Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] V
ariableLowbound, string EncodeType, string RepeatableOption)• GA Basic routine
– Generate_Population(string Population_Type) – Evalutate_Population() – Reproduce_Population (string METHOD) – Crossover_Population (string Population_Type) – Mutation_Population (string METHOD, double PM)
• Run() – (2 polymorphism)– Run (int Iteration) – Run (int Iteration, double PC, double PM)
![Page 3: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/3.jpg)
Methods can be override by user
• Method– public override double Fitness(double[] Sol
ution) – public override double Run (int Iteration)
![Page 4: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/4.jpg)
GAOption Class
• When you have no idea on determine the GA strategy, you could also expand the GAOption tree to find out each possible strategy combination under specific encoding.
• For example : GA basic routine– Reproduce_Population(GAOption.Select.Elite);– Crossover_Population(GAOption.Crossover.NonRepeatNumetric.Order, PC);– Mutation_Population(GAOption.Mutation.Numetric.Swap_Two_Position, PM);
![Page 5: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/5.jpg)
GAOption Class
Different GA strategy combinations
![Page 6: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/6.jpg)
GAOption Class
Different GA strategy combinations
![Page 7: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/7.jpg)
Step 1 and 2• using System;• using System.Collections.Generic;• using System.Text;• using System.IO;• using Metaheuristic;
• namespace Testing• {• class Rosenbrock : GA• {• • } • }
Include our MetaYourWay library
Create a problem solver class Rosenbrockwhich inheritance GA class
![Page 8: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/8.jpg)
Step 3• using …• namespace Testing• {• class Rosenbrock : GA• {• static void Main(string[] args)• {• Rosenbrock myga = new Rosenbrock();• }• public void Read()…
} • }
Create object.
![Page 9: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/9.jpg)
Step 4• using …• namespace Testing• {• class Rosenbrock : GA• {• static void Main(string[] args)• {• Rosenbrock myga = 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;• }• }• }
Override Fitness Function
![Page 10: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/10.jpg)
Step 5• using …• namespace Testing• {• class Rosenbrock : GA• {• double Up = 10;• double Low = 0;• static void Main(string[] args)• {• Rosenbrock myga = new Rosenbrock();• myga.Init(PopulationSize, VariableDimension, Up, Low,
EncodeType, RepeatbleOption, Subject2Minima); • }• public override double Fitness(double[] pos)…• } • }
If you want the optimal value of GbestFitness is minimizedSet this option to true
User can use arrays to input lower bound and upper boundfor each variable.
Define variable upper / lower bound.
![Page 11: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/11.jpg)
Step 6 – Simple Call• using …• namespace Testing• {• class Rosenbrock : GA• {• double Up = 10;• double Low = 0;• static void Main(string[] args)• {• Rosenbrock myga = new Rosenbrock ();• myga.Init(40, 10, Up, Low, “RealNumber”, “Repeatable”, true);• myga.Run(1000);
//myga.Run(1000,PC,PM);• }
public override double Fitness(double[] pos)…• } • }
Polymorphism
![Page 12: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/12.jpg)
Step 7 – Advanced Call
• public override void Run(int Iteration, double PC, double PM)• {• Generate_Population(“RealNumber”);
• for (int i = 0; i < Iteration; i++)• {• Evaluate_Population();
• Reproduce_Population(GAOption.Select.Elite);
• Crossover_Population(GAOption.Crossover. RealNumber.Arithmetic, PC);
• Mutation_Population(GAOption.Mutation.RealNumber.Uniform, PM);• }• Evaluate_Population();• }
If default GA strategy routine cannot satisfy your needsYou could also customize your own strategyTo do this, you must override the default Run() Method
You can customize your own ECODING and GA Strategy
ENCODING Customization
STRATEGY Customization
GA revolution interations
![Page 13: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/13.jpg)
Parameter requirement for GA• For example : Rosenbrock (Real Number) problem
User must define all the parameter above..– PopulationSize = 100, – VariableDimension = 4, – VariableLowbound = 0, – VariableUpbound = 10, (In this case, variable will be generated from 0 to 9)
– RepeatableOption = “Repeatable”– EncodeType = “RealNumber” (integer number)
– Iteration = 1000.– PC = 0.7 (Crossover rate)
– PM = 0.3 (Mutation rate)
• After excuting Run(…) method, you can retrieve the latest optimal solution information by read the variables above– Gbest (Best solution)– GbestFitness (Best fitness)
“BINARY”“NUMETRIC”“REALNUMBER”
“Repeatble”“Nonrepeatable”
![Page 14: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/14.jpg)
GA - TSP for Example
![Page 15: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/15.jpg)
Public methods (for general user)
• Init() – (4 polymorphism)– Init (int PopulationSize, int VariableDimension, double VariableUpbound, double Varia
bleLowbound, string EncodeType, string RepeatableOption, bool Minima) – Init (int PopulationSize, int VariableDimension, double VariableUpbound, double Varia
bleLowbound, string EncodeType, string RepeatableOption)– Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] V
ariableLowbound, string EncodeType, string RepeatableOption, bool Minima)– Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] V
ariableLowbound, string EncodeType, string RepeatableOption)• GA Basic routine
– Generate_Population(string Population_Type) – Evalutate_Population() – Reproduce_Population (string METHOD) – Crossover_Population (string Population_Type) – Mutation_Population (string METHOD, double PM)
• Run() – (2 polymorphism)– Run (int Iteration) – Run (int Iteration, double PC, double PM)
![Page 16: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/16.jpg)
Methods which can be override by user
• Method– public override double Fitness(double[] Sol
ution) – public override double Run (int Iteration)
![Page 17: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/17.jpg)
GAOption Class
• When you have no idea on determine the GA strategy, you could also expand the GAOption tree to find out each possible strategy combination under specific encoding.
• For example : GA basic routine– Reproduce_Population(GAOption.Select.Elite);– Crossover_Population(GAOption.Crossover.NonRepeatNumetric.Order, PC);– Mutation_Population(GAOption.Mutation.Numetric.Swap_Two_Position, PM);
![Page 18: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/18.jpg)
GAOption Class
![Page 19: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/19.jpg)
GAOption Class
![Page 20: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/20.jpg)
Step 1 and 2• using System;• using System.Collections.Generic;• using System.Text;• using System.IO;• using Metaheuristic;
• namespace Testing• {• class TSP : GA• {• • } • }
Include our MetaYourWay library
Create a problem solver class TSPwhich inheritance GA class
![Page 21: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/21.jpg)
Step 3• using …• namespace Testing• {• class TSP : GA• {• double[,] distance = new double[10, 10]; • static void Main(string[] args)• { }• public void TspRead()• {• 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();• }• } • }
Store city distance info.
![Page 22: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/22.jpg)
Step 4 and 5• using …• namespace Testing• {• class TSP : GA• {• double[,] distance = new double[10, 10]; • static void Main(string[] args)• {• TSP myga = new TSP();• }• public void Read()…
} • }
Creating object.
![Page 23: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/23.jpg)
Step 6• using …• namespace Testing• {• class TSP : GA• {• double[,] distance = new double[10, 10]; • static void Main(string[] args)• {• TSP myga = new TSP();• }• public void Read()…• public override double Fitness(double[] solution)• {• double sum = 0;• for (int j = 0; j < solution.GetLength(0) - 1; j++)• sum = sum + distance[(int)solution[j], (int) solution[j + 1]];• sum = sum + distance[(int) solution[solution.GetLength(0) - 1], (int) solution[0]];• return sum;• }• } • }
Override Fitness Function
![Page 24: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/24.jpg)
Step 7• using …• namespace Testing• {• class TSP : GA• {• 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 myga = new TSP();• myga.Init(PopulationSize, VariableDimension, Up, Low,
EncodeType, RepeatbleOption, Subject2Minima); • }• public void Read()…
public override double Fitness(double[] solution)…• } • }
If you wanna minimize the GbestFitnessSet this option to true
User can use arrays to input lower bound and upper boundfor each variable.
![Page 25: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/25.jpg)
Step 8 – Simple Call• using …• namespace Testing• {• class TSP : GA• {• double[,] distance = new double[10, 10]; • static void Main(string[] args)• {• TSP myga = new TSP();• myga.Init(PopulationSize, VariableDimension, VariableUpbound,
VariableLowbound, EncodeType, RepeatbleOption, Subject2Minima);• myga.Run(1000);
//myga.Run(1000,PC,PM);• }• public void Read()…
public override double Fitness(double[] solution)…• } • }
Polymorphism
![Page 26: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/26.jpg)
Step 8 – Advanced Call
• public override void Run(int Iteration, double PC, double PM)• {• Generate_Population(“NonRepeatNumetric”);
• for (int i = 0; i < Iteration; i++)• {• Evaluate_Population();
• Reproduce_Population(GAMethod.Select.Elite);
• Crossover_Population(GAMethod.Crossover.NonRepeatNumetric.Order, PC);
• Mutation_Population(GAMethod.Mutation.Numetric.Swap_Two_Position, PM);• }• Evaluate_Population();• }
If default GA strategy routine cannot satisfy your needsYou could also customize your own strategyTo do this, you must override the default Run() Method
You can customize your own ECODING and GA Strategy
ENCODING Customization
STRATEGY Customization
GA revolution interations
![Page 27: GA – Rosenbrock (10) for Example](https://reader036.fdocuments.us/reader036/viewer/2022081506/56814937550346895db67b01/html5/thumbnails/27.jpg)
Parameter requirement for GA• For example : TSP problem
User must define all the parameter above..– PopulationSize = 100, – VariableDimension = 10, – VariableLowbound = 0, – VariableUpbound = 10, (In this case, variable will be generated from 0 to 9)
– RepeatableOption = “Nonrepeatable”– EncodeType = “NUMETRIC” (integer number)
– Iteration = 1000.– PC = 0.7 (Crossover rate)
– PM = 0.3 (Mutation rate)
• After excuting Run(…) method, you can retrieve the latest optimal solution information by read the variables above– Gbest (Best solution)– GbestFitness (Best fitness)
“BINARY”“NUMETRIC”“REALNUMBER”
“Repeatble”“Nonrepeatable”