messo codice che segna i punti invece delle rette
This commit is contained in:
16
grafico.h
16
grafico.h
@@ -2,6 +2,11 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
} Punto;
|
||||||
|
|
||||||
// Segmento che parte dalle coordinate x,y di "inizio" a quelle di "fine"
|
// Segmento che parte dalle coordinate x,y di "inizio" a quelle di "fine"
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
@@ -13,8 +18,9 @@ void disegna_assi();
|
|||||||
void cls(int, int);
|
void cls(int, int);
|
||||||
void disegna_punti(int);
|
void disegna_punti(int);
|
||||||
void traccia_retta(double, double, int);
|
void traccia_retta(double, double, int);
|
||||||
int *coordinate(int, int);
|
int *coordinate(double, double);
|
||||||
void stampa_epoca(int);
|
void stampa_epoca(int);
|
||||||
|
void traccia_inputs(Punto, int);
|
||||||
|
|
||||||
/* void main() {
|
/* void main() {
|
||||||
allegro_init();
|
allegro_init();
|
||||||
@@ -67,6 +73,12 @@ void traccia_retta(double m, double q, int colore)
|
|||||||
sleep(0.1);
|
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()
|
void disegna_assi()
|
||||||
{
|
{
|
||||||
// determino il centro
|
// determino il centro
|
||||||
@@ -215,7 +227,7 @@ void disegna_punti(int tipo)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Mi da le coordinate in pixel dati i punti in ingresso
|
// 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
|
// determino il centro
|
||||||
int center_x = SCREEN_W / 2; // Coordinata x del centro
|
int center_x = SCREEN_W / 2; // Coordinata x del centro
|
||||||
|
|||||||
BIN
layer_multi
BIN
layer_multi
Binary file not shown.
@@ -13,7 +13,7 @@ int MAX_EPOCHE = 10000;
|
|||||||
4: NOR
|
4: NOR
|
||||||
5: XNOR
|
5: XNOR
|
||||||
*/
|
*/
|
||||||
int tipo = 2;
|
int tipo = 0;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
@@ -77,6 +77,18 @@ void main()
|
|||||||
Percettrone pout = crea_percettrone();
|
Percettrone pout = crea_percettrone();
|
||||||
int colore_blu = makecol(0, 0, 255);
|
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
|
// Contatore per fermare il percettrone, se vale 4 significa che ha indovinato tutte e 4 le combinazioni
|
||||||
int corrette = 0;
|
int corrette = 0;
|
||||||
|
|
||||||
@@ -110,7 +122,6 @@ void main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
readkey();
|
readkey();
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
BIN
layer_multi_addestrato
Executable file
BIN
layer_multi_addestrato
Executable file
Binary file not shown.
101
layer_multi_addestrato.c
Normal file
101
layer_multi_addestrato.c
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#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();
|
||||||
|
}
|
||||||
BIN
layer_singolo
BIN
layer_singolo
Binary file not shown.
@@ -12,7 +12,7 @@ int MAX_EPOCHE = 10000;
|
|||||||
4: NOR
|
4: NOR
|
||||||
5: XNOR
|
5: XNOR
|
||||||
*/
|
*/
|
||||||
int tipo = 1;
|
int tipo = 0;
|
||||||
|
|
||||||
void stampa_risultati(Percettrone);
|
void stampa_risultati(Percettrone);
|
||||||
void sleep_ms(int);
|
void sleep_ms(int);
|
||||||
@@ -77,6 +77,7 @@ void main()
|
|||||||
int corrette = 0;
|
int corrette = 0;
|
||||||
|
|
||||||
Retta *rette = (Retta *)malloc(sizeof(Retta));
|
Retta *rette = (Retta *)malloc(sizeof(Retta));
|
||||||
|
Punto *punti = (Punto *)malloc(sizeof(Punto));
|
||||||
|
|
||||||
// Soglia sigmoide
|
// Soglia sigmoide
|
||||||
double soglia_funzione_attivazione = 0.5;
|
double soglia_funzione_attivazione = 0.5;
|
||||||
@@ -88,6 +89,8 @@ void main()
|
|||||||
printf("\nEpoche necessarie: %d\n", i);
|
printf("\nEpoche necessarie: %d\n", i);
|
||||||
stampa_risultati_layer_singolo(p);
|
stampa_risultati_layer_singolo(p);
|
||||||
|
|
||||||
|
//cls(tipo, 0);
|
||||||
|
|
||||||
for (int z = 0; z < i; z++)
|
for (int z = 0; z < i; z++)
|
||||||
{
|
{
|
||||||
cls(tipo, 0);
|
cls(tipo, 0);
|
||||||
@@ -106,7 +109,6 @@ void main()
|
|||||||
|
|
||||||
for (int j = 0; j < 4; j++)
|
for (int j = 0; j < 4; j++)
|
||||||
{
|
{
|
||||||
|
|
||||||
double y = funzione_sigmoide(p, x[j][0], x[j][1]);
|
double y = funzione_sigmoide(p, x[j][0], x[j][1]);
|
||||||
|
|
||||||
double errore = -(output[j] - y);
|
double errore = -(output[j] - y);
|
||||||
|
|||||||
BIN
layer_singolo_addestrato
Executable file
BIN
layer_singolo_addestrato
Executable file
Binary file not shown.
68
layer_singolo_addestrato.c
Normal file
68
layer_singolo_addestrato.c
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#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();
|
||||||
|
}
|
||||||
@@ -21,6 +21,7 @@ int x[4][2] = {
|
|||||||
// Dichiarazione dei metodi
|
// Dichiarazione dei metodi
|
||||||
Percettrone crea_percettrone();
|
Percettrone crea_percettrone();
|
||||||
double randomico();
|
double randomico();
|
||||||
|
double randomico_positivo();
|
||||||
double funzione_sigmoide(Percettrone, double, double);
|
double funzione_sigmoide(Percettrone, double, double);
|
||||||
void correggi_pesi(Percettrone*, double, double, double);
|
void correggi_pesi(Percettrone*, double, double, double);
|
||||||
|
|
||||||
@@ -46,6 +47,11 @@ double randomico() {
|
|||||||
return ((double)(rand() % 101 * 0.01 * 2 ) -1);
|
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_sigmoide(Percettrone p, double x1, double x2) {
|
||||||
double funzione = (x1 * p.w1) + (x2 * p.w2) + p.bias;
|
double funzione = (x1 * p.w1) + (x2 * p.w2) + p.bias;
|
||||||
double potenza_e = exp(-funzione);
|
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) {
|
void stampa_risultati_layer_multi(Percettrone p1, Percettrone p2, Percettrone pout) {
|
||||||
printf("\nPercettrone 1:\n");
|
printf("\nPercettroni 1, 2 e out:\n");
|
||||||
printf("\t W1: %f, W2: %f, bias: %f\n", p1.w1, p1.w2, p1.bias);
|
printf("\np_ext_1.w1 = %f;", p1.w1);
|
||||||
printf("Percettrone 2:\n");
|
printf("\np_ext_1.w2 = %f;", p1.w2);
|
||||||
printf("\t W1: %f, W2: %f, bias: %f\n", p2.w1, p2.w2, p2.bias);
|
printf("\np_ext_1.bias = %f;\n", p1.bias);
|
||||||
printf("Percettrone OUT:\n");
|
|
||||||
printf("\t W1: %f, W2: %f, bias: %f\n", pout.w1, pout.w2, pout.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);
|
||||||
}
|
}
|
||||||
1
tempCodeRunnerFile.c
Normal file
1
tempCodeRunnerFile.c
Normal file
@@ -0,0 +1 @@
|
|||||||
|
_positivo
|
||||||
Reference in New Issue
Block a user