private void button1_Click(object sender, EventArgs e) { //if (this.lstCenters.Count == 0 || this.lstPoints.Count == 0) //{ // MessageBox.Show("יש להוסיף לפחות מרכז אחד ולפחות לקוח אחד"); //} //else { this.CreateGraphics().Clear(this.BackColor); //this.ShowSolution(this.SolveTSP(this.lstPoints)); //this.ShowVRPSolution(SingleVRP.SolveSingleVRP(this.lstPoints, this.lstCenters[0], (int)this.numericUpDown1.Value)); Dictionary <Point, int> dicCenterVehicles = new Dictionary <Point, int>(); foreach (Point pCenter in lstCenters) { dicCenterVehicles.Add(pCenter, (int)this.numericUpDown1.Value); } foreach (List <List <Point> > lstBla in MultiVRP. SolveMultiVRP(this.lstPoints, dicCenterVehicles, 10, 0.1, 2, 0.9, 0)) { this.ShowVRPSolution(lstBla); } } }
public static List <List <List <Point> > > SolveMultiVRP(List <Point> lstLocs, Dictionary <Point, int> dicCentersVehicles, int ants, double alpha, double beta, double q0, double tao0) { List <List <List <Point> > > lstSolution = new List <List <List <Point> > >(); Dictionary <Point, List <Point> > dicLocsToCenters = MultiVRP.DivideLocationsToCenters(lstLocs, dicCentersVehicles, ants, alpha, beta, q0, tao0); Parallel.ForEach(dicLocsToCenters.Keys, pCenter => { lstSolution.Add(SingleVRP.SolveSingleVRP(dicLocsToCenters[pCenter], pCenter, dicCentersVehicles[pCenter])); }); return(lstSolution); }
private static Dictionary <Point, List <Point> > DivideLocationsToCenters(List <Point> lstLocs, Dictionary <Point, int> dicCentersVehicles, int ants, double alpha, double beta, double q0, double tao0) { const int ITERATIONS = 5; Dictionary <KeyValuePair <Point, Point>, double> dicPhTrails = MultiVRP.InitPheromoneTrails(dicCentersVehicles.Keys.ToList(), lstLocs); List <Dictionary <Point, List <Point> > > lstSolutions = new List <Dictionary <Point, List <Point> > >(); Dictionary <Point, List <Point> > dicReallyBestSolution = null; for (int nIter = 0; nIter < ITERATIONS; nIter++) { lstSolutions.Clear(); for (int nAnt = 0; nAnt < ants; nAnt++) { lstSolutions.Add(MVRPAnt.MatchLocsToCenter(dicPhTrails, beta, q0)); } for (int nInd = 0; nInd < lstSolutions.Count; nInd++) { TrailUpdating(dicPhTrails, lstSolutions[nInd], alpha, tao0); } double dBestSolLength = (from sol in lstSolutions select GetTotalDist(sol)).Min(); Dictionary <Point, List <Point> > dicBestSol = (from sol in lstSolutions where GetTotalDist(sol) == dBestSolLength select sol).First(); TrailUpdating(dicPhTrails, dicBestSol, alpha, 1 / dBestSolLength); if (dicReallyBestSolution == null || dBestSolLength < GetTotalDist(dicReallyBestSolution)) { dicReallyBestSolution = dicBestSol; } } return(dicReallyBestSolution); }