Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

WILL at RedAnt

Retarded Bugs need help. Still needing somehelp.

This topic is 5537 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

Ok, if you have read Fupster's tutorials you'll know exactly what I'm trying to accomplish. What I am basically doing is taking a population of bugs each with it's own NN and setting them in a small 2D world where they move around and eat food. I'm using a GA to teach them to go after the food. Eventually after a good few generations(20-40 in fupster's program) they should be very good at getting the food and you can see the major difference. Well I'm not so lucky. For some reason despite my best efforts I have not been able to determine why my bugs just don't learn properly. The darn things just twirl in one direction or if I'm lucky go in a basic straight line. They don't really react to the food as they should. And high score from bugs eating the food seems to be pure fluke. Anyone that knows Pascal or Delphi(or just wants to help), I'd truly appreciate it. I've been at this darn program for months and I've looked at, I think everything. It all seems to check out ok. But the program doesn't work. :/ I have just updated the program today with a new binary and source. You can download it from here: http://aiworkshop.tripod.com/ It is called "AI Bug World" Build 9. I'd appreciate any feed back or assistance you may provide. I, myself am stumped. If you do now know what I'm talking about then please visit www.ai-junkie.com and read the tutorial on GAs and then his one on NNs and you'll see his program and you then can see what it is that I'm trying to mimic. Thanks, WILL [edited by - WILL at RedAnt on April 24, 2003 3:40:02 PM]

Share this post


Link to post
Share on other sites
Advertisement
How many hidden layers do your bug ANN''s have? And how many nodes are there in a layer? I''ve been experimenting with FUP''s demo and I get the best results with one hidden layer with between 2 and 8 nodes.

If I run FUP''s demo with more than one hidden layer of nodes, I get exactly the results you just described.

Sander Maréchal
[Lone Wolves GD][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]

Share this post


Link to post
Share on other sites
I use 2 layers and simulate the input layer with an array of Reals.

4 - inputs(array of reals not neurons)
6 - hidden layer
2 - output layer

Here is my NNs execution code:

procedure TNeuralNet.Execute(Input: Array of Real; var Output: Array of Real);
var i, j: Integer;
inBuffer, outBuffer: Array of Real;
begin
if (Length(Input) = NumberOfInputs) then
begin
SetLength(outBuffer, Layers[0].NumberOfNeurons);
Layers[0].Execute(Input, outBuffer);
if (NumberOfLayers > 1) then
for i := 1 to NumberOfLayers - 1 do
begin
SetLength(inBuffer, Layers[i - 1].NumberOfNeurons);
for j := 0 to Length(inBuffer) - 1 do
inBuffer[j] := outBuffer[j];

SetLength(outBuffer, Layers.NumberOfNeurons);
Layers[i].Execute(inBuffer, outBuffer);
end;
{Resolve Output}
for i := 0 to NumberOfOutputs - 1 do
if (DigitalOutput) then
begin
if (outBuffer[i] > 0) then
Output[i] := 1
else
Output[i] := 0;
end
else
Output[i] := outBuffer[i];
end;
inBuffer := Nil;
outBuffer := Nil;
end;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I had a similar problem with my AI project - evolving decision making animats, it may help to track down the problem by altering your fitness function so that the agents with connection weights closest to say, 0.5, scored highest. This will enable you to see whether they are evolving(provided you can see the weights.) If the weights are not tending toward 0.5 the problem is in the selection/cross-breeding/mutation, if they are, the problem is elsewhere (vision system/movement control maybe). Thats what i did anyway, i found the error and it now works brilliantly(ish).

Hope that all makes sense.

Share this post


Link to post
Share on other sites
Ok, that can be done. It does make sense, but does that mean that the weights will only lean towards +0.5 or will they tend toward -0.5 too? My weights all very from -1 to +1. Mind you it has ben suggested that I allow them to go beyond that.

I''ll check that out and post my findings here after. Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just had a look at your code, i dont know anything about pascal or delphi but it looks like your mutation isnt taking into account the original value. When you mutate weights you should be changing them slightly rather than replacing them with a random value. something like +-0.1 for every weight is what i have used in the past.

{- Mutation Rate -}
for j := 0 to NewPopulation.NumberOfValues - 1 do
if (Random <= Mutation_Rate) then
NewPopulation[i].Values[j] := (Random * 2) - 1;
end;

try this instead

NewPopulation[i].Values[j] := NewPopulation[i].Values[j]+(Random/5) - 0.1;

Share this post


Link to post
Share on other sites
I don''t know delphi so i don''t really understand your code thourougly but it looks OK to me. I guess the real problem must lie with your GA code, most probabey with your selection method. Are you using roulette wheel selection? Bugs with higher fitness should have a larger chance to produce offspring. This is the basis for evolution and improvements.


Sander Maréchal
[Lone Wolves GD][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]

Share this post


Link to post
Share on other sites
Well, some part of my code is lying.

