-
Notifications
You must be signed in to change notification settings - Fork 0
/
CThreadCA.cpp
377 lines (326 loc) · 10.9 KB
/
CThreadCA.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "CThreadCA.h"
#include "myCA.h"
#include "stdio.h"
#define MAX_NUM_CENTER 14
FILE *file = fopen("result.txt","w");
FILE *res = fopen("disp500.txt","a");
FILE *blocks = fopen("blocks.txt","a");
double eps=1e-4;
int ckw=1; //ïåðåìåííàß äëß óêàçàíèß òèïà îáõîäà â áëîêå: 1 - ckw, 0 - anticlockwise
unsigned long int nStep;
int defectOn=10; //0 - áåç äåôåêòà, 1 - åñòü ñëó÷àéíûé äåôåêò
//ãåíåðèðóåì êîîðäèíàòû
void CThreadCA::genSquare(float x, float y,Coordinates_t *ptr){
Coordinate_t cord;
cord.x = x;
cord.y = y+10.0f;
cord.z = 0;
ptr->push_back(cord);
cord.x = x + 8.66f;
cord.y = y + 5.0f;
ptr->push_back(cord);
cord.x = x + 8.66f;
cord.y = y - 5.0f;
ptr->push_back(cord);
cord.x = x;
cord.y = y - 10.0f;
ptr->push_back(cord);
cord.x = x - 8.66f;
cord.y = y - 5.0f;
ptr->push_back(cord);
cord.x = x - 8.66f;
cord.y = y + 5.0f;
ptr->push_back(cord);
}
//ãåíåðèðóåì ñåòêó
void CThreadCA::genGrid(){
Coordinates_t listCoordinates;
Faces_t listFaces;
listFigure.clear();
float cur_x = 0;
float cur_y = 0;
listCoordinates.clear();
listFaces.clear();
for(unsigned long int i=0; i<l; ++i){
for(unsigned long int j=0; j<w; ++j){
listCoordinates.clear();
genSquare(cur_x,cur_y,&listCoordinates);
listFaces.push_back(listCoordinates);
listFigure.push_back(listFaces);
listFaces.clear();
cur_x += 20;
}
if(i%2){
cur_x = 0;
cur_y += 20;
}
else{
cur_x = 10;
cur_y += 20;
}
}
}
void CThreadCA::init(){
nStep=0;
long ltime = time(NULL);
srand(ltime);
//ðàçìåðíîñòü ïîëÿ l * w * h
l = 50;
w = 50;
h = 1;
hasDefect=0; // 0 - no defects anywhere
complBlocks=0;
uncomplBlocks=0;
//ñîçäàåì ÊÀ myCA
ptrCA = new myCa(0,l,w,h);
uint id = 0;
//íàõîäèì îáùåå êîëè÷åñòâî êëåòîê
numCA = l*w*h;
//î÷èùàåì ÊÀ1, è ÊÀ2 êîòîðûå ïîòîì áóäóò îáìåíèâàòüñÿ
listCA1.clear();
listCA2.clear();
int def_num = rand()%(w*l*h);
int def_pos[]={2233,2205,2356,70,103,2452,1098,1402,495,1596};
//int def_pos[defectOn];
/* for(int i=0;i<defectOn;i++) {
def_pos[i]=rand()%(w*l*h);
while (def_pos[i]>=(w-10)*(l/2) && def_pos[i]<=(w+10)*l/2 && def_pos[i]%w>=(l/2-10/2) && def_pos[i]%w<(l/2+10/2)) {
def_pos[i]=rand()%(w*l*h);
}
}
*/
while (def_num>=(w-10)*(l/2) && def_num<=(w+10)*l/2 && def_num%w>=(l/2-10/2) && def_num%w<(l/2+10/2)) {
def_num=rand()%(w*l*h);
}
for(unsigned long int i=0; i<numCA; ++i){
//ñîçäàåì äâà àâòîìàòà è èíèöèàëèçèðóåì èõ
listCA1.push_back(new myCa(id,l,w,h));
listCA1.at(id)->init(id,def_pos,defectOn);
listCA2.push_back(new myCa(id,l,w,h));
listCA2.at(id)->init(id, def_pos,defectOn);
++id;
}
//îáîçíà÷àåì âûõîäíîé àâòîìàò
ptrOutputListCA = &listCA1;
//ãåíåðèðóåì ñåòêó
genGrid();
}
//äîáàâëÿåì id
bool CThreadCA::addId(CListInt *ptr,signed long int x, signed long int y){
if((x>=0)&&(x<l)){
if((y>=0)&&(y<w)){
ptr->push_back(y*l + x);
return 1;
} else return 0;
} else return 0;
}
signed long int CThreadCA::abs(signed long int x){
if(x<0){
return -x;
}
else{
return x;
}
}
//ïîëó÷èòü ñïèñîê id
CListInt CThreadCA::getId(signed long int x, signed long int y){
CListInt listId;
listId.clear();
if(ckw==1) { //åñëè îáõîä âíóòðè áëîêîâ ïî ÷àñ ñòð
if( ((y)/2+nStep)%2 == 0){
addId(&listId,x,y);
addId(&listId,x-1,y);
addId(&listId,x,y+1);
addId(&listId,x,y-1);
}
else{
addId(&listId,x,y);
addId(&listId,x+1,y);
addId(&listId,x-1,y-1);
addId(&listId,x-1,y+1);
}
} else if(ckw==0) { //åñëè îáõîä âíóòðè áëîêîâ ïðîòèâ ÷àñ ñòð
if( ((y)/2+nStep)%2 == 0){
addId(&listId,x,y);
addId(&listId,x-1,y);
addId(&listId,x,y-1);
addId(&listId,x,y+1);
}
else{
addId(&listId,x,y);
addId(&listId,x+1,y);
addId(&listId,x-1,y+1);
addId(&listId,x-1,y-1);
}
}
return listId; //return list of neighbors coordinates
}
bool CThreadCA::run(){
++nStep;
CListVariety listVariety;
CListInt listInt;
//î÷èùàåì ìíîæåñòâî
listVariety.clear();
int centergo=0; //ïåðåìåííàÿ äëÿ óêàçàíèÿ âåêòîðà ñìåùåíèÿ öåíòðîâ âñåõ áëîêîâ íà òåêóùåì øàãå: 0 áåç ñìåùåíèÿ
ptrCurListCA = &listCA2;
ptrNextListCA = &listCA1;
/*
if(nStep%2==0) { //even step ÷åòíûé øàã
// centergo=rand()%6; //ñìåùåíèå öåíòðà â (0,1,2,3,4,5,6)
//or
//centergo=1; //ñìåùåíèå öåíòðîâ â ïåðâîì íàïðàâëåíèè
//or
// centergo=2; //âî âòîðîì
//or
// centergo=3; //â òðåòüåì
//or
// centergo=4; //â ÷åòâåðòîì
//or
// centergo=5; //â ï\òîì
//or
// centergo=6; //â øåñòîì
//or
// centergo=rand()%6+1; //ñìåùåíèå öåíòðà â (1,2,3,4,5,6)
} else if(nStep%2!=0) { //íå÷åòíûé øàã odd step
centergo=0; //áåç ñìåùåíèÿ öåíòðà
}
*/
centergo=rand()%6+1; //ñìåùåíèå öåíòðà â (1,2,3,4,5,6)
/*****************************************************************************************************************************************
Èçíà÷àëüíî êàæäàÿ èç ÿ÷ååê öèêëà (+2 ïî èêñó, +2 ïî èãðåêó íà êàæäîì øàãå) - öåíòð ó-áëîêà.
 çàâèñèìîñòè îò çíà÷åíèŸ centergo, ìû ñìåùàåì öåíòð êàæäîãî áëîêà â îäíó èç ïåðèôåðèéíûõ Ÿ÷ååê èçíà÷àëüíîãî áëîêà.
Èëè íå ñìåùàåì, îñòàâëÿÿ öåíòð öåíòðîì, êîãäà îíà ðàâíà 0 (íà íå÷åòíîì øàãå âñåãäà òàê).
centergo ìåíÿåòñÿ êàæäûé øàã, â çàâèñèìîñòè îò åãî ÷åòíîñòè.
Íà êàæäîì øàãå áëîêè ÷åðåäóþòñÿ è ò.î. îíè íàêëàäûâàþòñÿ íà ñîñåäíèõ øàãàõ.
ckw - ïåðåìåííàÿ, ïîêàçûâàþùàÿ òèï îáõîäà áëîêà.  çàâèñèìîñòè îò íàïðàâëåíèÿ îáõîäà è ÷åòíîñòè/íå÷åòíîñòè øàãà è ñòðîêè ìû è öåíòð ñìåùàåì.
********************************************************************************************************************************************/
//ïðîõîäèì ïî âñåé øèðèíå, âêëþ÷àÿ ìíèìûå òî÷êè çà è ïîñëå
for(int y=-2;(int)y<(int(w+2));y+=2){ // i = -2:2:w+2
for(int x=-2;(int)x<(int)(l+2);x+=2){ //while x < l+2
if(centergo==0) {
listInt = getId(x,y); //list of neighbors coordinates, add only those that belong to field
} else if(centergo==1) {
listInt=getId(x+1,y+1);
} else if(centergo==2) {
listInt=getId(x+1,y);
} else if(centergo==3) {
listInt=getId(x+1,y-1);
} else if(centergo==4) {
listInt=getId(x-1,y-1);
} else if(centergo==5) {
listInt=getId(x-1,y);
} else if(centergo==6) {
listInt=getId(x-1,y+1);
}
listVariety.push_back(listInt); //push this list to listVariety
listInt.clear();
}
}
for(unsigned long int i=0; i<listVariety.size(); ++i){ //for all cells
listInt = listVariety.at(i); //take the neighbors list for current cell
for(unsigned long int j=0; j<listInt.size(); ++j){ //go around all its neighbors
unsigned long int n = listInt.at(j); //n is neighbor id
(ptrNextListCA->at(n)->copy((ptrCurListCA->at(n)->localRule(ptrCA,&listInt,ptrCurListCA,nStep)))); //the cell at next step
//is the current cell under localRule (pointer to CA, & to neighbors list for current original cell, ptr to cur list of CAs, step )
}
int rotationOn=rand()%1; //rotationOn is 0 or 1
//int rotationOn=0; // 1 - ïî ÷àñîâîé - ckw clockwise //0 - ïðîòèâ ÷àñîâîé - anticlockwise
if(rotationOn==1) {
// ïî ÷àñîâîé
double prev_state=0.0, next_state=0.0;
unsigned long int num=0;
for(unsigned long int j=0; j<listInt.size(); ++j){ //go around all original cell neighbors
num = listInt.at(j); //num is neighbor id
myCa* nCA_0 = dynamic_cast<myCa *>(ptrCurListCA->at(num));
if(nCA_0!=0 && nCA_0->state!=7)
{
prev_state=nCA_0->state;
j++;
if(j<listInt.size()) {
num=listInt.at(j); //n is next neighbour id
myCa *nCA_1 = dynamic_cast<myCa *>(ptrCurListCA->at(num));
if(nCA_1!=0 && nCA_1->state!=7)
{
next_state = nCA_1->state;
nCA_1->state=prev_state;
prev_state=next_state;
} else if (nCA_1->state==7 || nCA_1==0) {
break;
}
nCA_0->state=prev_state;
}
}
}
} else if(rotationOn==0)
{
double prev_state=0.0, next_state=0.0;
long int num=listInt.size()-1;
if(num>0) {
for(long int j=listInt.size()-1;j>=0; --j){ //go around all original cell neighbors
num = listInt.at(j); //num is neighbor id
myCa* nCA_0 = dynamic_cast<myCa *>(ptrCurListCA->at(num));
if(nCA_0!=0 && nCA_0->state!=7)
{
prev_state=nCA_0->state;
j--;
if(j>=0) {
num=listInt.at(j); //n is next neighbor id
myCa *nCA_1 = dynamic_cast<myCa *>(ptrCurListCA->at(num));
if(nCA_1!=0 && nCA_1->state!=7)
{
next_state = nCA_1->state;
nCA_1->state=prev_state;
prev_state=next_state;
} else if (nCA_1->state==7 || nCA_1==0) {
break;
}
nCA_0->state=prev_state;
}
}
}
}
}
}
double sum = 0;
double dispertion = 0.0;
if(defectOn==0) {
double state_on_ca=(double)600/(w*l*h); //6*10*10 / 2500
for(unsigned int i=0;i<ptrCurListCA->size();++i){ //go around all current CAs
if((((myCa *)(ptrCurListCA->at(i)))->state)!=7) { //if state of current cell is not 7
sum+=((myCa *)(ptrCurListCA->at(i)))->state; //increase the sum by cell's state
dispertion+=pow((double)((myCa *)(ptrCurListCA->at(i)))->state-state_on_ca,2); // dispertion increment is square of (state-0.24) 0.24 - average state 600/2500
//fprintf(file,"%f ",((myCa *)(ptrCurListCA->at(i)))->state);
}
}
dispertion=pow(dispertion/(l*w*h),0.5); //dispertion is square root of disp/(w*h*l)
fprintf(file,"%e\n",dispertion); //íà êàæäîì øàãå çàïèñûâàåì äèñïåðñèþ
fflush(file);
} else if(defectOn!=0) {
double state_on_ca=(double)600/(l*w*h-defectOn); //6*10*10-1 l*w*h minus 1 defect
for(unsigned int i=0;i<ptrCurListCA->size();++i){ //go around all current CAs
if((((myCa *)(ptrCurListCA->at(i)))->state)!=7) { //if state of current cell is not 7
sum+=((myCa *)(ptrCurListCA->at(i)))->state; //increase the sum by cell's state
dispertion+=pow((double)((myCa *)(ptrCurListCA->at(i)))->state-state_on_ca,2); // dispertion increment is square of (state-0.24) 0.24 - average state 600/2500
//fprintf(file,"%f ",((myCa *)(ptrCurListCA->at(i)))->state);
}
}
dispertion=pow(dispertion/(l*w*h-defectOn),0.5); //dispertion is square root of disp/2500
fprintf(file,"%e\n",dispertion); //íà êàæäîì øàãå çàïèñûâàåì äèñïåðñèþ
fflush(file);
}
// if((dispertion<1.05*eps)&&(dispertion>0.95*eps))
if(dispertion<eps)
{
fprintf(res,"%d\n",nStep);
fflush(res);
return false;
}
++step;
complBlocks=0;
uncomplBlocks=0;
//ptrOutputListCA = ptrNextListCA;
return true;
}
unsigned char CThreadCA::getNumModeVisualisation(){
return 2;
}