-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtHMM.cpp
More file actions
153 lines (146 loc) · 3.71 KB
/
tHMM.cpp
File metadata and controls
153 lines (146 loc) · 3.71 KB
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
/*
* tHMM.cpp
* HMMBrain
*
* Created by Arend on 9/16/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include "tHMM.h"
//#define feedbackON
tHMMU::tHMMU(){
}
tHMMU::~tHMMU(){
hmm.clear();
sums.clear();
ins.clear();
outs.clear();
posLevelOfFB.clear();
negLevelOfFB.clear();
chosenInPos.clear();
chosenInNeg.clear();
chosenOutPos.clear();
chosenOutNeg.clear();
}
void tHMMU::setup(vector<unsigned char> &genome, int start){
int i,j,k;
ins.clear();
outs.clear();
k=(start+2)%genome.size();
xDim=1+(genome[(k++)%genome.size()]&3);
yDim=1+(genome[(k++)%genome.size()]&3);
posFBNode=genome[(k++)%genome.size()]&(maxNodes-1);
negFBNode=genome[(k++)%genome.size()]&(maxNodes-1);
nrPos=genome[(k++)%genome.size()]&3;
nrNeg=genome[(k++)%genome.size()]&3;
//cout<<"setup "<<(int)genome[start+2]<<" "<<(int)xDim<<" "<<(int)yDim<<endl;
ins.resize(yDim);
outs.resize(xDim);
posLevelOfFB.resize(nrPos);
negLevelOfFB.resize(nrNeg);
for(i=0;i<yDim;i++)
ins[i]=genome[(k+i)%genome.size()]&(maxNodes-1);
for(i=0;i<xDim;i++)
outs[i]=genome[(k+4+i)%genome.size()]&(maxNodes-1);
for(i=0;i<nrPos;i++)
posLevelOfFB[i]=(int)(1+genome[(k+8+i)%genome.size()]);
for(i=0;i<nrNeg;i++)
negLevelOfFB[i]=(int)(1+genome[(k+12+i)%genome.size()]);
chosenInPos.clear();
chosenInNeg.clear();
chosenOutPos.clear();
chosenOutNeg.clear();
k=k+16;
hmm.resize(1<<yDim);
sums.resize(1<<yDim);
for(i=0;i<(1<<yDim);i++){
hmm[i].resize(1<<xDim);
for(j=0;j<(1<<xDim);j++){
// hmm[i][j]=(genome[(k+j+((1<<yDim)*i))%genome.size()]&1)*255;
hmm[i][j]=genome[(k+j+((1<<xDim)*i))%genome.size()];
if(hmm[i][j]==0) hmm[i][j]=1;
sums[i]+=hmm[i][j];
}
}
}
void tHMMU::update(unsigned char *states,unsigned char *newStates){
int I=0;
int i,j,r;
unsigned char mod;
#ifdef feedbackON
if((nrPos!=0)&&(states[posFBNode]==1)){
for(i=0;i<chosenInPos.size();i++){
mod=(unsigned char)(rand()%(int)posLevelOfFB[i]);
if((hmm[chosenInPos[i]][chosenOutPos[i]]+mod)<255){
hmm[chosenInPos[i]][chosenOutPos[i]]+=mod;
sums[chosenInPos[i]]+=mod;
}
}
}
if((nrNeg!=0)&&(states[negFBNode]==1)){
for(i=0;i<chosenInNeg.size();i++){
mod=(unsigned char)(rand()%(int)negLevelOfFB[i]);
if((hmm[chosenInNeg[i]][chosenOutNeg[i]]-mod)>0){
hmm[chosenInNeg[i]][chosenOutNeg[i]]-=mod;
sums[chosenInNeg[i]]-=mod;
}
}
}
#endif
for(i=0;i<ins.size();i++)
I=(I<<1)+((states[ins[i]])&1);
r=rand()%sums[I];
j=0;
// cout<<I<<" "<<(int)hmm.size()<<" "<<(int)hmm[0].size()<<endl;
while(r>hmm[I][j]){
r-=hmm[I][j];
j++;
}
for(i=0;i<outs.size();i++)
newStates[outs[i]]|=(j>>i)&1;
//newStates[outs[i]]=(j>>i)&1;
#ifdef feedbackON
chosenInPos.push_back(I);
chosenInNeg.push_back(I);
chosenOutPos.push_back(j);
chosenOutNeg.push_back(j);
while(chosenInPos.size()>nrPos) chosenInPos.pop_front();
while(chosenOutPos.size()>nrPos) chosenOutPos.pop_front();
while(chosenInNeg.size()>nrNeg) chosenInNeg.pop_front();
while(chosenOutNeg.size()>nrNeg) chosenOutNeg.pop_front();
#endif
}
void tHMMU::show(void){
int i,j;
cout<<"INS: ";
for(i=0;i<ins.size();i++)
cout<<(int)ins[i]<<" ";
cout<<endl;
cout<<"OUTS: ";
for(i=0;i<outs.size();i++)
cout<<(int)outs[i]<<" ";
cout<<endl;
for(i=0;i<hmm.size();i++){
for(j=0;j<hmm[i].size();j++)
cout<<" "<<(double)hmm[i][j]/sums[i];
cout<<endl;
}
cout<<endl;
cout<<"posFB: "<<(int)posFBNode<<" negFB: "<<(int)negFBNode<<endl;
cout<<"posQue:"<<endl;
for(i=0;i<posLevelOfFB.size();i++)
cout<<(int)posLevelOfFB[i]<<" ";
cout<<endl;
cout<<"negQue:"<<endl;
for(i=0;i<negLevelOfFB.size();i++)
cout<<(int)negLevelOfFB[i]<<" ";
cout<<endl;
/*
for(i=0;i<hmm.size();i++){
for(j=0;j<hmm[i].size();j++)
cout<<(int)hmm[i][j]<<" ";
cout<<endl;
}
*/
// cout<<"------"<<endl;
}