Yes, I am using Roulette Wheel Selection. I, as far as I know, am giving every bug a chance at getting into the next generation, but the chance goes up by one part based on the fitness score.

Here is the entire GA procedure I use in my code:

procedure Apply_Basic_Algorithm(var Population: Array of TChromosome; NumberOfPopulation, ChromoSize: Integer);
var i, j, k: Integer;
NewPopulation: Array of TChromosome;
TotalFitness: Integer;
RandomSelectionA, RandomSelectionB: Real;
SelectionA, SelectionB: Array of Real;
AFitness, BFitness: Integer;
SplitPos: Integer;
begin
{- Initalizing NewPopulation and getting Total Fitness -}
SetLength(NewPopulation, Length(Population));
TotalFitness := 0;
for i := 0 to NumberOfPopulation - 1 do
begin
NewPopulation := TChromosome.Init(ChromoSize);

inc(Population[i].FitnessScore);{Every creature has 1 peice of the pie!}

inc(TotalFitness, Population[i].FitnessScore);
end;

SetLength(SelectionA, ChromoSize);//Another Address problem!!!
SetLength(SelectionB, ChromoSize);
for i := 0 to NumberOfPopulation - 1 do
begin
{- Roulette Wheel Selection -}
{Select A}
RandomSelectionA := Random * TotalFitness;
j := 0;
repeat
begin
if (j > NumberOfPopulation - 1) then
j := 0;
RandomSelectionA := RandomSelectionA - Population[j].FitnessScore;
inc(j);
end;
until (RandomSelectionA < Population[j - 1].FitnessScore);

for k := 0 to Population[j - 1].NumberOfValues - 1 do
SelectionA[k] := Population[j - 1].Values[k];
AFitness := Population[j - 1].FitnessScore;

{Select B}
RandomSelectionB := Random * TotalFitness;
j := 0;
repeat
begin
if (j > NumberOfPopulation - 1) then
j := 0;
RandomSelectionB := RandomSelectionB - Population[j].FitnessScore;
inc(j);
end;
until (RandomSelectionB < Population[j - 1].FitnessScore);

for k := 0 to Population[j - 1].NumberOfValues - 1 do
SelectionB[k] := Population[j - 1].Values[k];
BFitness := Population[j - 1].FitnessScore;

{- Cross-Over Rate -}
if (Random <= CrossOver_Rate) then
begin
SplitPos := Round(Random * Population[i].NumberOfValues);
{First Half from A}
for j := 0 to SplitPos - 1 do
NewPopulation[i].Values[j] := SelectionA[j];
{Second Half from B}
for j := SplitPos to Population[i].NumberOfValues - 1 do
NewPopulation[i].Values[j] := SelectionB[j];
end
else
begin
if (AFitness > BFitness) then
for j := 0 to NewPopulation[i].NumberOfValues - 1 do
NewPopulation[i].Values[j] := SelectionA[j]
else
for j := 0 to NewPopulation[i].NumberOfValues - 1 do
NewPopulation[i].Values[j] := SelectionB[j];
end;


{- Mutation Rate -}
for j := 0 to NewPopulation[i].NumberOfValues - 1 do
if (Random <= Mutation_Rate) then
NewPopulation[i].Values[j] := (Random * 2) - 1;
// NewPopulation[i].Values[j] := NewPopulation[i].Values[j] + ((Random * 2) - 1); // Unlimit Bounds...
end;


{- Update Chromosomes -}
for i := 0 to NumberOfPopulation - 1 do
for j := 0 to Population[i].NumberOfValues - 1 do
begin
Population[i].Values[j] := NewPopulation[i].Values[j];
Population[i].FitnessScore := 0;
end;

{- Destroy Temporary Objects -}
for i := 0 to NumberOfPopulation - 1 do
NewPopulation[i].DeInit;
NewPopulation := nil;
SelectionA := nil;
SelectionB := nil;
end;

Share this post


Link to post
Share on other sites
My best guess is that the blame lies with your randomizer function. From the code you posted I undersstand that it doesn''t return a value between 0 and 1 but some other value (that''s why you keep reselecting a value until it is within the population size).

If Delphi is anything like QBasic the random function probabely returns an integer value between 0 and some value (I assume 65536 here, but you should look this up).

This integer randomizer and the ongoing reselection causes only a few individuals from your population to be selected. You should write your own randomizer that returns a value between 0 and 1. Here''s how (in C, hope you can follow):

float function MyRandomizer()
{
return (IntegerRandom / 65536)
}

This gives you a random value between 0 and 1

Sander Maréchal
[Lone Wolves GD][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]

Share this post


Link to post
Share on other sites
If its because of a random number not being generated correctly my guess would be since you are not calling randomize or setting the randseed its always returning the same values each time.

Just add:

Randomize;

BR

Share this post


Link to post
Share on other sites

  • 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!