-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAND OR XOR.cpp
More file actions
115 lines (107 loc) · 2.7 KB
/
AND OR XOR.cpp
File metadata and controls
115 lines (107 loc) · 2.7 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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;
const double m = 0.5;
const double e = 2.71828;
class SimpleGate {
public:
double weight[5];
string type;
double input[4][2] = { { 0.0, 0.0 },{ 1.0, 0.0 },{ 0.0, 1.0 },{ 1.0, 1.0 } };
double exOutput[4] = { 0.0, 0.0, 0.0, 1.0 };
double output[4];
SimpleGate(string type) {
srand(time(NULL));
for (int i = 0; i < 5; i++) {
weight[i] = (rand() % 110 - 50) * 0.001;
}
this->type = type;
if (type == "or") {
exOutput[1] = 1.0;
exOutput[2] = 1.0;
}
if (type == "nand") {
exOutput[0] = 1.0;
exOutput[1] = 1.0;
exOutput[2] = 1.0;
exOutput[3] = 0.0;
}
}
double* calc() {
for (int i = 0; i < 4; i++) {
double sum = weight[0] + weight[1] * input[i][0] + weight[2] * input[i][1];
double ok = 1 / (1 + pow(e, sum * -1));
output[i] = ok;
double err = (ok + 0.001) * (1.001 - ok) * (exOutput[i] - ok);
weight[0] = weight[0] + m * err;
weight[1] = weight[1] + m * err * input[i][0];
weight[2] = weight[2] + m * err * input[i][1];
}
return output;
}
void print() {
cout << "A B " << type << endl;
for (int i = 0; i < 4; i++) {
cout << input[i][0] << " " << input[i][1] << " " << output[i] << endl;
}
}
};
class MultiGate {
public:
double weight[3];
string type;
double input[4][2] = { { 0.0, 0.0 },{ 1.0, 0.0 },{ 0.0, 1.0 },{ 1.0, 1.0 } };
double exOutput[4] = { 0.0, 1.0, 1.0, 0.0 };
double output[4];
MultiGate() {
srand(time(NULL));
for (int i = 0; i < 3; i++) {
weight[i] = (rand() % 110 - 50) * 0.001;
}
this->type = "xor";
}
void calc(int num) {
SimpleGate nand("nand");
SimpleGate orr("or");
for (int j = 0; j < num; j++) {
double* input1 = nand.calc();
double* input2 = orr.calc();
for (int i = 0; i < 4; i++) {
double sum = weight[0] * input1[i] + weight[1] * input2[i] + weight[2];
double ok = 1 / (1 + pow(e, sum * -1));
output[i] = ok;
double err = (ok + 0.001) * (1.001 - ok) * (exOutput[i] - ok);
weight[0] += m * err * input1[i];
weight[1] += m * err * input2[i];
weight[2] += m * err;
}
}
}
void print() {
cout << "A B " << type << endl;
for (int i = 0; i < 4; i++) {
cout << input[i][0] << " " << input[i][1] << " " << output[i] << endl;
}
}
};
int main()
{
cout << "Enter or, and or xor:";
string type;
cin >> type;
if (type == "xor") {
MultiGate network;
network.calc(1000000);
network.print();
}
else if (type == "or" || type == "and") {
SimpleGate network(type);
for (int i = 0; i < 100000; i++) {
network.calc();
}
network.print();
}
return 0;
}