-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_creature.cc
65 lines (58 loc) · 2.16 KB
/
problem_creature.cc
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
65
#include "problem_creature.h"
ProblemCreature::ProblemCreature(ProblemDescription* pd, int massId, double T) {
this->problemDescription = pd;
this->T = T;
this->massId = massId;
this->hasGetCostCache = false;
this->pimodels = nullptr;
this->timesToCheck = -1;
// dna = [k1, k2, ..., kn, c1, c2, ..., cn]
this->dna = std::vector<Bounded>(pd->NumberOfSpringsAndDampers());
for (int i = 0; i < int(this->dna.size()); i++) {
(this->dna.at(i)).Set(Random());
}
}
ProblemCreature::ProblemCreature(ProblemDescription* pd, int massId,
Pimodels* pimodels, int timesToCheck) {
this->problemDescription = pd;
this->T = T;
this->massId = massId;
this->hasGetCostCache = false;
this->pimodels = pimodels;
this->timesToCheck = timesToCheck;
// dna = [k1, k2, ..., kn, c1, c2, ..., cn]
this->dna = std::vector<Bounded>(pd->NumberOfSpringsAndDampers());
for (int i = 0; i < int(this->dna.size()); i++) {
(this->dna.at(i)).Set(Random());
}
}
double ProblemCreature::GetCost() {
if (this->hasGetCostCache == true) {
return this->getCostCache;
}
if (this->pimodels == nullptr) {
// TODO: A function that edits an existing Problem by setting a dna
// could be slightly better, since BuildFromDNA creates the masses
// again, for example, which we don't alter at the dna.
auto p = this->problemDescription->BuildFromDNA(dna);
if (p.isError) {
throw std::runtime_error(
"Unexpected BuildFromDNA error in GetCost");
}
auto ei = p.val.Integrate(this->T);
if (ei.isError) {
throw std::runtime_error("Unexpected Integrate error in GetCost");
}
auto e1 = p.val.GetMassMaxAbsAccel(this->massId);
if (e1.isError) {
throw std::runtime_error(
"Unexpected GetMassMaxAbsAccel error in GetCost");
}
this->getCostCache = e1.val;
} else {
this->getCostCache =
this->pimodels->GetMaxAbsAccel(massId, timesToCheck, dna);
}
this->hasGetCostCache = true;
return this->getCostCache;
}