Quote: turnLeft(); or - turnRight move(); or - moveIntersection(int amount of times to move); checkinFront(); // - checks if wall is in front. turnDirection(Direction d); // you can write 'north' 'east' 'west' or 'south' pickThingUp(); getAvenue(); or - getStreet();Well you get the point. It must check every block on this grid to see if there's an object. Now I used a four loop to do this. However, my friend said it's a complete waste time and I should be using a while loop. So what's the best method to do this?
Robot Logic
I'm making an AI that is able to pick up things up that are in a similiar situation (as the picture below). I created methods that allow the robot to pick up an object.
Instead of asking you how to pick up the objects up, I'd like to know the most efficient way to cover all streets/avenues within this box? Would you use a for loop or a while loop?
Here's the sitution that the robot is in:
Commands that the robot can use are like:
Misusing a "for" loop when you could use a "while" loop is something I can forgive if anyone does it.
I can't tell what's the best way of transversing your data because I don't have a clue about how are you storing it.
I can't tell what's the best way of transversing your data because I don't have a clue about how are you storing it.
[size="2"]I like the Walrus best.
Especially at this level of learning, just make one that works first, and then worry about cleaning it up.
Work out how you, a human would walk the streets, and then make the robot do exactly that.
i.e. go either row by row, or column by column. Note that for efficiency you'll go left to right then right to left on the next row.
Again whether to use a for loop or a while loop depends entirely on how your data is stored. If it's just a 2D array of points, then for loop would make the most sense to me (but to some small extent that's personal choice).
-me
Work out how you, a human would walk the streets, and then make the robot do exactly that.
i.e. go either row by row, or column by column. Note that for efficiency you'll go left to right then right to left on the next row.
Again whether to use a for loop or a while loop depends entirely on how your data is stored. If it's just a 2D array of points, then for loop would make the most sense to me (but to some small extent that's personal choice).
-me
Well, here's my working solution:
EDIT:: Well that's basically it, except for the exit condition. But you get the point.
Tell me if this is pretty bad please or good.
boolean blnRight = true; // what the next turn will be:boolean finished = false; // set to 'true' when bot is doneint direction = 3; // amount of turns to do when we movewhile (finished == false){ if (guy.frontIsClear()) moveIt(guy); else { turnLeft(guy, direction); moveIt(guy); turnLeft(guy, direction); moveIt(guy); if (direction == 3) // if set to left turn, next turn is a right turn { direction = 1; } else // if set to a right turn, next turn is left. direction = 3; } }}public static void turnLeft(Robot me, int direction){ for (int i = 0; i < direction; i++) { me.turnLeft(); {}public static void moveIt(Robot me){ if (me.canPickThing()) me.pickThing(); else me.move();}
EDIT:: Well that's basically it, except for the exit condition. But you get the point.
Tell me if this is pretty bad please or good.
Will the robot always start in a corner? Will it always be the same corner? If the bot starts in a corner, the simplest solution to this problem is an inwards spiral. If it doesnt, then the alternatives are: (1) an outwards spiral from the starting position; (2) move to the centre of the map and perform an outwards spiral; or, (3) move to a corner and perform an inwards spiral. Any outward spiral has to possibility of excluding sub-regions if the search area is non-square, or the bot starts not from the centre of the map. The safest strategy is always to move to a corner and do an inwards spiral, although it's not the cheapest (in terms of time/movement cost).
Cheers,
Timkin
Cheers,
Timkin
November 16, 2006 04:53 PM
Quote: Original post by Instigator
Now I used a four loop to do this. However, my friend said it's a complete waste time and I should be using a while loop.
GOTO loops are better than either For or While loops!
.
.
.
seriously, the difference between for and while is the Last thing you should care about here, they're going to compile to about the same thing anyway.
Quote: Original post by Timkin
If the bot starts in a corner, the simplest solution to this problem is an inwards spiral.
Why is a spiral better than moving from left to right and then right to left each time (like a plow path)? Seems that the latter would be easiest to code:
for each row { for each column { check for an object and pick it up move left or right (on alternate rows) } move down}
---
Anyway,
Assuming you're starting at the corner, it makes no difference at all whether you are using a for loop or a while loop: if your current algorithm makes your robot hit every intersection exactly once, then you have already made the algorithm as efficient as it needs to be.
If you are not starting at a corner, the most efficient algorithm is non-trivial. I'd go for Timkin's solution and head for the nearest corner (if you know where it is), and not worry too much about the slight loss in efficiency.
---
Edit: I guess if you don't know the size of the box, it might be slightly simpler to code using a while loop:
While you're not at the end { While you're not facing a wall { Check for an object and pick it up move forward } turn (right/left) move forward turn (right/left)}
Quote: Original post by AsbestosQuote: Original post by Timkin
If the bot starts in a corner, the simplest solution to this problem is an inwards spiral.
Why is a spiral better than moving from left to right and then right to left each time (like a plow path)? Seems that the latter would be easiest to code:
Because if you don't start in a corner, there is less crossing of searched space if using a spiral (unless you use a hybrid spiral-sweep). If you do start in a corner, the two methods are basically equivalent, except that if you perform a spiral, you perform less turns than using a sweep path. The optimality of the spiral approach springs from robotics where turns cost energy and time and you want to conserve both. Obviously in a game or other simulated environment, you can arbitrarily set the cost of a turn to zero and actions can be performed instantaneously.
Cheers,
Timkin
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement