Robot movement and neural netting - variable tables

I’ll keep this simple.

Using FMX:

I have a scene in which a robot runs around an “arena”. It has two bump sensors. When a bump sensor comes into contact with the arena, it registers a collision with that sensor, and backs up and moves accordingly.

I already have code for precisely locating the position of the sensor when it hits something.

What I want to do is:

Have the robot run through the arena for a while, and everytime it bumps into an object (there are “rocks” in the arena), to write that exact location (x,y) to a list of some sort, or perhaps an array. Idealy I would like to write each value to an external text file.

This is where the cool stuff comes in. I am going to use the newly created “bump map” to tell the next robot where not to go.

I just can’t think of a good way to make a giant list of variables like I would need here. If only I could save the traces for these variables…

Any ideas for getting this huge variable list?

Just for fun here is an early version of what I am talking about (just the robots bouncing around). Code is not perfect so sometimes they escape. They also have an affinity for that light, so you can play around with that.

Thanks in advance.

here is? I dont see anything…

but on first guess, yes, an array would seem appropriate. Getting another robot to react to that array is a whole 'nother story - a challenge in fact without some cunning approaches.

anyway, whenever theres a collision you would do something like:

collision_array[collision_array.length] = [robot._x, robot._y];

assuming youve already declared collision_array as an array beforehand (ie. collision_array = new Array(); or collision_array = [];). It makes a 2d array, an array of arrays. collision_array contains arrays of locations, x and y, where there was a collision

collision_array[0] is the first array of x and y
collision_array[0][0] is the x
collision_array[0][1] is the y
etc.

oops! :-\ http://www.redscreen.net/exp/robot_simple.html

There it is.

Ok I believe I understand what you mean about an array of arrays, and that would seem to fit my application well. Will it just keep adding arrays to it, or do I have to specify # of rows, # of columns…? Know what I mean?

As for reacting to the array, yes, figuring that out will be a challenge, especially since I just learned actionscripting this past weekend. Here are some ideas I am tossing around.

Assuming I have an array of x,y coordinates:

Option 1: at every frame (or loop) draw magnitude vectors to all the points, and continuously check which ones are getting shorter, and which ones longer. Tell it that if one is getting too short, to turn in the opposite direction… shoot I have to go to class, I’ll write more later.

Just so you know, the current movie (linked above) uses several random number generators to produce a “wander” effect, and it goes to the light by useing the “draw vectors from sensor to light position, check which is shorter, turn that way” method.

Thanks! More later!

rather than a 2d array, i’d make an array of objects:

rocks=[];

to add a rock:

rocks.push({x:xvalue, y:yvalue});

now you can say:

rocks[0].x
rocks[0].y

Really good idea Bit!

So, by make an array of object - you mean, it’s kinda like the movie space is a grid, and every space on the grid corresponds to a coordinate on the array, like so:

__1 2 3 4 5 6 7
1 |0 0 0 0 0 0 0
2 |0 0 0 1 0 0 0
3 |0 1 0 0 0 0 0
4 |0 0 0 0 0 0 1
5 |0 0 0 0 0 0 0

So this array would show that there are objects at (2,4) , (3,2) , and (4,7).

That what you are getting at? Then I can just say "when bump sensor hits something, push the sensors x and y values into the array at coordinate this._x, this._y?

it really doesnt matter whether you use an object or an array. An object just lets you use letters instead of numbers to reference your values. In either case you’re making a list of two values

  1. movieClip location1 (x, y)
  2. movieClip location2 (x, y)
  3. movieClip location3 (x, y)
  4. movieClip location4 (x, y)

wether you keep them in arrays or objects is up to you.

array:
list = [
[x, y],
[x, y],
[x, y],
[x, y],
[x, y]
… ];

object:
list = [
{x: x, y: y},
{x: x, y: y},
{x: x, y: y},
{x: x, y: y},
{x: x, y: y}
… ];

but think of it as that, a simple list of your positions and not a grid of reference (though in essense thats what _x and _y give you are locations within a grid, but dont try to confuse yourself with that if you already know what _x and _y represent ;)).

so what you would end up doing is everytime there is a collision, you take your array of collisions, “rocks_array” for the example, and add in the coordinates of the location of that robot. example (using an object)

if (this.hitTest(rocks_mc)){
rocks_array.push({x:this._x, y:this._y});
// move backwards/react/etc.
}

Then whenever you need to reference a position in that rocks_array, you just use
rocks_array[number].x
rocks_array[number].y
where number is the collision number you want to reference, 0 being of course the first collision.

Yes yes I see it now! What I would also like to do is output all of these values to another movie clip - one that would just show all the little dots of the array. This doesn’t have any practical use, but I think it would be rad.

Thanks so much for your help guys, this should give me something to work on for a while. Be sure to have another post in here about this jazz later on.

in that case youd want to use _x and _y in the objects instead of x and y. Then you can use those objects as init objects in attached dots. ( rocks_array.push({_x:xvalue, _y:yvalue}); )

So first make a movieclip which will act as your ‘dot’. You can make it a circle, a triangle or a star or whatever and give it a linkage Identifier “dot” (in export for actionscript). Then, to attach them, you just say


for (i=0; i < rocks_array.length; i++){
	_root.attachMovie("dot", "dot"+i, i, rocks_array[**i]);
}

be aware that this is attaching clips in depths 0 - rocks_array.length-1 so if youve attached other clips on the screen at these depths, they may be replaced. Alternatively, you could place them in their own movieclip:


_root.createEmptyMovieClip("dotHolder",0);
for (i=0; i < rocks_array.length; i++){
	_root.dotHolder.attachMovie("dot", "dot"+i, i, rocks_array[**i]);
}

Now this attaches the “dots” all in one quick swoop, not everytime there is a collision, in which case you would just stick it in with the adding on of your array. Something along the lines of

if (this.hitTest(rocks_mc)){
rocks_array.push({x:this._x, y:this._y});
_root.dotHolder.attachMovie(“dot”, “dot”+depth, depth++, {_x:this._x, _y:this._y});
// move backwards/react/etc.
}

ID also like to add that this could very well clog things up because the number of collisions can quickly increase and the number of dots (or the size of your array for that matter) can get too large for Flash to easily handle and you might run into some problems there.

Yes I forsee this as well. Any ideas on a different way to do Parent/Child obstacle avoidance (as it could be applied in real life)?

you could attach (or even calculate without attaching for that matter) hidden circles of avoidance in an underlying layer/clip. Then, test other collision points as they arise to be either within or out of those circles. If within, ignore them, since that area is designated avoidable. If not, create a new circle in that point of collision. That way you greatly reduce the number of actual referencing collision markers in all. You can then use proximity on those clips to redirect another robot away from getting too close, therefore avoiding any collision which was previously determined possible in that area.

yeah,

and what if instead of attaching clips, you dynamically drew shapes into an avoid clip? then you detect everything with a single hitTest, albeit a complicated one.

harder to do avoidance though…

if you attach clips within a single clip, you can use hittest on that single clip to test for all the attached, though drawing would reduce mc overhead.

oh yeah (duh…) ; )

you might also consider giving your bot “eyes”. he could scan the area around him and then make a decision about which direction to travel in, taking into account where he wants to go and the immediate terrain.

thats a cool idea, the eyes :slight_smile:

*Originally posted by senocular *
**you could attach (or even calculate without attaching for that matter) hidden circles of avoidance in an underlying layer/clip. Then, test other collision points as they arise to be either within or out of those circles. If within, ignore them, since that area is designated avoidable. If not, create a new circle in that point of collision. That way you greatly reduce the number of actual referencing collision markers in all. You can then use proximity on those clips to redirect another robot away from getting too close, therefore avoiding any collision which was previously determined possible in that area. **

Great idea, and that seems to be the best solution.

I have no real idea how to test proximity with flash (not efficiently anyway). What I do now to get it to go towards the light is:

Math.sqrt(((Math.abs((leftbump._x)-(light._x)))^2)+((Math.abs((leftbump._y)-(light._y)))^2)) = leftbump_dist_to_light

Then I do the same with the right bumper.

Then I say “if rightbump_dist_to_light < leftbump_dist_to_light then turn right…etc.”

Maybe there is a better way to do this? I have honestly just started writting actionscript, and I haven’t ready anything on how to do any of it… so I’ll admit that a lot of the things you speak of (arrays and creating new movie clips / objects) is foreign to me right now, but I’m sure once I start writting it this will all come together.

Thanks again for all the help and code examples on this. I think it’s really interesting, and I hope to develop it further.

*Originally posted by sbeener *
**oh yeah (duh…) ; )

you might also consider giving your bot “eyes”. he could scan the area around him and then make a decision about which direction to travel in, taking into account where he wants to go and the immediate terrain. **

Hehe, yes, this is of course my ultimate goal. In fact I hope to simulate much of what I see in the robot world in flash, because it’s a really good way of getting immediate results while changing different variables.

As I have said I am completely new to action scripting, so I really have no idea how to write the code for eyes that scan the terrain, though this would rule. If anyone has ideas on how to do this, shoot!

…hmm, and as I think… could you maybe continually scan the area a certain radius from the bot (with a bunch of point hitTests), and then make decisions based on that? Hmm, wow that would be rad.

*Originally posted by jingman *
**could you maybe continually scan the area a certain radius from the bot (with a bunch of point hitTests), and then make decisions based on that? Hmm, wow that would be rad. **

yeah, I was going to mention that earlier, that if your goal is simply to try to avoid the obstacles all together (as opposed to the object being ‘learning’ which is cool within itself) you could just have an invisible (or visible for that matter) ‘antenna’ sticking out in front of the robot guy which would ‘feel’ for obstacles and cause a change in direction if one was found (hittest). This could be just a flat out static point out ahead or it could even scan back and forth if you want to get fancy, but it would act as that kind of scanning.

Insects were on to something when they grew those :wink:

insects are cool…

this kind of stuff is so fascinating.

it strikes me as an interesting idea for a contest. there hasn’t been one on kirupa for a while.

there’d have to be some rules. the objective would be to build a bot (under a given number of kb) that could navigate through some kind of terrain in the quickest time.