/*-------------------------------------------------------------------------- Genetic Algorithm Base program for MaTX 9-30.1994, Shinya Kisui Genetic Algorithm for Analog Space Last Modified 4-26.1995 This program is Genetic-Algorithm-Optimizeing-Routine for MaTX. Using this program, you can solve 最適値(準最適値)探索 problem. Format is as follows, ---------------(example)------------------ Matrix GA_opt( List ); Matrix Answer; List init_param; Answer = GA_opt( init_param ); ------------------------------------------ init_param has 8 parameters, init_param = { N_I, N_E, N_P, L_P, R_C, R_M, EXC, SED } Integer N_I; : Number of Individuals Integer N_E; : Number of Elites Integer N_P; : Number of Pheno Integer L_P; : Length of Pheno Integer EXC; : Number of Exchange-Step Real R_C; : Rate of Crossing Real R_M; : Rate of Mutation Integer SED; : Seed of Random returned value is ( N_E x N_P ) matrix, which means Elite-1 : pheno-1(Real) pheno-2(Real) ... Pheno-(N_P)(Real) Elite-2 : pheno-1(Real) pheno-2(Real) ... Pheno-(N_P)(Real) Elite-3 : pheno-1(Real) pheno-2(Real) ... Pheno-(N_P)(Real) . Elite-(N_E)................................................. You have to make fitnessfunction() in fitnessfunction.mm . Please look the sample of fitnessfunction.mm . If you have any question at this program, please mail to kisui@ctrl.titech.ac.jp ------------------------------------------------------------------------*/ /*------------------------------------------------------*/ /* GA - Main Routine */ /* */ /* Initialize Parameters, Excute Optimize, */ /* Display-Print the result............ */ /* And Return the Result for Main-Program */ /* */ /*------------------------------------------------------*/ Func Matrix GA_opt( init_param ) List init_param; { /*------------------------------------------------------*/ /* Conditions of the Space */ /*------------------------------------------------------*/ Integer Num_Ind; /* Number of Individuals */ Integer Num_Eleat; /* Number of Eites */ Integer Exch; /* Number of Exchange */ Integer step; /* Generation */ Real R_Cros; /* Rate of Crossing */ Real R_Mut; /* Rate of Mutation */ Integer G_Len; /* Length of Gene */ Integer G_Num; /* Number of Gene */ /*------------------------------------------------------*/ /* Parameters of the Individuals */ /*------------------------------------------------------*/ List Indiv; /* Individual */ List Group; /* Group of Individuals */ List Group_Eleat; /* Group of Elites */ Array Gene; /* Gene */ Matrix Pheno; /* Phenotype */ Real Fit; /* Fitness */ /*------------------------------------------------------*/ /* Parameters for DATA-Logging */ /*------------------------------------------------------*/ Array max_log; /* Log of Max-Fitness */ Array avg_log; /* Log of Avg-Fitness */ Array exc_log; /* Log of Exchange */ /*------------------------------------------------------*/ /* Another Parameters */ /*------------------------------------------------------*/ Integer i; Matrix Ans; /* Matrix for Return Answer */ /*------------------------------------------------------*/ /* Prototype of Functions */ /*------------------------------------------------------*/ List setup_val(); /* Parameters Setup */ List setup(); /* Initial Condition setup */ List make_Indiv(); /* Make the Individuals */ List mutate(); /* Excute the Mutation */ List crossover(); /* Excute the Crossing */ List calc_phenotype(); /* Calculate the Phenotype */ List calc_fitness(); /* Calculate the Fitness */ List reproduction(); /* Reproduction */ List sorting(); /* Sorting, save the Elites*/ Array log_max(); /* Save the Max-Fitness */ Array log_avg(); /* Save the Avg-Fitness */ Array log_exc(); /* Save the Exchange-Number*/ /*------------------------------------------------------*/ /* Program Start */ /*------------------------------------------------------*/ { Num_Ind, Num_Eleat, Exch, R_Cros, R_Mut, G_Len, G_Num } = setup_val( init_param ); { Group, Group_Eleat, Indiv, max_log, avg_log, exc_log } = setup( Num_Ind, Num_Eleat, G_Len, G_Num, Exch ); Group = make_Indiv( Group, Num_Ind ); for( step = 1 ; step <= Exch ; step++ ){ Group = calc_phenotype( Group, Num_Ind ); Group = calc_fitness( Group, Num_Ind ); { Group, Group_Eleat }= sorting( Group, Group_Eleat, Num_Ind, Num_Eleat ); max_log = log_max( step, Group_Eleat, max_log ); avg_log = log_avg( step, Group, Num_Ind, avg_log ); exc_log = log_exc( step, exc_log ); Group = reproduction( Group, Num_Ind ); Group = crossover( Group, Num_Ind, R_Cros ); Group = mutate( Group, Num_Ind, R_Mut ); print step; } print [ [exc_log][max_log][avg_log] ] >> "max_avg.log"; Ans = Z( Num_Eleat, G_Num ); for( i=1 ; i<= Num_Eleat ; i++ ){ Indiv = Group_Eleat( i, List ); Pheno = Indiv( 2, Matrix ); Ans( i, : ) = [Pheno]; } return Ans; } /*------------------------------------------------------*/ /* 初期値設定ルーチン */ /* */ /* ○ 生物数・エリート数 */ /* ○ 世代交代回数 */ /* ○ 突然変異率・遺伝子交差率 */ /* ○ 染色体本数・染色体長さ */ /* */ /* の各パラメータを設定・変更するルーチン */ /*------------------------------------------------------*/ Func List setup_val( param_list ) List param_list; { Integer Num_Ind; Integer Num_Eleat; Integer Exch; Real R_Cros; Real R_Mut; Integer G_Len; Integer G_Num; Integer select; Integer no_op, i; no_op = param_list( 8, Integer ); for( i = 1 ; i <= no_op ; i++ ){ rand(); } Num_Ind = param_list( 1, Integer ); Num_Eleat = param_list( 2, Integer ); G_Num = param_list( 3, Integer ); G_Len = param_list( 4, Integer ); R_Cros = param_list( 5, Real ); R_Mut = param_list( 6, Real ); Exch = param_list( 7, Integer ); return { Num_Ind, Num_Eleat, Exch, R_Cros, R_Mut, G_Len, G_Num }; } /*------------------------------------------------------*/ /* 仮想生物設定ルーチン */ /* */ /* ○ 仮想生物群・エリート群 */ /* */ /* の初期設定を行うルーチン */ /*------------------------------------------------------*/ Func List setup( Num_Ind, Num_Eleat, G_Len, G_Num, Exch ) Integer Num_Ind; Integer Num_Eleat; Integer G_Len; Integer G_Num; Integer Exch; { List Indiv; List Group; List Group_Eleat; Array max_log; Array avg_log; Array exc_log; Matrix gene_size; Matrix pheno_size; Real Fit; Integer i; gene_size = Z( G_Num, G_Len ); pheno_size = Z( 1, G_Num ); Fit = 0.0; Indiv = { gene_size, pheno_size, Fit }; Group = makelist( Num_Ind ); Group_Eleat = makelist( Num_Eleat ); for( i=1 ; i<=Num_Ind ; i++ ){ Group( i ) = Indiv; } for( i=1 ; i<=Num_Eleat ; i++ ){ Group_Eleat( i ) = Indiv; } max_log = Z( 1, Exch ); avg_log = Z( 1, Exch ); exc_log = Z( 1, Exch ); return { Group, Group_Eleat, Indiv, max_log, avg_log, exc_log }; } /*------------------------------------------------------*/ /* 生物集団発生ルーチン */ /* */ /* 設定された 生物数・染色体数・染色体長さ */ /* に基づいて、ランダムに遺伝子を決定し */ /* 生物集団を形成する。 */ /* */ /*------------------------------------------------------*/ Func List make_Indiv( Group, Num_Ind ) List Group; Integer Num_Ind; { List Indiv; Matrix Gene; Integer G_Len; Integer G_Num; Integer i, j, k; Indiv = Group( 1, List ); Gene = Indiv( 1, Matrix ); G_Len = Cols( Gene ); G_Num = Rows( Gene ); for( i = 1 ; i <= Num_Ind ; i++ ){ for( j = 1 ; j <= G_Num ; j++ ){ for( k = 1 ; k <= G_Len ; k++ ){ Gene( j, k ) = Integer( rand() ); } } Indiv( 1 ) = Gene; Group( i ) = Indiv; } return Group; } /*------------------------------------------------------*/ /* 突然変異ルーチン */ /* */ /* 設定された 突然変異率に基づいて 各生物の */ /* 遺伝子を反転する。 */ /*------------------------------------------------------*/ Func List mutate( Group, Num_Ind, R_Mut ) List Group; Integer Num_Ind; Real R_Mut; { List Indiv; Matrix Gene; Integer G_Len; Integer G_Num; Integer i, j, k; Real rate; Real stack; Indiv = Group( 1, List ); Gene = Indiv( 1, Matrix ); G_Len = Cols( Gene ); G_Num = Rows( Gene ); for( i=1 ; i <= Num_Ind ; i++ ){ Indiv = Group( i, List ); Gene = Indiv( 1, Matrix ); for( j=1 ; j<=G_Num ; j++ ){ for( k=1 ; k<=G_Len ; k++ ){ rate = rand(); if( rate < R_Mut ){ stack = abs( Gene( j, k ) - 1.0 ); Gene( j, k ) = stack; } } } Indiv( 1 ) = Gene; Group( i ) = Indiv; } return Group; } /*------------------------------------------------------*/ /* 遺伝子交差ルーチン */ /* */ /* 設定された 遺伝子交差率に基づいて 2つの */ /* 生物の 遺伝子をランダムな位置で交換する。 */ /*------------------------------------------------------*/ Func List crossover( Group, Num_Ind, R_Cros ) List Group; Integer Num_Ind; Real R_Cros; { List Indiv; Matrix Gene; Integer G_Len; Integer G_Num; Integer i, j, k, l; Real rate; Integer cut_pos; Matrix stack; Matrix gene1, gene2; List indiv1, indiv2; Indiv = Group( 1, List ); Gene = Indiv( 1, Matrix ); G_Len = Cols( Gene ); G_Num = Rows( Gene ); for( i=1 ; i<=(Num_Ind-1) ; i++ ){ for( j=(i+1) ; j