ECF 1.5
IADiv.cpp
1#include "GPSymbRegEvalOp.h"
2
3#include "IADiv.h"
4#include "IATree.h"
5#include <cmath>
6
7void IADiv::execute(void* result, Tree::Tree& tree)
8{
9 if (!calculateBoundsIfRootNode(result, tree)) {
10 return;
11 }
12
14}
15
16IABounds IADiv::calculateBounds(Tree::IATree& tree) {
17 // get next argument
18 tree.iNode_++;
19 IAPrimitiveP prim1 = boost::dynamic_pointer_cast<IAPrimitive>(tree[tree.iNode_]->primitive_);
20 // calculate bounds
21 IABounds bounds1 = prim1->calculateBounds(tree);
22
23 if (!bounds1.isWithinBounds) {
24 // subtree not within bounds, delete expression
25 return bounds1;
26 }
27
28 // get next argument
29 tree.iNode_++;
30 IAPrimitiveP prim2 = boost::dynamic_pointer_cast<IAPrimitive>(tree[tree.iNode_]->primitive_);
31 // calculate bounds
32 IABounds bounds2 = prim2->calculateBounds(tree);
33
34 if (!bounds2.isWithinBounds) {
35 // subtree not within bounds, delete expression
36 return bounds2;
37 } else if (boundsContainZero(bounds2)) {
38 IABounds retVal;
39 retVal.isWithinBounds = false;
40 return retVal;
41 } else {
42 // subtree is ok
43 double ll = bounds1.lowerBound / bounds2.lowerBound;
44 double lu = bounds1.lowerBound / bounds2.upperBound;
45 double ul = bounds1.upperBound / bounds2.lowerBound;
46 double uu = bounds1.upperBound / bounds2.upperBound;
47
48 IABounds retVal;
49 if (ll <= lu && ll <= ul && ll <= uu) {
50 // ll is min
51 retVal.isLowerBoundInclusive = bounds1.isLowerBoundInclusive && bounds2.isLowerBoundInclusive;
52 retVal.lowerBound = ll;
53 } else if (lu <= ll && lu <= ul && lu <= uu) {
54 // lu is min
55 retVal.isLowerBoundInclusive = bounds1.isLowerBoundInclusive && bounds2.isUpperBoundInclusive;
56 retVal.lowerBound = lu;
57 } else if (ul <= ll && ul <= lu && ul <= uu) {
58 // ul is min
59 retVal.isLowerBoundInclusive = bounds1.isUpperBoundInclusive && bounds2.isLowerBoundInclusive;
60 retVal.lowerBound = ul;
61 } else {
62 // uu is min
63 retVal.isLowerBoundInclusive = bounds1.isUpperBoundInclusive && bounds2.isUpperBoundInclusive;
64 retVal.lowerBound = uu;
65 }
66
67 if (ll >= lu && ll >= ul && ll >= uu) {
68 // ll is max
69 retVal.isUpperBoundInclusive = bounds1.isLowerBoundInclusive && bounds2.isLowerBoundInclusive;
70 retVal.upperBound = ll;
71 } else if (lu >= ll && lu >= ul && lu >= uu) {
72 // lu is max
73 retVal.isUpperBoundInclusive = bounds1.isLowerBoundInclusive && bounds2.isUpperBoundInclusive;
74 retVal.upperBound = lu;
75 } else if (ul >= ll && ul >= lu && ul >= uu) {
76 // ul is max
77 retVal.isUpperBoundInclusive = bounds1.isUpperBoundInclusive && bounds2.isLowerBoundInclusive;
78 retVal.upperBound = ul;
79 } else {
80 // uu is max
81 retVal.isUpperBoundInclusive = bounds1.isUpperBoundInclusive && bounds2.isUpperBoundInclusive;
82 retVal.upperBound = uu;
83 }
84
85 retVal.isWithinBounds = true;
86 return retVal;
87 }
88}
89
90bool boundsContainZero(IABounds bounds) {
91 double l = bounds.lowerBound;
92 double u = bounds.upperBound;
93
94 if (l < 0 && u > 0) {
95 return true;
96 }
97
98 if (l == 0 && bounds.isLowerBoundInclusive) {
99 return true;
100 }
101
102 if (u == 0 && bounds.isUpperBoundInclusive) {
103 return true;
104 }
105
106 return false;
107}
virtual void execute(void *result, Tree::Tree &tree)
Execute the primitive.
Definition: IADiv.cpp:7
void execute(void *result, Tree &tree)
Execute the primitive.
Definition: Div.h:41
Tree class - implements genotype as a tree.
Definition: Tree_c.h:29
uint iNode_
current node index (when parsing the tree)
Definition: Tree_c.h:75