Skip to content
Snippets Groups Projects
Commit 5ed0cc30 authored by Caron Olivier's avatar Caron Olivier
Browse files

update Archive inheritance

parent fa10f72a
Branches
No related tags found
No related merge requests found
Pipeline #4327 failed
...@@ -194,13 +194,13 @@ namespace core { ...@@ -194,13 +194,13 @@ namespace core {
* get the archive content * get the archive content
* @return * @return
*/ */
const std::vector<SOLUTION>& get_solutions() const { virtual const std::vector<SOLUTION>& get_solutions() const {
return this->archive; return this->archive;
} }
/** /**
* provide the best solution * provide the best solution
*/ */
const SOLUTION & getBestSolution() const { virtual const SOLUTION & getBestSolution() const {
unsigned long long int indBest = 0 ; unsigned long long int indBest = 0 ;
for (unsigned long long int ind = 1; ind < this->archive.size(); ind++) for (unsigned long long int ind = 1; ind < this->archive.size(); ind++)
if (this->archive[indBest].fitness() < this->archive[ind].fitness()) indBest=ind ; if (this->archive[indBest].fitness() < this->archive[ind].fitness()) indBest=ind ;
......
...@@ -205,6 +205,14 @@ namespace core::archive { ...@@ -205,6 +205,14 @@ namespace core::archive {
void sort() override { void sort() override {
this->inner_archive->sort(); this->inner_archive->sort();
} }
/**
* get the archive content
* @return
*/
virtual const std::vector<SOLUTION>& get_solutions() const override {
std::cout << "size:"<< this->inner_archive->get_solutions().size()<<std::endl;
return this->inner_archive->get_solutions();
}
protected: protected:
......
...@@ -30,6 +30,7 @@ Authors: Olivier Caron and additional contributors (see Authors) ...@@ -30,6 +30,7 @@ Authors: Olivier Caron and additional contributors (see Authors)
namespace opt::manySolutions::geneticAlgorithm::crossover { namespace opt::manySolutions::geneticAlgorithm::crossover {
/** /**
* Class representing the Order Crossover (OX) operator * Class representing the Order Crossover (OX) operator
* see ref below for explanations:https://www.researchgate.net/figure/Illustration-of-the-OX-crossover_fig3_331580514
* @tparam POPULATION * @tparam POPULATION
*/ */
template<typename POPULATION> template<typename POPULATION>
...@@ -56,21 +57,21 @@ namespace opt::manySolutions::geneticAlgorithm::crossover { ...@@ -56,21 +57,21 @@ namespace opt::manySolutions::geneticAlgorithm::crossover {
unsigned long long int size = _ind1.size(); unsigned long long int size = _ind1.size();
unsigned int len = util::RNGHelper::get()->random(size); unsigned int len = util::RNGHelper::get()->random(size);
unsigned int position = util::RNGHelper::get()->random(size - len); unsigned int position = util::RNGHelper::get()->random(size - len);
std::cout << "len:" << len << "pos:" << position << std::endl;
INDIVIDUAL child1; INDIVIDUAL child1;
INDIVIDUAL child2; INDIVIDUAL child2;
for (unsigned long long int i = 0, i1 = 0, i2 = 0; i < size; i++) { for (unsigned long long int i = 0, i1 = 0, i2 = 0; i < size; i++) {
if (i >= position && i < position + len) { if (i > position && i <= position + len) {
child1.push_back(_ind1[i]); child1.push_back(_ind1[i]);
child2.push_back(_ind2[i]); child2.push_back(_ind2[i]);
} else { } else {
while (std::find(_ind1.begin() + position, _ind1.begin() + position + len, _ind2[i2]) while (std::find(_ind1.begin() + position+1, (_ind1.begin() + position + len+1), _ind2[i2])
!= _ind1.begin() + position + len) != (_ind1.begin() + position + len+1)) i2++;
i2++; std::cout << "i:" << i << " - i2:" << i2 << std::endl ;
child1.push_back(_ind2[i2]); child1.push_back(_ind2[i2]);
i2++; i2++;
while (std::find(_ind2.begin() + position, _ind2.begin() + position + len, _ind1[i1]) while (std::find(_ind2.begin() + position+1, (_ind2.begin() + position + len+1), _ind1[i1])
!= _ind2.begin() + position + len) != (_ind2.begin() + position + len+1)) i1++;
i1++;
child2.push_back(_ind1[i1]); child2.push_back(_ind1[i1]);
i1++; i1++;
} }
......
...@@ -57,7 +57,7 @@ namespace opt::manySolutions::geneticAlgorithm::crossover { ...@@ -57,7 +57,7 @@ namespace opt::manySolutions::geneticAlgorithm::crossover {
INDIVIDUAL child1; child1.clear() ; INDIVIDUAL child1; child1.clear() ;
INDIVIDUAL child2; child2.clear() ; INDIVIDUAL child2; child2.clear() ;
for(unsigned long long int i=0; i<size; i++){ for(unsigned long long int i=0; i<size; i++){
if(i<=point){ if(i<point){
child1.push_back(_ind1[i]); child1.push_back(_ind1[i]);
child2.push_back(_ind2[i]); child2.push_back(_ind2[i]);
}else{ }else{
......
...@@ -45,10 +45,11 @@ namespace test { ...@@ -45,10 +45,11 @@ namespace test {
vector<unsigned long long int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1 , 0}; vector<unsigned long long int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1 , 0};
sol2.replace(v) ; sol2.replace(v) ;
auto pair = this->crossover->operator()(sol1, sol2) ; auto pair = this->crossover->operator()(sol1, sol2) ;
// random generated position : 2 len :2 // random generated position : 1 len :6
vector<unsigned long long int> expectedChild1 = {9, 8, 2 , 3, 7, 6, 5 , 4, 1, 0}; vector<unsigned long long int> expectedChild1 = {9, 8, 2, 3, 4, 5, 6, 7, 1, 0};
vector<unsigned long long int> expectedChild2 = {0, 1,7,6,2,3,4,5, 8, 9} ; vector<unsigned long long int> expectedChild2 = {0, 1, 7, 6, 5, 4, 3, 2, 8, 9} ;
for (unsigned long long int i = 0 ; i < 10; i++) { for (unsigned long long int i = 0 ; i < 10; i++) {
std::cout << i << " first:" << pair.first[i] << " second:" << pair.second[i] << std::endl;
ASSERT_EQ(pair.first[i], expectedChild1[i]) ; ASSERT_EQ(pair.first[i], expectedChild1[i]) ;
ASSERT_EQ(pair.second[i], expectedChild2[i]) ; ASSERT_EQ(pair.second[i], expectedChild2[i]) ;
} }
......
...@@ -53,10 +53,11 @@ namespace test { ...@@ -53,10 +53,11 @@ namespace test {
vector<unsigned long long int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1 , 0}; vector<unsigned long long int> v = {9, 8, 7, 6, 5, 4, 3, 2, 1 , 0};
sol2.replace(v) ; sol2.replace(v) ;
auto pair = this->crossover->operator()(sol1, sol2) ; auto pair = this->crossover->operator()(sol1, sol2) ;
// cross point generated is 2 // cross point generated should be 6
vector<unsigned long long int> expectedChild1 = {0, 1, 2, 6, 5, 4, 3, 2, 1 , 0}; vector<unsigned long long int> expectedChild1 = {0, 1, 2, 3, 4, 5, 3, 2, 1 , 0};
vector<unsigned long long int> expectedChild2 = {9, 8, 7, 3, 4 , 5 , 6, 7, 8, 9} ; vector<unsigned long long int> expectedChild2 = {9, 8, 7, 6, 5 , 4 , 6, 7, 8, 9} ;
for (unsigned long long int i = 0 ; i < 10; i++) { for (unsigned long long int i = 0 ; i < 10; i++) {
std::cout << i << " first:" << pair.first[i] << " second:" << pair.second[i] << std::endl;
ASSERT_EQ(pair.first[i], expectedChild1[i]) ; ASSERT_EQ(pair.first[i], expectedChild1[i]) ;
ASSERT_EQ(pair.second[i], expectedChild2[i]) ; ASSERT_EQ(pair.second[i], expectedChild2[i]) ;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment