Results 1 to 18 of 18

Thread: Navigating Bots

  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Question Navigating Bots

    Greetings Java Friends

    Warning: This is going to be slightly long.

    The Challenge:

    Create Two Bots that will exit the Graph safely. The first bot called ( QuickBot ) will aim to exit the Graph as fast as possible. The second bot called ( GreedyBot ) will aim to exit the graph with the highest amount of resource possible.

    A bot can not reach a resource level of 0 << it will die if that happens.

    My Solution:

    The Bot will always land at the Entrance node. There I will make the bot look through the graph via the getMap() method (returns Hashtable<Node<Adjustment>, ArrayList<Node<Adjustment>>>) and somehow get all the available paths to the Exit node. Each one of these will be saved in an ArrayList<Stack<String>>, the Stack represents a single path. Once that is done, The Bot will go through each of the available stacked paths and see which ones cause the Bot to die, and delete those. The Bot will then somehow calculate which of the Stack Paths gives the shortest route possible (for Quick) or the route that deducts least resources (for Greedy) to the Exit node.

    Once the ideal stack path is calculated (depending on nature of the Bot, Quick or Greedy), that stack will be left as a message at the Entrance node of the Graph. This way all the nodes entering later, can read the Stack from the message and follow the path specified in the Stack, and they too can Exit according to the Bot's nature.

    Relax ! I know that was a lot to take in . For me personally images always help. I hope it is the same for you. Here's a particular Graph. Called Graph5.



    I hope you appreciate my handy photoshop work, it took me 20 minutes to create that

    So that's what the Graph looks like on Paper. But in Code it is represented as a Hashtable. Here's an image representation of the Hashtable.



    On the left the Key representing a node. On the right are the values and they are an ArrayList of the neighbouring nodes.

    And now my code:

    Java Code:
    /**
    * The stuff in this method is run when the Bot is present at the node.
    */
    public void visitNodeActions() throws BotError {
        	
     	if(isAtEntrance())
        	{
        		// A place to store the safe paths.
        		ArrayList<Stack<String>> paths = new ArrayList<Stack<String>>();
        		
        		// The variable g stands for, and is the Graph object < which in itself is a Hashtable.
        		Hashtable<Node<Adjustment>, ArrayList<Node<Adjustment>>> g = getMap().getAdjacencyList();
        		
        		// Iterating through [g] to find safe paths.
        		Enumeration<Node<Adjustment>> e = g.keys();
        		
        		while(e.hasMoreElements())
        		{
        			
        			// e.nextElement can only be used once within the while loop. So I'm assigning it to a variable that can be used many times.
        			Node<Adjustment> x = (Node<Adjustment>)e.nextElement();
        			
        			// Check to see if we have the Start Node.
        			if(getLocation() == x.getName())
        			{
        				// A variable called [neighbours]. It represents the neighoubrs of the current node in the loop
            			ArrayList<Node<Adjustment>> neighbours = g.get(x);
            			
            			// The Path [p] represented as a Stack. This will be inserted into our safe [paths] ArrayList later on.
            			Stack<String> p = new Stack<String>();
            			
            			// Iterating through the neighbours of the current node, and foreach building a path [p] from them. 
            			for(Node<Adjustment> n : neighbours)
            			{
            				p.push(x.getName());
            				p.push(n.getName());
            				
            				// Adding the stacked path [p] to the safe paths list [paths]
                			paths.add(p);
                			
                			// Now that the stacked path [p] has been stored, we need to clear, empty [p] for the next path.
                			p.clear();
            			}
        			}    			
        		}
        		
        	}
        	
        }
    Where I'm Stuck:

    My code currently stores the following two paths successfully.

    AB
    AC

    But what I need it to do afterwards, is get me the following paths:

    ABF
    ACD

    Whilst still keeping the original two paths, so all in all the saved paths will be:

    AB
    AC
    ABF
    ACD

    and then after that, it will be:

    AB
    AC
    ABF
    ACD
    ACDE

    and so on ..

    I do not know how to get my code to get me results such as above. If you think there is ill-logic in my solution to the challenge, please help point them out. If not, I would greatly appreciate any help in getting the code to do what I need it to do.

    If you have any questions, or if I have not explained something clearly, please do not hesitate to ask. I have tried to make it as easy as possible for you to help me (I hope).

    Thank You So Much.
    Attached Files Attached Files
    Last edited by Ciwan; 06-26-2011 at 07:11 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    How do we execute your code to test it?

  3. #3
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Good Question. Because I still don't know how to execute code in Eclipse. What I do is copy it over BlueJ. There I right click on the Test Class, and choose Graph5. Here's a screenshot:



    The reason I use, Eclipse is because of all the advanced features like Auto Code Completion and a list of methods for each object as one is typing ! It would be nice if I could do the Unit testing in Eclipse, Unfortunately I have no idea how. >> That's another issue though. My main concern now is the problem I'm having above.

    Ooops ! How do YOU execute my code to test it. !! erm .. I don't know ! what do you suggest ? I don't think I can attach zip files to my posts !
    Last edited by Ciwan; 06-26-2011 at 06:28 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    How do YOU execute my code to test it
    I would compile it with the javac command and execute it with the java command.

  5. #5
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Hehe you are a Pro, your skills are light years ahead of mine. I've never really used the Java Command.

    Are you saying you would take the time to test my code ? that would mean the world to me. If Yes ... how do you suppose I provide the code to you and others so that they can test it ?

    Thanks

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    That's a problem with how you were taught. The first lesson should be how to use the javac and java commands to compile and execute a program. Later the IDE could be brought in.
    Starting with an IDE leaves a big hole in your knowledge of what is happening. Too much magic done by the IDE.

    Are you saying you would take the time to test my code
    Yes, but my approach is not to tell you what the problem is, if I find it, but rather tell you how to find the problem. Most often that is by telling you to add printlns to the code to see what is happening.

    You appear to have a logic problem. You need to learn how to solve these kinds of problems. If you have an interactive debugger, that will make some of it easier. Otherwise you can use printlns. Its a very iterative process.
    You print out some variables, see what is happening but realize you need more info and go back and add more printlns. Sometimes you have to add some special debugging code to show what the code is doing.

  7. #7
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    That's a problem with how you were taught. The first lesson should be how to use the javac and java commands to compile and execute a program. Later the IDE could be brought in.
    Starting with an IDE leaves a big hole in your knowledge of what is happening. Too much magic done by the IDE.
    You are right.

    Quote Originally Posted by Norm View Post
    You appear to have a logic problem. You need to learn how to solve these kinds of problems. If you have an interactive debugger, that will make some of it easier. Otherwise you can use printlns. Its a very iterative process.
    You print out some variables, see what is happening but realize you need more info and go back and add more printlns. Sometimes you have to add some special debugging code to show what the code is doing.
    Are you talking in general programming terms ? or are you saying you have found an error in my logic outlined in the first post ?

    Also, about the code ? how do people usually share projects files on here ?

    Thanks Norm.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I have not looked at your code

    how do people usually share projects files on here ?
    Go Advanced button and add an attachment. Usually a zip of the source.

  9. #9
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Go Advanced button and add an attachment. Usually a zip of the source.
    Nice, I have Exported my Eclipse project as a .jar file, then zipped it and attached it to my first post in this thread.

    Thanks Norm.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    There are over 30 java files there. I don't know when or if I'll try to wade thru all that.

  11. #11
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    You're right, even I myself don't know what the use of two thirds of them are. I've been told to only work on Quick and Greedy Bot.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    You are really in the control of your environment. The code you posted does not have a main method where execution starts. The is no way to execute the code you posted.

    What kind of course are you taking? Are you supposed to be learning Java programming? Your IDE or BlueJ isolates you from the real world.

    I suspect that what you have posted will only work with the tools that you are using.

  13. #13
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    The code you posted does not have a main method where execution starts. The is no way to execute the code you posted.
    I know, like I said I don't know what to put in the Main Static Method to get the execution rolling.

    Quote Originally Posted by Norm View Post
    What kind of course are you taking? Are you supposed to be learning Java programming? Your IDE or BlueJ isolates you from the real world.
    The course is about Internet Systems, but there are certain modules (like this one) where I honestly can't see any relations to the Web Development world. I did really good on the Web Development modules (e.g. ASP.NET MVC3, Ruby on Rails, SQL) but on this one, I did really bad.

    Quote Originally Posted by Norm View Post
    I suspect that what you have posted will only work with the tools that you are using.
    I would love to be able to work from Eclipse only, but I don't know how to make the Unit tests he (the tutor) has provided to work in Eclipse.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    the Unit tests he (the tutor) has provided to work in Eclipse
    Exactly the problem that I was describing in my last post. Some magic thing that you give your code to.
    I can't see any easy way to simulate the Unit test code. It must have 'hooks' that connect to your code and execute it.

    Good luck.

  15. #15
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Thanks I'll need it

  16. #16
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @OP:
    I tried to run it, but didn't succeed. I'll give you my comment-file, made in the process:
    The Main-class didn't do anything
    Made packages to organize the code.
    Classnames should indicate category (BotDeath is an error)
    StrippedBot is not extending Bot
    StrippedNode is not extending Node
    Class NodeInfo doesn't exist (compiler doesn't complain because of existing superclass Value)
    I made some errorconstructors public (needed because of my moving)
    The Maze-constructor needs a maze-specificationfile (not included) <--please provide one for the format
    Last edited by Jodokus; 06-27-2011 at 12:44 AM. Reason: spelling
    No bug ever had to calculate its fitnessfunction.

  17. #17
    hdot is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default

    I can test if you like?

    Regards

  18. #18
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    @OP: "public static void main" should be one of the first thing that is taught in java programming.
    You might want to read The Really Big Index

Similar Threads

  1. navigating between jsp
    By newto in forum New To Java
    Replies: 5
    Last Post: 05-12-2010, 01:38 PM
  2. Creating and Navigating a 2D Array from .dat file
    By RobertF in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 05:44 PM
  3. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 PM
  4. Navigating a URL
    By Paul77 in forum New To Java
    Replies: 0
    Last Post: 11-16-2008, 09:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •