Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Genetic Algorithm

Author
Message
Digger412
19
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 9th Apr 2010 05:00 Edited at: 9th Apr 2010 05:38
Here's a GA that I've written. It's goal is to find an equation that equals a random number (they are read left to right, instead of order of operations). However, it wants to 'bottom out' for some reason, or it seems to overshoot. I'm not sure what's wrong with it, but here's what I have:



Edit: I think that it's because of my fitness function, because it can return values over 1 somehow...
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 9th Apr 2010 15:31
Quote: "because it can return values over 1 somehow"


Any reason why you think it shouldn't?

Are you sure this line is correct?

Digger412
19
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 9th Apr 2010 23:50 Edited at: 10th Apr 2010 00:02
Well, the way I formed my fitness function I intended for a fitness of 1 to be a solution. If the goal was 45, and the gene calculated to 45, the fitness would be 1.0/((45+1)-45)=1.0/(46-45)=1/1=1. Maybe it's returning a value of over 1 if it's getting so close as to make the denominator less than 1, like: 1.0/((45+1)-45.9)=1.0/(46-45.9)=1.0/.1=10. I'll try taking the +1 out, so that way it should get higher as it gets closer to 45, instead of 46.

EDIT: Okay, I think that was it. However, the perfect fitness now would be undefined, since it'd divide by 0. Another problem I've found is that somehow the genes can get corrupted and become something like "1++3/2*6"..

EDIT2: Okay, I added a +1 in the function at the end, so now it works. However, it still seems to bottom out for some odd reason.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Apr 2010 00:02
Yes, I thought that was what you were trying to do but the details are not quite right. If your target is 45 then 45.6 and 44.4 are both wrong and both should give a fitness less than 1 presumably.

What about something like:



?

There are many different ways of implementing a genetic algorithm.
Digger412
19
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 10th Apr 2010 00:54 Edited at: 10th Apr 2010 03:57
Ahh, okay. I knew that it shouldn't be negative, but I didn't really know how it should be set up. Thanks a lot for the help!

I've ruled out my mutation function as the cause of the corrupted genes, and I've ruled out the generation function because they turn out right. I think it's the crossover function, but I'm not sure why it doesn't work. Each gene still has the same number of chromosomes/bits, so I'm not sure..

EDIT: Nevermind, the new fitness function seemed to fix that too! Thanks!!

EDIT2: Gah...the FF results in an exponential curve, so if it's only 1 off it get a FF rating of 1/2. Would scaling it somehow have a better result?
Digger412
19
Years of Service
User Offline
Joined: 12th Jun 2007
Location:
Posted: 11th Apr 2010 03:05 Edited at: 11th Apr 2010 05:42
Update:

The genes don't get corrupted anymore, I'm not even sure what was really causing the problem. I fixed my crossover function too, so now it properly has a crossover chance on EVERY gene, and not just two, and it now also uses elitism. I also fixed random little bugs here and there. Now that I have it working moderately well, I need to work on optimizing it to work faster. My first thought for that is changing the 'calculate' function to work on a gene-by-gene basis instead of re-calculating every gene every time, since only two genes change at a time in the crossover function.



Update 2:
I've changed the 'calculate' function to either calculate on a single-gene, or to do every one. I may change it so it uses a loop either way, like 'calculate(first,second)', so it can be either 1 to POP_SIZE, or something like 54 to 54 for a single gene. Anyways, here's the newest version (now MUCH faster at twice the POP_SIZE):

Login to post a reply

Server time is: 2026-07-26 15:31:36
Your offset time is: 2026-07-26 15:31:36