This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue displaying result of algorithm thanks to a loop with JSwing

 
Antoine Compagnie
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

I am trying to code A* algorithm along with Breadth First Search and Depth First Search on several game of fifteen (reduced to 3*3 games) problem stored in a file games.txt from which I show an exerpt:

% 2
142305678
120345678
% 4
125304678
125304678
% 5
301452678
301452678
142358607


I want to be able to display a following array:



Yet I am not able to display the result of the algorithms in the JFrame. On the one hand I am not able to iterate, on the other hand, I am not able to stop algorithm that would take too much time such as DFS.
Can you help me displaying the results in time in my JFrame each time an algorithm has solved or fill a blank if it took too much time?

My code (javaproject) is composed of several files:


  • [*]Problem
    [*]Project
    [*]Puzzle
    [*]PuzzleAction
    [*]Searchable


  • A Puzzle implements Searchable.

    The main is contained in Project. It contains the graphic components:
    Two classes display the result :
  • [*]Window
    |*]Panneau


  • Ask me for anymore details !

    Here are the two important files to modify:

    Problem:



    And here is the code of Project with the main:






     
    Antoine Compagnie
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I actually had some problem solving some puzzles so I started a timer for each algorithm when lunched which stop the algorithm after a second.
    I now have the results in time but I'm not able to display them properly on the JFrame. It writes the result at the same place in the middle each time it solve a problem with an algorithm.
     
    Campbell Ritchie
    Sheriff
    Posts: 51450
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is called Swing, not JSwing.
    Do you have have an application which shows an A* search without a GUI? Don't try writing a GUI until you have that application working. Make sure the app has a public interface with methods like getSerachResult(). Only when you have that working shou‍ld you even think about writing the GUI. That also means that you need to move all the logic out of the main method.
     
    Antoine Compagnie
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Indeed, I was maybe too much enthusistic of creating my first GUI. I do have a good A* algorithm but I'm not able to catch good statistics.

    I want to catch the mean of the time that took each algorithm on problems that were to be solved in specific times: % 2 has to be solved in two movments, % 3 in three movments. At the moment I am able to catch these times for every algorithm that hasn't failed to solve a given problem in less than a second.
    Yet, how to deal with algorithms that take too much time? How to calculate the mean?
     
    Antoine Compagnie
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a serious problem.
    I am a serious issue. Having the time that took each algorithm for each problem I am able to calculate the mean of the BFS algorithm but any one of the other ones:
    Here is the code to diplay the result and calculate the means:

    [code=java PrintWriter writer = new PrintWriter("result.txt", "UTF-8");
    writer.println("runTime DFS" + runTimesDFS);
    writer.println("run time BFS : " + runTimesBFS);
    writer.println("run Times AStar1" + runTimesAStar1);
    writer.println("run Times AStar2" + runTimesAStar2);

    writer.close();

    meanDFS.addLast((double)mean(runTimesDFS));
    meanBFS.addLast((double)mean(runTimesBFS));
    meanAStar1.addLast((double)mean(runTimesAStar1));
    meanAStar2.addLast((double)mean(runTimesAStar2));

    PrintWriter writer1 = new PrintWriter("mean.txt", "UTF-8");
    writer1.print("d\t");
    writer1.print("BFS\t");
    writer1.print("DFS\t");
    writer1.print("A*(h1)\t");
    writer1.println("A*(h2)\t");

    for(int i =0;i<problemSize;i++){
    if(i<=1||i==3)continue;
    if(i==2){
    writer1.print(i+"\t");
    writer1.print(meanDFS.get(0)+"\t");
    writer1.print(meanBFS.get(0)+"\t");
    writer1.print(meanAStar1.get(0)+"\t");
    writer1.println(meanAStar2.get(0)+"\t");
    continue;
    }
    writer1.print(i+"\t");
    writer1.print(meanDFS.get(i-2)+"\t");
    writer1.print(meanBFS.get(i-2)+"\t");
    writer1.print(meanAStar1.get(i-2)+"\t");
    writer1.println(meanAStar2.get(i-2)+"\t");


    }
    writer1.close();[/code]

    Here is the result.txt file content:




     
    Antoine Compagnie
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have a serious problem.
    I am a serious issue. Having the time that took each algorithm for each problem I am able to calculate the mean of the BFS algorithm but any one of the other ones:
    Here is the code to diplay the result and calculate the means:



    Here is the result.txt file content:



    And here is the mean.txt file




     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic