Download presentation
Presentation is loading. Please wait.
1
TS - TSP for Example
2
User can use methods Init() - polymorphism –Init(int NeighborSize,int VariableDimension, int VaraibleLowerbound, int VariableUpperbound, string RepeatableOption, int TabulistSize) –Init(int NeighborSize,int VariableDimension,int [] VaraibleLowerbound, int [] VariableUpperbound, string RepeatableOption, int TabulistSize) Run() - polymorphism –Run(int Iteration) –Run(int Iteration, string Strategy, double Aspiration) GenerateNeighbors() – polymorphism –GenerateNeighbors() –GenerateNeighbors(string Strategy) Select() – polymorphism –Select() –Select(double Aspiration)
3
User can override methods Method –public override double Fitness(int[] solution)
4
TSOption Class User used TSOption when having no idea to determine problem options or strategy options. TSOption –RepeatableOption Repeatable, Nonrepeatable –StrategyOption Inversion, TwoSwap, TwoListRnd
5
TSOption Class TSOOption.RepeatableOption.Repeatable TSOOption.RepeatableOption.NonRepeatable TSOOption.Strategy.Inversion TSOOption.Strategy.TwoSwap TSOOption.Strategy.TwoListRnd
6
Step 1 and 2 using System; using System.Collections.Generic; using System.Text; using System.IO; using Metaheuristic; namespace Testing { class TSP : TS { } Step 1: Include Metaheuristic. Step2: Problem class must inherit TS.
7
Step 3 using … namespace Testing { class TSP : TS { 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(); } To read city distance.
8
Step 4 and 5 using … namespace Testing { class TSP : TS { double[,] distance = new double[10, 10]; static void Main(string[] args) { TSP Tabu = new TSP(); Tabu.TspRead(); } public void TspRead()… } Step 4: Creating object. Step 5: Read TspData.
9
Step 6 using … namespace Testing { class TSP : TS { double[,] distance = new double[10, 10]; static void Main(string[] args) { TSP Tabu = new TSP(); Tabu.TspRead(); } public void TspRead()… 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.
10
Step 7 using … namespace Testing { class TSP : TS { 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 Tabu= new TSP(); Tabu.TspRead(); Tabu.Init(20, 10, 0, 9, TSOption.RepeatableOption.Nonrepeatable, 5); //Tabu.Init(20, 10, Low, Up, “Nonrepeatable”, 5); } public void TspRead()… public override double Fitness(int[] solution)… } User can use arrays to input lower bound and upper bound for each variable. RepeatableOption and StrategyOption have two expressions of TSOption and string. Polymorphism
11
Step 8 – Simple Call using … namespace Testing { class TSP :TS { double[,] distance = new double[10, 10]; static void Main(string[] args) { TSP Tabu = new TSP(); Tabu.TspRead(); Tabu.Init(20, 10, 0, 9, TSOption.RepeatableOption.Nonrepeatable, 5); Tabu.Run(1000); //Tabu.Run(1000, TSOption.Strategy.Inversion, 0.3); //Tabu.Run(1000, TSOption.Strategy.TwoSwap, 0.3); } public void TspRead()… public override double Fitness(int[] solution)… } Polymorphism
12
Step 8 – Advanced Call using … namespace Testing { class TSP :TS { double[,] distance = new double[10, 10]; static void Main(string[] args) { TSP Tabu= new TSP(); Tabu.TspRead(); Tabu.Init(20, 10, 0, 9, TSOption.RepeatableOption.Nonrepeatable, 5); for (int iter = 1; iter <= 1000; iter++) { Tabu.GenerateNeighbors(TSOption.Strategy.TwoSwap); Tabu.Select(0.3); } public void TspRead()… public override double Fitness(int[] solution)… }
13
User requirement for TSP User requirement. –NeighborSize = 20, –VariableDimension = 10, –VariableLowerbound = 0, –VariableUpperbound = 9, –RepeatableOption = “Nonrepeatable”, –StrategyOption = “TwoSwap” –TabulistSize) = 5, –Iteration = 1000. We will give two example 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).
14
Example 1 - Simple Call using … namespace Testing { class TSP : TS { double[,] distance = new double[10, 10]; static void Main(string[] args) { TSP Tabu= new TSP(); Tabu.TspRead(); Tabu.Init(20, 10, 0, 9, TSOption.RepeatableOption.Nonrepeatable, 5); Tabu.Run(1000, TSOption.Strategy.TwoSwap, 0.3); Console.WriteLine(“GbestFitness” + Tabu.GbestFitness); } public void TspRead()… public override double Fitness(int[] solution)… }
15
Example 2 - Advanced Call using … namespace Testing { class TSP : TS { double[,] distance = new double[10, 10]; static void Main(string[] args) { TSP Tabu = new TSP(); Tabu.TspRead(); Tabu.Init(100, 10, 0, 9, TSOption.RepeatableOption.Nonrepeatable, 5); for (int iter = 1; iter <= 1000; iter++) { Tabu.GenerateNeighbors(TSOption.Strategy.TwoSwap); Tabu.Select(0.3); } Console.WriteLine(“GbestFitness” + Tabu.GbestFitness); } public void TspRead()… public override double Fitness(int[] solution)… }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.