about summary refs log tree commit diff
path: root/sourcecodes/k-best/src/arcToNet.cc
blob: b071baabc23309195ef14be21019c37b4c29d787 (plain)
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
#include <iostream>
#include <fstream>
#include <iomanip>
//#include "varpar.h"
#include <stdlib.h>
#include <math.h>
#include <list>
#include <stdio.h>
#include <vector>
//#include "cfg.h"
#include <string.h>
#include <cmath>
//HR
//#define EPSILON 0.001
#define EPSILON 0.00001


//HR: Add for hash_set
#if __GNUC__ < 3 && __GNUC__ >= 2 && __GNUC_MINOR__ >= 95
# include <hash_set>
# include <functional>
# define gnu_namespace std
#elif __GNUC__ >= 3
# include <ext/hash_set>
# if __GNUC_MINOR__ == 0
# include <functional>
# define gnu_namespace std
# else
# include <ext/functional>
# define gnu_namespace __gnu_cxx
# endif
#else
# include <hash_set.h>
# include <functional.h>
# define gnu_namespace std
#endif
using namespace gnu_namespace;
#include <set>
//


using namespace std;


//HR: add from Engine.h in BayesNw
#define MARK 9e99
#define LOG_ZERO -1e101
//#define LOGMINUS_NEW(logA, logB) if(logA == MARK) logA = logB; else if (logA == LOG_ZERO) logA = logB; else if (logB == LOG_ZERO) logA = logA; else logA = logA + log(1-exp(logB-logA));
//HR: add from UpdateHR.h in BayesNw to deal with the problem in sum = 0 in comp_post()
//call by reference to the original value
//the log sum result is stored in logA
//may change double into long double; no, use double
void logAddComp(double & logA, double & logB){
	    if(logA == MARK){
    		logA = logB;
    	}
    	else if (logA == LOG_ZERO){
    		logA = logB;
    	}
    	else if(logB == MARK){
    		logA = logA;
    	}
    	else if(logB == LOG_ZERO){
    		logA = logA;
    	}
    	//Nither logA nor logB is MARK/LOG_ZERO
    	else{
    		//1
    		if(logA < 0 && logB < 0 ){
    			logA = logA + log( 1 + exp(logB-logA) );
    		}
    		//2
    		else if(logA > 0 && logB > 0){
    			//always make exp(.): . > 0
    			//2.1
    			if(logA >= logB){
    				logA = -( -logA + log( 1 + exp(-logB+logA) ) );
    			}
    			//2.2
    			else{
    				logA = -( -logB + log( 1 + exp(-logA+logB) ) );
    			}
    			
    		}
    		//3
    		else if(logA < 0 && logB > 0){
    			//3.1
    			if(logA + logB > 0){   				
    				//logA = logA + log( 1 - exp(-logB-logA) );
    				logA = -logB + log( exp(logA + logB) - 1 );
    				
    			}
    			//3.2
    			else if(logA + logB < 0){
    				logA = -( logA + log( exp(-logB-logA)  - 1) );
    			}
    			//3.3
    			else{
    				logA = LOG_ZERO;
    			}
    		}
    		//4
    		else if(logA > 0 && logB < 0){
    			//4.1
    			if(logA + logB > 0){
    				logA = -logA + log( exp(logB+logA) - 1);
    			}
    			//4.2
    			else if(logA + logB < 0){
    				//logA = -( -logA + log(1 - exp(logB+logA)) );
    				logA = -( logB + log( exp(-logA-logB) - 1) );
    				
    			}
    			//4.3
    			else{
    				logA = LOG_ZERO;
    			}			
    		}
		
    	}
	
}


//HR: Add
//Will be used for checking edges-based feasure for Engineer in BayesNW
void arcToNet(char * inFeasFileName, char* outFeasFileName){

	cout << "inFeasFileName: " << inFeasFileName << endl;
	
	cout << "outFeasFileName: " << outFeasFileName << endl;
	
	int MAX_VAR_NUM = 32;
	
	int inFeatureEdges[MAX_VAR_NUM];
	for(int i = 0; i < MAX_VAR_NUM; i++){
		inFeatureEdges[i] = 0;
	}
	
	string str;
	str.assign(inFeasFileName);
	
  
	FILE* fp = fopen(str.c_str(), "r");

	int fromNode;
	int toNode;
	//int i=0;
	while(!feof(fp)){
		char c;
		fromNode = -1;
		toNode = -1;

		fscanf(fp,"%d",&fromNode);
		fscanf(fp,"%d",&toNode);
		fscanf(fp,"%c",&c);
		
		cout << "fromNode = "<< fromNode << endl;
		cout << "toNode = "<< toNode << endl;

		inFeatureEdges[toNode] += (1 << fromNode);


		
	}
	fclose(fp);
	int no_var = toNode;
	
	for(int i = 0; i < MAX_VAR_NUM; i++){
		cout << "inFeatureEdges[" << i << "] " << inFeatureEdges[i] << endl; 
	}
	
	
	int outFeatureEdges[MAX_VAR_NUM];
	for(int i = 0; i < MAX_VAR_NUM; i++){
		outFeatureEdges[i] = 0;
	}
	
	string str2;
	str2.assign(outFeasFileName);
	
  
	fp=fopen(str2.c_str(), "r");

	//int fromNode;
	//int toNode;
	//int i=0;
	while(!feof(fp)){
		char c;
		fromNode = -1;
		toNode = -1;

		fscanf(fp,"%d",&fromNode);
		fscanf(fp,"%d",&toNode);
		fscanf(fp,"%c",&c);
		
		cout << "fromNode = "<< fromNode << endl;
		cout << "toNode = "<< toNode << endl;

		outFeatureEdges[toNode] += (1 << fromNode);


		
	}
	fclose(fp);
	//int no_var = toNode;
	
	
	for(int i = 0; i < MAX_VAR_NUM; i++){
		cout << "outFeatureEdges[" << i << "] " << outFeatureEdges[i] << endl; 
	}
	
	
	
	
}

	

//HR:F
int main(int argc, char* argv[]){
	
	if (argc!=3){
    	cout << "Please type the correct format: arcToNet inFeatureFileName outFeatureFileName\n" << endl;
        return 1;
    }
      
     arcToNet(argv[1], argv[2]);
		
}