Optimized neural network structure source program based on genetic algorithm

xiaoxiao2021-03-06  173

Sender: CoooOLDOG (handsome), letter area: Num_Analysis Title: [Sharing] Optimized Neural Network Structural Source Procedure Based on Genetic Algorithm [Essence Transfer? Send Station: Sun Moonlight (April 08, 2004)

13:56:22 Sunday)

#include #include #include #include #include #include #include "graph.c" # include "operator.c" / * macro definition * / # define POP_SIZE 20 # define S_RATE 0.4 # define M_RATE 0.01 # define I_MAX 5 # define H_MAX 20 # define O_MAX 5 # define P_MAX 100 # define MAX_G_LENGTH 100 # define GX1 360 # define GY1 66 # Define GX2 360 # Define GY2 257 # Define GXR 250 # Define GYR 100 # define GSTEP 2 / * Global Variable * / Unsigned Char Gene [Pop_Size] [MAX_G_LENGTH]; / * Individual genetic gene * / unsigned char i_Unit [i_max ], h_unit [H_MAX], o_unit [O_MAX]; unsigned char p_i_unit [P_MAX] [I_MAX]; unsigned char p_o_unit [P_MAX] [O_MAX]; double fitness [POP_SIZE]; double max_fit, avg_fit; int i_num, h_num, o_num, T_num; int p_num; int g_length;

Long Int Calc_G_Length () {INT I; Long Int Num; Num = 0; for (i = 1; I <= H_NUM; I ) NUM = NUM ​​ I_NUM (I-1); NUM = Num O_Num * (i_num h_num); return (num);}

Void set_network () {Int i, ok; long int work; char choice [2]; setTextStyle (0,0,4); Gprintf (215, 15, 4, 0, "GA-NN"); setColor (9) DISP_HZ24 ("Genetic Optimization Neural Network Structure", 170, 50, 25); Disp_hz16 ("Tongji University Computer System Wang Xiaoping", 150, 100, 20); Disp_hz16 ("========= Determines the number of network nodes = ========== ", 10, 150, 20); OK = 1; While (OK == 1) {setColor (9); DISP_HZ16 (" Enter Number: ", 10, 180, 20); GSCANF (300, 180 , 9, 15, 3, "% s", choice); i_num = atoi (choice); setColor (9); DISP_HZ16 ("Intoction points:", 10, 210, 20); GSCANF (300, 210, 15, 15, 3 , "% s", choice); h_num = atoi (choice); setColor (9); DISP_HZ16 ("Output Number:", 10, 240, 20); GSCANF (300, 240, 15, 0, 4, "% s", Choice); o_num = ATOI (Choice); t_num = i_num h_num o_num; work = CALC_G_LENGTH (); if (Work> Max_G_Length) DISP_HZ16 ("Too many nodes, please enter.", 10, 250, 20); Else {g_length = (int) Work; setColor (9); DISP_HZ16 ("OK? 1: NO, 2: YES", 10, 270, 20); GSCANF (300, 270, 4, 0, 4, "% s", choice); OK = ATOI (choid);}}} void set_problem () {INT I, J; file * fopen (), * fpt; if ((fpt = fopen ("// Ga // Sample", "R")))))) == NULL) EXIT (-1); else {fscanf (fpt, "% d", & p_num); for (i = 1; i <= p_num; i ) {for (j = 1; j <= i_num; j ) FSCANF (FPT, "% D", & p_i_unit [i-1] [j-1]); for (j = 1; j <= O _num; j ) fscanf (fpt, "% d", & p_o_unit [i-1] [j-1]);} fclose (fpt);}}

Void g_draw_frame (X1, Y1, X2, Y2, C1, C2, C3, C4, TEXT) INT X1, Y1, X2, Y2, C1, C2, C3, C4; CHAR * TEXT; {INT N, X3; G_Rectangle X1, Y1, X2, Y2, C1, 1); g_Rectangle (x1, y1, x2, y2, c2, 0); g_rectangle (x1, y1, x2, y1 16, c3, 1); g_rectangle (x1, y1 , X2, Y1 16, C2, 0); N = Strlen (Text); x3 = x1 ((x2-x1-n * 8) / 2); DISP_HZ16 (Text, X3, Y1, C4);}

