ga – rosenbrock(10) for example. public methods (for general user) init() – (4 polymorphism)...

27
GA – Rosenbrock(10) fo r Example

Post on 21-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

GA – Rosenbrock(10) for Example

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)

Methods can be override by user

• Method– public override double Fitness(double[] Sol

ution) – public override double Run (int Iteration)

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• {• class Rosenbrock : GA• {• • } • }

Include our MetaYourWay library

Create a problem solver class Rosenbrockwhich inheritance GA class

Step 3• using …• namespace Testing• {• class Rosenbrock : GA• {• static void Main(string[] args)• {• Rosenbrock myga = new Rosenbrock();• }• public void Read()…

} • }

Create object.

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

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.

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

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

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”

GA - TSP for Example

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)

Methods which can be override by user

• Method– public override double Fitness(double[] Sol

ution) – public override double Run (int Iteration)

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

GAOption Class

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

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.

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.

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

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.

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

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

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”