2016-04-06 25 views
-2

私は、3つの異なるCSVファイルからxとy座標の2つのclomunsを読み込み、すべてのポイントに一度だけ触れている間に短い方法を計算できるプログラムに取り組んでいます(Traveling Salesman問題)コードは異なる値で同じ結果を返す

私は2つの異なる問題があります:まず、同じcsvファイルに3回置くと、出力(正しい順番の点の一覧)は同じように見えますが(期待どおりですが)計算された方法は完全に異なっていますか?

第3に、3つの異なるcsvファイルを配置すると、3つの出力リストのそれぞれの最初のポイントが同じポイントになりますが、各ファイルの開始点が異なるため1。

//calls needed Methods 
     public static void main(String[] args) { 
     Main main = new Main(); 
     String csvFile1=main.getPath(); 
     String csvFile2=main.getPath(); 
     String csvFile3=main.getPath(); 
     double start = new Date().getTime(); 
     main.readData(csvFile1); 
     main.createPoints(); 
     main.calculateWay(); 
     main.cleanUp(); 
     main.readData(csvFile2); 
     main.createPoints(); 
     main.calculateWay(); 
     main.cleanUp(); 
     main.readData(csvFile3); 
     main.createPoints(); 
     main.calculateWay(); 
     main.cleanUp(); 
     double runningTime = new Date().getTime() - start; 
     main.time=runningTime/1000; 
     main.displayResult(); 
    } 

    //gets the path to the file 
    private String getPath() { 
     //... works fine 
    } 

    //reads x and y column and stores them in arraylists xlist and ylist 
    private void readData(String csvFile) { 
     //... works fine 
     } 
    } 

    //creates new point objects 
    private void createPoints() { 
     int i = 0; 
     while (i < listx.size()) { 
      setPoints(new Point(Integer.parseInt(listx.get(i)), Integer.parseInt(listy.get(i)))); 
      i++; 
     } 
     System.out.println(points.get(0).getX()+ " " +points.get(0).getY()); 
    } 

    //stores the point objects 
    private void setPoints(Point p) { 
     points.add(p); 
    } 

    //algorithm 
    private void calculateWay() { 
     Point local=null; 
     this.current=points.get(0); 
     double lastDistance; 
     int i=0; 

     while(i<points.size()) { 
      lastDistance=1000; 

      for (Point p: points) { 
       double distance=getDistance(p); 
       if (distance<lastDistance) { 
        lastDistance=distance; 
        local=p; 
       } 
      } 
      addWay(lastDistance); 
      createFinalList(local); 
      points.remove(local); 
      this.current=local; 
      i++; 
     } 
    } 

    //returns distance between 2 points 
    private double getDistance(Point p) { 
     double way=Math.sqrt((current.getX()-p.getX())*(current.getX()-p.getX())+(current.getY()-p.getY())*(current.getY()-p.getY())); 
     return way; 
    } 

    //adds the shortest found distance to the overall way 
    private void addWay(double x) { 
     switch (task) { 
     case 1: this.way1=way1+x; 
     case 2: this.way2=way2+x; 
     default: this.way3=way3+x; 
     } 
    } 

    //creates the list of points in correct order 
    private void createFinalList(Point p) { 
     switch (task) { 
     case 1: finalpoints1.add(p); 
     case 2: finalpoints2.add(p); 
     default: finalpoints3.add(p); 
     } 
    } 

    //sets up algorithm for new run 
    private void cleanUp() { 
     listx.clear(); 
     listy.clear(); 
     points.clear(); 
     this.current=null; 
     task++; 
    } 

    private void displayResult() { 
     //display informations on console 
    } 
} 
+0

に触れたことはありませんしているようで、デバッガを使用して自分でそれを把握。その彼らが – redFIVE

答えて

0

コードが読みかなり難しいですが、視力に次のことをジャンプ:

  • あなたが休憩を忘れてしまいました。あなたのスイッチのケースでのステートメントあなたのPoint pとあなたのPoint currentとの距離を計算する際には、それらが同じではないことを確認する必要があります(!p.equals(current){の場合)。 ..})。 'current'と 'p'がお互いに比較されるので、これは現在ゼロの距離になります
  • これは考えていませんが、同じ距離を何度も計算していると思います。
  • あなたの内側のループが小さく距離が見つからない場合でも、あなたはあなたの方法に
  • を事前に定義された10000を追加する「タスク」変数は
+0

のために作られたものは完全に正しいです、これはかなりobvだったはずです。しかし、私はそれを実現しませんでした。これは間違いなく同じ入力で異なる方法で問題を引き起こしました。どうも – Master1114

関連する問題