I'm geting double free or corruption (out) error on line "output = crosingOperator(parentA, parentB, begin, len);". This happens only for big sizes of parentA and parentB (They're always in same size) and not every time.
crosing():
std::vector<unsigned> geneticAlgorithm::crosing(std::vector<unsigned> parentA, std::vector<unsigned> parentB, int mode)
{
std::srand(std::time(nullptr));
std::vector<unsigned> output;
unsigned begin = 0;
unsigned len = 0;
int rand1 = std::rand();
int rand2 = std::rand();
int parasize = parentA.size();
if (mode == CROSSING_MODE_HALF)
{
begin = (rand1 % ((parentA.size() - 1) / 2));
len = (parentA.size() - 1) / 2;
}
if (mode == CROSSING_MODE_RANDOM)
{
begin = (rand1 % (parentA.size() - 1));
len = (rand2 % ((parentA.size() - 1) - begin));
}
output.reserve(parentA.size()+10);
output = crosingOperator(parentA, parentB, begin, len);
return output;
}
crosingOperator():
std::vector<unsigned> GeneticAlgorithm::crosingOperator(std::vector<unsigned> parentA, std::vector<unsigned> parentB, int begin, int len)
{
std::vector<unsigned> tmpA;
std::vector<unsigned> output;
tmpA.reserve(parentA.size());
tmpA.assign(parentA.begin() + begin, parentA.begin() + begin + len);
for (int i = 0; i < parentB.size(); i++)
{
for (unsigned value : tmpA)
{
if (parentB[i] == value)
{
parentB.erase(parentB.begin() + i);
i--;
}
}
}
output.reserve(parentA.size()*2);
std::vector<unsigned> part1;
//std::vector<unsigned> part2;
std::vector<unsigned> part3;
if(parentB.begin()!=parentB.end())
{
part1.assign(parentB.begin(),parentB.begin()+begin);
part3.assign(parentB.begin()+begin,parentB.end());
}
output.insert(output.end(),part1.begin(),part1.end());
output.insert(output.end(),tmpA.begin(),tmpA.end());
output.insert(output.end(),part3.begin(),part3.end());
return output;
}
Aucun commentaire:
Enregistrer un commentaire