diff --git a/DL/Restricted_Boltzmann_machine/RBMupdate.jpg b/DL/Restricted_Boltzmann_machine/RBMupdate.jpg new file mode 100644 index 0000000..63b151e Binary files /dev/null and b/DL/Restricted_Boltzmann_machine/RBMupdate.jpg differ diff --git a/DL/Restricted_Boltzmann_machine/Rbm b/DL/Restricted_Boltzmann_machine/Rbm new file mode 100755 index 0000000..05aa360 Binary files /dev/null and b/DL/Restricted_Boltzmann_machine/Rbm differ diff --git a/DL/Restricted_Boltzmann_machine/Rbm.cpp b/DL/Restricted_Boltzmann_machine/Rbm.cpp new file mode 100644 index 0000000..317b064 --- /dev/null +++ b/DL/Restricted_Boltzmann_machine/Rbm.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include "array.hpp" +#include +#include +using namespace std; +double corpus[8][9] = { + {1,0,0,0,0,0,0,0,1}, + {0,1,0,0,0,0,0,0,1}, + {0,0,1,0,0,0,0,0,1}, + {0,0,0,1,0,0,0,0,1}, + {0,0,0,0,1,0,0,0,1}, + {0,0,0,0,0,1,0,0,1}, + {0,0,0,0,0,0,1,0,1}, + {0,0,0,0,0,0,0,1,1} + }; + + +double corpus2[8][9]={ + {1,0,0,0,0,0,0,0,1}, + {0,1,0,0,0,0,0,0,1}, + {0,0,1,0,0,0,0,0,1}, + {0,0,0,1,0,0,0,0,1}, + {0,0,0,0,1,0,0,0,1}, + {0,0,0,0,0,1,0,0,1}, + {0,0,0,0,0,0,1,0,1}, + {0,0,0,0,0,0,0,1,1} + + }; + + + + +double sigmoid(double x) +{ + double ex = pow(2.718281828,x); + return ex/(1+ex); +} +double* sig_table; +double function_g(double x) +{ + x=x>3.99?4.0:x; + x=x<-3.99?-4.0:x; + //return sigmoid(x); + return sig_table[(uint32_t)((x+4.0)*8*128*1024)]; +}; +double init_sig_table() +{ + sig_table = (double*)malloc((64*128*1024+1)*sizeof(double)); + for(int i=0;i<64*128*1024;++i) + { + sig_table[i] = sigmoid((i/(1048576.0)-4.0)); + } + sig_table[64*128*1024] = 1; +} + +void init_matrix(t_array& w) +{ + for(int i=0;i +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +struct t_array +{ + t_array(uint32_t x, uint32_t y,const char* filename):m_X(x),m_Y(y) + { + if(filename == NULL) + { + m_in = (double*)malloc(x*y*sizeof(double)); + m_fd = -1; + } + else + { + struct stat sb; + uint32_t block_cnt = (x*y*sizeof(double)/1024)+1; + if (stat(filename, &sb) == -1) { + + char command_line[0x100]; + snprintf(command_line,0x100,"dd if=/dev/zero of=%s bs=1k count=%d",filename,block_cnt); + system(command_line); + } + m_fd = open(filename, O_CREAT|O_RDWR|O_NONBLOCK,00777); + m_flen = lseek(m_fd, 1, SEEK_END)-1; + m_in = (double*)mmap(0,m_flen,PROT_READ|PROT_WRITE,MAP_SHARED,m_fd,0); + } + }; + ~t_array() + { + if(m_fd == -1) + { + free(m_in); + } + else + { + msync(m_in,m_flen,MS_SYNC); + munmap(m_in,m_flen); + close(m_fd); + } + } + double* operator[](uint32_t idx) + { + //return (T*)((long)(&m_in[idx*m_Y])*(idx array(4,4); + + for(int i=0;i<4;++i) + { + for(int j=0;j<4;++j) + { + printf("%f\t",array[i][j]); + } + printf("\n"); + } +}*/ diff --git a/MRF/Parameter learning for MRFs.pdf b/MRF/Parameter learning for MRFs.pdf new file mode 100644 index 0000000..dd3d130 Binary files /dev/null and b/MRF/Parameter learning for MRFs.pdf differ diff --git a/MRF/algorithm.jpg b/MRF/algorithm.jpg new file mode 100644 index 0000000..9afadbb Binary files /dev/null and b/MRF/algorithm.jpg differ diff --git a/MRF/main.cpp b/MRF/main.cpp new file mode 100644 index 0000000..e56dc6b --- /dev/null +++ b/MRF/main.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; +char corpus[5][60]={ "zcngotcnx, igcump, zjcjs, lqpWiqu, cefmfhc, o, lb, fdc", + "m, r, xevo, ijjiir, b, to, jz, gsr, wq, vf, x, ga,", + "pcp, d, oziVlal, hzagh, yzop, io, advzmxnv,", + "emx, kayerf, mlj, rawzyb, jp, ag, ctdnnnbg, wgdw, t,", + "cy, spxcq, uzflbbf, dxtkkn, cxwx, jpd, ztzh, lv, zhpkv" + }; +float p_corpus(int c,float theta,float& f1) +{ + float sum = 0; + for(int i=0;i='a' && corpus[c][i]<='z' ? 1 : 0; + } + f1 = sum/sizeof(corpus[c]); + return pow(2.718281828,(theta * f1)); +} +int main(void) +{ + float theta1 = 1; + float f1[5] = {0}; + float p[5] = {0}; + for(int iter = 0; iter<100000;++iter) + { + float sum_p = 0.0; + for(int i=0;i<5;++i) + { + p[i] = p_corpus(i,theta1,f1[i]); + sum_p += p[i]; + } + //to get the Max value, so chose to add a positive gradient. + theta1 += 0.1*((f1[0]+f1[1]+f1[2]+f1[3]+f1[4])/5 - (f1[0]*p[0]/sum_p+f1[1]*p[1]/sum_p+f1[2]*p[2]/sum_p+f1[3]*p[3]/sum_p+f1[4]*p[4]/sum_p)); + printf ("%d\t%f\t%f\t%f\t%f\t%f\n",iter,p[0]/sum_p, p[1]/sum_p,p[2]/sum_p,p[3]/sum_p,p[4]/sum_p,p[5]/sum_p); + //printf ("%f\n",theta1); + } + return 0; +} diff --git a/MRF/main2.cpp b/MRF/main2.cpp new file mode 100644 index 0000000..559f9d9 --- /dev/null +++ b/MRF/main2.cpp @@ -0,0 +1,44 @@ +#include +#include +using namespace std; +char corpus[5][60]={ "zcngotcnx, igcump, zjcjs, lqpWiqu, cefmfhc, o, lb, fdc", + "m, r, xevo, ijjiir, b, to, jz, gsr, wq, vf, x, ga,", + "pcp, d, oziVlal, hzagh, yzop, io, advzmxnv,", + "emx, kayerf, mlj, rawzyb, jp, ag, ctdnnnbg, wgdw, t,", + "cy, spxcq, uzflbbf, dxtkkn, cxwx, jpd, ztzh, lv, zhpkv" + }; +float p_corpus(int c,float theta,float& f1) +{ + float sum = 0; + for(int i=0;i='a' && corpus[c][i]<='z' ? 1 : 0; + } + f1 = sum/sizeof(corpus[c]); + return pow(2.718281828,(theta * f1)); +} +int main(void) // using mini-batch with setting size = 2 +{ + float theta1 = 1; + float f1[5] = {0}; + float p[5] = {0}; + p[0] = p_corpus(0,theta1,f1[0]); + p[1] = p_corpus(1,theta1,f1[1]); + p[2] = p_corpus(2,theta1,f1[2]); + p[3] = p_corpus(3,theta1,f1[3]); + p[4] = p_corpus(4,theta1,f1[4]); + + for(int iter = 0; iter<100000;++iter) + { + + p[iter%5] = p_corpus(iter%5,theta1,f1[iter%5]); + iter++; + p[iter%5] = p_corpus(iter%5,theta1,f1[iter%5]); + float sum_p = p[(iter-1)%5]+p[iter%5]; + + theta1 -= -0.1*((f1[iter%5]+f1[(iter-1)%5])/2- (f1[iter%5]*p[iter%5]/sum_p+f1[(iter-1)%5]*p[(iter-1)%5]/sum_p)); + printf ("%d\t%f\t%f\n",iter,p[(iter-1)%5]/sum_p, p[iter%5]/sum_p); + //printf ("%f\n",theta1); + } + return 0; +} diff --git a/MRF/main3.cpp b/MRF/main3.cpp new file mode 100644 index 0000000..9353041 --- /dev/null +++ b/MRF/main3.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +using namespace std; +/* + * @ - @ - @ + * | | | + * @ - @ - @ + +*/ +using namespace std; +char corpus[5][60]={ "zcngotcnx, igcump, zjcjs, lqpWiqu, cefmfhc, o, lb, fdc", + "m, r, xevo, ijjiir, b, to, jz, gsr, wq, vf, x, ga,", + "pcp, d, oziVlal, hzagh, yzop, io, advzmxnv,", + "emx, kayerf, mlj, rawzyb, jp, ag, ctdnnnbg, wgdw, t,", + "cy, spxcq, uzflbbf, dxtkkn, cxwx, jpd, ztzh, lv, zhpkv" + }; +float p_corpus(int c,float theta1,float& f1) +{ + float sum = 0; // 小写字母的数量 + for(int i=0;i='a' && corpus[c][i]<='z' ? 1 : 0; + } + f1 = sum/sizeof(corpus[c]); + return pow(2.718281828,-1*(theta1 * f1)); +} +float p_corpus(char* corpus,float theta1,float& f1) +{ + float sum = 0; // 小写字母的数量 + for(int i=0;i='a' && corpus[i]<='z' ? 1 : 0; + } + f1 = sum/sizeof(corpus); + + return pow(2.718281828,-1*(theta1 * f1)); +} +typedef char t[100]; +void sample(t & xx ) +{ + for(int i=0;i<100;++i) + { + xx[i] = 'a'+ rand()%('z'-'a'); + } + for(int i=0;i<100;++i) + { + if(rand()%1000>500&&rand()%1000<300&&rand()%1000>400) + { + xx[i] = ','; + } + } +} + +int main(void) // using Monte-Carlo sampling, however here we have a new way we donnot want to explain in detail, just in code! +{ + float theta1 = 1; + float f1[5] = {0}; + float p[5] = {0}; + int i = 0; + float mc_iteration = 2000; + for(int iter = 0; iter<100000;++iter) + { + i = i%5; + for( int minibatch=i;true;) // mini batch have only one corpus + { + p_corpus(i,theta1,f1[i]); + p_corpus(i,theta1,f1[(++i)%5]); + break; + } + float mc = 0.0; + float sum_p = 0.0; + for(int i=0;i +#include +using namespace std; +int sample[10][4] = { + {1,0,1,1}, + {1,0,1,1}, + {0,1,1,-1}, + {0,1,0,-1}, + {1,1,1,1}, + {0,1,0,1}, + {0,1,1,1}, + {1,0,0,-1}, + {0,1,1,1}, + {1,0,0,-1} + }; +int f(int a[4]) +{ + if(a[0] == 1) return 1; + else return -1; +} +int g(int a[4]) +{ + if(a[1] == 1) return 1; + else return -1; +} +int h(int a[4]) +{ + if(a[2] == 1) return 1; + else return -1; +} +typedef int (*func) (int a[4]); +int c_iter_number = 5; +int main(int argc, char** argv) +{ + func func_arr[3] = {f,g,h}; + int m = 10; + double D[m]; + for(int i=0;i0?1:-1)*(error[0]-0.5); + int max_idx = 0 ; + for(int i = 1; i<3;++i) + { + if((error[i]-0.5>0?1:-1)*(error[i]-0.5)>max) + { + max = error[i]; + max_idx = i; + } + } + + alpha[t] = 0.5*log((1-error[max_idx])/error[max_idx]); + h[t] = max_idx; + double sum = 0; + double alpha_t = alpha[t]; + for(int i=0;i<10;++i) + { + int f = h[t]; + double t = D[i] * exp(-1*alpha_t* (func_arr[f](sample[i])*sample[i][3]) ); + sum += t; + D[i] = t; + } + for( int i=0;i<10;++i) + { + D[i] /= sum; + } + + } + int correct_number = 0; + for(int s=0;s<10;++s) + { + double result = 0; + for(int i = 0;i0?1:-1) == sample[s][3] ) correct_number++ ; + cout<0?1:-1) == sample[s][3])<0;++i) + { + j=0; + update_mean_var();; + update_p(); + printf("iter:%d",i); + for(int k=0;k +#include +#include +#include +#include +#include +using namespace std; +double sigmoid(double x) +{ + double ex = pow(2.718281828,x); + return ex/(1+ex); +} +double beta = -1; //make sure that the small energe means high probability and huge energe means low probability. +double e(double x) +{ + return pow(2.718281828,beta*x); +} +double* sig_table; +double function_g(double x) +{ + x=x>3.99?4.0:x; + x=x<-3.99?-4.0:x; + //return sigmoid(x); + return sig_table[(uint32_t)((x+4.0)*8*128*1024)]; +}; +double init_sig_table() +{ + sig_table = (double*)malloc((64*128*1024+1)*sizeof(double)); + for(int i=0;i<64*128*1024;++i) + { + sig_table[i] = sigmoid((i/(1048576.0)-4.0)); + } + sig_table[64*128*1024] = 1; +} +pair corpus[4096]; +void sample() +{ + for(int i=0;i<4096;++i) + { + double x = ((rand()%4096)/4095.0*2) - 1; + double y = x*x; + corpus[i] = make_pair(x,y); + } +} + +int main(int argc, char ** argv) +{ + init_sig_table(); + sample(); + double s = atof(argv[1]); + float d = atof(argv[2]); + + double w1[20]; + double w2[20]; + double h[20]; + double b[20]; + double b2 = 0; + + + double w1_g1[20]; + double w2_g1[20]; + double b_g1[20]; + double b2_g1 = 0; + + double w1_g2[20]; + double w2_g2[20]; + double b_g2[20]; + double b2_g2 = 0; + + double output; + bool start = false; + for(int i=0;i<20;++i) + { + w1[i] = 0.01*(((rand()%4096)/4095.0*2) -1); + w2[i] = 0.01*(((rand()%4096)/4095.0*2) -1); + b[i] = 0.01*(((rand()%4096)/4095.0*2) -1); + } + for(int iter = 0;iter<20000;++iter) + { + double sum_error = 0.0; + + for(int index=0;index<4096;++index) + { + + for(int i = 0;i<20;++i) + { + w1_g1[i] = 0; + w2_g1[i] = 0; + b_g1[i] = 0; + b2_g1 = 0; + } + double x = 0; + for(int mini_batch=0;mini_batch<1;++mini_batch) + { + x = corpus[(index+mini_batch)%4096].first; + + for(int i=0;i<20;++i) + { + h[i] = function_g(w1[i]*x+b[i]); + } + double sum_tmp = 0.0; + for(int i=0;i<20;++i) + { + sum_tmp += w2[i]*h[i]; + } + sum_tmp += b2; + output = function_g(sum_tmp); + if(start) + cout<0?+1:-1; + sum_error += error>0?error:-1*error; + for(int i=0;i<20;++i) + { + w2_g1[i] += sign*h[i]*(output*(1-output)); + } + b2_g1 += sign*(output*(1-output)); + + for(int i=0;i<20;++i) + { + w1_g1[i] += sign*w2[i]*x*((output*(1-output))*h[i]*(1-h[i])); + b_g1[i] += sign*w2[i]*((output*(1-output))*h[i]*(1-h[i])); + } + } + index+=1; + //sample_20_y + double sum_p = 0.0; + { + for(int i = 0;i<20;++i) + { + w1_g2[i] = 0; + w2_g2[i] = 0; + b_g2[i] = 0; + b2_g2 = 0; + } + for(int sample_idx=0;sample_idx<20;++sample_idx) + { + double tmp = ((rand()%4096)/4095.0*2) - 1.0; + double tmp2 = x;//((rand()%4096)/4095.0*2) - 1.0; + double y = tmp*tmp; + for(int i=0;i<20;++i) + { + h[i] = function_g(w1[i]*tmp2+b[i]); + } + double sum_tmp = 0.0; + for(int i=0;i<20;++i) + { + sum_tmp += w2[i]*h[i]; + } + sum_tmp += b2; + output = function_g(sum_tmp); + double errorx = output - y; + double sign = errorx>0?+1:-1; + errorx = errorx>0?errorx:-1*errorx; + double t = e(errorx); + sum_p += t; + for(int i=0;i<20;++i) + { + w2_g2[i] += (t)*sign*h[i]*(output*(1-output)); + } + b2_g2 += (t)*sign*(output*(1-output)); + for(int i=0;i<20;++i) + { + w1_g2[i] += (t)*sign*w2[i]*x*((output*(1-output))*h[i]*(1-h[i])); + b_g2[i] += (t)*sign*w2[i]*((output*(1-output))*h[i]*(1-h[i]));; + } + } + } + + for(int i=0;i<20;++i) + { + w1[i] -= s*(w1_g1[i] - d*w1_g2[i]/sum_p); + w2[i] -= s*(w2_g1[i] - d*w2_g2[i]/sum_p); + b[i] -= s*(b_g1[i] - d*b_g2[i]/sum_p); + } + b2 -= s*(b2_g1 - d*b2_g2/sum_p); + } + cout<0?errorx:-1*errorx; + cout< test.0.1_0 & +nohup ./nll2 0.1 1 > test.0.1_1 & +nohup ./nll2 0.1 0.1 > test.0.1_0.1 & +nohup ./nll2 0.01 0.1 > test.0.01_0.1 & +nohup ./nll2 0.01 0 > test.0.01_0 & +nohup ./nll2 0.01 1 > test.0.01_1 & +nohup ./nll2 0.001 0 > test.0.001_0 & +nohup ./nll2 0.001 1 > test.0.001_1 & +nohup ./nll2 0.001 0.1 > test.0.001_0.1 & + +# cat test.* | sort -k4n | head -2000 | cut -f3 | sort | uniq -c diff --git a/perceptron/lec1.pdf b/perceptron/lec1.pdf new file mode 100644 index 0000000..2501a33 Binary files /dev/null and b/perceptron/lec1.pdf differ diff --git a/perceptron/lec2.pdf b/perceptron/lec2.pdf new file mode 100644 index 0000000..4186997 Binary files /dev/null and b/perceptron/lec2.pdf differ diff --git a/perceptron/makefile b/perceptron/makefile new file mode 100644 index 0000000..ab472d9 --- /dev/null +++ b/perceptron/makefile @@ -0,0 +1,5 @@ + +main: + g++ -o perceptron perceptron.cpp +clean: + rm -rf perceptron diff --git a/perceptron/perceptron.cpp b/perceptron/perceptron.cpp new file mode 100644 index 0000000..140c6fb --- /dev/null +++ b/perceptron/perceptron.cpp @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include + +using namespace std; + +typedef pair direct_t; + +ostream& operator<<(ostream &os , direct_t& p) +{ + cout< 0) + { + correct_number++; + } + else + { + theta = theta + positive_samples[i]; + } + } + for(int i=0;i +#include +#include +#include +using namespace std; + +//typedef int event_vector[4]; +struct event_vector +{ + int m_v[4]; + event_vector() + { + m_v[0] = 0; m_v[1] = 0; m_v[2] = 0; m_v[3] = 0; + } + event_vector(const event_vector& e) + { + m_v[0] = e.m_v[0]; + m_v[1] = e.m_v[1]; + m_v[2] = e.m_v[2]; + m_v[3] = e.m_v[3]; + } + int &operator [] (int idx) + { + return m_v[idx]; + } + +}; +double P_C[2] = {0.5,0.5}; +double P_S_O_C[2]={0.1,0.5}; +double P_R_O_C[2] ={0.8,0.2}; +double P_W_O_S_R[4]={0.99,0.9,0.9,0}; + +bool rdm(double true_probability) +{ + if( ((random()%4096)/4095.0) < true_probability ) + { + return true; + } +} + +event_vector ret; +void Gibbs_Sampling() +{ + ret[1] = 1; + ret[3] = 1; + + if(ret[2] == 1 && ret[1] == 1 && rdm((P_S_O_C[0]*P_R_O_C[0]))) + { + ret[0] = 1; + } + else if( ret[2] == 0 && ret[1] == 1 && rdm((P_S_O_C[0]*(1-P_R_O_C[0])))) + { + ret[0] = 1; + } + /*else if( ret[2] == 1 && rdm((P_S_O_C[1]*P_R_O_C[1]))) + { + ret[0] = 0; + } + else if( ret[2] == 0 && rdm((P_S_O_C[1]*(1-P_R_O_C[1])))) + { + ret[0] = 0; + }*/ + else{ ret[0] = 0;} + + if (ret[0] == 1 && ret[1] == 1 && ret[3] == 1 && rdm(P_R_O_C[0]*P_W_O_S_R[0])) + { + ret[2] = 1; + } + else if(ret[0] == 0 && ret[1] == 1 && ret[3] == 1 && rdm((1-P_R_O_C[0])*P_W_O_S_R[0])) + { + ret[2] = 1; + } + /*else if(ret[0] == 1 && ret[1] == 1 && ret[3] == 1 && rdm(P_R_O_C[1]*P_W_O_S_R[1])) + { + ret[2] = 0; + } + else if(ret[0] == 0 && ret[1] == 1 && ret[3] == 1 && rdm((1-P_R_O_C[1])*P_W_O_S_R[1])) + { + ret[2] = 0; + }*/ + else + { + ret[2] = 0; + } +} + +struct weight_counter +{ + vector< pair > m_v; + double sum; + void print_all(void) + { + for(int i=0;i +#include +#include +#include +using namespace std; + +//typedef int event_vector[4]; +struct event_vector +{ + int m_v[4]; + event_vector() + { + m_v[0] = 0; m_v[1] = 0; m_v[2] = 0; m_v[3] = 0; + } + event_vector(const event_vector& e) + { + m_v[0] = e.m_v[0]; + m_v[1] = e.m_v[1]; + m_v[2] = e.m_v[2]; + m_v[3] = e.m_v[3]; + } + int &operator [] (int idx) + { + return m_v[idx]; + } + +}; +double P_C[2] = {0.5,0.5}; +double P_S_O_C[2]={0.1,0.5}; +double P_R_O_C[2] ={0.8,0.2}; +double P_W_O_S_R[4]={0.99,0.9,0.9,0}; + +bool rdm(double true_probability) +{ + if( ((random()%4096)/4095.0) < true_probability ) + { + return true; + } +} + +event_vector ret; +bool Prior_Sample() +{ + if(rdm(P_C[0])) + { + ret[0] = 1; + } + else + { + ret[0] = 0; + } + + if (ret[0] == 1 && rdm(P_S_O_C[0])) + { + ret[1] = 1; + } + else if (ret[0] == 0 && rdm(P_S_O_C[1])) + { + ret[1] = 1; + } + else + { + ret[1] = 0; + } + if (ret[0] == 1 && rdm(P_R_O_C[0])) + { + ret[2] = 1; + } + else if (ret[0] == 0 && rdm(P_R_O_C[1])) + { + ret[2] = 1; + } + else + { + ret[2] = 0; + } + + if (ret[1] == 1 && ret[2] == 1 && rdm(P_W_O_S_R[0])) + { + ret[3] = 1; + } + else if (ret[1] == 1 && ret[2] == 0 && rdm(P_W_O_S_R[1])) + { + ret[3] = 1; + } + else if (ret[1] == 0 && ret[2] == 1 && rdm(P_W_O_S_R[2])) + { + ret[3] = 1; + } + else if (ret[1] == 0 && ret[2] == 0 && rdm(P_W_O_S_R[3])) + { + ret[3] = 1; + } + else + { + ret[3] = 0; + } + return true; +}; + +double Weighted_Sample() +{ + double w_ret = 1; + if(rdm(P_C[0])) + { + ret[0] = 1; + } + else + { + ret[0] = 0; + } + ret[1] = 1; + if(ret[0] == 1) + w_ret *= 0.1; + else + w_ret *= 0.5; + + if (ret[0] == 1 && rdm(P_R_O_C[0])) + { + ret[2] = 1; + } + else if (ret[0] == 0 && rdm(P_R_O_C[1])) + { + ret[2] = 1; + } + else + { + ret[2] = 0; + } + ret[3] = 1; + if(ret[1] == 1 && ret[2] == 1) + { + w_ret *= 0.99; + } + else if(ret[1] == 1 && ret[2] == 0) + { + w_ret *= 0.9; + } + return w_ret; +} + +struct weight_counter +{ + vector< pair > m_v; + double sum; + void print_all(void) + { + for(int i=0;i +#include +using namespace std; + +typedef int event_vector[4]; + +double P_C[2] = {0.5,0.5}; +double P_S_O_C[2]={0.1,0.5}; +double P_R_O_C[2] ={0.8,0.2}; +double P_W_O_S_R[4]={0.99,0.9,0.9,0}; + +bool rdm(double true_probability) +{ + if( ((random()%4096)/4095.0) < true_probability ) + { + return true; + } +} + +event_vector ret = {0}; +bool Prior_Sample() +{ + if(rdm(P_C[0])) + { + ret[0] = 1; + } + else + { + ret[0] = 0; + } + + if (ret[0] == 1 && rdm(P_S_O_C[0])) + { + ret[1] = 1; + } + else if (ret[0] == 0 && rdm(P_S_O_C[1])) + { + ret[1] = 1; + } + else + { + ret[1] = 0; + } + if (ret[0] == 1 && rdm(P_R_O_C[0])) + { + ret[2] = 1; + } + else if (ret[0] == 0 && rdm(P_R_O_C[1])) + { + ret[2] = 1; + } + else + { + ret[2] = 0; + } + + if (ret[1] == 1 && ret[2] == 1 && rdm(P_W_O_S_R[0])) + { + ret[3] = 1; + } + else if (ret[1] == 1 && ret[2] == 0 && rdm(P_W_O_S_R[1])) + { + ret[3] = 1; + } + else if (ret[1] == 0 && ret[2] == 1 && rdm(P_W_O_S_R[2])) + { + ret[3] = 1; + } + else if (ret[1] == 0 && ret[2] == 0 && rdm(P_W_O_S_R[3])) + { + ret[3] = 1; + } + else + { + ret[3] = 0; + } + return true; +}; +int main(void) +{ + int sum = 0; + srand(time(NULL)); + for(int i=0;i<10000;++i) + { + Prior_Sample(); + cout< +#include +using namespace std; + +typedef int event_vector[4]; + +double P_C[2] = {0.5,0.5}; +double P_S_O_C[2]={0.1,0.5}; +double P_R_O_C[2] ={0.8,0.2}; +double P_W_O_S_R[4]={0.99,0.9,0.9,0}; + +bool rdm(double true_probability) +{ + if( ((random()%4096)/4095.0) < true_probability ) + { + return true; + } +} + +event_vector ret = {0}; +bool Prior_Sample() +{ + if(rdm(P_C[0])) + { + ret[0] = 1; + } + else + { + ret[0] = 0; + } + + if (ret[0] == 1 && rdm(P_S_O_C[0])) + { + ret[1] = 1; + } + else if (ret[0] == 0 && rdm(P_S_O_C[1])) + { + ret[1] = 1; + } + else + { + ret[1] = 0; + } + if (ret[0] == 1 && rdm(P_R_O_C[0])) + { + ret[2] = 1; + } + else if (ret[0] == 0 && rdm(P_R_O_C[1])) + { + ret[2] = 1; + } + else + { + ret[2] = 0; + } + + if (ret[1] == 1 && ret[2] == 1 && rdm(P_W_O_S_R[0])) + { + ret[3] = 1; + } + else if (ret[1] == 1 && ret[2] == 0 && rdm(P_W_O_S_R[1])) + { + ret[3] = 1; + } + else if (ret[1] == 0 && ret[2] == 1 && rdm(P_W_O_S_R[2])) + { + ret[3] = 1; + } + else if (ret[1] == 0 && ret[2] == 0 && rdm(P_W_O_S_R[3])) + { + ret[3] = 1; + } + else + { + ret[3] = 0; + } + return true; +}; +int main(void) +{ + int sum = 0; + srand(time(NULL)); + int N[2] = {0}; + for(int i=0;i<100;++i) + { + Prior_Sample(); + if(ret[1] == 1) //Sprinkler=true + { + if(ret[2] == 1) + { + N[0]++; + } + else + { + N[1]++; + } + //cout< +#include +using namespace std; + +typedef int event_vector[4]; + +double P_C[2] = {0.5,0.5}; +double P_S_O_C[2]={0.1,0.5}; +double P_R_O_C[2] ={0.8,0.2}; +double P_W_O_S_R[4]={0.99,0.9,0.9,0}; + +bool rdm(double true_probability) +{ + if( ((random()%4096)/4095.0) < true_probability ) + { + return true; + } +} + +event_vector ret = {0}; +bool M() // can this model generate a event (t,f,t,t) +{ + //t,f,t,t + if(rdm(P_C[0])) + { + ret[0] = 1; + } + else + { + return false; + } + + if (ret[0] == 1 && rdm(1-P_S_O_C[0])) + { + ret[1] = 0; + } + else + { + return false; + } + + if (ret[0] == 1 && rdm(P_R_O_C[0])) + { + ret[2] = 1; + } + else + { + return false; + } + + if (ret[0] == 1 && ret[1] == 0 && ret[2] == 1 && rdm(P_W_O_S_R[2])) + { + ret[3] = 1; + } + else + { + return false; + } + return true; +}; +int main(void) +{ + int sum = 0; + srand(time(NULL)); + for(int i=0;i<10000000;++i) + { + if( M()) + { + sum++; + } + } + cout<