-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneSeedProduction.cpp
64 lines (52 loc) · 1.89 KB
/
GeneSeedProduction.cpp
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
56
57
58
59
60
61
62
63
64
#include "GeneSeedProduction.h"
#include "Phenotype.h"
#include <Random.h>
#include <QLocale>
GeneSeedProduction::GeneSeedProduction(Energy seedSize)
: seedSize(seedSize)
{
}
void GeneSeedProduction::ConfigureJsonSerialisationHelper(util::JsonSerialisationHelper<GeneSeedProduction>& helper)
{
helper.RegisterConstructor(
helper.CreateParameter<Energy>("Energy", &GeneSeedProduction::seedSize)
);
}
std::string GeneSeedProduction::TypeName() const
{
return std::string(util::TypeName<GeneSeedProduction>());
}
QString GeneSeedProduction::ToString() const
{
return QLocale::system().toString(seedSize, 'f', 2) + "j";
}
QString GeneSeedProduction::Description() const
{
return "Defines the quantity of energy required to produce a single seed.";
}
std::shared_ptr<Gene> GeneSeedProduction::Mutated() const
{
return std::make_shared<GeneSeedProduction>(seedSize + Random::Number(-10_j, 10_j));
}
std::shared_ptr<Gene> GeneSeedProduction::Crossed(const std::shared_ptr<Gene>& other) const
{
std::shared_ptr<GeneSeedProduction> otherSeedProductionGene = std::dynamic_pointer_cast<GeneSeedProduction>(other);
if (otherSeedProductionGene) {
return std::make_shared<GeneSeedProduction>(seedSize + Random::Number(-10_j, 10_j));
}
return nullptr;
}
double GeneSeedProduction::Similarity(const std::shared_ptr<Gene>& other) const
{
double percentSimilarity = 0;
std::shared_ptr<GeneSeedProduction> otherSeedProductionGene = std::dynamic_pointer_cast<GeneSeedProduction>(other);
if (otherSeedProductionGene) {
double proportionalDifference = std::abs(seedSize - otherSeedProductionGene->seedSize) / seedSize;
percentSimilarity = (1.0 - proportionalDifference) * 100;
}
return std::clamp(percentSimilarity, 0.0, 100.0);
}
void GeneSeedProduction::Express(Phenotype& phenotype) const
{
phenotype.seedSize = seedSize;
}