Java最短成本路径:来自(.txt)文本文件的未知/已知搜索

时间:2012-02-23 17:59:30

标签: java artificial-intelligence helper shortest-path

我正在处理不知情的搜索任务,但我被困住了。

http://i.stack.imgur.com/d4n18.png

我有90%的工作,但我想:

1)从.txt文件而不是代码中加载数据(城市和里程)。 2)允许程序获取三个命令行输入参数:输入文件名,源城市和目标城市

示例:findroute inputFilename originCity DestinationCity

命令行示例:findroute input1.txt慕尼黑柏林

这是我现在的代码的一部分:

    package graph;
   import java.util.Formatter;
   import java.util.List;
   import bisearch.UniformCostSearch;
   import search.Action;

public class findRoute {
   /**
    * finds the shortest path 
    */




      public static void main(final String[] args) {
           Graph graph = findRoute.Map();

           GraphStateSpaceSearchProblem sssp = new GraphStateSpaceSearchProblem(
              graph, " Zerind ", " Oradea ");

          bisearch.Search bisearch = new UniformCostSearch();
          List<Action> actions = bisearch.search(sssp);
  findRoute.printOutput(bisearch.nodesExplored(), actions);





   }


   /**
    * prints the path found
    */
   private static void printOutput(final int nodesExplored,
         final List<Action> actions) {


      double cost = 0;
      for (final Action action : actions)

     cost += action.cost();
      System.out.println("Distance: " + new Formatter().format("%.2f", cost)+" m");
      System.out.println ("Route: ");

      for (final Action action : actions)

         System.out.println(action);

   }



   /**
    * creates a map of as a Graph

    */


   private static Graph Map() {
      final Graph graph = new Graph();

      graph.addUndirectedEdge("Oradea", "Zerind", 71);
      graph.addUndirectedEdge("Zerind", "Arad", 75);
      graph.addUndirectedEdge("Arad", "Sibiu", 140);
      graph.addUndirectedEdge("Sibiu", "Oradea", 151);
      graph.addUndirectedEdge("Timisoara", "Arad", 118);
      graph.addUndirectedEdge("Timisoara", "Lugoj", 111);
      graph.addUndirectedEdge("Lugoj", "Mehadia", 70);
      graph.addUndirectedEdge("Mehadia", "Dobreta", 75);
      graph.addUndirectedEdge("Dobreta", "Craiova", 120);
      graph.addUndirectedEdge("Sibiu", "Fagaras", 99);
      graph.addUndirectedEdge("Fagaras", "Bucharest", 211);
      graph.addUndirectedEdge("Sibiu", "Rimnicu Vilcea", 80);
      graph.addUndirectedEdge("Pitesti", "Rimnicu Vilcea", 97);
      graph.addUndirectedEdge("Craiova", "Rimnicu Vilcea", 146);
      graph.addUndirectedEdge("Craiova", "Pitesti", 136);
      graph.addUndirectedEdge("Pitesti", "Bucharest", 101);
      graph.addUndirectedEdge("Bucharest", "Giurgiu", 90);
      graph.addUndirectedEdge("Bucharest", "Urziceni", 85);
      graph.addUndirectedEdge("Urziceni", "Hirsova", 98);
      graph.addUndirectedEdge("Hirsova", "Eforie", 86);
      graph.addUndirectedEdge("Urziceni", "Vaslui", 142);
      graph.addUndirectedEdge("Vaslui", "Iasi", 92);
      graph.addUndirectedEdge("Neamt", "Iasi", 87);
      return graph;


}

}

我无法弄清楚如何做到这一点。任何帮助将不胜感激。提前致谢

3 个答案:

答案 0 :(得分:0)

1)逐行阅读文件,用你的分隔符拆分行,你应该好好去(google怎么做)

2)程序已经接受参数。它们在你的args数组中。你只需要使用它们(args [0],args [1] ...)

答案 1 :(得分:0)

你需要使用args []。命令行参数通过args []变量作为数组传递给java程序。

args[0] = inputFilename
args[1] = originCity 
args[2] = DestinationCity

通过解析文件来填充地图。其余代码应该按原样运行。

答案 2 :(得分:0)

对于第1部分,您希望使用Java为您提供OOB的File对象。然后,您需要读取该文件中的值,我会推荐FileReader对象。实例化File对象时,需要使用args String数组中的索引查找。

对于第二部分,该程序已经这样做了。如果要限制参数的数量,需要根据所需的输入参数检查args.length变量。