-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.cpp
237 lines (219 loc) · 7.5 KB
/
graph.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "graph.h"
using namespace std;
// helper function that checks whether the given string is number or not.
bool isNumber(const string& s)
{
char* end = 0;
double val = strtod(s.c_str(), &end);
return end != s.c_str() && val != HUGE_VAL;
}
//helper function that splits an input line into words
template <class Container>
void split(const std::string& str, Container& cont)
{
std::istringstream iss(str);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(cont));
}
int Graph::getVariable(string inp){
int res;
if( isNumber(inp) ){
double val = stod(inp.c_str());
idCount++;
name[idCount] = inp;
vars[idCount] = new Variable(idCount, inp, val);
res = idCount;
type[idCount] = VARIABLE;
}
else{
if(id.find(inp)==id.end()){
idCount++;
id[inp] = idCount;
name[idCount] = inp;
vars[idCount] = new Variable(idCount, inp);
res = idCount;
type[idCount] = VARIABLE;
}
else{
res = id[inp];
}
}
// printf("%s -> %d\n",inp.c_str(), res);
return res;
}
int Graph::getFunction(string fnc){
idCount++;
name[idCount] = fnc;
type[idCount] = FUNCTION;
Function *f;
// cout << "new Function: " << fnc << endl;
if(fnc.compare("mult")==0)
f = new Multiplication(idCount, fnc);
else if(fnc.compare("add")==0)
f = new Addition(idCount, fnc);
else if(fnc.compare("subs")==0)
f = new Subtraction(idCount, fnc);
else if(fnc.compare("divide")==0)
f = new Division(idCount, fnc);
else if(fnc.compare("sin")==0)
f = new Sine(idCount, fnc);
else if(fnc.compare("cos")==0)
f = new Cosine(idCount, fnc);
else if(fnc.compare("identity")==0)
f = new Identity(idCount, fnc);
else if(fnc.compare("tan")==0)
f = new Tangent(idCount, fnc);
else if(fnc.compare("acos")==0)
f = new ArcCosine(idCount, fnc);
else if(fnc.compare("asin")==0)
f = new ArcSine(idCount, fnc);
else if(fnc.compare("atan")==0)
f = new ArcTangent(idCount, fnc);
else if(fnc.compare("exp")==0)
f = new Exponential(idCount, fnc);
else if(fnc.compare("log")==0)
f = new Log(idCount, fnc);
else if(fnc.compare("log10")==0)
f = new Log10(idCount, fnc);
else if(fnc.compare("pow")==0)
f = new Power(idCount, fnc);
else if(fnc.compare("sqrt")==0)
f = new Sqrt(idCount, fnc);
fncs[idCount] = f;
return idCount;
}
void Graph::addUnaryFunction(string fnc, string inp, string out){
int fId = getFunction(fnc);
int inpId = getVariable(inp);
int outId = getVariable(out);
fncs[fId]->addInput(vars[inpId]);
fncs[fId]->setOutput(vars[outId]);
vars[inpId]->addTo(fncs[fId]);
vars[outId]->setFrom(fncs[fId]);
}
void Graph::addBinaryFunction(string fnc, string inp1, string inp2, string out){
int fId = getFunction(fnc);
int inpId1 = getVariable(inp1);
int inpId2 = getVariable(inp2);
int outId = getVariable(out);
fncs[fId]->addInput(vars[inpId1]);
fncs[fId]->addInput(vars[inpId2]);
fncs[fId]->setOutput(vars[outId]);
vars[inpId1]->addTo(fncs[fId]);
vars[inpId2]->addTo(fncs[fId]);
vars[outId]->setFrom(fncs[fId]);
}
void Graph::addAssignment(string lvalue, string rvalue) {
addUnaryFunction("identity", rvalue, lvalue);
}
void Graph::readGraph(string fileName){
ifstream infile(fileName);
string line;
//process the lines until the end of file is reached
while(getline(infile, line)){
vector<string> words;
split(line, words);
//if there are two words in the line, either input or output definition
if(words.size() == 2){
if(words[0].compare("input") == 0)
inputNodes.push_back( getVariable(words[1]) );
else if( words[0].compare("output") == 0 )
outputNode = getVariable(words[1]);
else
cout <<"Wrong input file format!" << endl;
}
//if there are three words in the line it means an assigment is added to the graph
else if( words.size() == 3 ){
addAssignment(words[0], words[2]);
}
//if there are four words in the line it means a unary function is added to the graph
else if(words.size() == 4){
addUnaryFunction(words[2], words[3], words[0]);
}
//if there are five words in the line it means a binary function is added to the graph
else if(words.size() == 5){
addBinaryFunction(words[2], words[3], words[4] ,words[0]);
}else{
cout <<"Wrong input file format!" << endl;
}
}
//if end of file is not reached there is error while reading
if( !infile.eof() )
cout << "Error reading the file!" << endl;
infile.close();
}
void Graph::initTopologicalOrder(){
//stores number of visited nodes
int visited_nodes = 0;
//stores in-degrees of nodes
vector<int> in_degree(idCount,0);
//fill in-degrees
for(unordered_map<int, Variable*>::iterator itr = vars.begin(); itr != vars.end(); itr++){
in_degree[itr->first-1] = itr->second->getIncomings().size();
}
for(unordered_map<int, Function*>::iterator itr = fncs.begin(); itr != fncs.end(); itr++){
in_degree[itr->first-1] = itr->second->getIncomings().size();
}
//queue for ids of nodes that have in-degree 0
queue<int> q;
for(int i=0; i<idCount; i++){
if(in_degree[i]==0)
q.push(i);
}
//process queue
while(!q.empty()){
int curr_node = q.front();
q.pop();
topological_order.push_back(curr_node+1);
//decrease in-degree of the neighbors of curr_node by 1
vector<Node*> curr_neighbors;
if(type[curr_node+1] == VARIABLE)
curr_neighbors = vars[curr_node+1]->getOutgoings();
else
curr_neighbors = fncs[curr_node+1]->getOutgoings();
for(int i=0; i<curr_neighbors.size(); i++)
//if in-degree becomes 0 push it to the queue
if(--in_degree[curr_neighbors[i]->id-1] == 0)
q.push(curr_neighbors[i]->id-1);
visited_nodes++;
}
isCyclic = visited_nodes != idCount;
}
double Graph::forwardPass(vector<double> inputValues){
for(int i=0; i< inputNodes.size(); i++){
vars[inputNodes[i]]->value = inputValues[i];
}
for(int i=0; i<topological_order.size(); i++){
if(type[topological_order[i]] == FUNCTION)
fncs[topological_order[i]]->doForward();
}
return vars[outputNode]->value;
}
vector<double> Graph::backwardPass(){
//reset derivative values of variables
for(unordered_map<int, Variable*>::iterator itr = vars.begin(); itr != vars.end(); itr++){
itr->second->derivative = 0;
}
vars[outputNode]->derivative = 1.0;
for(int i=topological_order.size()-1; i>=0; i--){
if(type[topological_order[i]] == FUNCTION)
fncs[topological_order[i]]->doBackward();
}
vector<double> derivatives;
for(int i=0; i<inputNodes.size(); i++){
derivatives.push_back(vars[inputNodes[i]]->derivative);
}
return derivatives;
}
string Graph::getOutputName(){
return name[outputNode];
}
string Graph::getDerivativeName(){
stringstream ss;
for(int i=0; i<inputNodes.size(); i++){
ss << "d" << name[outputNode] << "/d" << name[inputNodes[i]] << " ";
}
return ss.str();
}
Graph::~Graph() {}