I'm not sure how to improve the performance, since reading this code is pretty difficult. It's not the worst I've seen, but it could still be improved.
For starters, your main
function is way too long. For readability, it's generally recognized that a function should be no longer than what can take up one screen, or one printed page. This helps with readability and maintainability. I like to try to keep mine below about 70 lines.
You're also using a goto
, and while this is hotly debatedthis is hotly debated, I'd recommend against using it.
Here's how you might structure this:
class Pieces
{
[member variables]
};
int aleADN()
{
[...]
}
string* readFile(string filename){
[..]
}
int evaluate(Pieces * pieces)
{
int result = 1;
[...]
return result;
}
double selection(Pieces* pieces)
{
[...]
return fitness;
}
int reproduction(int geneA, int geneB, int j)
{
[...]
}
void mate(pieces)
{
[...]
}
void evolution(Pieces* pieces)
{
do
{
[...]
int done = evaluate(pieces);
if (done == 0)
return;
fitness = selection(pieces);
// mating leads to reproduction
mate(pieces, fitness);
mutate();
} while (1);
}
int main()
{
string* file_lines = readFile(filename);
checkIntegrity();
initialization();
Pieces * pieces = new Pieces[POPULATION];
evolution(pieces);
// solution printout here
}
Once you get this structured properly, it will be very easy to set up things like timers or any memory monitoring utility like valgrind to see how much memory each function is using so you can track down what part of your code is taking too long or using too much memory.