-
Notifications
You must be signed in to change notification settings - Fork 0
/
Source.cpp
159 lines (126 loc) · 4.02 KB
/
Source.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
#include "cell.h"
#include <vector>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
#include <string>
using namespace std;
void printCells(cell* myCell_ptr);
#define INBITITERATIONS 100
#define DELTA 0.5
#define TAUMAX 50
#define TAUINCREMENT 0.1
#define TAUMIN 0.1
//not a great function
void checkOFile(ofstream& outputFile){
//Open and define filesteam at once
if (outputFile.good()){
cout << "outfile opened successfully" << endl;
//outputFile << "writing to my new file!" << endl;
}
else
{
cerr << "outfile not opened successfully. Terminator." << endl;
}
cout << endl;
}
int genBit(){
int bit;
double p0 = 0.5*(1 + DELTA);
double p1 = 0.5*(1 - DELTA);
float randN = ((float)rand() / (float)(RAND_MAX));
if (randN < p0){
bit = 0;
}
else{
bit = 1;
}
return bit;
}
void changeCell(int& bit, const cell (&myCells_ptr)[6], cell const *& currentCell_ptr){
//cout << "changeCell" << endl;
//cout << sizeof(myCells_ptr) << endl;
for (int i = 0; i < sizeof(myCells_ptr) / sizeof(cell); i++){
if ((myCells_ptr + i)->getState() == currentCell_ptr->getState() && (int)(myCells_ptr + i)->getBit() == bit){
currentCell_ptr = myCells_ptr + i;
break;
}
}
}
void demonInteract(cell const *& currentCell_ptr, const double & tau, const double & sojournTimeEdge){
//cout << "in ";
//currentCell_ptr->printInfo();
//cout << "timesteps = " << tau*sojournTimeEdge << endl;
for (double i = 0; i < tau*sojournTimeEdge; i++){
//cout << "i = " << i << endl;
//generate random number, tell thing to go left, right, stay.
double num = ((double)rand() / (double)(RAND_MAX));
//cout << num << endl;
if (num <= 1 - (currentCell_ptr->getPLeft() + currentCell_ptr->getPRight())){
//stay still
}
else if (num <= 1 - currentCell_ptr->getPRight()){
//move left. pStill < num < pLeft + pStill
currentCell_ptr --;
}
else{
//move right. pLeft + PStill < num < 1
currentCell_ptr ++;
}
//currentCell_ptr->printInfo();
//cout << endl;
if (currentCell_ptr->getBit() != 1 && currentCell_ptr->getBit() != 0){
cout << "broken" << endl;
currentCell_ptr->printInfo();
exit(1);
}
}
//cout << "out ";
//currentCell_ptr->printInfo();
}
int main(){
const cell a0('A', 0, 0, 0.1f);
const cell b0('B', 0, 0.1f, 0.1f);
const cell c0('C', 0, 0.1f, 0.1f);
const cell a1('A', 1, 0.1f, 0.1f);
const cell b1('B', 1, 0.1f, 0.1f);
const cell c1('C', 1, 0.1f, 0);
ofstream phiFile("phi.txt");
checkOFile(phiFile);
ofstream tauFile("tau.txt");
checkOFile(tauFile);
const cell myCells_ptr[6]{a0, b0, c0, a1, b1, c1};
cell const * currentCell_ptr = myCells_ptr;
float sojournTime[6];
for (int i = 0; i < 6; i++){
sojournTime[i] = 1 / (myCells_ptr[i].getPLeft() + myCells_ptr[i].getPRight()); //Is this formula correct?
}
srand((unsigned int)time(NULL)); //seed. Only needs to be seeded each time program runs. rand() produces a sequence of randome numbers
//repeat with different TAU (plot the whole graph)
for (double tau = TAUMIN; tau <= TAUMAX; tau += TAUINCREMENT){
cout << "tau = " << tau << endl;
tauFile << tau << endl;
int netRotation = 0; //sum deltaChi
//loop over the input bit stream
for (int iteration = 1; iteration <= INBITITERATIONS; iteration++){
//generate bit with probabilities p0 and p1, input delta, output a bit
int bit = genBit();
//cout << bit << endl;
//change starting cell depending on input bit
changeCell(bit, myCells_ptr, currentCell_ptr);
//loop over timesteps (interaction with demon)
demonInteract(currentCell_ptr, tau, sojournTime[0]);
int deltaChi;
deltaChi = currentCell_ptr->getBit() - bit;
//cout << deltaChi << endl;
netRotation += deltaChi;
}
cout << netRotation << endl;
cout << (float)netRotation / INBITITERATIONS << endl;
cout << "-----" << endl;
phiFile << (float)netRotation / INBITITERATIONS << endl;
}
return 0;
}