Well yann, part of the reason I tidied up that last piece of code was because I was hoping to communicate something to you: you will only be hampered in your efforts if you cannot use the C language well. I think you are overly excited about solving your problem, and so you fail to give enough attention to learning how to code. Unfortunately, you will have to learn to code before you learn to implement complex algorithms.
This seems to be a pretty "classic" illustration of of the consequence, because it looks to me like you are trying to write a multi-layer perceptron network that can learn XOR when in fact you have not bothered to learn how to do a XOR yourself!
Code:
printf("inputs:\n");
scanf("%d", &input[1]);
scanf("%d", &input[2]);
int goal;
printf("goal:\n");
scanf("%d", &goal);
Now, I presume you are piping in a file of values here and not sitting there entering numbers 4500 times! But why do you even need to do that? Here is a version of the target() function that performs a XOR: *
Code:
bool target(int y, int z) {
if ((y && !z) || (z && !y)) return 1;
return 0;
}
Does that make sense? If you had understood the use of logical operators, a basic element of the language, this would have been easy for you to do. Instead, presumably, you have wasted a lot of time on some kind of work around. Please correct me if I am wrong about my assumption.
So here's another illustration of how to exploit the "power" of basic C syntax. Which it is basic and will not be hard for you to learn if you take the time to try:
Code:
int x, *ptr;
for (i=0; i<15; i++;) {
if ((i == 1) || (i == 8) || (i == 11)) x = 0;
else if ((i == 1) || (i == 8) || (i == 11)) x = 1;
else if ((i == 2) || (i == 5) || (i == 13)) x = 2;
else if ((i == 3) || (i == 14)) x = 3;
else if (i == 6) x = 4;
else if (i == 9) x = 5;
else if (i == 12) x = 6;
if ((i == 9) || (i==12) || (i<7)) ptr = input;
else ptr = percept;
weight[i] += learning_rate*sign*ptr[x];
printf("%f", weight[i]);
}
[corrected]
This replaces the series of assignments beginning on line 38. Now, there is not a big difference, but notice - this is less than half the number of lines
- it is comperable efficiency wise
- it is more logically organized
The more you expand this list, the more organizing it this way will help.
Enough "not a big difference" type things will add up to a very big difference as the code you are working on gets longer and more complex. Readability and logic are important. Right now, you are relying on your own memory: you understand the logic of the code because you wrote it. But once you get up to a few hundred or thousand lines of code, you will not be able to do that as easily. So by writing in a concise, well organized way, you will save your self time and headaches.
* ps I did try that with the single perceptron, it truly cannot learn XOR.