Perceprtion

mail@pastecode.io avatar
unknown
c_cpp
6 months ago
2.3 kB
12
Indexable
Never
#include <iostream>
#include<ctime>
#include<cstdlib>
using namespace std;

struct input {

    int x;
    float priority;

};

int randomBin() {

    if (rand() < 16384) {
        return 0;
    }
    return 1;

}

float result, bias;
float learning = 0.1;
int aiResult, demanded;
string aiResultText;
char userInput;
int answers[3];


bool reversChecker(int in0, int in1, int in2) {
    bool c;

    if (in0==answers[0] &&  in1==answers[1] && in2==answers[2]) {
        c = true;
        return true;
    }
    else {
        c = false;
        return false;
    }
}

int main() {
    answers[0] = 1;
    answers[1] = 1;
    answers[2] = 1;
    int z = 0;
    input data[3];
    srand(time(NULL));
    for (int x = 0; x < 3; x++) {
        data[x].priority = 0.3;
    }
    bias = 0.1;

    do {
        z++;

        for (int x = 0; x < 3; x++) {
            data[x].x = randomBin();
        }

        result = (data[0].x * data[0].priority) + (data[1].x * data[1].priority) + (data[2].x * data[2].priority) + bias;

        if (result < 0) {
            aiResult = 0;
            aiResultText = "false";
        }
        else {
            aiResult = 1;
            aiResultText = "true";
        }


        //wypis
        for (int x = 0; x < 3; x++) {
            cout << "input numb " << x << ": " << data[x].x << "\t priority:" << data[x].priority << endl;
        }
        cout << "BIAS: " << bias << endl;
        cout << "Result: " << aiResultText << endl;

        cout << "Correct? y/n: " << "\t";
        if (reversChecker(data[0].x, data[1].x, data[2].x)) {
            cout << "y" << endl;
        }
        else {
            cout << "n" << endl;
        }
        //cin >> userInput;
        cout << endl;
        //userInput == 'n'
        if (!reversChecker(data[0].x, data[1].x, data[2].x)) {

            if (aiResult == 1) {
                demanded = 0;
            }
            else {
                demanded = 1;
            }

            for (int x = 0; x < 3; x++) {
                data[x].priority = data[x].priority + learning * (demanded - result) * data[x].x;
            }

            bias = bias + learning * (demanded - result);

        }
    } while (z<1000);
}