diff --git a/grafico.h b/grafico.h index a480725..0c45add 100644 --- a/grafico.h +++ b/grafico.h @@ -2,6 +2,11 @@ #include #include +typedef struct { + double x; + double y; +} Punto; + // Segmento che parte dalle coordinate x,y di "inizio" a quelle di "fine" typedef struct { @@ -13,8 +18,9 @@ void disegna_assi(); void cls(int, int); void disegna_punti(int); void traccia_retta(double, double, int); -int *coordinate(int, int); +int *coordinate(double, double); void stampa_epoca(int); +void traccia_inputs(Punto, int); /* void main() { allegro_init(); @@ -67,6 +73,12 @@ void traccia_retta(double m, double q, int colore) sleep(0.1); } +void traccia_inputs(Punto punto, int colore) { + int *point = coordinate(punto.x, punto.y); + //printf("Ho generato coordinate: %dx%d", point[0], point[1]); + circlefill(screen, point[0], point[1], 1.5, colore); +} + void disegna_assi() { // determino il centro @@ -215,7 +227,7 @@ void disegna_punti(int tipo) } // Mi da le coordinate in pixel dati i punti in ingresso -int *coordinate(int x, int y) +int *coordinate(double x, double y) { // determino il centro int center_x = SCREEN_W / 2; // Coordinata x del centro diff --git a/layer_multi b/layer_multi index a95a7d5..bd2fa6c 100755 Binary files a/layer_multi and b/layer_multi differ diff --git a/layer_multi.c b/layer_multi.c index 6e86f18..1ee801a 100644 --- a/layer_multi.c +++ b/layer_multi.c @@ -13,7 +13,7 @@ int MAX_EPOCHE = 10000; 4: NOR 5: XNOR */ -int tipo = 2; +int tipo = 0; void main() { @@ -77,6 +77,18 @@ void main() Percettrone pout = crea_percettrone(); int colore_blu = makecol(0, 0, 255); + p_ext_1.w1 = 1.332870; + p_ext_1.w2 = -0.628797; + p_ext_1.bias = 0.729138; + + p_ext_2.w1 = 0.459249; + p_ext_2.w2 = 0.394682; + p_ext_2.bias = 0.833102; + + pout.w1 = -0.004388; + pout.w2 = 0.090205; + pout.bias = -0.067931; + // Contatore per fermare il percettrone, se vale 4 significa che ha indovinato tutte e 4 le combinazioni int corrette = 0; @@ -110,7 +122,6 @@ void main() } readkey(); - break; } diff --git a/layer_multi_addestrato b/layer_multi_addestrato new file mode 100755 index 0000000..81a8b07 Binary files /dev/null and b/layer_multi_addestrato differ diff --git a/layer_multi_addestrato.c b/layer_multi_addestrato.c new file mode 100644 index 0000000..d26f079 --- /dev/null +++ b/layer_multi_addestrato.c @@ -0,0 +1,101 @@ +#include +#include "percettrone.h" +#include "grafico.h" + +int MAX_TRY = 10000; + +/* + il tipo indica quali punti vogliamo disegnare nel grafico: + 0: AND + 1: OR + 2: XOR + 3: NAND + 4: NOR + 5: XNOR +*/ +int tipo = 0; + +// Soglia sigmoide +double soglia_funzione_attivazione = 0.5; + +void sleep_ms(int); + +void main() +{ + srand(time(NULL)); + + allegro_init(); + install_keyboard(); + set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); + cls(tipo, 1); + + int colore_rosso = makecol(255, 0, 0); + int colore_verde = makecol(0, 255, 0); + + Percettrone p_ext_1; + Percettrone p_ext_2; + Percettrone pout; + + //AND + /* p_ext_1.w1 = 1.332870; + p_ext_1.w2 = -0.628797; + p_ext_1.bias = 0.729138; + + p_ext_2.w1 = 0.459249; + p_ext_2.w2 = 0.394682; + p_ext_2.bias = 0.833102; + + pout.w1 = -0.004388; + pout.w2 = 0.090205; + pout.bias = -0.067931; */ + + //XOR + p_ext_1.w1 = 1.423181; + p_ext_1.w2 = -1.798159; + p_ext_1.bias = -1.757881; + + p_ext_2.w1 = -0.828619; + p_ext_2.w2 = 0.410466; + p_ext_2.bias = -0.087439; + + pout.w1 = 0.145840; + pout.w2 = 0.194722; + pout.bias = -0.114644; + + for (int i = 0; i < MAX_TRY; i++) + { + //printf("\nCiclo %d\n", i); + + Punto input; + input.x = randomico(); + input.y = randomico(); + + + double y_ext_1 = funzione_sigmoide(p_ext_1, input.x, input.y); + double y_ext_2 = funzione_sigmoide(p_ext_2, input.x, input.y); + double yout = funzione_sigmoide(pout, y_ext_1, y_ext_2); + + int previsione = -1; + + if (yout >= soglia_funzione_attivazione) { + previsione = 1; + } + else { + previsione = 0; + } + + /* Punto output; + output.x = y_ext_1; + output.y = y_ext_2; */ + + printf("Inputs: %f:%f -> previsione: %d\n", input.x, input.y, previsione); + + if (previsione == 1) + traccia_inputs(input, colore_verde); + else + traccia_inputs(input, colore_rosso); + } + + sleep_ms(10); + readkey(); +} \ No newline at end of file diff --git a/layer_singolo b/layer_singolo index 5379335..2da4287 100755 Binary files a/layer_singolo and b/layer_singolo differ diff --git a/layer_singolo.c b/layer_singolo.c index 4f3ad15..085e13c 100644 --- a/layer_singolo.c +++ b/layer_singolo.c @@ -12,7 +12,7 @@ int MAX_EPOCHE = 10000; 4: NOR 5: XNOR */ -int tipo = 1; +int tipo = 0; void stampa_risultati(Percettrone); void sleep_ms(int); @@ -77,6 +77,7 @@ void main() int corrette = 0; Retta *rette = (Retta *)malloc(sizeof(Retta)); + Punto *punti = (Punto *)malloc(sizeof(Punto)); // Soglia sigmoide double soglia_funzione_attivazione = 0.5; @@ -88,6 +89,8 @@ void main() printf("\nEpoche necessarie: %d\n", i); stampa_risultati_layer_singolo(p); + //cls(tipo, 0); + for (int z = 0; z < i; z++) { cls(tipo, 0); @@ -106,7 +109,6 @@ void main() for (int j = 0; j < 4; j++) { - double y = funzione_sigmoide(p, x[j][0], x[j][1]); double errore = -(output[j] - y); diff --git a/layer_singolo_addestrato b/layer_singolo_addestrato new file mode 100755 index 0000000..a1046ef Binary files /dev/null and b/layer_singolo_addestrato differ diff --git a/layer_singolo_addestrato.c b/layer_singolo_addestrato.c new file mode 100644 index 0000000..8309d04 --- /dev/null +++ b/layer_singolo_addestrato.c @@ -0,0 +1,68 @@ +#include +#include "percettrone.h" +#include "grafico.h" + +int MAX_CICLI = 10000; +/* + il tipo indica quali punti vogliamo disegnare nel grafico: + 0: AND + 1: OR + 2: XOR + 3: NAND + 4: NOR + 5: XNOR +*/ +int tipo = 1; + +// Soglia sigmoide +double soglia_funzione_attivazione = 0.5; + +void sleep_ms(int); + +void main() +{ + srand(time(NULL)); + + allegro_init(); + install_keyboard(); + set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); + cls(tipo, 0); + + Percettrone p; + p.w1 = 0.042967; + p.w2 = 0.029020; + p.bias = -0.019373; + + int colore_rosso = makecol(255, 0, 0); + int colore_verde = makecol(0, 255, 0); + + for (int i = 0; i < MAX_CICLI; i++) + { + //printf("\nCiclo: %d\n", i); + + Punto input; + input.x = randomico_positivo(); + input.y = randomico_positivo(); + + double y = funzione_sigmoide(p,input.x, input.y); + + int previsione = -1; + + if (y >= soglia_funzione_attivazione) { + previsione = 1; + } + else { + previsione = 0; + } + + printf("Inputs: %f:%f -> previsione: %d\n", input.x, input.y, previsione); + + if(previsione == 1) + traccia_inputs(input, colore_verde); + else + traccia_inputs(input, colore_rosso); + } + + sleep_ms(100); + readkey(); +} \ No newline at end of file diff --git a/percettrone.h b/percettrone.h index 0adac87..9a0e24c 100644 --- a/percettrone.h +++ b/percettrone.h @@ -21,6 +21,7 @@ int x[4][2] = { // Dichiarazione dei metodi Percettrone crea_percettrone(); double randomico(); +double randomico_positivo(); double funzione_sigmoide(Percettrone, double, double); void correggi_pesi(Percettrone*, double, double, double); @@ -46,6 +47,11 @@ double randomico() { return ((double)(rand() % 101 * 0.01 * 2 ) -1); } +double randomico_positivo() { + // Genero numeri nell'intervallo [0,1] + return ((double)(rand() % 101 * 0.01)); +} + double funzione_sigmoide(Percettrone p, double x1, double x2) { double funzione = (x1 * p.w1) + (x2 * p.w2) + p.bias; double potenza_e = exp(-funzione); @@ -85,10 +91,16 @@ void stampa_risultati_layer_singolo(Percettrone p) { } void stampa_risultati_layer_multi(Percettrone p1, Percettrone p2, Percettrone pout) { - printf("\nPercettrone 1:\n"); - printf("\t W1: %f, W2: %f, bias: %f\n", p1.w1, p1.w2, p1.bias); - printf("Percettrone 2:\n"); - printf("\t W1: %f, W2: %f, bias: %f\n", p2.w1, p2.w2, p2.bias); - printf("Percettrone OUT:\n"); - printf("\t W1: %f, W2: %f, bias: %f\n", pout.w1, pout.w2, pout.bias); + printf("\nPercettroni 1, 2 e out:\n"); + printf("\np_ext_1.w1 = %f;", p1.w1); + printf("\np_ext_1.w2 = %f;", p1.w2); + printf("\np_ext_1.bias = %f;\n", p1.bias); + + printf("\np_ext_2.w1 = %f;", p2.w1); + printf("\np_ext_2.w2 = %f;", p2.w2); + printf("\np_ext_2.bias = %f;\n", p2.bias); + + printf("\npout.w1 = %f;", pout.w1); + printf("\npout.w2 = %f;", pout.w2); + printf("\npout.bias = %f;\n", pout.bias); } \ No newline at end of file diff --git a/tempCodeRunnerFile.c b/tempCodeRunnerFile.c new file mode 100644 index 0000000..b930919 --- /dev/null +++ b/tempCodeRunnerFile.c @@ -0,0 +1 @@ +_positivo \ No newline at end of file