-
1 Attachment(s)
:(
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
-
-
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 :)
-
Next thing -- whatever I set num to, it prints "matched in 0 generations" and does nothing :confused:
-
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? :)
-
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!
-
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.
-
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 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.
-
I did read that, I just ignored it by turning on ANSI-compliance in MSVC++ ;)
-
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!
-
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.
-
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?
-
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.
-
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!):rolleyes:
heh heh.
-
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 :)