static string GetNearFaciAndDistan(string[] facilities, int restPointIndex, List <DemandPoint> demandPoints) { int neareastFacility = 0; double minDistance = double.MaxValue; for (int faciIndex = 0; faciIndex < facilities.Length; faciIndex++) { int oneFacilityIndex = Convert.ToInt32(facilities[faciIndex]); DemandPoint oneFacility = demandPoints[oneFacilityIndex]; //double oneDistance = distanceArr[restPointIndex, oneFacility]; DemandPoint restPoint = demandPoints[restPointIndex]; double oneDistance = restPoint.DistanceTo(oneFacility.location); if (oneDistance < minDistance) { minDistance = oneDistance; neareastFacility = oneFacilityIndex; } } return(neareastFacility.ToString() + ',' + minDistance.ToString()); }
public double noweightDistanceTo(DemandPoint demandPoint) { return(location.DistanceTo(demandPoint.location)); }
static void Main(string[] args) { DateTime dt1 = DateTime.Now; string dateStr = "01"; StreamReader sr = new StreamReader(@"E:\DATA\TAXI\day" + dateStr + "_2ndJoin.txt", Encoding.Default); List <DemandPoint> dpoints = new List <DemandPoint>(); string line; while ((line = sr.ReadLine()) != null) //0,120.856620029,30.6984000665,0 { string[] oneRecord = line.Split(','); int objectID = Convert.ToInt32(oneRecord[0]); double x = Convert.ToDouble(oneRecord[1]); double y = Convert.ToDouble(oneRecord[2]); int number = Convert.ToInt32(oneRecord[3]); dpoints.Add(new DemandPoint(new GISVertex(x, y), number, objectID)); } //List<DemandPoint> dpoints = new List<DemandPoint>(); // id == index //DemandPoint dp0 = new DemandPoint(new GISVertex(0, 0), 10, 0); //DemandPoint dp1 = new DemandPoint(new GISVertex(10, 0), 1, 1); //DemandPoint dp2 = new DemandPoint(new GISVertex(20, 0), 1, 2); //dpoints.Add(dp0); dpoints.Add(dp1); dpoints.Add(dp2); //double[,] distanceArr = Get2DdistanceArray(dpoints); int[] IDs = new int[dpoints.Count]; for (int i = 0; i < IDs.Length; i++) { IDs[i] = dpoints[i].objectID; } int N = dpoints.Count; //总共demand point个数 int P = 1; //总共facility个数 List <double> SumList = new List <double>(); //比较每一个方案的Sum //所有可供选择的facility方案——string数组,每个item对应一个facility方案 List <string> CnmResult = GetCnmResult(IDs, P); int solutionNum = CnmResult.Count; int solutionPercent = solutionNum / 100; //one percent of total int process = 0; //percentage //遍历所有的可能方案 for (int i = 0; i < CnmResult.Count; i++) { double sum = 0; string[] facilities = CnmResult[i].Split(','); List <string> restPoints = ArrayExclude(IDs, facilities); //遍历一个方案的restPoints for (int j = 0; j < restPoints.Count; j++) { int restPoint = Convert.ToInt32(restPoints[j]); //遍历Facilities,为剩下的每个point找到最近的facility,计算距离 int int_facility = Convert.ToInt32(GetNearFaciAndDistan(facilities, restPoint, dpoints).Split(',')[0]); DemandPoint facility = dpoints[int_facility]; //restPoint过来facilit的权重*距离 double distance = facility.weightedDistanceTo(dpoints[restPoint]); //restPoint过来facility的距离 //double distance = facility.noweightDistanceTo(dpoints[restPoint]); sum += distance; } SumList.Add(sum); //Show progress if (solutionPercent != 0 && SumList.Count % solutionPercent == 0) { process += 1; Console.WriteLine("calculate distance: " + process.ToString() + " percent finished!"); } } //得到sum列表中最小 double min = Double.MaxValue; int minIndex = 0; string minString = ""; for (int i = 0; i < SumList.Count; i++) { if (SumList[i] <= min) { min = SumList[i]; minIndex = i; minString += CnmResult[minIndex] + ";"; } } Console.WriteLine("Best IDs: " + CnmResult[minIndex]); string[] results = CnmResult[minIndex].Split(','); for (int i = 0; i < results.Length; i++) { int result = Convert.ToInt32(results[i]); string resultPosition = dpoints[result].location.ToString(); Console.WriteLine("Point " + i.ToString() + resultPosition); } Console.WriteLine("Best Cost: " + min.ToString()); DateTime dt2 = DateTime.Now; Console.WriteLine("所用时间:" + TimeDiff(dt1, dt2)); Console.ReadKey(); }
public double weightedDistanceTo(DemandPoint anotherDemandPoint) { double distance = location.DistanceTo(anotherDemandPoint.location); return(anotherDemandPoint.CarNumber * distance); }