/// <summary> /// Create a problem /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click(object sender, EventArgs e) // create a problem { int travelers; if (!int.TryParse(txtTravelersAmount.Text, out travelers)) { MessageBox.Show("Please write a number"); return; } DistanceOperator dOp; if (ControlProgram.tsp != null) // Load From TSPLib95 { TransferTspLibItemToPoints(); dOp = new DistanceOperator(ControlProgram.tsp.Problem.NodeProvider.CountNodes()); dOp.CalculateDistance(ControlProgram.tsp.Problem); } else { dOp = new DistanceOperator(this.Points.Count, Points); } TspManager = new TSPManager(dOp, travelers, int.Parse(cmbDepoId.Text), NextGeneration); ReportManager pManager = new ReportManager(1, 2, ControlProgram.tsp.Problem.Name); TspManager.NextGenerationEvent += pManager.NextGeneration; ControlProgram.Start(TspManager.Population, TspManager.dOp, TspManager.Environment); btnRun.Enabled = true; btnChooseOperator.Enabled = true; }
public TSPManager(DistanceOperator dOp, int travellerAmount, int depoId, NextGenerationEventHandler nextGenerationEventHandler = null) { this.dOp = dOp; Environment = new TSPAnde.Lib.GA.Environment(); Environment.TravelersAmount = travellerAmount; Environment.DepoId = depoId; DistanceOperator.InitializeOperator(dOp, Environment); Population = new Population(Environment); NextGenerationEvent = nextGenerationEventHandler; }
public ActionResult GetShortestPath(BaiduApiMTspRequest request) { var dOp = new DistanceOperator(request.Count, request.DistancesMatrix); TSPManager tspManager = new TSPManager(dOp, request.TravellerAmount, request.Home); tspManager.Start(); var response = tspManager.GetFullDistancesInArray(); return(Json(response)); }
public double CalculateDistance() { Distances.Clear(); Distances.Add(0); int trevelid = 0; //Distances.Last _distance = 0; double temp = 0; for (int i = 0; i < genes.Count; ++i) { if (genes[i].Id == genes[(i + 1) % genes.Count].Id) { temp = Environment.BigDist; } else { temp = DistanceOperator.dOperator.Matrix[genes[i].Id, genes[(i + 1) % genes.Count].Id]; } _distance += temp; if (genes[i].IsDepo) { Distances.Add(0); trevelid++; } Distances[trevelid] += temp; //temp = this.t[i].distanceTo(this.t[(i + 1) % this.t.Count]); } if (Distances.Count > Environment.TravelersAmount) { Distances[0] += Distances[trevelid]; Distances.RemoveAt(trevelid); } BalanceProportion = DistanceOperator.GetBalanceProportion(Distances, _distance); Distance = _distance; return(Distance); //_distance = 0; //for (int i = 0; i < genes.Count - 1; i++) //{ // _distance += DistanceOperator.dOperator.Matrix[genes[i].Id, genes[i + 1].Id]; //} //Distance = _distance + DistanceOperator.dOperator.Matrix[genes[genes.Count - 1].Id, genes[0].Id]; //return Distance; }
private void rbBalanceProportionDispersion_CheckedChanged(object sender, EventArgs e) { DistanceOperator.SetBalanceProportionCalc(new BalanceProportionVarianceCalc()); }
private void rbBalanceDevPlusPercent_CheckedChanged(object sender, EventArgs e) { DistanceOperator.SetBalanceProportionCalc(new BalanceProportionMinDevideByMaxWithPercent()); }
private void rbDev_CheckedChanged(object sender, EventArgs e) { DistanceOperator.SetBalanceProportionCalc(new BalanceProportionMinDevideByMax()); }
private void radioButton2_CheckedChanged(object sender, EventArgs e) { DistanceOperator.SetBalanceProportionCalc(new BalanceProportionMaxWithSumCalc()); }
public static void Start(Population population, DistanceOperator dOp, TSPAnde.Lib.GA.Environment environment) { Environment = environment; Population = population; // population.Init(travelers: TSPAnde.Lib.GA.Environment.travelers, depo : 1); }
static void Main(string[] args) { //timerInterval = new List<double> //{ // 100 //}; //duration = new List<double> //{ // 600 //}; TspLib95 lib = new TspLib95(tspLibPath); var tspList = lib.LoadAllTSP().ToList(); var tspItemNumbers = new List <int> { 39 // - 21 city , 2 //- 49 , 27 //- 70+ , 25 //- 101 , 44 //- 666 }; var tspItems = new Dictionary <int, TspLib95Item>(); foreach (var id in tspItemNumbers) { tspItems.Add(id, tspList[id]); } List <ICrossoverOperator> crossovers = new List <ICrossoverOperator>() { new CrossoverOperatorOX(), new CrossoverOperatorPMX(), new CrossoverOperatorAEX(), //new CrossoverOperatorAEXWithShortestDistance() }; List <IMutationOperator> mutations = new List <IMutationOperator>() { new MutationOperatorRSM(), new MutationOperatorInsertions(), new MutationOperatorPSM(), //new MutationOperatorHalfRSMHalfPSM(), }; var number = 100; //int city = 0, cI = 0, mI = 0, nI = 0; //var dOp = new DistanceOperator(tspItems[tspItemNumbers[city]].Problem.NodeProvider.CountNodes()); //dOp.CalculateDistance(tspItems[tspItemNumbers[city]].Problem); //ChromosomeOperator.ChangeOperator(crossovers[cI]); //ChromosomeOperator.ChangeOperator(mutations[mI]); //var reportManager = new ReportManager(number[nI], tspItemNumbers[city], // tspItems[tspItemNumbers[city]].Problem.Name); //var tspManager = new TSPManager(dOp, 1, 1, reportManager.NextGeneration); //tspManager.Start(); //reportManager.EndOfAlgorithm(tspManager.Population); var indx = 0; var total = tspItemNumbers.Count * crossovers.Count * mutations.Count * number; for (int city = 0; city < tspItemNumbers.Count; city++) { var dOp = new DistanceOperator(tspItems[tspItemNumbers[city]].Problem.NodeProvider.CountNodes()); dOp.CalculateDistance(tspItems[tspItemNumbers[city]].Problem); for (int cI = 0; cI < crossovers.Count; cI++) { ChromosomeOperator.ChangeOperator(crossovers[cI]); for (int mI = 0; mI < mutations.Count; mI++) { ChromosomeOperator.ChangeOperator(mutations[mI]); for (int nI = 2; nI <= number; nI++) { Console.WriteLine("{0}%", (double)indx / total * 100); Console.WriteLine("start {0} : {1} {2} {3} {4}", indx++, tspItemNumbers[city], crossovers[cI].GetType().Name, mutations[mI].GetType().Name, nI ); var reportManager = new ReportManager(nI, tspItemNumbers[city], tspItems[tspItemNumbers[city]].Problem.Name); //var tspManager = new TSPManager(dOp, 1, 1, reportManager.NextGeneration); var tspManager = new TSPManager(dOp, 1, 1, reportManager.NextGeneration); var timer = new Timer(timerInterval[city]); currentCity = city; timer.Elapsed += NextTick; SetUpNewTest(tspManager.Population, reportManager); timer.Start(); tspManager.Start(StopFunc); timer.Stop(); reportManager.EndOfAlgorithm(tspManager.Population); } } } } }