180 likes | 312 Views
ACO - TSP for Example. User can use methods. Init() - polymorphism Init( int PopulationSize, int VariableDimension, double VariableLowerbound, double VariableUpperbound, string RepeatableOption, string CycleOption)
E N D
User can use methods • Init() - polymorphism • Init(int PopulationSize, int VariableDimension, double VariableLowerbound, double VariableUpperbound, string RepeatableOption, string CycleOption) • Init(int PopulationSize, int VariableDimension, double[] VariableLowerbound, double[] VariableUpperbound, string RepeatableOption, string CycleOption) • Run() - polymorphism • Run(int Iteration) • Run(int Iteration, double Alpha, double Beta, double GlobalPersisitenceRate, double GlobalConstant) • Run(int Iteration, double Alpha, double Beta, double LocalPersistenceRate, double LocalConstant, double GlobalPersistenceRate, double GlobalConstant) • GenerateAntPath() - polymorphism • GenerateAntPath(double Alpha, double Beta) • GenerateAntPath(double Alpha, double Beta, double LocalPersistenceRate, double LocalConstant) • GlobalPheromoneUpdate() • GlobalPheromoneUpdate(double GlobalPersistenceRate, double GlobalConstant)
User can override methods • Method • public override doubleInitVisibility() • public override double Fitness(int[] solution)
ACOOption Class • User used ACOOption when having no idea to determine problem options or strategy options. • ACOOption • RepeatableOption • Repeatable, Nonrepeatable • CycleOption • Cycle, Noncycle.
ACOOption Class ACOOption.RepeatableOption.Repeatable ACOOption.RepeatableOption.Nonrepeatable ACOOption.CycleOption.Cycle ACOOption.CycleOption.Noncycle
Step 1 and 2 • using System; • using System.Collections.Generic; • using System.Text; • using System.IO; • using Metaheuristic; • namespace Testing • { • classTSP : ACO • { • } • } Step 1: Include Metaheuristic. Step2: Problem class must inherit PSO.
Step 3 • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • {} • publicvoid Read() • { • 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(); • } • } • } To read city distance.
Step 4 and 5 • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • } • public void Read()… • public override void InitVisibility() • { • for (int i = 0; i < Visibility.GetLength(0); i++) • for (int j = 0; j < Visibility.GetLength(1); j++) • Visibility[i, j] = 1 / distance[i, j]; • } • } • } Step 3: Creating object. Step 4: To override InitVisibility.
Step 6 • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution) • { • double sum = 0; • for (int j = 0; j < solution.GetLength(0) - 1; j++) • sum = sum + distance[solution[j], solution[j + 1]]; • sum = sum + distance[solution[solution.GetLength(0) - 1], solution[0]]; • return sum; • } • } • } To override Fitness funcitoin.
Step 7 • using … • namespace Testing • { • classTSP : ACO • { • 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) • { • TSPant = newTSP(); • ant.Init(100, 10, 0, 9, ACOOption.RepeatableOption.Nonrepeatable, “Cycle”); • //ant.Init(100, 10, Low, Up, “Nonrepeatable”, ACOOption.RepeatableOption.Cycle); • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution)… • } • } RepeatableOption and CycleOption have two expressions of ACOOption and string. Polymorphism User can use arrays to input lower bound and upper bound for each variable.
Step 8 – Simple Call • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • ant.Init(100, 10, 0, 9, ACOOption.RepeatableOption.Nonrepeatable, “Cycle”); • ant.Run(1000); • //ant.Run(1000, 1, 5, 0.9, 1); • //ant.Run(1000, 1, 5, 0.9, 0.001, 0.9, 1); • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution)… • } • } Polymorphism
Step 8 – Advanced Call • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • ant.Init(100, 10, 0, 9, ACOOption.RepeatableOption.Nonrepeatable, “Cycle”); • for (int iter = 1; iter <= 1000; iter++) • { • ant.GenerateAntPath(1, 5); • //ant.GenerateAntPath(1, 5, 0.9, 0.001); • ant.GlobalPheromoneUpdate(0.9, 1); • } • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution)… • } • } Polymorphism
User requirement 1 for TSP • User requirement. • PopulationSize = 100, VariableDimension = 10, VariableLowerbound = 0, VariableUpperbound = 9, RepeatableOption = “Nonrepeatable”, CycleOption = “Cycle”. • Iteration = 1000. • State transition rule • Alpha = 1, Beta = 5 • Global pheromone update. • GlobalPersisitenceRate = 0.9, GlobalConstant = 1. • We will give two examples to satisfy him if user has requirement above. • User can access public attributes for Gbest (the best solution for global) and GbestFitness (the best fitness for global).
Example 1 - Simple Call • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • ant.Init(100, 10, 0, 9, ACOOption.RepeatableOption.Nonrepeatable, • ACOOption.RepeatableOption.Cycle); • ant.Run(1000, 1, 5, 0.9, 1); • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution)… • } • }
Example 2 - Advanced Call • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • ant.Init(100, 10, 0, 9, ACOOption.RepeatableOption.Nonrepeatable, • ACOOption.RepeatableOption.Cycle); • for (int iter = 1; iter <= 1000; iter++) • { • ant.GenerateAntPath(1, 5); • ant.GlobalPheromoneUpdate(0.9, 1); • } • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution)… • } • }
User requirement 2 for TSP • User requirement. • PopulationSize = 100, VariableDimension = 10, VariableLowerbound = 0, VariableUpperbound = 9, RepeatableOption = “Nonrepeatable”, CycleOption = “Cycle”. • Iteration = 1000. • State transition rule • Alpha = 1, Beta = 5 • Local pheromone update. • LocalPersisitenceRate = 0.9, LocalConstant = 0.001. • Global pheromone update. • GlobalPersisitenceRate = 0.9, GlobalConstant = 1. • We will give two examples to satisfy him if user has requirement above. • User can access public attributes for Gbest (the best solution for global) and GbestFitness (the best fitness for global).
Example 1 - Simple Call • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • ant.Init(100, 10, 0, 9, ACOOption.RepeatableOption.Nonrepeatable, • ACOOption.RepeatableOption.Cycle); • ant.Run(1000, 1, 5, 0.9, 0.001, 0.9, 1); • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution)… • } • }
Example 2 - Advanced Call • using … • namespace Testing • { • classTSP : ACO • { • double[,] distance = new double[10, 10]; • static void Main(string[] args) • { • TSPant = newTSP(); • ant.Init(100, 10, 0, 9, ACOOption.RepeatableOption.Nonrepeatable, • ACOOption.RepeatableOption.Cycle); • for (int iter = 1; iter <= 1000; iter++) • { • ant.GenerateAntPath(1, 5, 0.9, 0.001); • ant.GlobalPheromoneUpdate(0.9, 1); • } • } • public void Read()… • public override void InitVisibility()… • public override double Fitness(int[] solution)… • } • }