//get OffSpring of a treenode public List<TreeNode> generateOffspring(TreeNode n) { List<BusNode> newArrayBusNode; BusStop newStop, currentStop; currentStop = searchArrayStop(n.getCurrentNode().stopCode); List<TreeNode> newList = new List<TreeNode>(); if (n.getCurrentNode().nextNode != null) { string stop_name = n.getCurrentNode().nextNode.stopCode; newStop = searchArrayStop(stop_name); newArrayBusNode = newStop.arrayNode; } else { return null; } int newSize = newArrayBusNode.Count(); for (int i = 0; i < newSize; i++) { TreeNode newTreeNode; try { newTreeNode = new TreeNode(newArrayBusNode[i], n); if ((newTreeNode.getCurrentNode().busCode == n.getCurrentNode().busCode) && (newTreeNode.getCurrentNode().stopCode == n.getCurrentNode().stopCode)) { continue; } else if (checkLine(newTreeNode) >= 0) { newTreeNode.setParent(n); newList.Insert(0, newTreeNode); return newList; } else if (!(newTreeNode.getCurrentNode().busCode == n.getCurrentNode().busCode) || //(newTreeNode.getCurrentNode().getLine().getDirection() != n.getCurrentNode().getLine().getDirection() && newTreeNode.getCurrentNode().busCode == n.getCurrentNode().busCode) { //) { newTreeNode.setNumberOfChange(n.getNumberOfChange() + 1); newTreeNode.setPreviousDistance(n.getPreviousDistance() + BusFunction.convertToDistance(currentStop.geo, newStop.geo)); newTreeNode.setWeight(newTreeNode.getNumberOfChange() * 6.25 + newTreeNode.getPreviousDistance() + heuristicFuntion(newStop, endStop)); newTreeNode.setParent(n); newList.Add(newTreeNode); } else { newTreeNode.setNumberOfChange(n.getNumberOfChange()); newTreeNode.setPreviousDistance(n.getPreviousDistance() + BusFunction.convertToDistance(newStop.geo, currentStop.geo)); newTreeNode.setWeight(newTreeNode.getNumberOfChange() * 6.25 + newTreeNode.getPreviousDistance() + heuristicFuntion(newStop, endStop)); newTreeNode.setParent(n); newList.Add(newTreeNode); } } catch (Exception exc) { //e.printStackTrace(); } } return newList; }
/* * 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]); } } } } }