test: add eval and real_value.h in t-eoRankingCached.cpp
This commit is contained in:
parent
9f924ed212
commit
64fd702843
1 changed files with 29 additions and 35 deletions
|
|
@ -2,15 +2,18 @@
|
||||||
#include <es/eoReal.h>
|
#include <es/eoReal.h>
|
||||||
#include <utils/eoRNG.h>
|
#include <utils/eoRNG.h>
|
||||||
#include <eoRanking.h>
|
#include <eoRanking.h>
|
||||||
|
#include <apply.h>
|
||||||
|
#include "real_value.h"
|
||||||
|
|
||||||
class RankingTest
|
class RankingTest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RankingTest(eoParser &parser, unsigned size = 100) : rng(0),
|
RankingTest(eoParser &parser, eoEvalFuncCounter<eoReal<double>> &_eval, unsigned size = 100) : rng(0),
|
||||||
popSize(size),
|
popSize(size),
|
||||||
seedParam(parser.createParam(uint32_t(time(0)), "seed", "Random seed", 'S')),
|
seedParam(parser.createParam(uint32_t(time(0)), "seed", "Random seed", 'S')),
|
||||||
pressureParam(parser.createParam(1.5, "pressure", "Selective pressure", 'p')),
|
pressureParam(parser.createParam(1.5, "pressure", "Selective pressure", 'p')),
|
||||||
exponentParam(parser.createParam(1.0, "exponent", "Ranking exponent", 'e'))
|
exponentParam(parser.createParam(1.0, "exponent", "Ranking exponent", 'e')),
|
||||||
|
eval(_eval)
|
||||||
{
|
{
|
||||||
rng.reseed(seedParam.value());
|
rng.reseed(seedParam.value());
|
||||||
initPopulation();
|
initPopulation();
|
||||||
|
|
@ -19,7 +22,6 @@ public:
|
||||||
void initPopulation()
|
void initPopulation()
|
||||||
{
|
{
|
||||||
pop.clear();
|
pop.clear();
|
||||||
pop.resize(popSize);
|
|
||||||
for (unsigned i = 0; i < popSize; ++i)
|
for (unsigned i = 0; i < popSize; ++i)
|
||||||
{
|
{
|
||||||
eoReal<double> ind;
|
eoReal<double> ind;
|
||||||
|
|
@ -27,6 +29,7 @@ public:
|
||||||
ind[0] = rng.uniform();
|
ind[0] = rng.uniform();
|
||||||
pop.push_back(ind);
|
pop.push_back(ind);
|
||||||
}
|
}
|
||||||
|
apply<eoReal<double>>(eval, pop);
|
||||||
}
|
}
|
||||||
|
|
||||||
const unsigned popSize;
|
const unsigned popSize;
|
||||||
|
|
@ -39,12 +42,15 @@ private:
|
||||||
eoValueParam<uint32_t> &seedParam;
|
eoValueParam<uint32_t> &seedParam;
|
||||||
eoValueParam<double> &pressureParam;
|
eoValueParam<double> &pressureParam;
|
||||||
eoValueParam<double> &exponentParam;
|
eoValueParam<double> &exponentParam;
|
||||||
|
eoEvalFuncCounter<eoReal<double>> eval;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Test case 1: Verify both implementations produce identical results
|
// Test case 1: Verify both implementations produce identical results
|
||||||
void test_Consistency(eoParser &parser)
|
void test_Consistency(eoParser &parser)
|
||||||
{
|
{
|
||||||
RankingTest fixture(parser);
|
eoEvalFuncPtr<eoReal<double>, double, const std::vector<double> &> mainEval(real_value);
|
||||||
|
eoEvalFuncCounter<eoReal<double>> eval(mainEval);
|
||||||
|
RankingTest fixture(parser, eval);
|
||||||
|
|
||||||
eoRanking<eoReal<double>> ranking(fixture.pressure(), fixture.exponent());
|
eoRanking<eoReal<double>> ranking(fixture.pressure(), fixture.exponent());
|
||||||
eoRankingCached<eoReal<double>> rankingCached(fixture.pressure(), fixture.exponent());
|
eoRankingCached<eoReal<double>> rankingCached(fixture.pressure(), fixture.exponent());
|
||||||
|
|
@ -65,28 +71,7 @@ void test_Consistency(eoParser &parser)
|
||||||
std::cout << "Test 1 passed: Both implementations produce identical results\n";
|
std::cout << "Test 1 passed: Both implementations produce identical results\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test case 2: Verify ranking order is preserved
|
// Test case 2: Test edge case with minimum population size
|
||||||
void test_RankingOrder(eoParser &parser)
|
|
||||||
{
|
|
||||||
RankingTest fixture(parser);
|
|
||||||
|
|
||||||
eoRankingCached<eoReal<double>> ranking(fixture.pressure(), fixture.exponent());
|
|
||||||
ranking(fixture.pop);
|
|
||||||
|
|
||||||
fixture.pop.sort();
|
|
||||||
const std::vector<double> &values = ranking.value();
|
|
||||||
|
|
||||||
for (unsigned i = 1; i < fixture.pop.size(); ++i)
|
|
||||||
{
|
|
||||||
if (values[i] > values[i - 1])
|
|
||||||
{
|
|
||||||
throw std::runtime_error("Ranking order not preserved");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
std::cout << "Test 2 passed: Ranking order is preserved\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test case 3: Test edge case with minimum population size
|
|
||||||
void test_MinPopulationSize(eoParser &parser)
|
void test_MinPopulationSize(eoParser &parser)
|
||||||
{
|
{
|
||||||
eoPop<eoReal<double>> smallPop;
|
eoPop<eoReal<double>> smallPop;
|
||||||
|
|
@ -97,11 +82,15 @@ void test_MinPopulationSize(eoParser &parser)
|
||||||
ind2[0] = 1.0;
|
ind2[0] = 1.0;
|
||||||
smallPop.push_back(ind1);
|
smallPop.push_back(ind1);
|
||||||
smallPop.push_back(ind2);
|
smallPop.push_back(ind2);
|
||||||
|
eoEvalFuncPtr<eoReal<double>, double, const std::vector<double> &> mainEval(real_value);
|
||||||
|
eoEvalFuncCounter<eoReal<double>> eval(mainEval);
|
||||||
|
|
||||||
RankingTest fixture(parser, 2); // Use fixture to get parameters
|
RankingTest fixture(parser, eval, 2); // Use fixture to get parameters
|
||||||
eoRanking<eoReal<double>> ranking(fixture.pressure(), fixture.exponent());
|
eoRanking<eoReal<double>> ranking(fixture.pressure(), fixture.exponent());
|
||||||
eoRankingCached<eoReal<double>> rankingCached(fixture.pressure(), fixture.exponent());
|
eoRankingCached<eoReal<double>> rankingCached(fixture.pressure(), fixture.exponent());
|
||||||
|
|
||||||
|
apply<eoReal<double>>(eval, smallPop);
|
||||||
|
|
||||||
ranking(smallPop);
|
ranking(smallPop);
|
||||||
rankingCached(smallPop);
|
rankingCached(smallPop);
|
||||||
|
|
||||||
|
|
@ -110,13 +99,15 @@ void test_MinPopulationSize(eoParser &parser)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Invalid ranking for population size 2");
|
throw std::runtime_error("Invalid ranking for population size 2");
|
||||||
}
|
}
|
||||||
std::cout << "Test 3 passed: Minimum population size handled correctly\n";
|
std::cout << "Test 2 passed: Minimum population size handled correctly\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test case 4: Verify caching actually works
|
// Test case 3: Verify caching actually works
|
||||||
void test_CachingEffectiveness(eoParser &parser)
|
void test_CachingEffectiveness(eoParser &parser)
|
||||||
{
|
{
|
||||||
RankingTest fixture(parser, 50); // Fixed size for cache test
|
eoEvalFuncPtr<eoReal<double>, double, const std::vector<double> &> mainEval(real_value);
|
||||||
|
eoEvalFuncCounter<eoReal<double>> eval(mainEval);
|
||||||
|
RankingTest fixture(parser, eval, 50); // Fixed size for cache test
|
||||||
|
|
||||||
eoRankingCached<eoReal<double>> rankingCached(fixture.pressure(), fixture.exponent());
|
eoRankingCached<eoReal<double>> rankingCached(fixture.pressure(), fixture.exponent());
|
||||||
|
|
||||||
|
|
@ -128,6 +119,8 @@ void test_CachingEffectiveness(eoParser &parser)
|
||||||
for (auto &ind : fixture.pop)
|
for (auto &ind : fixture.pop)
|
||||||
ind[0] = fixture.rng.uniform();
|
ind[0] = fixture.rng.uniform();
|
||||||
|
|
||||||
|
apply<eoReal<double>>(eval, fixture.pop);
|
||||||
|
|
||||||
// Second run - should use cached coefficients
|
// Second run - should use cached coefficients
|
||||||
rankingCached(fixture.pop);
|
rankingCached(fixture.pop);
|
||||||
|
|
||||||
|
|
@ -137,10 +130,12 @@ void test_CachingEffectiveness(eoParser &parser)
|
||||||
newInd[0] = fixture.rng.uniform();
|
newInd[0] = fixture.rng.uniform();
|
||||||
fixture.pop.push_back(newInd);
|
fixture.pop.push_back(newInd);
|
||||||
|
|
||||||
|
apply<eoReal<double>>(eval, fixture.pop);
|
||||||
|
|
||||||
// Third run - should recompute coefficients
|
// Third run - should recompute coefficients
|
||||||
rankingCached(fixture.pop);
|
rankingCached(fixture.pop);
|
||||||
|
|
||||||
std::cout << "Test 4 passed: Caching mechanism properly invalidated\n";
|
std::cout << "Test 3 passed: Caching mechanism properly invalidated\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
|
@ -149,7 +144,6 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
eoParser parser(argc, argv);
|
eoParser parser(argc, argv);
|
||||||
test_Consistency(parser);
|
test_Consistency(parser);
|
||||||
test_RankingOrder(parser);
|
|
||||||
test_MinPopulationSize(parser);
|
test_MinPopulationSize(parser);
|
||||||
test_CachingEffectiveness(parser);
|
test_CachingEffectiveness(parser);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue