Results 1 to 16 of 16

Thread: Why isn't this Genetic Algorithm working?

  1. #1
    wossname
    Guest

    Hi.

    Being a newbie to C++, I thought I had better get used to using strings, so I have written a Genetic Algorithm to come up with a random string of characters which it then mutates and "breeds" from, ultimately to match a target phrase.

    I have a number (#defined as num) of such offspring.

    Now, when there are only 2 offspring in each iteration, the program runs perfectly. But as soon as I use a different number...

    #define num 4

    the program doesn't even seem to bother running through the algorithm.

    I am utterly lost as to why this should be the case since i have used the macro name throughout the program to denote the loop parameters and array offsets.

    If anyone can give me a hand in trying to locate the reason for this problem, then I'd be much obliged.

    The program compiles in the DJGPP compiler (in which it was written).

    The program doesn't crash as such, it just exits early for some weird reason.

    Don't worry about the weird target phrase, its a shakespearian quote. If you have read "The Blind Watchmaker" it will make sense!

    Please feel free to criticise the code to your heart's content, I need all the help I can get!

    The file should be attached here

    |
    |

    V
    Attached Files Attached Files

  2. #2
    wossname
    Guest
    Help!

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Just downloaded the code...looking now...

    Thing I noticed first -- your function names seem a bit off...random and srandom...where do they come from? The ANSI names are rand and srand

    Second, use capital letters for macros/constants. It makes life a lot easier when debugging
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Next thing -- whatever I set num to, it prints "matched in 0 generations" and does nothing
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    wossname
    Guest
    Yeah, the num thing is the problem, have you tried setting it to 2?

    I know there are 2 random functions like you mentioned, but the documentation says that random is better when you are mod-ing it by a small number.

    I think the random function works ok. But for the life of me i cant work out why the num macro isn't playing fair.

    What compiler are you compiling it in?

  6. #6
    wossname
    Guest
    By the way, what you should see when it works is something like this..

    Code:
    SKWNVHUEKHLLPMA  FEHEJDHQQRS
    ...
    MDHEWGBVSG GGR SIKETNTASGREL 
    ...
    METHINSFAST IS LIKEFSFAFASEL 
    ...
    ...
    ...
    METHINKS IT IS LIKE A WEASEL - MATCH!
    You'll definitely know when its working!

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It doesn't even work at all! I haven't changed any numbers, I'm trying to get it to DO something

    I'm using MSVC++ (as always). It doesn't have any knowledge of random.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Okay, got it working...I had to change a line to this:
    Code:
    int i = 0, j = 0, cc = 0, best = 0, max = 0, gen = 0;
    It works as normal with 2, and when I tried it with 3 & 4 it was okay as well.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    wossname
    Guest
    I don't know if you read my original post all the way through, but It was written in DJGPP for dos.

    please feel free to change it for the rand() version instead if it works better on yours it should be interchangeable.

    DJGPP is an ansi standard compiler, but its a bit of a pain in the ass when you're trying to learn C++!

    I have tried MSVC++ but it refuses to install on my PC

    But, maybe an experienced programmer like yourself can spot the fault by looking at my code. On the off chance that it wont compile for you.

    I'm losing the will to live with this compiler, and I'm thinking of sticking to VB!

    C++ is a fast language, which is why I want to learn it.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I did read that, I just ignored it by turning on ANSI-compliance in MSVC++
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11
    wossname
    Guest
    Okay, got it working...I had to change a line to this:

    code:--------------------------------------------------------------------------------int i = 0, j = 0, cc = 0, best = 0, max = 0, gen = 0;--------------------------------------------------------------------------------

    It works as normal with 2, and when I tried it with 3 & 4 it was okay as well.


    Wow, nice one dude.

    Hmm, why do you think we need to init the variables with 0 when its the default beginning value?

    I had to set gen to 0 on declaration to solve a counting problem that made the generation numbers 137849 when it should have been 45!

    Odd.

    Hey thanks for the help Parksie.

    You get another king Geek vote from me!

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    C/C++ doesn't initialise variables. Using int x merely allocates space for that variable, what the contents of that memory location are is undefined (unless it's MSVC++'s debug mode in which case it's 0xCDCD ).

    You have to initialise it yourself.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    This is not a bad thing, it allows you to make more efficient code. No point initialising a variable if you don't know what value it's going to start with.

    Didn't know VC++ initialised it for you in debug mode... why 0xCDCD?
    Harry.

    "From one thing, know ten thousand things."

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No idea. But it makes life easier because it can whinge at you if you use an uninitialised variable because if it prints 0xCDCD you know you've done something wrong.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  15. #15
    wossname
    Guest
    Wow, I didn't know that about C++ not initialising variables. I must have taken it for granted being such a VB enthusiast for all this time!

    (good grief, these new-fangled Vis-yoo-all Baysic programmer people don't know they're born, being pampered...I remember when all this was fields and you could get to Aberdeen and back, buy a new suit and take a young lady to the pictures, and still have change from a tenner!)

    heh heh.

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It took a few hard crashes for me to realise that Fortunately this was using Beebug C on a BBC so it wasn't too bad
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width