public double heuristicFuntion(BusStop currentStop, BusStop endStop) { Geopoint geo1 = currentStop.geo; Geopoint geo2 = endStop.geo; return BusFunction.convertToDistance(geo1, geo2); }
public AStarAlgo(BusStop startStop, BusStop endStop) { this.startStop = startStop; this.endStop = endStop; this.arrayStop = new List<BusStop>(SampleDataSource.GetBusGroup().Stops); count=0; }
/* * OPEN is the List which include BusNode not been traverse * answer is the solution */ public void algorithm() { answer.Clear(); if (startStop == null) { startStop = searchArrayStop(startPlace); } if (endStop == null) { endStop = searchArrayStop(endPlace); } if (startStop == null || endStop == null) { return; } else { arrLine = new List<BusItem>(); List<BusNode> tempArrayNode = startStop.arrayNode; for (int i = 0; i < tempArrayNode.Count; i++) { TreeNode newTreeNode = new TreeNode(tempArrayNode[i], 0, 0); newTreeNode.setWeight(heuristicFuntion(startStop, endStop)); newTreeNode.setParent(null); OPEN.Add(newTreeNode); } for (int i = 0; i < endStop.arrayNode.Count; i++) { //arrLine.Add(endStop.arrayNode[i].getLine()); BusItem bus = SampleDataSource.FindBus(endStop.arrayNode[i].busCode); arrLine.Add(bus); } while (true) { if (!OPEN.Any()) { //System.out.println("12345"); OPEN.Clear(); if (offspring != null) { offspring.Clear(); } break; } //find the min index and the minimum weight of OPEN's BusNode minIndex = 0; fmin = OPEN[minIndex].getWeight(); int openSize = OPEN.Count(); for (int i = 0; i < openSize; i++) { if (OPEN[i].getWeight() < fmin) { minIndex = i; fmin = OPEN[i].getWeight(); } } TreeNode newTreeNode = OPEN[minIndex]; OPEN.Remove(newTreeNode); count++; //if you reach end place, save the answer and return if (newTreeNode.getCurrentNode().stopCode == endStop.Code) { //System.out.println("reach end place"); saveAnswer(newTreeNode); offspring.Clear(); return; } if (count==MAX_SIZE) { answer = null; return; } //get offspring of newTreeNode offspring = generateOffspring(newTreeNode); if (offspring != null) { int index = checkLine(offspring[0]); if (index >= 0) { TreeNode endTreeNode = new TreeNode(endStop.arrayNode[index], offspring[0]); //System.out.println("reach end place (1)"); saveAnswer(endTreeNode); offspring.Clear(); return; } int offspringSize = offspring.Count(); //add offspring into OPEN for (int i = 0; i < offspringSize; i++) { OPEN.Insert(0, offspring[i]); } } } } }
private async Task GetSampleDataAsync() { if (this._buses.Count != 0) return; ObservableCollection<BusItem> obi = new ObservableCollection<BusItem>(); ObservableCollection<BusStop> obs = new ObservableCollection<BusStop>(); for (int i = 1; i <= 74; i++) { List<Geopoint> RouteGoGeo = new List<Geopoint>(); List<BusStop> RouteGoStations = new List<BusStop>(); List<Geopoint> RouteReturnGeo = new List<Geopoint>(); List<BusStop> RouteReturnStations = new List<BusStop>(); List<BusNode> goNode = new List<BusNode>(); List<BusNode> returnNode = new List<BusNode>(); Uri dataUri = new Uri("ms-appx:///DataModel/" + i.ToString() + ".txt"); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(dataUri); string jsonText = await FileIO.ReadTextAsync(file); JsonObject jsonObject = JsonObject.Parse(jsonText)["dt"].GetObject(); // Get Go JsonObject goObject = jsonObject["Go"].GetObject(); JsonArray goGeoArray = goObject["Geo"].GetArray(); foreach (JsonValue value in goGeoArray) { JsonObject goGeoObject = value.GetObject(); double lng = goGeoObject["Lng"].GetNumber(); double lat = goGeoObject["Lat"].GetNumber(); RouteGoGeo.Add(new Geopoint(new BasicGeoposition { Latitude = lat, Longitude = lng })); } JsonArray goStationArray = goObject["Station"].GetArray(); foreach (JsonValue value in goStationArray) { JsonObject goStationObject = value.GetObject(); double lng = goStationObject["Geo"].GetObject()["Lng"].GetNumber(); double lat = goStationObject["Geo"].GetObject()["Lat"].GetNumber(); string code = ""; try { code = goStationObject["ObjectID"].GetNumber().ToString(); //code = goStationObject["Code"].GetString(); } catch (Exception exc) { string errMsg = exc.Message; } BusStop bs = new BusStop(code, goStationObject["Name"].GetString(), goStationObject["FleetOver"].GetString(), new Geopoint(new BasicGeoposition { Latitude = lat, Longitude = lng })); RouteGoStations.Add(bs); BusNode bn = new BusNode(jsonObject["Code"].GetString(), code, null); goNode.Add(bn); if (!obs.Any(a => a.Code == code)) { bs.arrayNode.Add(bn); obs.Add(bs); } else { obs.Where(x => x.Code == code).FirstOrDefault().arrayNode.Add(bn); } } // Get Return JsonObject reObject = jsonObject["Re"].GetObject(); JsonArray reGeoArray = reObject["Geo"].GetArray(); foreach (JsonValue value in reGeoArray) { JsonObject reGeoObject = value.GetObject(); double lng = reGeoObject["Lng"].GetNumber(); double lat = reGeoObject["Lat"].GetNumber(); RouteReturnGeo.Add(new Geopoint(new BasicGeoposition { Latitude = lat, Longitude = lng })); } JsonArray reStationArray = reObject["Station"].GetArray(); foreach (JsonValue value in reStationArray) { JsonObject reStationObject = value.GetObject(); double lng = reStationObject["Geo"].GetObject()["Lng"].GetNumber(); double lat = reStationObject["Geo"].GetObject()["Lat"].GetNumber(); string code = ""; try { code = reStationObject["ObjectID"].GetNumber().ToString(); } catch (Exception exc) { string errMsg = exc.Message; } BusStop bs = new BusStop(code, reStationObject["Name"].GetString(), reStationObject["FleetOver"].GetString(), new Geopoint(new BasicGeoposition { Latitude = lat, Longitude = lng })); RouteReturnStations.Add(bs); BusNode bn = new BusNode(jsonObject["Code"].GetString(), code, null); returnNode.Add(bn); if (!obs.Any(a => a.Code == code)) { bs.arrayNode.Add(bn); obs.Add(bs); } else { obs.Where(x => x.Code == code).First().arrayNode.Add(bn); } } BusItem bus = new BusItem(jsonObject["FleetID"].GetNumber().ToString(), jsonObject["Code"].GetString(), jsonObject["Name"].GetString(), jsonObject["OperationsTime"].GetString(), jsonObject["Frequency"].GetString(), jsonObject["Cost"].GetString(), goObject["Route"].GetString(), RouteGoGeo, RouteGoStations, reObject["Route"].GetString(), RouteReturnGeo, RouteReturnStations); bus.goNode = goNode; bus.returnNode = returnNode; obi.Add(bus); } BusGroup bg = new BusGroup(obi, obs); this.Buses.Add(bg); foreach (BusItem bi in bg.Items) { for (int i = 0; i < bi.goNode.Count(); i++) { if (i < bi.goNode.Count()-1) bi.goNode[i].nextNode = bi.goNode[i + 1]; } for (int i = 0; i < bi.returnNode.Count(); i++) { if (i < bi.returnNode.Count()-1) bi.returnNode[i].nextNode = bi.returnNode[i + 1]; } } }