-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrossover.cpp
More file actions
55 lines (42 loc) · 1.84 KB
/
Copy pathCrossover.cpp
File metadata and controls
55 lines (42 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include "Crossover.h"
#include "Random.h"
using namespace std;
TwoPointCrossover::TwoPointCrossover() : TwoPointCrossover(0.1, 0.1, 0.8){}
TwoPointCrossover::TwoPointCrossover(const double & leafPickProb, const double & subtreeLeafPickProb, const double & parentLeafPickProbb)
{
this->leafPickProb = leafPickProb;
this->subtreeLeafPickProb = subtreeLeafPickProb;
this->parentLeafPickProb = parentLeafPickProbb;
}
Individual TwoPointCrossover::createOffspring(const Individual& parent1, const Individual& parent2, const int& maxDepth) const
{
while(true){
//cout << "Create offspring start" << endl;
//cout << "A" << endl;
// 1. Vyber body pro køížení
int idx1 = (Random::randProb() <= this->parentLeafPickProb)
? parent1.pickRandomLeafIdx()
: parent1.pickRandomInnerNodeIdx();
int idx2 = (Random::randProb() <= this->subtreeLeafPickProb)
? parent2.pickRandomLeafIdx()
: parent2.pickRandomInnerNodeIdx();
// 2. Spoèítej hloubky obou uzlù
int depth1 = Individual::calculateDepthFromIdx(idx1);
int depth2 = Individual::calculateDepthFromIdx(idx2);
//cout << "parant1.depth: " << parent1.getMaxDepth() << " parant2.depth: " << parent2.getMaxDepth() << endl;
//cout << "depth1: " << depth1 << ", depth2: " << depth2 << endl;
// 3. Spoèítej výšku podstromu z parent2 (od idx2 dolù)
int subtreeDepth = parent2.getMaxDepth() - depth2 + 1;
// 4. Spoèítej oèekávanou hloubku offspringa
int predictedDepth = parent1.predictOffspringDepthAfterSubtreeReplace(idx1, depth1, subtreeDepth);
// 5. Pokud bude offspring validní, vytvoø ho
if (predictedDepth <= maxDepth) {
Individual subtree = parent2.extractSubtree(idx2);
Individual offspring = Individual(parent1);
offspring.replaceNodeWithSubTree(std::move(subtree), idx1, depth1);
offspring.validateTreeStructure();
return offspring;
}
}
}