Jump to content
  • Advertisement
Sign in to follow this  
Instigator

Robot Logic

This topic is 4227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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: Robot Commands that the robot can use are like:
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?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Well, here's my working solution:


boolean blnRight = true; // what the next turn will be:
boolean finished = false; // set to 'true' when bot is done
int direction = 3; // amount of turns to do when we move

while (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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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)
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Asbestos
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:

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!