Syntax Error

Genetic Algorithm

Page 1 of 1

10 Replies - 793 Views - Last Post: 19 February 2010 - 05:12 PM Rate Topic: -----

#1 Guest_Awfa3*


Reputation:

Syntax Error

Posted 18 February 2010 - 08:53 PM

So I'm experimenting with genetic algorithms while following a guide of theory on it and I'm having problems with my decodeChromosome() function in my code. What it does is that it takes in a string like this, "011010100101110001001101001010100001" split it up into four groups like this, "0110 1010 0101 1100 0100 1101 0010 1010 0001", and then converts that using a table I have like this, "6 + 5 * 4 / 2 + 1". And the return the value of 23. (Order of operations doesn't matter here, it just goes straight) However, the programs seems to screw up somewhere in the decodeChromosome function and always return 0 even if I explicitly write return 14; Even with printf, when debugging things it just spews out random numbers that aren't even possible ( I initialized all variables so there is no junk ) such as -1074790400. ( For operBuf )

Please help find my problem.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define cleanUp while(getchar()!='\n');

#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define POPULATION 100 //Must be an even number and less than RAND_MAX

void initChromosomes();
double decodeChromosome(char *chromosome);
void resetOperArray(char *operArry);

char geneTypes[][5] = {
	"0000",	// Zero
	"0001",	// One
	"0010",	// Two
	"0011",	// Three
	"0100",	// Four
	"0101",	// Five
	"0110",	// Six
	"0111",	// Seven
	"1000",	// Eight
	"1001", // Nine
	"1010", // Plus
	"1011",	// Minus
	"1100",	// Multiply
	"1101"	// Divide
};

char chromosomes[POPULATION-1][37];

int main() {
	int targetNumber;
	
	printf("Please enter the target number: ");
	scanf("%d", &targetNumber);
	cleanUp
	initChromosomes();
	
	printf("%d", decodeChromosome("011010100101110001001101001010100001"));
	getchar();
}

void initChromosomes() {
	int i;
	int numberOfChromosomes;
	srand(time(0));
	char buffer[36];
	for (numberOfChromosomes = 0; numberOfChromosomes < POPULATION; ++numberOfChromosomes)
		for (i = 0; i <=35; ++i)
			chromosomes[numberOfChromosomes][i] = ((int)(rand() / (((double)RAND_MAX + 1)/ 2))) + '0';
}

double decodeChromosome(char *chromosome) {
	double answer = 0;
	int i;
	int x;
	int y;
	char buf[9][5];
	char operation[5] = "0000";
	double operand = -1;
	int operBuf = -1;
	
	for (x = 0; x <= 9; ++x) {
		for (y = 0; y <= 3; ++y)
			buf[x][y] = chromosome[y+(x*4)];
		buf[x][y] = '\0';
	}
	
	for (i = 0; i <= 9; ++i) {
		for (x = 0; x <= 13; ++x)
			if (!strcmp(buf[i], geneTypes[x])) {
				operBuf = x;
				break;
			}
			else 
				operBuf = -1;
			printf("Iteration: %i\tOperand: %d\tOperBuf: %i\n", i, operand, operBuf);
		if (operBuf <= 9 && operBuf >= 0) {
			if (!strcmp(operation, "0000")) {
				if (!(operand <= 9 && operand >= 0))
					operand = operBuf;
			}
			else
				if (!strcmp(operation, "1010")) {
					answer = operand + operBuf;
					operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1011")) {
					answer = operand - operBuf;
					operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1100")) {
					answer = operand * operBuf;
					operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1101")) {
					answer = operand / operBuf;
					operand += answer;
					resetOperArray(operation);
				}
		}
		else if (operBuf > 9 && operBuf <= 13)
			switch (operBuf) {
				case 10:
					operation[0] = '1';
					operation[1] = '0';
					operation[2] = '1';
					operation[3] = '0';
					break;
				case 11:
					operation[0] = '1';
					operation[1] = '0';
					operation[2] = '1';
					operation[3] = '1';
					break;
				case 12:
					operation[0] = '1';
					operation[1] = '1';
					operation[2] = '0';
					operation[3] = '0';
					break;
				case 13:
					operation[0] = '1';
					operation[1] = '1';
					operation[2] = '0';
					operation[3] = '1';
					break;
			}
	}
	return operand;
}

void resetOperArray(char *operArry) {
	operArry[0] = '0';
	operArry[1] = '0';
	operArry[2] = '0';
	operArry[3] = '0';
}



Is This A Good Question/Topic? 0

Replies To: Syntax Error

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Syntax Error

Posted 18 February 2010 - 09:50 PM

Legal array indicies are zero to n-1 where n is the array's declared size.

You are addressing the array with out of bounds indexes:

...
char buf[9][5];
...
for (x = 0; x <= 9; ++x) {
...
for (i = 0; i <= 9; ++i) {




should be:

...
char buf[9][5];
...
for (x = 0; x < 9; x++) {
...
for (i = 0; i < 9; i++) {



This post has been edited by n8wxs: 18 February 2010 - 09:53 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_javabie*


Reputation:

Re: Syntax Error

Posted 18 February 2010 - 10:08 PM

http://www.cplusplus.../cstdio/printf/
visit that site and learn about printf
specially... see the specifier for int and double.

also

				if (!strcmp(operation, "1010")) {
					operand += operBuf;
					//operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1011")) {
					operand -= operBuf;
					//operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1100")) {
					operand *= operBuf;
					//operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1101")) {
					operand /= operBuf;
					//operand += answer;
					resetOperArray(operation);
				}


.. was changed... you will see wat i've changed and i think u will know why i changed.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define cleanUp while(getchar()!='\n');

#define CROSSOVER_RATE 0.7
#define MUTATION_RATE 0.001
#define POPULATION 100 //Must be an even number and less than RAND_MAX

void initChromosomes();
double decodeChromosome(char *chromosome);
void resetOperArray(char *operArry);

char geneTypes[][5] = {
	"0000", // Zero
	"0001", // One
	"0010", // Two
	"0011", // Three
	"0100", // Four
	"0101", // Five
	"0110", // Six
	"0111", // Seven
	"1000", // Eight
	"1001", // Nine
	"1010", // Plus
	"1011", // Minus
	"1100", // Multiply
	"1101"  // Divide
};

char chromosomes[POPULATION-1][37];

int main() {
	int targetNumber;
	
	printf("Please enter the target number: ");
	scanf("%d", &targetNumber);
	cleanUp
	initChromosomes();
	
	printf("%f", decodeChromosome("011010100101110001001101001010100001"));
	getchar();
}

void initChromosomes() {
	int i;
	int numberOfChromosomes;
	srand(time(0));
	char buffer[36];
	for (numberOfChromosomes = 0; numberOfChromosomes < POPULATION; ++numberOfChromosomes)
		for (i = 0; i <=35; ++i)
			chromosomes[numberOfChromosomes][i] = ((int)(rand() / (((double)RAND_MAX + 1)/ 2))) + '0';
}

double decodeChromosome(char *chromosome) {
	double answer = 0;
	int i;
	int x;
	int y;
	char buf[9][5];
	char operation[5] = "0000";
	double operand = 0.0;
	int operBuf = 0;
	
	for (x = 0; x <= 9; ++x) {
		for (y = 0; y <= 3; ++y)
			buf[x][y] = chromosome[y+(x*4)];
		buf[x][y] = '\0';
	}
	
	for (i = 0; i <= 9; ++i) {
		for (x = 0; x <= 13; ++x)
			if (!strcmp(buf[i], geneTypes[x])) {
				operBuf = x;
				break;
			}
			else 
				operBuf = 0;
		printf("Operand: %f\nOperBuf: %d\n\n\n\n", operand, operBuf);
		
		if (operBuf <= 9 && operBuf > 0) {
			if (!strcmp(operation, "0000")) {
				if (!(operand <= 9 && operand > 0))
					operand = operBuf;
			}
			else
				if (!strcmp(operation, "1010")) {
					operand += operBuf;
					//operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1011")) {
					operand -= operBuf;
					//operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1100")) {
					operand *= operBuf;
					//operand += answer;
					resetOperArray(operation);
				}
				else if (!strcmp(operation, "1101")) {
					operand /= operBuf;
					//operand += answer;
					resetOperArray(operation);
				}
		}
		else if (operBuf > 9 && operBuf <= 13)
			switch (operBuf) {
				case 10:
					operation[0] = '1';
					operation[1] = '0';
					operation[2] = '1';
					operation[3] = '0';
					break;
				case 11:
					operation[0] = '1';
					operation[1] = '0';
					operation[2] = '1';
					operation[3] = '1';
					break;
				case 12:
					operation[0] = '1';
					operation[1] = '1';
					operation[2] = '0';
					operation[3] = '0';
					break;
				case 13:
					operation[0] = '1';
					operation[1] = '1';
					operation[2] = '0';
					operation[3] = '1';
					break;
			}
	}
	return operand;
}

void resetOperArray(char *operArry) {
	operArry[0] = '0';
	operArry[1] = '0';
	operArry[2] = '0';
	operArry[3] = '0';
}

Was This Post Helpful? 0

#4 Guest_javabie*


Reputation:

Re: Syntax Error

Posted 18 February 2010 - 10:15 PM

View Postn8wxs, on 18 February 2010 - 08:50 PM, said:

Legal array indicies are zero to n-1 where n is the array's declared size.

You are addressing the array with out of bounds indexes:

...
char buf[9][5];
...
for (x = 0; x <= 9; ++x) {
...
for (i = 0; i <= 9; ++i) {




should be:

...
char buf[9][5];
...
for (x = 0; x < 9; x++) {
...
for (i = 0; i < 9; i++) {





this doesn't matter in for-loop...

but it does matter in some other cases...
like calling a function

int x = 5;
foo (x++); //5 gets passed in

different from ...

int x = 5;
foo(++x); //6 gets passed in
Was This Post Helpful? 0

#5 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Syntax Error

Posted 18 February 2010 - 10:20 PM

You missed the point of the post.

It wasn't post vs pre increment. It was that the OP was accessing a point of the array that didn't exist.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_javabie*


Reputation:

Re: Syntax Error

Posted 18 February 2010 - 10:56 PM

ooops.. i think i did.
i apologise.
Was This Post Helpful? 0

#7 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Syntax Error

Posted 19 February 2010 - 04:06 PM

Thanks Javabie, it works great now. However I don't understand plasticine guy's point of getting out of the array. If there are 10 elements (0,1,2...9) And my "x" variable goes up to 9, then its still in bounds? Please clarify as I know you may be right.
Was This Post Helpful? 0
  • +
  • -

#8 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Syntax Error

Posted 19 February 2010 - 04:18 PM

Yes.

Unfortunately you actually declared 9 elements:
char buf[9][5];


Was This Post Helpful? 0
  • +
  • -

#9 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Syntax Error

Posted 19 February 2010 - 04:28 PM

0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 Including the zero is ten elements? I'm really confused
Was This Post Helpful? 0
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Syntax Error

Posted 19 February 2010 - 04:31 PM

char array[x];
That declares an array of x elements (0, 1, 2, ..., x-1), not up to and including x (0, 1, 2, ..., x).

This post has been edited by PlasticineGuy: 19 February 2010 - 04:31 PM

Was This Post Helpful? 0
  • +
  • -

#11 awfa3  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-July 09

Re: Syntax Error

Posted 19 February 2010 - 05:12 PM

Ahh ok
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1