270 likes | 395 Views
GA – Rosenbrock (10) for Example. Public methods (for general user). Init() – (4 polymorphism ) Init ( int PopulationSize, int VariableDimension, double VariableUpbound, double VariableLowbound, string EncodeType, string RepeatableOption, bool Minima)
E N D
Public methods (for general user) • Init() – (4 polymorphism) • Init (int PopulationSize, int VariableDimension, double VariableUpbound, double VariableLowbound, string EncodeType, string RepeatableOption, bool Minima) • Init (int PopulationSize, int VariableDimension, double VariableUpbound, double VariableLowbound, string EncodeType, string RepeatableOption) • Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] VariableLowbound, string EncodeType, string RepeatableOption, bool Minima) • Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] VariableLowbound, 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)
Methods can be override by user • Method • public override doubleFitness(double[] Solution) • public override double Run (intIteration)
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);
GAOption Class Different GA strategy combinations
GAOption Class Different GA strategy combinations
Step 1 and 2 • using System; • using System.Collections.Generic; • using System.Text; • using System.IO; • using Metaheuristic; • namespace Testing • { • classRosenbrock : GA • { • } • } Include our MetaYourWay library Create a problem solver class Rosenbrockwhich inheritance GA class
Step 3 • using … • namespace Testing • { • classRosenbrock : GA • { • static void Main(string[] args) • { • Rosenbrockmyga = newRosenbrock(); • } • public void Read()… } • } Create object.
Step 4 • using … • namespace Testing • { • classRosenbrock : GA • { • static void Main(string[] args) • { • Rosenbrockmyga = newRosenbrock(); • } • 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
Step 5 • using … • namespace Testing • { • classRosenbrock : GA • { • double Up = 10; • double Low = 0; • static void Main(string[] args) • { • Rosenbrockmyga = newRosenbrock(); • myga.Init(PopulationSize, VariableDimension, Up, Low, EncodeType, RepeatbleOption, Subject2Minima); • } • public override double Fitness(double[] pos)… • } • } Define variable upper / lower bound. If you want the optimal value of GbestFitness is minimizedSet this option to true User can use arrays to input lower bound and upper bound for each variable.
Step 6 – Simple Call • using … • namespace Testing • { • classRosenbrock : GA • { • double Up = 10; • double Low = 0; • static void Main(string[] args) • { • Rosenbrockmyga = newRosenbrock (); • myga.Init(40, 10, Up, Low, “RealNumber”, “Repeatable”, true); • myga.Run(1000); //myga.Run(1000,PC,PM); • } public override double Fitness(double[] pos)… • } • } Polymorphism
Step 7 – Advanced Call If default GA strategy routine cannot satisfy your needsYou could also customize your own strategy To do this, you must override the default Run() Method • 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(); • } ENCODING Customization GA revolution interations STRATEGY Customization You can customize your own ECODING and GAStrategy
Parameter requirement for GA • For example : Rosenbrock (Real Number) problemUser 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) “Repeatble”“Nonrepeatable” “BINARY”“NUMETRIC” “REALNUMBER”
Public methods (for general user) • Init() – (4 polymorphism) • Init (int PopulationSize, int VariableDimension, double VariableUpbound, double VariableLowbound, string EncodeType, string RepeatableOption, bool Minima) • Init (int PopulationSize, int VariableDimension, double VariableUpbound, double VariableLowbound, string EncodeType, string RepeatableOption) • Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] VariableLowbound, string EncodeType, string RepeatableOption, bool Minima) • Init (int PopulationSize, int VariableDimension, double [] VariableUpbound, double [] VariableLowbound, 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)
Methods which can be override by user • Method • public override doubleFitness(double[] Solution) • public override double Run (intIteration)
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);
Step 1 and 2 • using System; • using System.Collections.Generic; • using System.Text; • using System.IO; • using Metaheuristic; • namespace Testing • { • classTSP : GA • { • } • } Include our MetaYourWay library Create a problem solver class TSPwhich inheritance GA class
Step 3 • using … • namespace Testing • { • classTSP : GA • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • {} • publicvoid TspRead() • { • StreamReader sr = newStreamReader(@” 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.
Step 4 and 5 • using … • namespace Testing • { • classTSP : GA • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPmyga = newTSP(); • } • public void Read()… } • } Creating object.
Step 6 • using … • namespace Testing • { • classTSP : GA • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPmyga = newTSP(); • } • 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
Step 7 • using … • namespace Testing • { • classTSP : 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) • { • TSPmyga = newTSP(); • 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 bound for each variable.
Step 8 – Simple Call • using … • namespace Testing • { • classTSP : GA • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPmyga = newTSP(); • 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
Step 8 – Advanced Call If default GA strategy routine cannot satisfy your needsYou could also customize your own strategy To do this, you must override the default Run() Method • 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(); • } ENCODING Customization GA revolution interations STRATEGY Customization You can customize your own ECODING and GAStrategy
Parameter requirement for GA • For example : TSP problemUser 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) “Repeatble”“Nonrepeatable” “BINARY”“NUMETRIC” “REALNUMBER”