Void g_init_frames () / * Initialization Screen * / {INT I, J, CX, CY, X, Y; CHAR TEXT [17]; g_draw_frame (0, 0, 639, 399, 15, 0, 4, 15, "Genetic Optimization Neural Network Structure "); g_draw_frame (0, 16, 320, 170, 7, 15, 8, 15," training sample "); for (i = 1; i <= p_num; i ) {y = 48 (i-1) * 16; setColor (9); DISP_HZ16 ("NO:", 20, Y, 15); ITOA (I, Text, 10); g_text (48, y 8, 4, text); x = 76; for (j = 1 ; J <= i_num; j ) {ITOA (p_i_unit [i-1] [j-1], text, 3); g_text (x, y 8, 4, text); x = x 8;} setColor 4); DISP_HZ16 ("===>", x, y, 15); x = x 30; for (j = 1; j <= o_num; j ) {ITOA (p_o_unit [i-1] [J- 1], Text, 3); G_Text (x, y 8, 4, text); x = x 16;}} g_draw_frame (0, 170, 320, 399, 7, 15, 8, 15, "the most suitable neuron corresponding to the individual Connection matrix "); g_draw_frame (320, 16, 639, 207, 7, 15, 8, 15," genetic algorithm curve "); g_draw_frame (320, 207, 639, 399, 7, 15, 8, 15," random search method adaptive curve "); }

Void g_init_graphs () {g_Rectangle (GX1, GY1, GX1 GXR, GY1 GYR, 0, 1); G_Rectangle (GX1, GY1, GX1 GXR, GY1 GYR, 6, 0); SetColor (1); DISP_HZ16 ( "Maximum Adaptation", GX1 5, GY1-18, 15); G_LINE (GX1 90, GY1-10, GX1 110, GY1-10, 1); setColor (4); DISP_HZ16 ("Average Adaptivity" GX1 120, GY1-18, 15); G_LINE (GX1 205, GY1-10, GX1 225, GY1-10, 4); setColor (15); DISP_HZ16 ("number of generations", GX1 168, GY1 GYR 10, 15); g_text (GX1-20, GY1, 15, "1.0"); g_text (gx1-20, gy1 gyr, 15, "0.0");

G_Rectangle (GX2, GY2, GX2 GXR, GY2 GYR, 0, 1); g_Rectangle (GX2, GY2, GX2 GXR, GY2 GYR, 6, 0); SetColor (1); DISP_HZ16 ("Maximum Adaptivity" , GX2 5, GY2-18, 15); G_Line (GX2 90, GY2-10, GX2 110, GY2-10, 1); setColor (4); Disp_hz16 ("average fitness", GX2 120, GY2-18, 15); G_LINE (GX2 205, GY2-10, GX2 225, GY2-10, 4); setColor (15); DISP_HZ16 ("number of generations", GX2 168, Gy2 GYR 10, 15); g_text (GX2-20, GY2, 15, "1.0"); g_text (GX2-20, Gy2 Gyr, 15, "0.0");} Void swap_fit (n2) INT N1, N2; {Unsigned Char C; Double F; Int i; for (i = 0; i

Gene [n1] = gene [n2] [i]; gene [n2] [i] = c;} f = fitness [n1]; fitness [n1] = fitness [n2]; fitness [n2] = f }

