-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
81 lines (63 loc) · 1.65 KB
/
main.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
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class Ai {
private:
double lr;
double bias;
vector<double> weights;
public:
Ai(){
srand(time(0));
weights = {
1.0*rand()/RAND_MAX,
1.0*rand()/RAND_MAX,
1.0*rand()/RAND_MAX
};
lr = 1;
bias = 1;
}
void learn(double input1, double input2, double output){
cout << weights[0] << " " << weights[1] << " " << weights[2] << "\n";
double outputP = input1*weights[0]+input2*weights[1]+bias*weights[2];
if (outputP > 0){
outputP = 1;
} else {
outputP = 0;
}
double error = output - outputP;
weights[0] = weights[0] + error * input1 * lr;
weights[1] = weights[1] + error * input2 * lr;
weights[2] = weights[2] + error * bias * lr;
}
double solve(double input1, double input2){
return input1*weights[0] + input2*weights[1] + bias*weights[2];
}
};
int main(int argc, char* argv[]){
Ai ai{};
for (int i = 0; i < 50; i++){
ai.learn(1.0, 1.0, 1.0);
ai.learn(1.0, 0.0, 1.0);
ai.learn(0.0, 1.0, 1.0);
ai.learn(0.0, 0.0, 0.0);
}
int f = 0;
while (f < 10){
int x, y;
cout << "Type x:\n";
cin >> x;
cout << "Type y:\n";
cin >> y;
double outputP = ai.solve(x, y);
cout << "temp result is: " << outputP << "\n";
if (outputP > 0){
outputP = 1;
} else {
outputP = 0;
}
cout << "result is: " << outputP << "\n\n";
f++;
}
}