summaryrefslogtreecommitdiff
path: root/perceptron.c
blob: 0d0d35a631e69b50f6fdf8ece8a7ff6c3c58de78 (plain)
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
#include <stdio.h>

// perceptron to decide if a person is friend or foe
// values range from 0 to 10

struct weights {
    float nice;
    float intelligent;
    float funny;
    float based;
};

struct person {
    int nice;
    int intelligent;
    int funny;
    int based;
    char name[64];
};

int perceptron(struct weights *weights, struct person *person, float activation_value);

int main(void)
{
    struct person tim = {8, 8, 6, 10, "Tim"};
    struct person lara = {3, 3, 4, 1, "Lara"};
    struct person finja = {10, 6, 7, 8, "Finja"};
    struct person marlon = {8, 6, 6, 5, "Marlon"};

    struct weights personal_weights = {0.7, 0.5, 0.7, 1.5};
    struct person perceptron_person = lara;
    int decision;

    decision = perceptron(&personal_weights, &perceptron_person, 0.6);
    if (decision) {
        printf("%s is your friend\n", perceptron_person.name);
    } else {
        printf("%s is your foe\n", perceptron_person.name);
    }
}

int perceptron(struct weights *weights, struct person *person, float activation_value)
{
    float nice_value, intelligent_value, funny_value, based_value, general_value;
    
    nice_value = weights->nice / 10 * person->nice;
    intelligent_value = weights->intelligent / 10 * person->intelligent;
    funny_value = weights->funny / 10 * person->funny;
    based_value = weights->based / 10 * person->based;

    general_value = (nice_value + intelligent_value + funny_value + based_value) / 4;
    return (general_value > activation_value) ? 1 : 0;
}