Void sort_fitness (p_size) int p_size; {INT I, J; For (i = 0; i FITNESS [i]) swap_fit (j, i); max_fit = fitness [0]; avg_fit = 0; for (i = 0; i

Void make_offspring (G1, G2, G3, Ratio) Int G1, G2, G3; Double Ratio; {INT I, RND; UNI_CROSSOVER (Gene, G1, G2, G3, Ratio, g_length); for (i = 0; i < g_length; i ) {RND = Random (100); if (RND <= 32) Gene [g3] [i] = 0; Else IF (RND <= 65) Gene [g3] [i] = 1; else Gene [ g3] [i] = 2;}}

Void Ga_reProduction () {INT I, N, P1, P2; N = (int); for (i = 0; i

Void g_disp_fitness (n, gen_num, mfold, Afold, mf, af) int N, gen_num; double mfold, Afold, MF, AF; {INT X, Y, GX, GY, X_OLD, Y_OLD; CHAR TEXT [8]; IF (n == 1) {gx = GX1; Gy = Gy1;} else {gx = gx2; Gy = Gy2;} if (gen_num% 10 == 0) {x = GX (Gen_Num-1) * gstep; g_Line X, Gy 1, X, Gy GYR-1, 1); Sprintf (Text, "% D", Gen_Num); if (Gen_Num <100 || GEN_NUM% 20 == 0) g_text (x-8, gy GYR 7, 15, TEXT);} x_old = (int) (GX (GEN_NUM-1) * GSTEP); x = x_old (int) gstep; y_old = (int) GY GYR-MFOLD * GYR; Y = (int) GY GYR-MF * GYR; G_LINE (x_old, y_old, x, y, 1); y_old = Gy Gyr- (int) (INT) (INT) (AFOLD * GYR); Y = Gy GYR- (int) (AF) * GYR); g_line (x_old, y_old, x, y, 4);}

Void Calc_fitness (p_size) int p_size; {INT I, J, K, M, COUNTER, SUM; INT GNUM, CORRECT, Weight; Double D1, D2; For (i = 0; i 0) {for (m = 0; M 0) h_unit [k] = 1; Else H_Unit [k] = 0;} for (k = 0; k 0) O_Unit [k ] = 1; Else O_Unit [K] = 0;} Correct = 1; for (k = 0; k = x1 && x <= x2-10 && y> = Y1 && y <= y2-10) {SWITCH (V) {Case 0: STRCPY (C, "0/0"); Break; Case 1: STRCPY (C, " / 0"); Break; Case 2: Strcpy (C, "- / 0"); Break; Case 3: STRCPY (C, "X / 0");} g_text (x, y, 4, c);}}

Void g_disp_best_indv () {INT X, Y; INT I, J, N, G_NUM; X1 = 40; Y1 = 190; x2 = 304; Y2 = 384; g_Rectangle (x1, y1, x2 , Y2, 7, 1); Y = Y1 16; g_num = 0; for (i = 0; I = i_num && i = i_num h_num && j

Void make_random_gene (p_size) {INT I, J, RND; for (i = 0; i

void ga_search () {int gen_num; double mfold, afold; make_random_gene (POP_SIZE); calc_fitness (POP_SIZE); sort_fitness (POP_SIZE); g_disp_best_indv (); mfold = max_fit; afold = avg_fit; ga_reproduction (); for (gen_num = 1; gen_num <= 120; gen_num ) {calc_fitness (POP_SIZE); sort_fitness (POP_SIZE); g_disp_best_indv (); g_disp_fitness (1, gen_num, mfold, afold, max_fit, avg_fit); mfold = max_fit; afold = avg_fit; ga_reproduction ();} }

void random_search () {int p_size, gen_num; double mfold, afold; p_size = (int) (POP_SIZE * S_RATE); make_random_gene (p_size); calc_fitness (p_size); sort_fitness (p_size); / * g_disp_best_indv (); * / mfold = max_fit; afold = avg_fit; for (gen_num = 1; gen_num <= 120; gen_num ) {make_random_gene (p_size); calc_fitness (p_size); sort_fitness (p_size); / * g_disp_best_indv (); * / g_disp_fitness (2, gen_num, MFOLD, AFOLD, MAX_FIT, AVG_FIT); Mfold = Max_fit; Afold = Avg_fit;}}

Main () {randomize (); / * Graphical interface initialization * / g_init (); init_mouse (0, 6, 1); setColor (5); mouse_on (0); set_network (); set_problem (); g_init_frames (); g_init_graphs (); ga_search (); random_search (); setColor (5); Disp_hz16 ("Enter key end", 10, 430, 20); getCh ();} The program comes from "Genetic Algorithm Principle - Theory, Methods and Software Implement the combo disc of a book

- "Any decision made by the even MM, even resolutely maintained;" Any even MM's instructions are always invoicing. "[This is not the principle, but ..." "Gao Gao's great banner of the" hot love mm 'theory, "has a specialty of the building, blind, love, and the great cause of the two-year-old !! "[Note: If the even MM can live to the 31st century, the above" 21 "is replaced by" thirty-one ". 】

※ Source: · Sun and Moonlight BBS.Fudan.edu.cn · http [from: 202.120.97.159]

转载请注明原文地址:https://www.9cbs.com/read-128844.html

New Post(0)