Java 最短成本路径 : Uninformed/Informed search from a (. txt) 文本文件

标签 java artificial-intelligence helper shortest-path

我正在执行一项不知情的搜索任务,但我陷入困境。

/image/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;


}

}

我就是不知道该怎么做。任何帮助将不胜感激。提前致谢

最佳答案

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

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

通过解析文件来填充 map 。其余代码应该按原样工作。

关于Java 最短成本路径 : Uninformed/Informed search from a (. txt) 文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418545/

相关文章:

java - xjc 命令未创建模式中定义的所有类

c++ - "Edge Detection"和 "Image Contours"之间的区别

algorithm - 人工智能/规则来猜测用户对服装/服装的品味

php - codeigniter 中的自定义助手

java - 使用正则表达式,是否可以使用 'Followed by' 或 'Preceded by' 等表达式

java - XML 处理 - 性能问题

artificial-intelligence - A* 图搜索

ruby - 如何在 Sinatra 中制作模块化助手

ruby-on-rails - 如何测试使用 RSpec 接受 block 的 Rails 助手?

java - 使用预定义字符混合/混淆字符串的简单算法