-
Notifications
You must be signed in to change notification settings - Fork 19
/
basicFunctions.cpp
167 lines (166 loc) · 3.18 KB
/
basicFunctions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* basicFunctions.cpp
* GeneticProg
*
* Created by Peter Harrington on 7/1/11.
* Copyright 2011 Clean Micro, LLC. All rights reserved.
*
*/
#include <iostream>
#include "basicFunctions.h"
using namespace std;
//
// ConstNode
//
ConstNode::ConstNode(){
numChildren = 0;
constVal = rand()/(double)RAND_MAX;
char str[20] = "";
sprintf(str, "Const: %f", constVal);
label = str;
}
ConstNode::ConstNode(double preSetVal){
numChildren = 0;
constVal = preSetVal;
char str[20] = "";
sprintf(str, "Const: %f", constVal);
label = str;
}
double ConstNode::eval(double inVal){
return constVal;
}
ConstNode* ConstNode::clone(){
ConstNode* retTree = new ConstNode(constVal);
return retTree;
}
string ConstNode::getLabel(){
return label;
}
//
// InputNode
//
InputNode::InputNode(int numPossibleInputs){
numChildren = 0;
inputIndex = rand() % numPossibleInputs;
setValues(inputIndex);
}
double InputNode::eval(double inVal){
return inVal;
}
void InputNode::setValues(int inIndex){
char str[20] = "";
sprintf(str, "InputVal: %d", inIndex);
label = str;
}
InputNode* InputNode::clone(){
InputNode* retTree = new InputNode(1);
retTree->setValues(inputIndex);
return retTree;
}
string InputNode::getLabel(){
return label;
}
//
// Add
//
Add::Add(){
numChildren = 2;
label = "Add";
}
double Add::eval(double inVal){
if (children[0] && children[1]){
return children[0]->eval(inVal) + children[1]->eval(inVal);
}
else {
cerr << "left and right not defined in add"<<endl;
return -1.0;
}
}
Add* Add::clone(){
Add* retNode = new Add();
for (int i=0; i<numChildren; i++) {
retNode->children[i] = children[i]->clone();
}
return retNode;
}
string Add::getLabel(){
return label;
}
//
// Subtract
//
Subtract::Subtract(){
numChildren = 2;
label = "Subtract";
}
double Subtract::eval(double inVal){
if (children[0] && children[1]){
return children[0]->eval(inVal) - children[1]->eval(inVal);
}
else {
cerr << "left and right not defined in subtract"<<endl;
return -1.0;
}
}
Subtract* Subtract::clone(){
Subtract* retNode = new Subtract();
for (int i=0; i<numChildren; i++) {
retNode->children[i] = children[i]->clone();
}
return retNode;
}
string Subtract::getLabel(){
return label;
}
//
// Multiply
//
Multiply::Multiply(){
numChildren = 2;
label = "Multiply";
}
double Multiply::eval(double inVal){
if (children[0] && children[1]){
return children[0]->eval(inVal) * children[1]->eval(inVal);
}
else {
cerr << "left and right not defined in multiply"<<endl;
return -1.0;
}
}
Multiply* Multiply::clone(){
Multiply* retNode = new Multiply();
for (int i=0; i<numChildren; i++) {
retNode->children[i] = children[i]->clone();
}
return retNode;
}
string Multiply::getLabel(){
return label;
}
//
// Divide
//
Divide::Divide(){
numChildren = 2;
label = "Divide";
}
double Divide::eval(double inVal){
if (children[0] && children[1]){
return children[0]->eval(inVal) / children[1]->eval(inVal);
}
else {
cerr << "left and right not defined in multiply"<<endl;
return -1.0;
}
}
Divide* Divide::clone(){
Divide* retNode = new Divide;
for (int i=0; i<numChildren; i++) {
retNode->children[i] = children[i]->clone();
}
return retNode;
}
string Divide::getLabel(){
return label;
}