├── TINY_GSHCGP.nbtxt ├── TINY_GSGP.nbtxt ├── TINY_GSHCGP _ARIT.nbtxt ├── TINY_GSGP_ARIT.nbtxt ├── README.md ├── TINY_GSHCGP.py ├── TINY_GSGP.py ├── TINY_GSHCGP.nb ├── TINY_GSGP.nb ├── TINY_GSHCGP _ARIT.nb └── TINY_GSGP_ARIT.nb /TINY_GSHCGP.nbtxt: -------------------------------------------------------------------------------- 1 | (* ************************************************************************************************************; 2 | 3 | TINY_GSHCGP.nb:An Implementation of Geometric Semantic ***Hill Climber*** Genetic Programming in Mathematica Using Algebraic Simplification; 4 | 5 | Author:Alberto Moraglio (albmor@gmail.com); 6 | 7 | Features; 8 | - The fitness landscape seen by Geometric Semantic operators is always unimodal. A hill-climber can reach the optimum; 9 | - Mutation embeds parent expression in the offspring expression; 10 | - Algebraic simplification of offspring; 11 | - Offspring size growth without simplification is linear in the number of generation (simplification is not strictly needed for space efficiency); 12 | - Final solution short and understandable; 13 | 14 | ************************************************************************************************************ *) 15 | 16 | 17 | (****** PARAMETERS ******) 18 | 19 | NUMVARS=5; (* number of input variables *) 20 | GENERATIONS=300; (* number of generations *) 21 | 22 | (************************) 23 | 24 | vars = Table[Symbol["x"<>ToString[i]],{i,NUMVARS}]; (* variable names *) 25 | 26 | targetfunct = Apply[Xor, vars]; (* parity function *) 27 | 28 | fitness[individual_] := (* fitness (error) function, lower is better *) 29 | Count[MapThread[Xor,{BooleanTable[targetfunct],BooleanTable[individual]}], True]; 30 | 31 | mutation[p_] := Module[{minterm}, (* mutation substitutes parent and minterm expressions in mutation scheme to make offspring *) 32 | minterm=BooleanMinterms[{Table[RandomInteger[],{NUMVARS}]},vars]; 33 | If[RandomInteger[]==0, p || minterm, p && !minterm]]; 34 | 35 | (****** CLIMB ******) 36 | 37 | curr = BooleanFunction[RandomInteger[2^(2^NUMVARS) - 1], vars]; (* initial individual *) 38 | currfit = fitness[curr]; (* evaluate fitness *) 39 | For[gen=0, gen0.1]; (* offspring is simplified and replaces parent if better *) 44 | currfit = offfit]; (* update fitness *) 45 | If[Mod[gen , 10]==0, Print["gen: ",gen, " fit: ",currfit]]; (* print stats *) 46 | ] 47 | 48 | Print["Best individual:"]; 49 | Print[curr]; (* genotype of final solution *) 50 | Print[curr/.{x1->True,x2->True,x3->True,x4->True,x5->True}]; (* query genotype of final solution *) 51 | 52 | -------------------------------------------------------------------------------- /TINY_GSGP.nbtxt: -------------------------------------------------------------------------------- 1 | (* ************************************************************************************************************; 2 | 3 | TINY_GSGP.nb: A Tiny Implementation of Geometric Semantic Genetic Programming in Mathematica Using Algebraic Simplification; 4 | 5 | Author: Alberto Moraglio (albmor@gmail.com); 6 | 7 | This is a reimplementation of TINY_GSGP.py in Mathematica to compare the effect of simplification of offsrping; 8 | 9 | Features; 10 | - Individuals are represented using symbolic expressions (Boolean expressions); 11 | - Uniform initialisation/generation of random expressions; 12 | - Crossover and mutation embed parent expressions in the offspring expression; 13 | - Algebraic simplification of offspring prevents exponential growth; 14 | 15 | ************************************************************************************************************ *) 16 | 17 | 18 | (****** PARAMETERS ******) 19 | 20 | NUMVARS=5 (* number of input variables *) 21 | (* DEPTH=4 *) (* maximum depth of expressions in the initial population *) 22 | POPSIZE=20 (* population size *) 23 | GENERATIONS=30 (* number of generations *) 24 | TRUNC=0.5 (* proportion of population to retain in truncation selection *) 25 | 26 | (************************) 27 | 28 | vars=Table[Symbol["x"<>ToString[i]],{i,NUMVARS}]; (* variable names *) 29 | 30 | targetfunct=Apply[Xor, vars]; (* parity function *) 31 | 32 | fitness[individual_]:= (* fitness (error) function, lower is better *) 33 | Count[MapThread[Xor,{BooleanTable[targetfunct],BooleanTable[individual]}], True]; 34 | 35 | crossover[p1_,p2_]:=Module[{mask}, (* crossover substitutes parent and mask expressions in crossover scheme to make offspring *) 36 | mask=BooleanFunction[RandomInteger[2^(2^NUMVARS) - 1], vars]; 37 | (p1 && mask)||(p2 && !mask)]; 38 | 39 | mutation[p_]:=Module[{minterm}, (* mutation substitutes parent and minterm expressions in mutation scheme to make offspring *) 40 | minterm=BooleanMinterms[{Table[RandomInteger[],{NUMVARS}]},vars]; 41 | If[RandomInteger[]==0,p || minterm,p && !minterm]]; 42 | 43 | (****** EVOLVE ******) 44 | 45 | pop =Table[BooleanFunction[RandomInteger[2^(2^NUMVARS) - 1], vars], {POPSIZE}]; (* initialise population with uniform distribution on functions *) 46 | For[gen=0,gen0.1]; (* create offspring and simplify it *) 55 | ] 56 | ] 57 | 58 | Print["Best individual in last population:"]; 59 | Print[sortedpop[[1]]]; (* genotype of final solution *) 60 | Print[sortedpop[[1,2]]/.{x1->True,x2->True,x3->True,x4->True,x5->True}]; (* query genotype of final solution *) 61 | 62 | 63 | -------------------------------------------------------------------------------- /TINY_GSHCGP _ARIT.nbtxt: -------------------------------------------------------------------------------- 1 | (* ************************************************************************************************************; 2 | 3 | TINY_GSHCGP _ARIT.nb: Geometric Semantic Hill Climber Genetic Programming in Mathematica for Arithmetic Expressions; 4 | 5 | Author:Alberto Moraglio (albmor@gmail.com); 6 | 7 | Features; 8 | - It searches the space of arithmentic expressions (polynomials or fractional polynomials if division is used); 9 | - Fithess is based on a training set (not on all inputs as for Boolean expressions); 10 | - Algebraic simplification of offspring; 11 | - Generalisation test (on unseen examples); 12 | 13 | ************************************************************************************************************ *) 14 | 15 | 16 | (****** PARAMETERS ******) 17 | 18 | NUMVARS=5; (* number of input variables *) 19 | DEPTH=4; (* maximum depth of expressions in the initial population *) 20 | GENERATIONS=1000; (* number of generations *) 21 | NUMCASES = 10000; (* number of training examples *) 22 | MUTSTEP = 0.01; (* mutation step size *) 23 | 24 | (************************) 25 | 26 | vars = Table[Symbol["x"<>ToString[i]],{i,NUMVARS}]; (* variable names *) 27 | 28 | randexpr[dep_]:= (* Create a random arithmetic expression *) 29 | If[dep==1 || RandomReal[] < N[1.0/(2^dep-1)], (* terminal *) 30 | If[RandomReal[]>N[1/(NUMVARS+1)], 31 | Return[RandomChoice[vars]], (* variable *) 32 | RandomReal[{-1,1}]], (* number *) 33 | If[RandomReal[]0.1]; (* offspring is simplified and replaces parent if better *) 63 | currfit = offfit]; (* update fitness *) 64 | If[Mod[gen , 10]==0, Print["gen: ",gen, " fit: ",currfit]]; (* print stats *) 65 | ] 66 | 67 | Print["Best individual:"]; 68 | Print[curr]; (* genotype of final solution *) 69 | 70 | (****** GENERALISATION TEST ******) 71 | 72 | Print["Training error: ", currfit]; (* fitness of best individual is training error *) 73 | traininginputs = Table[RandomReal[{-1,1},NUMVARS],{NUMCASES}]; (* generate test inputs uniformly at random *) 74 | trainingoutputs = MapThread[targetfunct,traininginputs]; (* compute target outputs on test inputs *) 75 | Print["Generalisation error: ", fitness[curr]]; (* fitness of best individual replacing training inputs with test inputs is generalisation error *) 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /TINY_GSGP_ARIT.nbtxt: -------------------------------------------------------------------------------- 1 | (* ************************************************************************************************************; 2 | 3 | TINY_GSGP_ARIT.nb: Geometric Semantic Genetic Programming in Mathematica for Arithmetic Expressions; 4 | 5 | Author:Alberto Moraglio (albmor@gmail.com); 6 | 7 | Features; 8 | - It evolves arithmentic expressions (polynomials or fractional polynomials if division is used); 9 | - Fithess is based on a training set (not on all inputs as for Boolean expressions); 10 | - Algebraic simplification of offspring; 11 | - Generalisation test (on unseen examples); 12 | 13 | ************************************************************************************************************ *) 14 | 15 | 16 | (****** PARAMETERS ******) 17 | 18 | NUMVARS = 5; (* number of input variables *) 19 | DEPTH = 4; (* maximum depth of expressions in the initial population *) 20 | POPSIZE = 20; (* population size *) 21 | GENERATIONS = 100; (* number of generations *) 22 | NUMCASES = 10000; (* number of training examples *) 23 | MUTSTEP = 0.01; (* mutation step size *) 24 | TRUNC = 0.5; (* proportion of population to retain in truncation selection *) 25 | 26 | (************************) 27 | 28 | vars = Table[Symbol["x"<>ToString[i]],{i,NUMVARS}]; (* variable names *) 29 | 30 | randexpr[dep_]:= (* Create a random arithmetic expression *) 31 | If[dep==1 || RandomReal[] < N[1.0/(2^dep-1)], (* terminal *) 32 | If[RandomReal[]>N[1/(NUMVARS+1)], 33 | Return[RandomChoice[vars]], (* variable *) 34 | RandomReal[{-1,1}]], (* number *) 35 | If[RandomReal[]0.1]; (* create offspring and simplify it *) 72 | ] 73 | ] 74 | 75 | Print["Best individual in last population:"]; 76 | Print[sortedpop[[1]]];(* genotype of final solution *) 77 | 78 | (****** GENERALISATION TEST ******) 79 | 80 | Print["Training error: ", sortedpop[[1,1]]]; (* fitness of best individual is training error *) 81 | traininginputs = Table[RandomReal[{-1,1},NUMVARS],{NUMCASES}]; (* generate test inputs uniformly at random *) 82 | trainingoutputs = MapThread[targetfunct,traininginputs]; (* compute target outputs on test inputs *) 83 | Print["Generalisation error: ", fitness[sortedpop[[1]]]]; (* fitness of best individual replacing training inputs with test inputs is generalisation error *) 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | TINY_GSGP.py: 3 | ============= 4 | 5 | A Tiny and Efficient Implementation of Geometric Semantic Genetic Programming Using Higher-Order Functions and Memoization 6 | 7 | Author: Alberto Moraglio (albmor@gmail.com) 8 | 9 | Features: 10 | 11 | - Individuals are represented directly as Python (anonymous) functions. 12 | 13 | - Crossover and mutation are higher-order functions. 14 | 15 | - Offspring functions call parent functions rather than embed their definitions (no grwoth, implicit ancestry trace). 16 | 17 | - Memoization of individuals turns time complexity of fitness evalutation from exponential to constant. 18 | 19 | - The final solution is a compiled function. It can be extracted using the ancestry trace to reconstruct its 'source code'. 20 | 21 | This implementation is to evolve Boolean expressions. It can be easily adapted to evolve arithmetic expressions or classifiers. 22 | 23 | TINY_GSGP.nb: 24 | ============= 25 | 26 | A Tiny Implementation of Geometric Semantic Genetic Programming in Mathematica Using Algebraic Simplification. 27 | 28 | Author: Alberto Moraglio (albmor@gmail.com) 29 | 30 | This is a reimplementation of TINY_GSGP.py in Mathematica to compare the effect of simplification of offsrping. 31 | 32 | Features: 33 | 34 | - Individuals are represented using symbolic expressions (Boolean expressions). 35 | 36 | - Uniform initialisation/generation of random expressions. 37 | 38 | - Crossover and mutation embed parent expressions in the offspring expression. 39 | 40 | - Algebraic simplification of offspring prevents exponential growth. 41 | 42 | - The final solution is short and understandable (no black-box). 43 | 44 | TINY_GSHCGP.py: 45 | =============== 46 | 47 | An Implementation of Geometric Semantic *Hill Climber* Genetic Programming Using Higher-Order Functions and Memoization 48 | 49 | Author: Alberto Moraglio (albmor@gmail.com) 50 | 51 | Features: 52 | 53 | - Same as TINY_GSGP.py, substituting the evolutionary algorithm with a hill-climber. 54 | 55 | - The fitness landscape seen by Geometric Semantic operators is always unimodal. A hill climber can reach the optimum. 56 | 57 | - Offspring functions call parent functions rather than embed their definitions (no grwoth, implicit ancestry trace). 58 | 59 | - Even if offspring functions embedded parent function definition, the growth is linear in the number of generation (not exponential as with crossover). 60 | 61 | - Memoization of individuals turns time complexity of fitness evalutation from linear to constant (not exponential to constant as with crossover). 62 | 63 | - Implicit ancestry trace and memoization not strictly necessary with hill-climber for efficent implementation. 64 | 65 | - The final solution is a compiled function. It can be extracted using the ancestry trace to reconstruct its 'source code'. 66 | 67 | This implementation is to evolve Boolean expressions. It can be easily adapted to evolve arithmetic expressions or classifiers. 68 | 69 | TINY_GSHCGP.nb: 70 | =============== 71 | 72 | An Implementation of Geometric Semantic *Hill Climber* Genetic Programming in Mathematica Using Algebraic Simplification. 73 | 74 | Author: Alberto Moraglio (albmor@gmail.com) 75 | 76 | Features: 77 | 78 | - The fitness landscape seen by Geometric Semantic operators is always unimodal. A hill-climber can reach the optimum. 79 | 80 | - Mutation embeds parent expression in the offspring expression. 81 | 82 | - Algebraic simplification of offspring. 83 | 84 | - Offspring size growth without simplification is linear in the number of generation (simplification is not strictly needed for space efficiency). 85 | 86 | - Final solution short and understandable. 87 | 88 | TINY_GSHCGP _ARIT.nb: 89 | ===================== 90 | 91 | Geometric Semantic Hill Climber Genetic Programming in Mathematica for *Arithmetic Expressions*. 92 | 93 | Author: Alberto Moraglio (albmor@gmail.com) 94 | 95 | Features: 96 | 97 | - It searches the space of arithmentic expressions (polynomials or fractional polynomials if division is used). 98 | 99 | - Fithess is based on a training set (not on all inputs as for Boolean expressions). 100 | 101 | - Algebraic simplification of offspring. 102 | 103 | - Generalisation test (on unseen examples). 104 | 105 | TINY_GSGP _ARIT.nb: 106 | =================== 107 | 108 | Geometric Semantic Genetic Programming in Mathematica for *Arithmetic Expressions*. 109 | 110 | Author: Alberto Moraglio (albmor@gmail.com) 111 | 112 | Features: 113 | 114 | - It evolves arithmentic expressions (polynomials or fractional polynomials if division is used). 115 | 116 | - Fithess is based on a training set (not on all inputs as for Boolean expressions). 117 | 118 | - Algebraic simplification of offspring. 119 | 120 | - Generalisation test (on unseen examples). 121 | 122 | 123 | -------------------------------------------------------------------------------- /TINY_GSHCGP.py: -------------------------------------------------------------------------------- 1 | 2 | ''' 3 | 4 | TINY_GSHCGP.py: An Implementation of Geometric Semantic ***Hill Climber*** Genetic Programming Using Higher-Order Functions and Memoization 5 | 6 | Author: Alberto Moraglio (albmor@gmail.com) 7 | 8 | Features: 9 | 10 | - Same as TINY_GSGP.py, substituting the evolutionary algorithm with a hill-climber. 11 | 12 | - The fitness landscape seen by Geometric Semantic operators is always unimodal. A hill climber can reach the optimum. 13 | 14 | - Offspring functions call parent functions rather than embed their definitions (no grwoth, implicit ancestry trace). 15 | 16 | - Even if offspring functions embedded parent function definition, the growth is linear in the number of generation (not exponential as with crossover). 17 | 18 | - Memoization of individuals turns time complexity of fitness evalutation from linear to constant (not exponential to constant as with crossover). 19 | 20 | - Implicit ancestry trace and memoization not strictly necessary with hill-climber for efficent implementation. 21 | 22 | - The final solution is a compiled function. It can be extracted using the ancestry trace to reconstruct its 'source code'. 23 | 24 | This implementation is to evolve Boolean expressions. It can be easily adapted to evolve arithmetic expressions or classifiers. 25 | 26 | ''' 27 | 28 | import random 29 | import itertools 30 | 31 | #### PARAMETERS #### 32 | 33 | NUMVARS = 5 # number of input variables 34 | DEPTH = 4 # maximum depth of expressions in the initial population 35 | GENERATIONS = 400 # number of generations 36 | 37 | #################### 38 | 39 | vars = ['x'+str(i) for i in range(NUMVARS)] # variable names 40 | 41 | def memoize(f): 42 | 'Add a cache memory to the input function.' 43 | f.cache = {} 44 | def decorated_function(*args): 45 | if args in f.cache: 46 | return f.cache[args] 47 | else: 48 | f.cache[args] = f(*args) 49 | return f.cache[args] 50 | return decorated_function 51 | 52 | def randexpr(dep): 53 | 'Create a random Boolean expression.' 54 | if dep==1 or random.random()<1.0/(2**dep-1): 55 | return random.choice(vars) 56 | if random.random()<1.0/3: 57 | return 'not' + ' ' + randexpr(dep-1) 58 | else: 59 | return '(' + randexpr(dep-1) + ' ' + random.choice(['and','or']) + ' ' + randexpr(dep-1) + ')' 60 | 61 | def randfunct(): 62 | 'Create a random Boolean function. Individuals are represented _directly_ as Python functions.' 63 | re = randexpr(DEPTH) 64 | rf = eval('lambda ' + ', '.join(vars) + ': ' + re) # create function of n input variables 65 | rf = memoize(rf) # add cache to the function 66 | rf.geno = lambda: re # store genotype 67 | return rf 68 | 69 | def targetfunct(*args): 70 | 'Parity function of any number of input variables' 71 | return args.count(True) % 2 == 1 72 | 73 | def fitness(individual): 74 | 'Determine the fitness (error) of an individual. Lower is better.' 75 | fit = 0 76 | somelists = [[True,False] for i in range(NUMVARS)] 77 | for element in itertools.product(*somelists): # generate all input combinations for n variables 78 | if individual(*element) != targetfunct(*element): 79 | fit = fit + 1 80 | return fit 81 | 82 | def mutation(p): 83 | 'The mutation operator is a higher order function. The parent function is called by the offspring function.' 84 | mintermexpr = ' and '.join([random.choice([x,'not ' + x]) for x in vars]) # random minterm expression of n variables 85 | minterm = eval('lambda ' + ', '.join(vars) + ': ' + mintermexpr) # turn minterm into a function 86 | if random.random()<0.5: 87 | offspring = lambda *x: p(*x) or minterm(*x) 88 | offspring = memoize(offspring) # add cache 89 | offspring.geno = lambda: '(' + p.geno() + ' or ' + mintermexpr + ')' # to reconstruct genotype 90 | else: 91 | offspring = lambda *x: p(*x) and not minterm(*x) 92 | offspring = memoize(offspring) # add cache 93 | offspring.geno = lambda: '(' + p.geno() + ' and not ' + mintermexpr + ')' # to reconstruct genotype 94 | return offspring 95 | 96 | def climb(): 97 | 'Main function. As the landscape is always unimodal the climber can find the optimum.' 98 | curr = randfunct() # initial individual 99 | curr.fit = fitness(curr) # evaluate fitness 100 | 101 | for gen in xrange(GENERATIONS+1): 102 | off = mutation(curr) # create offspring 103 | off.fit = fitness(off) # fitness offspring 104 | if off.fit < curr.fit: curr = off # offspring replaces parent if better 105 | if gen % 10 == 0: print 'gen: ', gen , ' fit: ', curr.fit # print stats 106 | 107 | print 'Best individual: ' 108 | print curr.geno() # reconstruct genotype of final solution (LINEAR SIZE IN NUMBER OF GENERATIONS) 109 | print 'Query best individual with all True inputs:' 110 | print curr(*([True] * NUMVARS)) # query final solution 111 | 112 | climb() 113 | -------------------------------------------------------------------------------- /TINY_GSGP.py: -------------------------------------------------------------------------------- 1 | 2 | ''' 3 | 4 | TINY_GSGP.py: A Tiny and Efficient Implementation of Geometric Semantic Genetic Programming Using Higher-Order Functions and Memoization 5 | 6 | Author: Alberto Moraglio (albmor@gmail.com) 7 | 8 | Features: 9 | 10 | - Individuals are represented directly as Python (anonymous) functions. 11 | 12 | - Crossover and mutation are higher-order functions. 13 | 14 | - Offspring functions call parent functions rather than embed their definitions (no grwoth, implicit ancestry trace). 15 | 16 | - Memoization of individuals turns time complexity of fitness evalutation from exponential to constant. 17 | 18 | - The final solution is a compiled function. It can be extracted using the ancestry trace to reconstruct its 'source code'. 19 | 20 | This implementation is to evolve Boolean expressions. It can be easily adapted to evolve arithmetic expressions or classifiers. 21 | 22 | ''' 23 | 24 | import random 25 | import itertools 26 | 27 | #### PARAMETERS #### 28 | 29 | NUMVARS = 5 # number of input variables 30 | DEPTH = 4 # maximum depth of expressions in the initial population 31 | POPSIZE = 20 # population size 32 | GENERATIONS = 30 # number of generations 33 | TRUNC = 0.5 # proportion of population to retain in truncation selection 34 | 35 | #################### 36 | 37 | vars = ['x'+str(i) for i in range(NUMVARS)] # variable names 38 | 39 | def memoize(f): 40 | 'Add a cache memory to the input function.' 41 | f.cache = {} 42 | def decorated_function(*args): 43 | if args in f.cache: 44 | return f.cache[args] 45 | else: 46 | f.cache[args] = f(*args) 47 | return f.cache[args] 48 | return decorated_function 49 | 50 | def randexpr(dep): 51 | 'Create a random Boolean expression.' 52 | if dep==1 or random.random()<1.0/(2**dep-1): 53 | return random.choice(vars) 54 | if random.random()<1.0/3: 55 | return 'not' + ' ' + randexpr(dep-1) 56 | else: 57 | return '(' + randexpr(dep-1) + ' ' + random.choice(['and','or']) + ' ' + randexpr(dep-1) + ')' 58 | 59 | def randfunct(): 60 | 'Create a random Boolean function. Individuals are represented _directly_ as Python functions.' 61 | re = randexpr(DEPTH) 62 | rf = eval('lambda ' + ', '.join(vars) + ': ' + re) # create function of n input variables 63 | rf = memoize(rf) # add cache to the function 64 | rf.geno = lambda: re # store genotype 65 | return rf 66 | 67 | def targetfunct(*args): 68 | 'Parity function of any number of input variables' 69 | return args.count(True) % 2 == 1 70 | 71 | def fitness(individual): 72 | 'Determine the fitness (error) of an individual. Lower is better.' 73 | fit = 0 74 | somelists = [[True,False] for i in range(NUMVARS)] 75 | for element in itertools.product(*somelists): # generate all input combinations for n variables 76 | if individual(*element) != targetfunct(*element): 77 | fit = fit + 1 78 | return fit 79 | 80 | def crossover(p1,p2): 81 | """ 82 | The crossover operator is a higher order function that takes parent functions and return an offspring function. 83 | The definitions of parent functions are _not substituted_ in the definition of the offspring function. 84 | Instead parent functions are _called_ from the offspring function. This prevents exponential growth. 85 | """ 86 | mask = randfunct() 87 | offspring = lambda *x: (p1(*x) and mask(*x)) or (p2(*x) and not mask(*x)) 88 | offspring = memoize(offspring) # add cache 89 | offspring.geno = lambda: '(('+ p1.geno() + ' and ' + mask.geno() + ') or (' + p2.geno() + ' and not ' + mask.geno() + '))' # to reconstruct genotype 90 | return offspring 91 | 92 | def mutation(p): 93 | 'The mutation operator is a higher order function. The parent function is called by the offspring function.' 94 | mintermexpr = ' and '.join([random.choice([x,'not ' + x]) for x in vars]) # random minterm expression of n variables 95 | minterm = eval('lambda ' + ', '.join(vars) + ': ' + mintermexpr) # turn minterm into a function 96 | if random.random()<0.5: 97 | offspring = lambda *x: p(*x) or minterm(*x) 98 | offspring = memoize(offspring) # add cache 99 | offspring.geno = lambda: '(' + p.geno() + ' or ' + mintermexpr + ')' # to reconstruct genotype 100 | else: 101 | offspring = lambda *x: p(*x) and not minterm(*x) 102 | offspring = memoize(offspring) # add cache 103 | offspring.geno = lambda: '(' + p.geno() + ' and not ' + mintermexpr + ')' # to reconstruct genotype 104 | return offspring 105 | 106 | def evolve(): 107 | 'Main function.' 108 | pop = [ randfunct() for _ in xrange(POPSIZE) ] # initialise population 109 | 110 | for gen in xrange(GENERATIONS+1): 111 | graded_pop = [ (fitness(ind), ind) for ind in pop ] # evaluate population fitness 112 | sorted_pop = [ ind[1] for ind in sorted(graded_pop)] # sort population on fitness 113 | print 'gen: ', gen , ' min fit: ', fitness(sorted_pop[0]), ' avg fit: ', sum(ind[0] for ind in graded_pop)/(POPSIZE*1.0) # print stats 114 | parent_pop = sorted_pop[:int(TRUNC*POPSIZE)] # selected parents 115 | if gen == GENERATIONS: break 116 | for i in xrange(POPSIZE): # create offspring population 117 | par = random.sample(parent_pop, 2) # pick two random parents 118 | pop[i] = mutation(crossover(par[0],par[1])) # create offspring 119 | 120 | print 'Best individual in last population: ' 121 | #print (sorted_pop[0]).geno() # reconstruct genotype of final solution (WARNING: EXPONENTIALLY LONG IN NUMBER OF GENERATIONS!) 122 | print 'Query best individual in last population with all True inputs:' 123 | print sorted_pop[0](*([True] * NUMVARS)) # however querying it to make predictions is quick 124 | 125 | evolve() 126 | 127 | -------------------------------------------------------------------------------- /TINY_GSHCGP.nb: -------------------------------------------------------------------------------- 1 | (* Content-type: application/vnd.wolfram.mathematica *) 2 | 3 | (*** Wolfram Notebook File ***) 4 | (* http://www.wolfram.com/nb *) 5 | 6 | (* CreatedBy='Mathematica 8.0' *) 7 | 8 | (*CacheID: 234*) 9 | (* Internal cache information: 10 | NotebookFileLineBreakTest 11 | NotebookFileLineBreakTest 12 | NotebookDataPosition[ 157, 7] 13 | NotebookDataLength[ 14654, 321] 14 | NotebookOptionsPosition[ 14409, 308] 15 | NotebookOutlinePosition[ 14752, 323] 16 | CellTagsIndexPosition[ 14709, 320] 17 | WindowFrame->Normal*) 18 | 19 | (* Beginning of Notebook Content *) 20 | Notebook[{ 21 | Cell[BoxData[ 22 | RowBox[{ 23 | RowBox[{"(*", " ", 24 | RowBox[{ 25 | RowBox[{ 26 | RowBox[{ 27 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 28 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 29 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 30 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 31 | "**", "**", "**", "**", "**"}], ";", "\[IndentingNewLine]", 32 | "\[IndentingNewLine]", 33 | RowBox[{"TINY_GSHCGP", ".", 34 | RowBox[{"mat", ":", 35 | RowBox[{"An", " ", "Implementation", " ", "of", " ", "Geometric", " ", 36 | RowBox[{"Semantic", " ", "**"}], "*", "Hill", " ", 37 | RowBox[{"Climber", "**"}], "*", " ", "Genetic", " ", "Programming", 38 | " ", "in", " ", "Mathematica", " ", "Using", " ", "Algebraic", " ", 39 | "Simplification"}]}]}], ";", "\[IndentingNewLine]", 40 | RowBox[{"Author", ":", 41 | RowBox[{"Alberto", " ", "Moraglio", " ", 42 | RowBox[{"(", 43 | RowBox[{ 44 | RowBox[{"albmor", "@", "gmail"}], ".", "com"}], ")"}]}]}], ";", "\n", 45 | "\[IndentingNewLine]", "Features", ";", "\[IndentingNewLine]", 46 | RowBox[{ 47 | RowBox[{ 48 | RowBox[{"-", " ", "The"}], " ", "fitness", " ", "landscape", " ", 49 | "seen", " ", "by", " ", "Geometric", " ", "Semantic", " ", "operators", 50 | " ", "is", " ", "always", " ", 51 | RowBox[{"unimodal", ".", " ", "A"}], " ", "hill"}], "-", 52 | RowBox[{ 53 | "climber", " ", "can", " ", "reach", " ", "the", " ", "optimum"}]}], 54 | ";", "\[IndentingNewLine]", 55 | RowBox[{ 56 | RowBox[{"-", " ", "Mutation"}], " ", "embeds", " ", "parent", " ", 57 | "expression", " ", "in", " ", "the", " ", "offspring", " ", 58 | "expression"}], ";", "\[IndentingNewLine]", 59 | RowBox[{ 60 | RowBox[{"-", " ", "Algebraic"}], " ", "simplification", " ", "of", " ", 61 | "offspring"}], ";", " ", "\[IndentingNewLine]", 62 | RowBox[{ 63 | RowBox[{"-", " ", "Offspring"}], " ", "size", " ", "growth", " ", 64 | "without", " ", "simplification", " ", "is", " ", "linear", " ", "in", 65 | " ", "the", " ", "number", " ", "of", " ", "generation", " ", 66 | RowBox[{"(", 67 | RowBox[{ 68 | "simplification", " ", "is", " ", "not", " ", "strictly", " ", 69 | "needed", " ", "for", " ", "space", " ", "efficeincy"}], ")"}]}], ";", 70 | "\[IndentingNewLine]", 71 | RowBox[{ 72 | RowBox[{"-", " ", "Final"}], " ", "solution", " ", "short", " ", "and", 73 | " ", "understandable"}], ";"}], "\[IndentingNewLine]", 74 | "\[IndentingNewLine]", "**", "**", "**", "**", "**", "**", "**", "**", "**", 75 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 76 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 77 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 78 | "**", "**", "**", "**", "**", "**"}], " ", "*)"}], "\[IndentingNewLine]", 79 | "\[IndentingNewLine]", "\[IndentingNewLine]", 80 | RowBox[{"(*", 81 | RowBox[{ 82 | RowBox[{"**", "**"}], "*", " ", "PARAMETERS"}], " ", "******)"}], 83 | "\[IndentingNewLine]", "\n", 84 | RowBox[{ 85 | RowBox[{ 86 | RowBox[{"NUMVARS", "=", "5"}], ";"}], " ", 87 | RowBox[{"(*", " ", 88 | RowBox[{"number", " ", "of", " ", "input", " ", "variables"}], " ", 89 | "*)"}], "\n", 90 | RowBox[{ 91 | RowBox[{"GENERATIONS", "=", "300"}], ";", " ", 92 | RowBox[{"(*", " ", 93 | RowBox[{"number", " ", "of", " ", "generations"}], " ", "*)"}], "\n", 94 | "\[IndentingNewLine]", 95 | RowBox[{"(*", 96 | RowBox[{"**", "**", "**", "**", "**", "**", "**", "**", "**"}], 97 | "*****)"}], "\[IndentingNewLine]", "\n", 98 | RowBox[{"vars", "=", 99 | RowBox[{"Table", "[", 100 | RowBox[{ 101 | RowBox[{"Symbol", "[", 102 | RowBox[{"\"\\"", "<>", 103 | RowBox[{"ToString", "[", "i", "]"}]}], "]"}], ",", 104 | RowBox[{"{", 105 | RowBox[{"i", ",", "NUMVARS"}], "}"}]}], "]"}]}], ";", 106 | RowBox[{"(*", " ", 107 | RowBox[{"variable", " ", "names"}], " ", "*)"}], "\[IndentingNewLine]", 108 | "\[IndentingNewLine]", 109 | RowBox[{"targetfunct", "=", 110 | RowBox[{"Apply", "[", 111 | RowBox[{"Xor", ",", " ", "vars"}], "]"}]}], ";", " ", 112 | RowBox[{"(*", " ", 113 | RowBox[{"parity", " ", "function"}], " ", "*)"}], "\[IndentingNewLine]", 114 | "\[IndentingNewLine]", 115 | RowBox[{ 116 | RowBox[{"fitness", "[", "individual_", "]"}], ":=", " ", 117 | RowBox[{"(*", " ", 118 | RowBox[{ 119 | RowBox[{"fitness", " ", 120 | RowBox[{"(", "error", ")"}], " ", "function"}], ",", " ", 121 | RowBox[{"lower", " ", "is", " ", "better"}]}], " ", "*)"}], 122 | "\[IndentingNewLine]", 123 | RowBox[{"Count", "[", 124 | RowBox[{ 125 | RowBox[{"MapThread", "[", 126 | RowBox[{"Xor", ",", 127 | RowBox[{"{", 128 | RowBox[{ 129 | RowBox[{"BooleanTable", "[", "targetfunct", "]"}], ",", 130 | RowBox[{"BooleanTable", "[", "individual", "]"}]}], "}"}]}], "]"}], 131 | ",", " ", "True"}], "]"}]}], ";"}], " ", "\[IndentingNewLine]", 132 | "\[IndentingNewLine]", 133 | RowBox[{ 134 | RowBox[{ 135 | RowBox[{"mutation", "[", "p_", "]"}], ":=", 136 | RowBox[{"Module", "[", 137 | RowBox[{ 138 | RowBox[{"{", "minterm", "}"}], ",", 139 | RowBox[{"(*", " ", 140 | RowBox[{ 141 | "mutation", " ", "substitutes", " ", "parent", " ", "and", " ", 142 | "minterm", " ", "expressions", " ", "in", " ", "mutation", " ", 143 | "scheme", " ", "to", " ", "make", " ", "offspring"}], " ", "*)"}], 144 | "\[IndentingNewLine]", 145 | RowBox[{ 146 | RowBox[{"minterm", "=", 147 | RowBox[{"BooleanMinterms", "[", 148 | RowBox[{ 149 | RowBox[{"{", 150 | RowBox[{"Table", "[", 151 | RowBox[{ 152 | RowBox[{"RandomInteger", "[", "]"}], ",", 153 | RowBox[{"{", "NUMVARS", "}"}]}], "]"}], "}"}], ",", "vars"}], 154 | "]"}]}], ";", "\[IndentingNewLine]", 155 | RowBox[{"If", "[", 156 | RowBox[{ 157 | RowBox[{ 158 | RowBox[{"RandomInteger", "[", "]"}], "\[Equal]", "0"}], ",", 159 | RowBox[{"p", " ", "||", " ", "minterm"}], ",", 160 | RowBox[{"p", " ", "&&", " ", 161 | RowBox[{"!", "minterm"}]}]}], "]"}]}]}], "]"}]}], ";"}], 162 | "\[IndentingNewLine]", "\[IndentingNewLine]", 163 | RowBox[{"(*", 164 | RowBox[{ 165 | RowBox[{"**", "**"}], "*", " ", "CLIMB"}], " ", "******)"}], 166 | "\[IndentingNewLine]", "\[IndentingNewLine]", 167 | RowBox[{ 168 | RowBox[{"curr", " ", "=", 169 | RowBox[{"BooleanFunction", "[", 170 | RowBox[{ 171 | RowBox[{"RandomInteger", "[", 172 | RowBox[{ 173 | RowBox[{"2", "^", 174 | RowBox[{"(", 175 | RowBox[{"2", "^", "NUMVARS"}], ")"}]}], " ", "-", " ", "1"}], 176 | "]"}], ",", " ", "vars"}], "]"}]}], ";", " ", 177 | RowBox[{"(*", " ", 178 | RowBox[{"initial", " ", "individual"}], " ", "*)"}], 179 | "\[IndentingNewLine]", 180 | RowBox[{"currfit", " ", "=", " ", 181 | RowBox[{"fitness", "[", "curr", "]"}]}], ";", " ", 182 | RowBox[{"(*", " ", 183 | RowBox[{"evaluate", " ", "fitness"}], " ", "*)"}], "\[IndentingNewLine]", 184 | RowBox[{"For", "[", 185 | RowBox[{ 186 | RowBox[{"gen", "=", "0"}], ",", 187 | RowBox[{"gen", "<", 188 | RowBox[{"GENERATIONS", "+", "1"}]}], ",", 189 | RowBox[{"gen", "++"}], ",", "\[IndentingNewLine]", 190 | RowBox[{ 191 | RowBox[{"off", " ", "=", " ", 192 | RowBox[{"mutation", "[", "curr", "]"}]}], ";", 193 | RowBox[{"(*", " ", 194 | RowBox[{"create", " ", "offspring"}], " ", "*)"}], 195 | "\[IndentingNewLine]", 196 | RowBox[{"offfit", " ", "=", " ", 197 | RowBox[{"fitness", "[", "off", "]"}]}], ";", 198 | RowBox[{"(*", " ", 199 | RowBox[{"fitness", " ", "offspring"}], " ", "*)"}], 200 | "\[IndentingNewLine]", 201 | RowBox[{"If", "[", 202 | RowBox[{ 203 | RowBox[{"offfit", " ", "<", " ", "currfit"}], ",", " ", 204 | "\[IndentingNewLine]", 205 | RowBox[{ 206 | RowBox[{"curr", " ", "=", " ", 207 | RowBox[{"Simplify", "[", 208 | RowBox[{"off", ",", " ", 209 | RowBox[{"TimeConstraint", "\[Rule]", "0.1"}]}], "]"}]}], ";", 210 | RowBox[{"(*", " ", 211 | RowBox[{ 212 | "offspring", " ", "is", " ", "simplified", " ", "and", " ", 213 | "replaces", " ", "parent", " ", "if", " ", "better"}], " ", 214 | "*)"}], "\[IndentingNewLine]", 215 | RowBox[{"currfit", " ", "=", " ", "offfit"}]}]}], "]"}], ";", " ", 216 | RowBox[{"(*", " ", 217 | RowBox[{"update", " ", "fitness"}], " ", "*)"}], 218 | "\[IndentingNewLine]", 219 | RowBox[{"If", "[", 220 | RowBox[{ 221 | RowBox[{ 222 | RowBox[{"Mod", "[", 223 | RowBox[{"gen", " ", ",", " ", "10"}], "]"}], "\[Equal]", "0"}], 224 | ",", " ", 225 | RowBox[{"Print", "[", 226 | RowBox[{ 227 | "\"\\"", ",", "gen", ",", " ", "\"\< fit: \>\"", ",", 228 | "currfit"}], "]"}]}], "]"}], ";"}]}], " ", 229 | RowBox[{"(*", " ", 230 | RowBox[{"print", " ", "stats"}], " ", "*)"}], "\[IndentingNewLine]", 231 | "]"}]}], " ", "\n", "\[IndentingNewLine]", 232 | RowBox[{ 233 | RowBox[{"Print", "[", "\"\\"", "]"}], ";"}], 234 | "\[IndentingNewLine]", 235 | RowBox[{ 236 | RowBox[{"Print", "[", "curr", "]"}], ";", 237 | RowBox[{"(*", " ", 238 | RowBox[{"genotype", " ", "of", " ", "final", " ", "solution"}], " ", 239 | "*)"}], "\n", 240 | RowBox[{"Print", "[", 241 | RowBox[{"curr", "/.", 242 | RowBox[{"{", 243 | RowBox[{ 244 | RowBox[{"x1", "->", "True"}], ",", 245 | RowBox[{"x2", "->", "True"}], ",", 246 | RowBox[{"x3", "->", "True"}], ",", 247 | RowBox[{"x4", "->", "True"}], ",", 248 | RowBox[{"x5", "->", "True"}]}], "}"}]}], "]"}], ";", 249 | RowBox[{"(*", " ", 250 | RowBox[{ 251 | "query", " ", "genotype", " ", "of", " ", "final", " ", "solution"}], 252 | " ", "*)"}], "\[IndentingNewLine]", "\n"}]}]}]], "Input", 253 | CellChangeTimes->{{3.6178615652943487`*^9, 3.6178617291847224`*^9}, { 254 | 3.6178618197119*^9, 3.617861839921056*^9}, {3.617861875817109*^9, 255 | 3.6178618836335564`*^9}, {3.617861920657674*^9, 3.6178619238388557`*^9}, { 256 | 3.617861958067814*^9, 3.617862073114394*^9}, {3.6178621197200594`*^9, 257 | 3.6178621307036877`*^9}, {3.617868039043626*^9, 3.6178680597558107`*^9}, { 258 | 3.6178694054347787`*^9, 3.617869423575816*^9}, {3.617869461450983*^9, 259 | 3.6178695208423796`*^9}, {3.617869630694663*^9, 3.61786964637856*^9}, { 260 | 3.6178699531861086`*^9, 3.6178699587894287`*^9}, {3.6178700442033143`*^9, 261 | 3.617870047954529*^9}, {3.6178701085449944`*^9, 3.6178701236508584`*^9}, { 262 | 3.617870158892874*^9, 3.617870213116976*^9}, {3.6178702734854283`*^9, 263 | 3.617870280698841*^9}, {3.6178703213861685`*^9, 3.617870399202619*^9}, { 264 | 3.6178704598820896`*^9, 3.6178704654924107`*^9}, {3.6178705041936245`*^9, 265 | 3.617870647223805*^9}, {3.617870731268612*^9, 3.6178707669156513`*^9}, { 266 | 3.617870815976457*^9, 3.6178708616390686`*^9}, {3.6178708974981203`*^9, 267 | 3.6178709185403233`*^9}, {3.617871034366948*^9, 3.617871149225518*^9}, { 268 | 3.6178712674552803`*^9, 3.6178713464547987`*^9}, {3.617871523168906*^9, 269 | 3.6178715885946484`*^9}, {3.61787167096836*^9, 3.6178717393442707`*^9}, { 270 | 3.617871783411791*^9, 3.617871814753584*^9}, 3.6178718828814807`*^9, { 271 | 3.617872136417982*^9, 3.6178721377630587`*^9}, {3.617872183613682*^9, 272 | 3.6178723369254503`*^9}, {3.617872367564203*^9, 3.617872408882566*^9}, { 273 | 3.617872461381569*^9, 3.617872515057639*^9}, {3.617872563890432*^9, 274 | 3.617872577510211*^9}, {3.617872664548189*^9, 3.6178726902776613`*^9}, { 275 | 3.6178727467788925`*^9, 3.617872765191946*^9}, {3.617872887079918*^9, 276 | 3.617872922988971*^9}, {3.6178729867686195`*^9, 3.6178730460770116`*^9}, { 277 | 3.617873083594157*^9, 3.617873110882718*^9}, {3.6178731435085845`*^9, 278 | 3.6178731764354677`*^9}, {3.6178734047155247`*^9, 279 | 3.6178734711863265`*^9}, {3.6178735139207706`*^9, 3.6178735252114162`*^9}, 280 | 3.6178735939083457`*^9, {3.617873836581226*^9, 3.617873875315441*^9}, { 281 | 3.617873991919111*^9, 3.61787403684268*^9}, 3.617874067851454*^9, { 282 | 3.617874180164878*^9, 3.6178742249974422`*^9}, {3.617874342500163*^9, 283 | 3.6178743631953464`*^9}, {3.6178744872604427`*^9, 3.617874504737442*^9}, { 284 | 3.6178745381523533`*^9, 3.617874620217047*^9}, {3.6178747152614837`*^9, 285 | 3.6178747237439685`*^9}, {3.617874837673485*^9, 3.6178750503996525`*^9}, { 286 | 3.6178751101130676`*^9, 3.6178751913527145`*^9}, {3.6178752759735546`*^9, 287 | 3.6178752989958715`*^9}, {3.617875335828978*^9, 3.617875368636854*^9}, { 288 | 3.617875411729319*^9, 3.617875442433075*^9}, {3.617879011463212*^9, 289 | 3.617879195609745*^9}, {3.6178819079778833`*^9, 3.6178819110390587`*^9}, 290 | 3.6178819462950745`*^9, {3.617881990513604*^9, 3.617882022420429*^9}, { 291 | 3.617882100677905*^9, 3.6178821090443835`*^9}, {3.617882355376473*^9, 292 | 3.617882361839843*^9}, {3.6178824124697385`*^9, 3.6178824126157465`*^9}, { 293 | 3.6178827039704113`*^9, 3.6178827656639404`*^9}, {3.617882802911071*^9, 294 | 3.6178830055656614`*^9}, {3.617883057594638*^9, 3.617883175181363*^9}, { 295 | 3.6178832078292303`*^9, 3.617883249834633*^9}, {3.6178833009385557`*^9, 296 | 3.6178833341904583`*^9}, {3.6178833807461205`*^9, 3.617883407559654*^9}, { 297 | 3.6178834814318795`*^9, 3.6178836345376368`*^9}, {3.617883717898405*^9, 298 | 3.6178837260558715`*^9}, {3.617887204268814*^9, 3.6178872184616256`*^9}, { 299 | 3.617964322287301*^9, 3.617964363386652*^9}, {3.617964406623125*^9, 300 | 3.6179644973153124`*^9}, {3.617964536628561*^9, 3.6179645853763494`*^9}, { 301 | 3.617964622472471*^9, 3.6179646934075284`*^9}, {3.6179647445884557`*^9, 302 | 3.617964992780651*^9}, {3.6179650401463604`*^9, 3.617965217194487*^9}, { 303 | 3.6179655319794917`*^9, 3.6179655866446185`*^9}, 3.617965676869779*^9, { 304 | 3.6179657608995852`*^9, 3.617965770916158*^9}, {3.617966174974269*^9, 305 | 3.617966175252285*^9}, {3.617966274365954*^9, 3.6179663013154955`*^9}, { 306 | 3.6179663457940397`*^9, 3.6179664542822447`*^9}, {3.617966602652731*^9, 307 | 3.617966801752119*^9}}] 308 | }, 309 | WindowSize->{1362, 656}, 310 | WindowMargins->{{Automatic, 0}, {Automatic, 0}}, 311 | FrontEndVersion->"8.0 for Microsoft Windows (64-bit) (November 7, 2010)", 312 | StyleDefinitions->"Default.nb" 313 | ] 314 | (* End of Notebook Content *) 315 | 316 | (* Internal cache information *) 317 | (*CellTagsOutline 318 | CellTagsIndex->{} 319 | *) 320 | (*CellTagsIndex 321 | CellTagsIndex->{} 322 | *) 323 | (*NotebookFileOutline 324 | Notebook[{ 325 | Cell[557, 20, 13848, 286, 1032, "Input"] 326 | } 327 | ] 328 | *) 329 | 330 | (* End of internal cache information *) 331 | -------------------------------------------------------------------------------- /TINY_GSGP.nb: -------------------------------------------------------------------------------- 1 | (* Content-type: application/vnd.wolfram.mathematica *) 2 | 3 | (*** Wolfram Notebook File ***) 4 | (* http://www.wolfram.com/nb *) 5 | 6 | (* CreatedBy='Mathematica 8.0' *) 7 | 8 | (*CacheID: 234*) 9 | (* Internal cache information: 10 | NotebookFileLineBreakTest 11 | NotebookFileLineBreakTest 12 | NotebookDataPosition[ 157, 7] 13 | NotebookDataLength[ 17917, 414] 14 | NotebookOptionsPosition[ 17672, 401] 15 | NotebookOutlinePosition[ 18015, 416] 16 | CellTagsIndexPosition[ 17972, 413] 17 | WindowFrame->Normal*) 18 | 19 | (* Beginning of Notebook Content *) 20 | Notebook[{ 21 | Cell[BoxData[ 22 | RowBox[{ 23 | RowBox[{"(*", " ", 24 | RowBox[{ 25 | RowBox[{ 26 | RowBox[{ 27 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 28 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 29 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 30 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 31 | "**", "**", "**", "**", "**"}], ";", "\[IndentingNewLine]", 32 | "\[IndentingNewLine]", 33 | RowBox[{"TINY_GSGP", ".", 34 | RowBox[{"mat", ":", 35 | RowBox[{ 36 | "A", " ", "Tiny", " ", "Implementation", " ", "of", " ", "Geometric", 37 | " ", "Semantic", " ", "Genetic", " ", "Programming", " ", "in", " ", 38 | "Mathematica", " ", "Using", " ", "Algebraic", " ", 39 | "Simplification"}]}]}], ";", "\[IndentingNewLine]", 40 | RowBox[{"Author", ":", 41 | RowBox[{"Alberto", " ", "Moraglio", " ", 42 | RowBox[{"(", 43 | RowBox[{ 44 | RowBox[{"albmor", "@", "gmail"}], ".", "com"}], ")"}]}]}], ";", "\n", 45 | "\[IndentingNewLine]", 46 | RowBox[{ 47 | "This", " ", "is", " ", "a", " ", "reimplementation", " ", "of", " ", 48 | RowBox[{"TINY_GSGP", ".", "py"}], " ", "in", " ", "Mathematica", " ", 49 | "to", " ", "compare", " ", "the", " ", "effect", " ", "of", " ", 50 | "simplification", " ", "of", " ", "offsrping"}], ";", " ", 51 | "\[IndentingNewLine]", "\[IndentingNewLine]", "Features", ";", 52 | "\[IndentingNewLine]", 53 | RowBox[{ 54 | RowBox[{"-", " ", "Individuals"}], " ", "are", " ", "represented", " ", 55 | "using", " ", "symbolic", " ", "expressions", " ", 56 | RowBox[{"(", 57 | RowBox[{"Boolean", " ", "expressions"}], ")"}]}], ";", 58 | "\[IndentingNewLine]", 59 | RowBox[{ 60 | RowBox[{"-", " ", "Uniform"}], " ", 61 | RowBox[{"initialisation", "/", "generation"}], " ", "of", " ", "random", 62 | " ", "expressions"}], ";", "\[IndentingNewLine]", 63 | RowBox[{ 64 | RowBox[{"-", " ", "Crossover"}], " ", "and", " ", "mutation", " ", 65 | "embed", " ", "parent", " ", "expressions", " ", "in", " ", "the", " ", 66 | "offspring", " ", "expression"}], ";", "\[IndentingNewLine]", 67 | RowBox[{ 68 | RowBox[{"-", " ", "Algebraic"}], " ", "simplification", " ", "of", " ", 69 | "offspring", " ", "prevents", " ", "exponential", " ", "growth"}], 70 | ";"}], "\[IndentingNewLine]", "\[IndentingNewLine]", "**", "**", "**", "**", 71 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 72 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 73 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 74 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**"}], " ", 75 | "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 76 | "\[IndentingNewLine]", 77 | RowBox[{"(*", 78 | RowBox[{ 79 | RowBox[{"**", "**"}], "*", " ", "PARAMETERS"}], " ", "******)"}], 80 | "\[IndentingNewLine]", "\n", 81 | RowBox[{ 82 | RowBox[{ 83 | RowBox[{"NUMVARS", "=", "5"}], ";"}], " ", 84 | RowBox[{"(*", " ", 85 | RowBox[{"number", " ", "of", " ", "input", " ", "variables"}], " ", 86 | "*)"}], "\n", 87 | RowBox[{"(*", " ", 88 | RowBox[{"DEPTH", "=", "4"}], " ", "*)"}], " ", 89 | RowBox[{"(*", " ", 90 | RowBox[{ 91 | "maximum", " ", "depth", " ", "of", " ", "expressions", " ", "in", " ", 92 | "the", " ", "initial", " ", "population"}], " ", "*)"}], "\n", 93 | RowBox[{ 94 | RowBox[{"POPSIZE", "=", "20"}], ";"}], " ", 95 | RowBox[{"(*", " ", 96 | RowBox[{"population", " ", "size"}], " ", "*)"}], "\n", 97 | RowBox[{ 98 | RowBox[{"GENERATIONS", "=", "30"}], ";"}], " ", 99 | RowBox[{"(*", " ", 100 | RowBox[{"number", " ", "of", " ", "generations"}], " ", "*)"}], "\n", 101 | RowBox[{ 102 | RowBox[{"TRUNC", "=", "0.5"}], ";", " ", 103 | RowBox[{"(*", " ", 104 | RowBox[{ 105 | "proportion", " ", "of", " ", "population", " ", "to", " ", "retain", 106 | " ", "in", " ", "truncation", " ", "selection"}], " ", "*)"}], 107 | "\[IndentingNewLine]", "\[IndentingNewLine]", 108 | RowBox[{"(*", 109 | RowBox[{"**", "**", "**", "**", "**", "**", "**", "**", "**"}], 110 | "*****)"}], "\n", "\[IndentingNewLine]", 111 | RowBox[{"vars", "=", 112 | RowBox[{"Table", "[", 113 | RowBox[{ 114 | RowBox[{"Symbol", "[", 115 | RowBox[{"\"\\"", "<>", 116 | RowBox[{"ToString", "[", "i", "]"}]}], "]"}], ",", 117 | RowBox[{"{", 118 | RowBox[{"i", ",", "NUMVARS"}], "}"}]}], "]"}]}], ";", 119 | RowBox[{"(*", " ", 120 | RowBox[{"variable", " ", "names"}], " ", "*)"}], "\[IndentingNewLine]", 121 | "\[IndentingNewLine]", 122 | RowBox[{"targetfunct", "=", 123 | RowBox[{"Apply", "[", 124 | RowBox[{"Xor", ",", " ", "vars"}], "]"}]}], ";", " ", 125 | RowBox[{"(*", " ", 126 | RowBox[{"parity", " ", "function"}], " ", "*)"}], "\[IndentingNewLine]", 127 | "\[IndentingNewLine]", 128 | RowBox[{ 129 | RowBox[{"fitness", "[", "individual_", "]"}], ":=", " ", 130 | RowBox[{"(*", " ", 131 | RowBox[{ 132 | RowBox[{"fitness", " ", 133 | RowBox[{"(", "error", ")"}], " ", "function"}], ",", " ", 134 | RowBox[{"lower", " ", "is", " ", "better"}]}], " ", "*)"}], 135 | "\[IndentingNewLine]", 136 | RowBox[{"Count", "[", 137 | RowBox[{ 138 | RowBox[{"MapThread", "[", 139 | RowBox[{"Xor", ",", 140 | RowBox[{"{", 141 | RowBox[{ 142 | RowBox[{"BooleanTable", "[", "targetfunct", "]"}], ",", 143 | RowBox[{"BooleanTable", "[", "individual", "]"}]}], "}"}]}], "]"}], 144 | ",", " ", "True"}], "]"}]}], ";"}], " ", "\[IndentingNewLine]", 145 | "\[IndentingNewLine]", 146 | RowBox[{ 147 | RowBox[{ 148 | RowBox[{"crossover", "[", 149 | RowBox[{"p1_", ",", "p2_"}], "]"}], ":=", 150 | RowBox[{"Module", "[", 151 | RowBox[{ 152 | RowBox[{"{", "mask", "}"}], ",", 153 | RowBox[{"(*", " ", 154 | RowBox[{ 155 | "corssover", " ", "substitutes", " ", "parent", " ", "and", " ", 156 | "mask", " ", "expressions", " ", "in", " ", "crossover", " ", 157 | "scheme", " ", "to", " ", "make", " ", "offspring"}], " ", "*)"}], 158 | "\[IndentingNewLine]", 159 | RowBox[{ 160 | RowBox[{"mask", "=", 161 | RowBox[{"BooleanFunction", "[", 162 | RowBox[{ 163 | RowBox[{"RandomInteger", "[", 164 | RowBox[{ 165 | RowBox[{"2", "^", 166 | RowBox[{"(", 167 | RowBox[{"2", "^", "NUMVARS"}], ")"}]}], " ", "-", " ", "1"}], 168 | "]"}], ",", " ", "vars"}], "]"}]}], ";", "\[IndentingNewLine]", 169 | RowBox[{ 170 | RowBox[{"(", 171 | RowBox[{"p1", " ", "&&", " ", "mask"}], ")"}], "||", 172 | RowBox[{"(", 173 | RowBox[{"p2", " ", "&&", " ", 174 | RowBox[{"!", "mask"}]}], ")"}]}]}]}], "]"}]}], ";"}], 175 | "\[IndentingNewLine]", "\[IndentingNewLine]", 176 | RowBox[{ 177 | RowBox[{ 178 | RowBox[{"mutation", "[", "p_", "]"}], ":=", 179 | RowBox[{"Module", "[", 180 | RowBox[{ 181 | RowBox[{"{", "minterm", "}"}], ",", 182 | RowBox[{"(*", " ", 183 | RowBox[{ 184 | "mutation", " ", "substitutes", " ", "parent", " ", "and", " ", 185 | "minterm", " ", "expressions", " ", "in", " ", "mutation", " ", 186 | "scheme", " ", "to", " ", "make", " ", "offspring"}], " ", "*)"}], 187 | "\[IndentingNewLine]", 188 | RowBox[{ 189 | RowBox[{"minterm", "=", 190 | RowBox[{"BooleanMinterms", "[", 191 | RowBox[{ 192 | RowBox[{"{", 193 | RowBox[{"Table", "[", 194 | RowBox[{ 195 | RowBox[{"RandomInteger", "[", "]"}], ",", 196 | RowBox[{"{", "NUMVARS", "}"}]}], "]"}], "}"}], ",", "vars"}], 197 | "]"}]}], ";", "\[IndentingNewLine]", 198 | RowBox[{"If", "[", 199 | RowBox[{ 200 | RowBox[{ 201 | RowBox[{"RandomInteger", "[", "]"}], "\[Equal]", "0"}], ",", 202 | RowBox[{"p", " ", "||", " ", "minterm"}], ",", 203 | RowBox[{"p", " ", "&&", " ", 204 | RowBox[{"!", "minterm"}]}]}], "]"}]}]}], "]"}]}], ";"}], 205 | "\[IndentingNewLine]", "\[IndentingNewLine]", 206 | RowBox[{"(*", 207 | RowBox[{ 208 | RowBox[{"**", "**"}], "*", " ", "EVOLVE"}], " ", "******)"}], 209 | "\[IndentingNewLine]", "\[IndentingNewLine]", 210 | RowBox[{ 211 | RowBox[{"pop", " ", "=", 212 | RowBox[{"Table", "[", 213 | RowBox[{ 214 | RowBox[{"BooleanFunction", "[", 215 | RowBox[{ 216 | RowBox[{"RandomInteger", "[", 217 | RowBox[{ 218 | RowBox[{"2", "^", 219 | RowBox[{"(", 220 | RowBox[{"2", "^", "NUMVARS"}], ")"}]}], " ", "-", " ", "1"}], 221 | "]"}], ",", " ", "vars"}], "]"}], ",", " ", 222 | RowBox[{"{", "POPSIZE", "}"}]}], "]"}]}], ";", " ", 223 | RowBox[{"(*", " ", 224 | RowBox[{ 225 | "initialise", " ", "population", " ", "with", " ", "uniform", " ", 226 | "distribution", " ", "on", " ", "functions"}], " ", "*)"}], 227 | "\[IndentingNewLine]", 228 | RowBox[{"For", "[", 229 | RowBox[{ 230 | RowBox[{"gen", "=", "0"}], ",", 231 | RowBox[{"gen", "<", 232 | RowBox[{"GENERATIONS", "+", "1"}]}], ",", 233 | RowBox[{"gen", "++"}], ",", "\[IndentingNewLine]", 234 | RowBox[{ 235 | RowBox[{"gradedpop", " ", "=", " ", 236 | RowBox[{"Table", "[", 237 | RowBox[{ 238 | RowBox[{"{", 239 | RowBox[{ 240 | RowBox[{"fitness", "[", 241 | RowBox[{"pop", "[", 242 | RowBox[{"[", "i", "]"}], "]"}], "]"}], ",", 243 | RowBox[{"pop", "[", 244 | RowBox[{"[", "i", "]"}], "]"}]}], "}"}], ",", 245 | RowBox[{"{", 246 | RowBox[{"i", ",", "POPSIZE"}], "}"}]}], "]"}]}], ";", 247 | RowBox[{"(*", " ", 248 | RowBox[{"evaluate", " ", "population", " ", "fitness"}], " ", "*)"}], 249 | "\[IndentingNewLine]", 250 | RowBox[{"sortedpop", " ", "=", " ", 251 | RowBox[{"Sort", "[", 252 | RowBox[{"gradedpop", ",", " ", 253 | RowBox[{ 254 | RowBox[{ 255 | RowBox[{"#1", "[", 256 | RowBox[{"[", "1", "]"}], "]"}], "<", 257 | RowBox[{"#2", "[", 258 | RowBox[{"[", "1", "]"}], "]"}]}], "&"}]}], "]"}]}], " ", ";", 259 | RowBox[{"(*", " ", 260 | RowBox[{"sort", " ", "population", " ", "on", " ", "fitness"}], " ", 261 | "*)"}], "\[IndentingNewLine]", 262 | RowBox[{"Print", "[", 263 | RowBox[{ 264 | "\"\\"", ",", "gen", ",", " ", "\"\< min fit: \>\"", ",", 265 | RowBox[{"sortedpop", "[", 266 | RowBox[{"[", 267 | RowBox[{"1", ",", "1"}], "]"}], "]"}], ",", "\"\< avg fit: \>\"", 268 | ",", " ", 269 | RowBox[{"N", "[", 270 | RowBox[{"Mean", "[", 271 | RowBox[{"sortedpop", "[", 272 | RowBox[{"[", 273 | RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}], "]"}]}], "]"}], 274 | ";", " ", 275 | RowBox[{"(*", " ", 276 | RowBox[{"print", " ", "stats"}], " ", "*)"}], "\[IndentingNewLine]", 277 | RowBox[{"parentpop", "=", 278 | RowBox[{"sortedpop", "[", 279 | RowBox[{"[", 280 | RowBox[{ 281 | RowBox[{"1", ";;", 282 | RowBox[{"Round", "[", 283 | RowBox[{"TRUNC", "*", "POPSIZE"}], "]"}]}], ",", "2"}], "]"}], 284 | "]"}]}], ";", " ", 285 | RowBox[{"(*", " ", 286 | RowBox[{"selected", " ", "parents"}], " ", "*)"}], 287 | "\[IndentingNewLine]", 288 | RowBox[{"If", "[", 289 | RowBox[{ 290 | RowBox[{"gen", "\[Equal]", "GENERATIONS"}], ",", " ", 291 | RowBox[{"Break", "[", "]"}]}], "]"}], ";", "\[IndentingNewLine]", 292 | RowBox[{"For", "[", 293 | RowBox[{ 294 | RowBox[{"i", "=", "1"}], ",", 295 | RowBox[{"i", "<", 296 | RowBox[{"POPSIZE", "+", "1"}]}], ",", 297 | RowBox[{"i", "++"}], ",", 298 | RowBox[{"(*", " ", 299 | RowBox[{"create", " ", "offspring", " ", "population"}], " ", 300 | "*)"}], "\[IndentingNewLine]", 301 | RowBox[{ 302 | RowBox[{"par", "=", 303 | RowBox[{"RandomSample", "[", 304 | RowBox[{"parentpop", ",", "2"}], "]"}]}], ";", " ", 305 | RowBox[{"(*", " ", 306 | RowBox[{"pick", " ", "two", " ", "random", " ", "parents"}], " ", 307 | "*)"}], "\[IndentingNewLine]", 308 | RowBox[{ 309 | RowBox[{"pop", "[", 310 | RowBox[{"[", "i", "]"}], "]"}], "=", 311 | RowBox[{"Simplify", "[", 312 | RowBox[{ 313 | RowBox[{"mutation", "[", 314 | RowBox[{"crossover", "[", 315 | RowBox[{ 316 | RowBox[{"par", "[", 317 | RowBox[{"[", "1", "]"}], "]"}], ",", 318 | RowBox[{"par", "[", 319 | RowBox[{"[", "2", "]"}], "]"}]}], "]"}], "]"}], ",", 320 | RowBox[{"TimeConstraint", "\[Rule]", "0.1"}]}], "]"}]}], ";"}]}], 321 | " ", 322 | RowBox[{"(*", " ", 323 | RowBox[{ 324 | "create", " ", "offspring", " ", "and", " ", "simplify", " ", "it"}], 325 | " ", "*)"}], "\[IndentingNewLine]", "]"}]}]}], " ", 326 | "\[IndentingNewLine]", "]"}]}], " ", "\[IndentingNewLine]", "\n", 327 | RowBox[{ 328 | RowBox[{ 329 | "Print", "[", "\"\\"", "]"}], ";"}], 330 | "\[IndentingNewLine]", 331 | RowBox[{ 332 | RowBox[{"Print", "[", 333 | RowBox[{"sortedpop", "[", 334 | RowBox[{"[", "1", "]"}], "]"}], "]"}], ";", 335 | RowBox[{"(*", " ", 336 | RowBox[{"genotype", " ", "of", " ", "final", " ", "solution"}], " ", 337 | "*)"}], "\n", 338 | RowBox[{"Print", "[", 339 | RowBox[{ 340 | RowBox[{"sortedpop", "[", 341 | RowBox[{"[", 342 | RowBox[{"1", ",", "2"}], "]"}], "]"}], "/.", 343 | RowBox[{"{", 344 | RowBox[{ 345 | RowBox[{"x1", "->", "True"}], ",", 346 | RowBox[{"x2", "->", "True"}], ",", 347 | RowBox[{"x3", "->", "True"}], ",", 348 | RowBox[{"x4", "->", "True"}], ",", 349 | RowBox[{"x5", "->", "True"}]}], "}"}]}], "]"}], ";", 350 | RowBox[{"(*", " ", 351 | RowBox[{ 352 | "query", " ", "genotype", " ", "of", " ", "final", " ", "solution"}], 353 | " ", "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", "\n", 354 | "\[IndentingNewLine]"}]}]}]], "Input", 355 | CellChangeTimes->{{3.6178615652943487`*^9, 3.6178617291847224`*^9}, { 356 | 3.6178618197119*^9, 3.617861839921056*^9}, {3.617861875817109*^9, 357 | 3.6178618836335564`*^9}, {3.617861920657674*^9, 3.6178619238388557`*^9}, { 358 | 3.617861958067814*^9, 3.617862073114394*^9}, {3.6178621197200594`*^9, 359 | 3.6178621307036877`*^9}, {3.617868039043626*^9, 3.6178680597558107`*^9}, { 360 | 3.6178694054347787`*^9, 3.617869423575816*^9}, {3.617869461450983*^9, 361 | 3.6178695208423796`*^9}, {3.617869630694663*^9, 3.61786964637856*^9}, { 362 | 3.6178699531861086`*^9, 3.6178699587894287`*^9}, {3.6178700442033143`*^9, 363 | 3.617870047954529*^9}, {3.6178701085449944`*^9, 3.6178701236508584`*^9}, { 364 | 3.617870158892874*^9, 3.617870213116976*^9}, {3.6178702734854283`*^9, 365 | 3.617870280698841*^9}, {3.6178703213861685`*^9, 3.617870399202619*^9}, { 366 | 3.6178704598820896`*^9, 3.6178704654924107`*^9}, {3.6178705041936245`*^9, 367 | 3.617870647223805*^9}, {3.617870731268612*^9, 3.6178707669156513`*^9}, { 368 | 3.617870815976457*^9, 3.6178708616390686`*^9}, {3.6178708974981203`*^9, 369 | 3.6178709185403233`*^9}, {3.617871034366948*^9, 3.617871149225518*^9}, { 370 | 3.6178712674552803`*^9, 3.6178713464547987`*^9}, {3.617871523168906*^9, 371 | 3.6178715885946484`*^9}, {3.61787167096836*^9, 3.6178717393442707`*^9}, { 372 | 3.617871783411791*^9, 3.617871814753584*^9}, 3.6178718828814807`*^9, { 373 | 3.617872136417982*^9, 3.6178721377630587`*^9}, {3.617872183613682*^9, 374 | 3.6178723369254503`*^9}, {3.617872367564203*^9, 3.617872408882566*^9}, { 375 | 3.617872461381569*^9, 3.617872515057639*^9}, {3.617872563890432*^9, 376 | 3.617872577510211*^9}, {3.617872664548189*^9, 3.6178726902776613`*^9}, { 377 | 3.6178727467788925`*^9, 3.617872765191946*^9}, {3.617872887079918*^9, 378 | 3.617872922988971*^9}, {3.6178729867686195`*^9, 3.6178730460770116`*^9}, { 379 | 3.617873083594157*^9, 3.617873110882718*^9}, {3.6178731435085845`*^9, 380 | 3.6178731764354677`*^9}, {3.6178734047155247`*^9, 381 | 3.6178734711863265`*^9}, {3.6178735139207706`*^9, 3.6178735252114162`*^9}, 382 | 3.6178735939083457`*^9, {3.617873836581226*^9, 3.617873875315441*^9}, { 383 | 3.617873991919111*^9, 3.61787403684268*^9}, 3.617874067851454*^9, { 384 | 3.617874180164878*^9, 3.6178742249974422`*^9}, {3.617874342500163*^9, 385 | 3.6178743631953464`*^9}, {3.6178744872604427`*^9, 3.617874504737442*^9}, { 386 | 3.6178745381523533`*^9, 3.617874620217047*^9}, {3.6178747152614837`*^9, 387 | 3.6178747237439685`*^9}, {3.617874837673485*^9, 3.6178750503996525`*^9}, { 388 | 3.6178751101130676`*^9, 3.6178751913527145`*^9}, {3.6178752759735546`*^9, 389 | 3.6178752989958715`*^9}, {3.617875335828978*^9, 3.617875368636854*^9}, { 390 | 3.617875411729319*^9, 3.617875442433075*^9}, {3.617879011463212*^9, 391 | 3.617879195609745*^9}, {3.6178819079778833`*^9, 3.6178819110390587`*^9}, 392 | 3.6178819462950745`*^9, {3.617881990513604*^9, 3.617882022420429*^9}, { 393 | 3.617882100677905*^9, 3.6178821090443835`*^9}, {3.617882355376473*^9, 394 | 3.617882361839843*^9}, {3.6178824124697385`*^9, 3.6178824126157465`*^9}, { 395 | 3.6178827039704113`*^9, 3.6178827656639404`*^9}, {3.617882802911071*^9, 396 | 3.6178830055656614`*^9}, {3.617883057594638*^9, 3.617883175181363*^9}, { 397 | 3.6178832078292303`*^9, 3.617883249834633*^9}, {3.6178833009385557`*^9, 398 | 3.6178833341904583`*^9}, {3.6178833807461205`*^9, 3.617883407559654*^9}, { 399 | 3.6178834814318795`*^9, 3.6178836345376368`*^9}, {3.617883717898405*^9, 400 | 3.6178837260558715`*^9}, {3.617887204268814*^9, 3.6178872184616256`*^9}}] 401 | }, 402 | WindowSize->{1362, 656}, 403 | WindowMargins->{{Automatic, 0}, {Automatic, 0}}, 404 | FrontEndVersion->"8.0 for Microsoft Windows (64-bit) (November 7, 2010)", 405 | StyleDefinitions->"Default.nb" 406 | ] 407 | (* End of Notebook Content *) 408 | 409 | (* Internal cache information *) 410 | (*CellTagsOutline 411 | CellTagsIndex->{} 412 | *) 413 | (*CellTagsIndex 414 | CellTagsIndex->{} 415 | *) 416 | (*NotebookFileOutline 417 | Notebook[{ 418 | Cell[557, 20, 17111, 379, 1272, "Input"] 419 | } 420 | ] 421 | *) 422 | 423 | (* End of internal cache information *) 424 | -------------------------------------------------------------------------------- /TINY_GSHCGP _ARIT.nb: -------------------------------------------------------------------------------- 1 | (* Content-type: application/vnd.wolfram.mathematica *) 2 | 3 | (*** Wolfram Notebook File ***) 4 | (* http://www.wolfram.com/nb *) 5 | 6 | (* CreatedBy='Mathematica 8.0' *) 7 | 8 | (*CacheID: 234*) 9 | (* Internal cache information: 10 | NotebookFileLineBreakTest 11 | NotebookFileLineBreakTest 12 | NotebookDataPosition[ 157, 7] 13 | NotebookDataLength[ 23771, 518] 14 | NotebookOptionsPosition[ 23525, 505] 15 | NotebookOutlinePosition[ 23869, 520] 16 | CellTagsIndexPosition[ 23826, 517] 17 | WindowFrame->Normal*) 18 | 19 | (* Beginning of Notebook Content *) 20 | Notebook[{ 21 | Cell[BoxData[ 22 | RowBox[{ 23 | RowBox[{"(*", " ", 24 | RowBox[{ 25 | RowBox[{ 26 | RowBox[{ 27 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 28 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 29 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 30 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 31 | "**", "**", "**", "**", "**"}], ";", "\[IndentingNewLine]", 32 | "\[IndentingNewLine]", 33 | RowBox[{"TINY_GSHCGP", 34 | RowBox[{"_ARIT", ".", 35 | RowBox[{"nb", ":", " ", 36 | RowBox[{ 37 | "Geometric", " ", "Semantic", " ", "Hill", " ", "Climber", " ", 38 | "Genetic", " ", "Programming", " ", "in", " ", "Mathematica", " ", 39 | "for", " ", "Arithmetic", " ", "Expressions"}]}]}]}], ";", 40 | "\[IndentingNewLine]", 41 | RowBox[{"Author", ":", 42 | RowBox[{"Alberto", " ", "Moraglio", " ", 43 | RowBox[{"(", 44 | RowBox[{ 45 | RowBox[{"albmor", "@", "gmail"}], ".", "com"}], ")"}]}]}], ";", "\n", 46 | "\[IndentingNewLine]", "Features", ";", "\[IndentingNewLine]", 47 | RowBox[{ 48 | RowBox[{"-", " ", "It"}], " ", "searches", " ", "the", " ", "space", 49 | " ", "of", " ", "arithmentic", " ", "expressions", " ", 50 | RowBox[{"(", 51 | RowBox[{ 52 | "polynomials", " ", "or", " ", "fractional", " ", "polynomials", " ", 53 | "if", " ", "division", " ", "is", " ", "used"}], ")"}]}], ";", 54 | "\[IndentingNewLine]", 55 | RowBox[{ 56 | RowBox[{"-", " ", "Fithess"}], " ", "is", " ", "based", " ", "on", " ", 57 | "a", " ", "training", " ", "set", " ", 58 | RowBox[{"(", 59 | RowBox[{ 60 | "not", " ", "on", " ", "all", " ", "inputs", " ", "as", " ", "for", 61 | " ", "Boolean", " ", "expressions"}], ")"}]}], ";", " ", 62 | "\[IndentingNewLine]", 63 | RowBox[{ 64 | RowBox[{"-", " ", "Algebraic"}], " ", "simplification", " ", "of", " ", 65 | "offspring"}], ";", "\[IndentingNewLine]", 66 | RowBox[{ 67 | RowBox[{"-", " ", "Generalisation"}], " ", "test", " ", 68 | RowBox[{"(", 69 | RowBox[{"on", " ", "unseen", " ", "examples"}], ")"}]}], ";"}], " ", 70 | "\[IndentingNewLine]", "\[IndentingNewLine]", "**", "**", "**", "**", "**", 71 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 72 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 73 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 74 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**"}], " ", 75 | "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 76 | "\[IndentingNewLine]", 77 | RowBox[{"(*", 78 | RowBox[{ 79 | RowBox[{"**", "**"}], "*", " ", "PARAMETERS"}], " ", "******)"}], 80 | "\[IndentingNewLine]", "\n", 81 | RowBox[{ 82 | RowBox[{ 83 | RowBox[{"NUMVARS", "=", "5"}], ";"}], " ", 84 | RowBox[{"(*", " ", 85 | RowBox[{"number", " ", "of", " ", "input", " ", "variables"}], " ", 86 | "*)"}], "\n", 87 | RowBox[{ 88 | RowBox[{"DEPTH", "=", "4"}], ";", " ", 89 | RowBox[{"(*", " ", 90 | RowBox[{ 91 | "maximum", " ", "depth", " ", "of", " ", "expressions", " ", "in", " ", 92 | "the", " ", "initial", " ", "population"}], " ", "*)"}], 93 | "\[IndentingNewLine]", 94 | RowBox[{"GENERATIONS", "=", "1000"}], ";", " ", 95 | RowBox[{"(*", " ", 96 | RowBox[{"number", " ", "of", " ", "generations"}], " ", "*)"}], 97 | "\[IndentingNewLine]", 98 | RowBox[{"NUMCASES", " ", "=", " ", "10000"}], ";", " ", 99 | RowBox[{"(*", " ", 100 | RowBox[{"number", " ", "of", " ", "training", " ", "examples"}], " ", 101 | "*)"}], "\n", 102 | RowBox[{"MUTSTEP", " ", "=", " ", "0.01"}], ";", " ", 103 | RowBox[{"(*", " ", 104 | RowBox[{"mutation", " ", "step", " ", "size"}], " ", "*)"}], "\n", 105 | "\[IndentingNewLine]", 106 | RowBox[{"(*", 107 | RowBox[{"**", "**", "**", "**", "**", "**", "**", "**", "**"}], 108 | "*****)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 109 | RowBox[{"vars", "=", 110 | RowBox[{"Table", "[", 111 | RowBox[{ 112 | RowBox[{"Symbol", "[", 113 | RowBox[{"\"\\"", "<>", 114 | RowBox[{"ToString", "[", "i", "]"}]}], "]"}], ",", 115 | RowBox[{"{", 116 | RowBox[{"i", ",", "NUMVARS"}], "}"}]}], "]"}]}], ";", 117 | RowBox[{"(*", " ", 118 | RowBox[{"variable", " ", "names"}], " ", "*)"}], "\[IndentingNewLine]", 119 | "\[IndentingNewLine]", 120 | RowBox[{ 121 | RowBox[{"randexpr", "[", "dep_", "]"}], ":=", " ", 122 | RowBox[{"(*", " ", 123 | RowBox[{ 124 | "Create", " ", "a", " ", "random", " ", "arithmetic", " ", 125 | "expression"}], " ", "*)"}], "\[IndentingNewLine]", 126 | RowBox[{"If", "[", 127 | RowBox[{ 128 | RowBox[{ 129 | RowBox[{"dep", "\[Equal]", "1"}], " ", "||", " ", 130 | RowBox[{ 131 | RowBox[{"RandomReal", "[", "]"}], " ", "<", " ", 132 | RowBox[{"N", "[", 133 | RowBox[{"1.0", "/", 134 | RowBox[{"(", 135 | RowBox[{ 136 | RowBox[{"2", "^", "dep"}], "-", "1"}], ")"}]}], "]"}]}]}], ",", 137 | " ", 138 | RowBox[{"(*", " ", "terminal", " ", "*)"}], "\[IndentingNewLine]", 139 | RowBox[{"If", "[", 140 | RowBox[{ 141 | RowBox[{ 142 | RowBox[{"RandomReal", "[", "]"}], ">", 143 | RowBox[{"N", "[", 144 | RowBox[{"1", "/", 145 | RowBox[{"(", 146 | RowBox[{"NUMVARS", "+", "1"}], ")"}]}], "]"}]}], ",", " ", 147 | "\[IndentingNewLine]", 148 | RowBox[{"Return", "[", 149 | RowBox[{"RandomChoice", "[", "vars", "]"}], "]"}], ",", " ", 150 | RowBox[{"(*", " ", "variable", " ", "*)"}], "\[IndentingNewLine]", 151 | RowBox[{"RandomReal", "[", 152 | RowBox[{"{", 153 | RowBox[{ 154 | RowBox[{"-", "1"}], ",", "1"}], "}"}], "]"}]}], "]"}], ",", " ", 155 | RowBox[{"(*", " ", "number", " ", "*)"}], "\[IndentingNewLine]", 156 | RowBox[{"If", "[", 157 | RowBox[{ 158 | RowBox[{ 159 | RowBox[{"RandomReal", "[", "]"}], "<", 160 | RowBox[{"N", "[", 161 | RowBox[{"1.0", "/", "5"}], "]"}]}], ",", " ", 162 | "\[IndentingNewLine]", 163 | RowBox[{"Return", "[", 164 | RowBox[{"-", " ", 165 | RowBox[{"randexpr", "[", 166 | RowBox[{"dep", "-", "1"}], "]"}]}], "]"}], ",", " ", 167 | RowBox[{"(*", " ", 168 | RowBox[{"unary", " ", "operation"}], " ", "*)"}], 169 | "\[IndentingNewLine]", 170 | RowBox[{"Return", "[", 171 | RowBox[{"Apply", "[", 172 | RowBox[{ 173 | RowBox[{"RandomChoice", "[", 174 | RowBox[{"{", 175 | RowBox[{ 176 | RowBox[{ 177 | RowBox[{"(", 178 | RowBox[{"#1", "+", "#2"}], ")"}], "&"}], ",", " ", 179 | RowBox[{ 180 | RowBox[{"(", 181 | RowBox[{"#1", "-", "#2"}], ")"}], "&"}], ",", 182 | RowBox[{ 183 | RowBox[{"(", 184 | RowBox[{"#1", "*", "#2"}], ")"}], "&"}]}], " ", 185 | RowBox[{"(*", 186 | RowBox[{",", " ", 187 | RowBox[{ 188 | RowBox[{"(", 189 | RowBox[{"#1", "/", "#2"}], ")"}], "&"}]}], "*)"}], "}"}], 190 | "]"}], ",", 191 | RowBox[{"{", 192 | RowBox[{ 193 | RowBox[{"randexpr", "[", 194 | RowBox[{"dep", "-", "1"}], "]"}], ",", 195 | RowBox[{"randexpr", "[", 196 | RowBox[{"dep", "-", "1"}], "]"}]}], "}"}]}], "]"}], "]"}]}], 197 | "]"}]}], "]"}]}]}], " ", 198 | RowBox[{"(*", " ", 199 | RowBox[{"binary", " ", "operations"}], " ", "*)"}], "\[IndentingNewLine]", 200 | "\[IndentingNewLine]", 201 | RowBox[{"(*", 202 | RowBox[{ 203 | RowBox[{"**", "**"}], "*", " ", "TRAINING", " ", "SET"}], " ", 204 | "******)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 205 | RowBox[{ 206 | RowBox[{"targetexpr", "=", 207 | RowBox[{"randexpr", "[", "DEPTH", "]"}]}], ";", " ", 208 | RowBox[{"(*", " ", 209 | RowBox[{ 210 | "target", " ", "is", " ", "a", " ", "random", " ", "expression"}], " ", 211 | "*)"}], "\[IndentingNewLine]", 212 | RowBox[{"Print", "[", 213 | RowBox[{"\"\\"", ",", " ", "targetexpr"}], "]"}], 214 | ";"}], "\[IndentingNewLine]", 215 | RowBox[{ 216 | RowBox[{"targetfunct", " ", "=", " ", 217 | RowBox[{"Function", "[", 218 | RowBox[{ 219 | RowBox[{"Evaluate", "[", "vars", "]"}], ",", 220 | RowBox[{"Evaluate", "[", "targetexpr", "]"}]}], "]"}]}], ";", " ", 221 | RowBox[{"(*", " ", 222 | RowBox[{ 223 | "convert", " ", "target", " ", "into", " ", "a", " ", "function"}], " ", 224 | "*)"}], "\[IndentingNewLine]", 225 | RowBox[{"traininginputs", " ", "=", " ", 226 | RowBox[{"Table", "[", 227 | RowBox[{ 228 | RowBox[{"RandomReal", "[", 229 | RowBox[{ 230 | RowBox[{"{", 231 | RowBox[{ 232 | RowBox[{"-", "1"}], ",", "1"}], "}"}], ",", "NUMVARS"}], "]"}], 233 | ",", 234 | RowBox[{"{", "NUMCASES", "}"}]}], "]"}]}], ";", " ", 235 | RowBox[{"(*", " ", 236 | RowBox[{ 237 | "generate", " ", "training", " ", "inputs", " ", "uniformly", " ", "at", 238 | " ", "random"}], " ", "*)"}], "\[IndentingNewLine]", 239 | RowBox[{"trainingoutputs", " ", "=", 240 | RowBox[{"MapThread", "[", 241 | RowBox[{"targetfunct", ",", "traininginputs"}], "]"}]}], ";", " ", 242 | RowBox[{"(*", " ", 243 | RowBox[{"compute", " ", "target", " ", "outputs"}], " ", "*)"}], 244 | "\[IndentingNewLine]", "\[IndentingNewLine]", 245 | RowBox[{"(*", 246 | RowBox[{ 247 | RowBox[{ 248 | RowBox[{ 249 | RowBox[{ 250 | RowBox[{"**", "**"}], "**"}], "**"}], "**"}], "**"}], 251 | "*************)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 252 | RowBox[{ 253 | RowBox[{"fitness", "[", "individual_", "]"}], ":=", " ", 254 | RowBox[{"Module", "[", 255 | RowBox[{ 256 | RowBox[{"{", 257 | RowBox[{"indfunct", ",", " ", "indoutputs"}], "}"}], ",", 258 | RowBox[{"(*", " ", 259 | RowBox[{ 260 | RowBox[{"fitness", " ", 261 | RowBox[{"(", "error", ")"}], " ", "function"}], ",", " ", 262 | RowBox[{"lower", " ", "is", " ", "better"}]}], " ", "*)"}], 263 | "\[IndentingNewLine]", 264 | RowBox[{ 265 | RowBox[{"indfunct", "=", 266 | RowBox[{"Function", "[", 267 | RowBox[{ 268 | RowBox[{"Evaluate", "[", "vars", "]"}], ",", 269 | RowBox[{"Evaluate", "[", "individual", "]"}]}], "]"}]}], ";", " ", 270 | RowBox[{"(*", " ", 271 | RowBox[{ 272 | "convert", " ", "individual", " ", "into", " ", "a", " ", 273 | "function"}], " ", "*)"}], "\[IndentingNewLine]", 274 | RowBox[{"indoutputs", " ", "=", 275 | RowBox[{"MapThread", "[", 276 | RowBox[{"indfunct", ",", "traininginputs"}], "]"}]}], ";", " ", 277 | RowBox[{"(*", " ", 278 | RowBox[{"compute", " ", "individual", " ", "outputs"}], " ", "*)"}], 279 | "\[IndentingNewLine]", 280 | RowBox[{"EuclideanDistance", "[", 281 | RowBox[{"indoutputs", ",", "trainingoutputs"}], "]"}]}]}], "]"}]}], 282 | " ", ";", " ", 283 | RowBox[{"(*", " ", 284 | RowBox[{ 285 | "finess", " ", "is", " ", "distance", " ", "between", " ", "output", " ", 286 | "vectors"}], " ", "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 287 | RowBox[{ 288 | RowBox[{"mutation", "[", "p_", "]"}], ":=", " ", 289 | RowBox[{"p", " ", "+", 290 | RowBox[{"MUTSTEP", " ", "*", " ", 291 | RowBox[{"(", 292 | RowBox[{ 293 | RowBox[{"randexpr", "[", "DEPTH", "]"}], "-", 294 | RowBox[{"randexpr", "[", "DEPTH", "]"}]}], ")"}]}]}]}], ";", " ", 295 | RowBox[{"(*", " ", 296 | RowBox[{ 297 | RowBox[{ 298 | "mutation", " ", "perturbs", " ", "parent", " ", "with", " ", "zero"}], 299 | "-", 300 | RowBox[{"average", " ", "random", " ", "function"}]}], " ", "*)"}], 301 | "\[IndentingNewLine]", "\[IndentingNewLine]", 302 | RowBox[{"(*", 303 | RowBox[{ 304 | RowBox[{"**", "**"}], "*", " ", "CLIMB"}], " ", "******)"}], 305 | "\[IndentingNewLine]", "\[IndentingNewLine]", 306 | RowBox[{"curr", " ", "=", 307 | RowBox[{"randexpr", "[", "DEPTH", "]"}]}], ";", " ", 308 | RowBox[{"(*", " ", 309 | RowBox[{"initial", " ", "individual"}], " ", "*)"}], 310 | "\[IndentingNewLine]", 311 | RowBox[{"currfit", " ", "=", " ", 312 | RowBox[{"fitness", "[", "curr", "]"}]}], ";", " ", 313 | RowBox[{"(*", " ", 314 | RowBox[{"evaluate", " ", "fitness"}], " ", "*)"}], "\[IndentingNewLine]", 315 | RowBox[{"For", "[", 316 | RowBox[{ 317 | RowBox[{"gen", "=", "0"}], ",", 318 | RowBox[{"gen", "<", 319 | RowBox[{"GENERATIONS", "+", "1"}]}], ",", 320 | RowBox[{"gen", "++"}], ",", "\[IndentingNewLine]", 321 | RowBox[{ 322 | RowBox[{"off", " ", "=", " ", 323 | RowBox[{"mutation", "[", "curr", "]"}]}], ";", 324 | RowBox[{"(*", " ", 325 | RowBox[{"create", " ", "offspring"}], " ", "*)"}], 326 | "\[IndentingNewLine]", 327 | RowBox[{"offfit", " ", "=", " ", 328 | RowBox[{"fitness", "[", "off", "]"}]}], ";", 329 | RowBox[{"(*", " ", 330 | RowBox[{"fitness", " ", "offspring"}], " ", "*)"}], 331 | "\[IndentingNewLine]", 332 | RowBox[{"If", "[", 333 | RowBox[{ 334 | RowBox[{"offfit", " ", "<", " ", "currfit"}], ",", " ", 335 | "\[IndentingNewLine]", 336 | RowBox[{ 337 | RowBox[{"curr", " ", "=", " ", 338 | RowBox[{"Simplify", "[", 339 | RowBox[{"off", ",", " ", 340 | RowBox[{"TimeConstraint", "\[Rule]", "0.1"}]}], "]"}]}], ";", 341 | RowBox[{"(*", " ", 342 | RowBox[{ 343 | "offspring", " ", "is", " ", "simplified", " ", "and", " ", 344 | "replaces", " ", "parent", " ", "if", " ", "better"}], " ", 345 | "*)"}], "\[IndentingNewLine]", 346 | RowBox[{"currfit", " ", "=", " ", "offfit"}]}]}], "]"}], ";", " ", 347 | RowBox[{"(*", " ", 348 | RowBox[{"update", " ", "fitness"}], " ", "*)"}], 349 | "\[IndentingNewLine]", 350 | RowBox[{"If", "[", 351 | RowBox[{ 352 | RowBox[{ 353 | RowBox[{"Mod", "[", 354 | RowBox[{"gen", " ", ",", " ", "10"}], "]"}], "\[Equal]", "0"}], 355 | ",", " ", 356 | RowBox[{"Print", "[", 357 | RowBox[{ 358 | "\"\\"", ",", "gen", ",", " ", "\"\< fit: \>\"", ",", 359 | "currfit"}], "]"}]}], "]"}], ";"}]}], " ", 360 | RowBox[{"(*", " ", 361 | RowBox[{"print", " ", "stats"}], " ", "*)"}], "\[IndentingNewLine]", 362 | "]"}]}], " ", "\n", "\[IndentingNewLine]", 363 | RowBox[{ 364 | RowBox[{"Print", "[", "\"\\"", "]"}], ";"}], 365 | "\[IndentingNewLine]", 366 | RowBox[{ 367 | RowBox[{"Print", "[", "curr", "]"}], ";", 368 | RowBox[{"(*", " ", 369 | RowBox[{"genotype", " ", "of", " ", "final", " ", "solution"}], " ", 370 | "*)"}], "\[IndentingNewLine]", "\n", 371 | RowBox[{"(*", 372 | RowBox[{ 373 | RowBox[{"**", "**"}], "*", " ", "GENERALISATION", " ", "TEST"}], " ", 374 | "******)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 375 | RowBox[{"Print", "[", 376 | RowBox[{"\"\\"", ",", " ", "currfit"}], "]"}], ";", 377 | " ", 378 | RowBox[{"(*", " ", 379 | RowBox[{ 380 | "fitness", " ", "of", " ", "best", " ", "individual", " ", "is", " ", 381 | "training", " ", "error"}], " ", "*)"}], "\[IndentingNewLine]", 382 | RowBox[{"traininginputs", " ", "=", " ", 383 | RowBox[{"Table", "[", 384 | RowBox[{ 385 | RowBox[{"RandomReal", "[", 386 | RowBox[{ 387 | RowBox[{"{", 388 | RowBox[{ 389 | RowBox[{"-", "1"}], ",", "1"}], "}"}], ",", "NUMVARS"}], "]"}], 390 | ",", 391 | RowBox[{"{", "NUMCASES", "}"}]}], "]"}]}], ";", " ", 392 | RowBox[{"(*", " ", 393 | RowBox[{ 394 | "generate", " ", "test", " ", "inputs", " ", "uniformly", " ", "at", " ", 395 | "random"}], " ", "*)"}], "\[IndentingNewLine]", 396 | RowBox[{"trainingoutputs", " ", "=", 397 | RowBox[{"MapThread", "[", 398 | RowBox[{"targetfunct", ",", "traininginputs"}], "]"}]}], ";", " ", 399 | RowBox[{"(*", " ", 400 | RowBox[{ 401 | "compute", " ", "target", " ", "outputs", " ", "on", " ", "test", " ", 402 | "inputs"}], " ", "*)"}], "\[IndentingNewLine]", 403 | RowBox[{"Print", "[", 404 | RowBox[{"\"\\"", ",", " ", 405 | RowBox[{"fitness", "[", "curr", "]"}]}], "]"}], ";", " ", 406 | RowBox[{"(*", " ", 407 | RowBox[{ 408 | "fitness", " ", "of", " ", "best", " ", "individual", " ", "replacing", 409 | " ", "training", " ", "inputs", " ", "with", " ", "test", " ", "inputs", 410 | " ", "is", " ", "generalisation", " ", "error"}], " ", "*)"}], " ", 411 | "\[IndentingNewLine]", "\[IndentingNewLine]", "\n", 412 | "\[IndentingNewLine]"}]}]}]], "Input", 413 | CellChangeTimes->{{3.6178615652943487`*^9, 3.6178617291847224`*^9}, { 414 | 3.6178618197119*^9, 3.617861839921056*^9}, {3.617861875817109*^9, 415 | 3.6178618836335564`*^9}, {3.617861920657674*^9, 3.6178619238388557`*^9}, { 416 | 3.617861958067814*^9, 3.617862073114394*^9}, {3.6178621197200594`*^9, 417 | 3.6178621307036877`*^9}, {3.617868039043626*^9, 3.6178680597558107`*^9}, { 418 | 3.6178694054347787`*^9, 3.617869423575816*^9}, {3.617869461450983*^9, 419 | 3.6178695208423796`*^9}, {3.617869630694663*^9, 3.61786964637856*^9}, { 420 | 3.6178699531861086`*^9, 3.6178699587894287`*^9}, {3.6178700442033143`*^9, 421 | 3.617870047954529*^9}, {3.6178701085449944`*^9, 3.6178701236508584`*^9}, { 422 | 3.617870158892874*^9, 3.617870213116976*^9}, {3.6178702734854283`*^9, 423 | 3.617870280698841*^9}, {3.6178703213861685`*^9, 3.617870399202619*^9}, { 424 | 3.6178704598820896`*^9, 3.6178704654924107`*^9}, {3.6178705041936245`*^9, 425 | 3.617870647223805*^9}, {3.617870731268612*^9, 3.6178707669156513`*^9}, { 426 | 3.617870815976457*^9, 3.6178708616390686`*^9}, {3.6178708974981203`*^9, 427 | 3.6178709185403233`*^9}, {3.617871034366948*^9, 3.617871149225518*^9}, { 428 | 3.6178712674552803`*^9, 3.6178713464547987`*^9}, {3.617871523168906*^9, 429 | 3.6178715885946484`*^9}, {3.61787167096836*^9, 3.6178717393442707`*^9}, { 430 | 3.617871783411791*^9, 3.617871814753584*^9}, 3.6178718828814807`*^9, { 431 | 3.617872136417982*^9, 3.6178721377630587`*^9}, {3.617872183613682*^9, 432 | 3.6178723369254503`*^9}, {3.617872367564203*^9, 3.617872408882566*^9}, { 433 | 3.617872461381569*^9, 3.617872515057639*^9}, {3.617872563890432*^9, 434 | 3.617872577510211*^9}, {3.617872664548189*^9, 3.6178726902776613`*^9}, { 435 | 3.6178727467788925`*^9, 3.617872765191946*^9}, {3.617872887079918*^9, 436 | 3.617872922988971*^9}, {3.6178729867686195`*^9, 3.6178730460770116`*^9}, { 437 | 3.617873083594157*^9, 3.617873110882718*^9}, {3.6178731435085845`*^9, 438 | 3.6178731764354677`*^9}, {3.6178734047155247`*^9, 439 | 3.6178734711863265`*^9}, {3.6178735139207706`*^9, 3.6178735252114162`*^9}, 440 | 3.6178735939083457`*^9, {3.617873836581226*^9, 3.617873875315441*^9}, { 441 | 3.617873991919111*^9, 3.61787403684268*^9}, 3.617874067851454*^9, { 442 | 3.617874180164878*^9, 3.6178742249974422`*^9}, {3.617874342500163*^9, 443 | 3.6178743631953464`*^9}, {3.6178744872604427`*^9, 3.617874504737442*^9}, { 444 | 3.6178745381523533`*^9, 3.617874620217047*^9}, {3.6178747152614837`*^9, 445 | 3.6178747237439685`*^9}, {3.617874837673485*^9, 3.6178750503996525`*^9}, { 446 | 3.6178751101130676`*^9, 3.6178751913527145`*^9}, {3.6178752759735546`*^9, 447 | 3.6178752989958715`*^9}, {3.617875335828978*^9, 3.617875368636854*^9}, { 448 | 3.617875411729319*^9, 3.617875442433075*^9}, {3.617879011463212*^9, 449 | 3.617879195609745*^9}, {3.6178819079778833`*^9, 3.6178819110390587`*^9}, 450 | 3.6178819462950745`*^9, {3.617881990513604*^9, 3.617882022420429*^9}, { 451 | 3.617882100677905*^9, 3.6178821090443835`*^9}, {3.617882355376473*^9, 452 | 3.617882361839843*^9}, {3.6178824124697385`*^9, 3.6178824126157465`*^9}, { 453 | 3.6178827039704113`*^9, 3.6178827656639404`*^9}, {3.617882802911071*^9, 454 | 3.6178830055656614`*^9}, {3.617883057594638*^9, 3.617883175181363*^9}, { 455 | 3.6178832078292303`*^9, 3.617883249834633*^9}, {3.6178833009385557`*^9, 456 | 3.6178833341904583`*^9}, {3.6178833807461205`*^9, 3.617883407559654*^9}, { 457 | 3.6178834814318795`*^9, 3.6178836345376368`*^9}, {3.617883717898405*^9, 458 | 3.6178837260558715`*^9}, {3.617887204268814*^9, 3.6178872184616256`*^9}, { 459 | 3.617964322287301*^9, 3.617964363386652*^9}, {3.617964406623125*^9, 460 | 3.6179644973153124`*^9}, {3.617964536628561*^9, 3.6179645853763494`*^9}, { 461 | 3.617964622472471*^9, 3.6179646934075284`*^9}, {3.6179647445884557`*^9, 462 | 3.617964992780651*^9}, {3.6179650401463604`*^9, 3.617965217194487*^9}, { 463 | 3.6179655319794917`*^9, 3.6179655866446185`*^9}, 3.617965676869779*^9, { 464 | 3.6179657608995852`*^9, 3.617965770916158*^9}, {3.617966174974269*^9, 465 | 3.617966175252285*^9}, {3.617966274365954*^9, 3.6179663013154955`*^9}, { 466 | 3.6179663457940397`*^9, 3.6179664542822447`*^9}, {3.617966602652731*^9, 467 | 3.617966801752119*^9}, {3.6180341222011557`*^9, 3.6180342238509693`*^9}, { 468 | 3.618034345493927*^9, 3.618034409580593*^9}, {3.618034465318781*^9, 469 | 3.6180344797476063`*^9}, {3.618034550992681*^9, 3.618034577276184*^9}, { 470 | 3.6180346174674835`*^9, 3.61803462422787*^9}, {3.6180346794070263`*^9, 471 | 3.6180346928877974`*^9}, {3.618034763808853*^9, 3.6180348264484363`*^9}, { 472 | 3.6180348636055613`*^9, 3.6180349537467175`*^9}, {3.618035022994678*^9, 473 | 3.618035102339216*^9}, {3.6180351877701025`*^9, 3.6180352524588027`*^9}, { 474 | 3.618035975334149*^9, 3.6180359764272113`*^9}, {3.6180360359896183`*^9, 475 | 3.618036037915728*^9}, {3.618036073109741*^9, 3.618036319276821*^9}, { 476 | 3.618036567504019*^9, 3.618036595250606*^9}, {3.6180369486918216`*^9, 477 | 3.618036963043642*^9}, {3.6180370028369184`*^9, 3.618037031379551*^9}, { 478 | 3.6180373682778206`*^9, 3.6180373959254017`*^9}, {3.618037446047269*^9, 479 | 3.6180375567105985`*^9}, {3.618037692853385*^9, 3.61803770903131*^9}, { 480 | 3.618041309090222*^9, 3.6180413370228195`*^9}, {3.6180413689086437`*^9, 481 | 3.618041372990877*^9}, {3.6180414737396393`*^9, 3.6180414781608925`*^9}, { 482 | 3.6180430621504917`*^9, 3.618043246905059*^9}, {3.618043592282813*^9, 483 | 3.618043605181551*^9}, {3.6180436639419117`*^9, 3.6180436706382947`*^9}, { 484 | 3.618043744142499*^9, 3.618043974169656*^9}, {3.618044004674401*^9, 485 | 3.6180440523171253`*^9}, {3.618044186769816*^9, 3.6180441989665136`*^9}, { 486 | 3.618044242809021*^9, 3.6180442448711395`*^9}, 3.618044275764906*^9, { 487 | 3.6180443668421154`*^9, 3.6180445561509438`*^9}, {3.6180446610779448`*^9, 488 | 3.6180446957609286`*^9}, {3.6180447297278714`*^9, 489 | 3.6180447439396844`*^9}, {3.6180448383210826`*^9, 490 | 3.6180449241149893`*^9}, {3.6180451787395535`*^9, 3.618045194488454*^9}, { 491 | 3.618045301469573*^9, 3.6180453633171105`*^9}, 3.6180454108208275`*^9, 492 | 3.6180454434206924`*^9, {3.6180454758415465`*^9, 3.6180454837299976`*^9}, { 493 | 3.618045571944043*^9, 3.6180455723870687`*^9}, {3.6180456533366985`*^9, 494 | 3.6180456588450136`*^9}, {3.6180457165823164`*^9, 495 | 3.6180457518883357`*^9}, {3.618045831479888*^9, 3.6180459624753804`*^9}, { 496 | 3.618046173948476*^9, 3.6180462916792097`*^9}, {3.618046600880895*^9, 497 | 3.618046605127138*^9}, {3.618046711481221*^9, 3.6180467463592157`*^9}, { 498 | 3.6180468051775804`*^9, 3.618046807250699*^9}, {3.618046842480714*^9, 499 | 3.6180468462299285`*^9}, {3.6180468794818306`*^9, 3.618047070289744*^9}, 500 | 3.6180472879211917`*^9, {3.6180474943279977`*^9, 3.618047497470177*^9}, { 501 | 3.618047550962237*^9, 3.6180475514842663`*^9}, {3.6180476224493256`*^9, 502 | 3.6180476307948027`*^9}, {3.6180478533415318`*^9, 3.618048158580991*^9}, { 503 | 3.6180482178443804`*^9, 3.6180482931776896`*^9}, {3.6180483604965396`*^9, 504 | 3.618048361067572*^9}}] 505 | }, 506 | WindowSize->{1354, 656}, 507 | WindowMargins->{{-4, Automatic}, {Automatic, 0}}, 508 | FrontEndVersion->"8.0 for Microsoft Windows (64-bit) (November 7, 2010)", 509 | StyleDefinitions->"Default.nb" 510 | ] 511 | (* End of Notebook Content *) 512 | 513 | (* Internal cache information *) 514 | (*CellTagsOutline 515 | CellTagsIndex->{} 516 | *) 517 | (*CellTagsIndex 518 | CellTagsIndex->{} 519 | *) 520 | (*NotebookFileOutline 521 | Notebook[{ 522 | Cell[557, 20, 22964, 483, 1572, "Input"] 523 | } 524 | ] 525 | *) 526 | 527 | (* End of internal cache information *) 528 | -------------------------------------------------------------------------------- /TINY_GSGP_ARIT.nb: -------------------------------------------------------------------------------- 1 | (* Content-type: application/vnd.wolfram.mathematica *) 2 | 3 | (*** Wolfram Notebook File ***) 4 | (* http://www.wolfram.com/nb *) 5 | 6 | (* CreatedBy='Mathematica 8.0' *) 7 | 8 | (*CacheID: 234*) 9 | (* Internal cache information: 10 | NotebookFileLineBreakTest 11 | NotebookFileLineBreakTest 12 | NotebookDataPosition[ 157, 7] 13 | NotebookDataLength[ 27564, 616] 14 | NotebookOptionsPosition[ 27319, 603] 15 | NotebookOutlinePosition[ 27662, 618] 16 | CellTagsIndexPosition[ 27619, 615] 17 | WindowFrame->Normal*) 18 | 19 | (* Beginning of Notebook Content *) 20 | Notebook[{ 21 | Cell[BoxData[ 22 | RowBox[{ 23 | RowBox[{"(*", " ", 24 | RowBox[{ 25 | RowBox[{ 26 | RowBox[{ 27 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 28 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 29 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 30 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 31 | "**", "**", "**", "**", "**"}], ";", "\[IndentingNewLine]", 32 | "\[IndentingNewLine]", 33 | RowBox[{"TINY_GSGP", 34 | RowBox[{"_ARIT", ".", 35 | RowBox[{"nb", ":", " ", 36 | RowBox[{ 37 | "Geometric", " ", "Semantic", " ", "Genetic", " ", "Programming", " ", 38 | "in", " ", "Mathematica", " ", "for", " ", "Arithmetic", " ", 39 | "Expressions"}]}]}]}], ";", "\[IndentingNewLine]", 40 | RowBox[{"Author", ":", 41 | RowBox[{"Alberto", " ", "Moraglio", " ", 42 | RowBox[{"(", 43 | RowBox[{ 44 | RowBox[{"albmor", "@", "gmail"}], ".", "com"}], ")"}]}]}], ";", "\n", 45 | "\[IndentingNewLine]", "Features", ";", "\[IndentingNewLine]", 46 | RowBox[{ 47 | RowBox[{"-", " ", "It"}], " ", "evolves", " ", "arithmentic", " ", 48 | "expressions", " ", 49 | RowBox[{"(", 50 | RowBox[{ 51 | "polynomials", " ", "or", " ", "fractional", " ", "polynomials", " ", 52 | "if", " ", "division", " ", "is", " ", "used"}], ")"}]}], ";", 53 | "\[IndentingNewLine]", 54 | RowBox[{ 55 | RowBox[{"-", " ", "Fithess"}], " ", "is", " ", "based", " ", "on", " ", 56 | "a", " ", "training", " ", "set", " ", 57 | RowBox[{"(", 58 | RowBox[{ 59 | "not", " ", "on", " ", "all", " ", "inputs", " ", "as", " ", "for", 60 | " ", "Boolean", " ", "expressions"}], ")"}]}], ";", " ", 61 | "\[IndentingNewLine]", 62 | RowBox[{ 63 | RowBox[{"-", " ", "Algebraic"}], " ", "simplification", " ", "of", " ", 64 | "offspring"}], ";", "\[IndentingNewLine]", 65 | RowBox[{ 66 | RowBox[{"-", " ", "Generalisation"}], " ", "test", " ", 67 | RowBox[{"(", 68 | RowBox[{"on", " ", "unseen", " ", "examples"}], ")"}]}], ";"}], " ", 69 | "\[IndentingNewLine]", "\[IndentingNewLine]", "**", "**", "**", "**", "**", 70 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 71 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 72 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", "**", 73 | "**", "**", "**", "**", "**", "**", "**", "**", "**", "**"}], " ", 74 | "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 75 | "\[IndentingNewLine]", 76 | RowBox[{"(*", 77 | RowBox[{ 78 | RowBox[{"**", "**"}], "*", " ", "PARAMETERS"}], " ", "******)"}], 79 | "\[IndentingNewLine]", "\n", 80 | RowBox[{ 81 | RowBox[{ 82 | RowBox[{"NUMVARS", "=", "5"}], ";"}], " ", 83 | RowBox[{"(*", " ", 84 | RowBox[{"number", " ", "of", " ", "input", " ", "variables"}], " ", 85 | "*)"}], "\n", 86 | RowBox[{ 87 | RowBox[{"DEPTH", "=", "4"}], ";"}], " ", 88 | RowBox[{"(*", " ", 89 | RowBox[{ 90 | "maximum", " ", "depth", " ", "of", " ", "expressions", " ", "in", " ", 91 | "the", " ", "initial", " ", "population"}], " ", "*)"}], 92 | "\[IndentingNewLine]", 93 | RowBox[{ 94 | RowBox[{"POPSIZE", "=", "20"}], ";"}], " ", 95 | RowBox[{"(*", " ", 96 | RowBox[{"population", " ", "size"}], " ", "*)"}], "\[IndentingNewLine]", 97 | RowBox[{ 98 | RowBox[{"GENERATIONS", "=", "100"}], ";", " ", 99 | RowBox[{"(*", " ", 100 | RowBox[{"number", " ", "of", " ", "generations"}], " ", "*)"}], 101 | "\[IndentingNewLine]", 102 | RowBox[{"NUMCASES", " ", "=", " ", "10000"}], ";", " ", 103 | RowBox[{"(*", " ", 104 | RowBox[{"number", " ", "of", " ", "training", " ", "examples"}], " ", 105 | "*)"}], "\n", 106 | RowBox[{"MUTSTEP", " ", "=", " ", "0.01"}], ";", " ", 107 | RowBox[{"(*", " ", 108 | RowBox[{"mutation", " ", "step", " ", "size"}], " ", "*)"}], 109 | "\[IndentingNewLine]", 110 | RowBox[{"TRUNC", "=", "0.5"}], ";", " ", 111 | RowBox[{"(*", " ", 112 | RowBox[{ 113 | "proportion", " ", "of", " ", "population", " ", "to", " ", "retain", 114 | " ", "in", " ", "truncation", " ", "selection"}], " ", "*)"}], 115 | "\[IndentingNewLine]", "\[IndentingNewLine]", 116 | RowBox[{"(*", 117 | RowBox[{"**", "**", "**", "**", "**", "**", "**", "**", "**"}], 118 | "*****)"}], "\n", "\[IndentingNewLine]", 119 | RowBox[{"vars", "=", 120 | RowBox[{"Table", "[", 121 | RowBox[{ 122 | RowBox[{"Symbol", "[", 123 | RowBox[{"\"\\"", "<>", 124 | RowBox[{"ToString", "[", "i", "]"}]}], "]"}], ",", 125 | RowBox[{"{", 126 | RowBox[{"i", ",", "NUMVARS"}], "}"}]}], "]"}]}], ";", 127 | RowBox[{"(*", " ", 128 | RowBox[{"variable", " ", "names"}], " ", "*)"}], "\[IndentingNewLine]", 129 | "\[IndentingNewLine]", 130 | RowBox[{ 131 | RowBox[{"randexpr", "[", "dep_", "]"}], ":=", " ", 132 | RowBox[{"(*", " ", 133 | RowBox[{ 134 | "Create", " ", "a", " ", "random", " ", "arithmetic", " ", 135 | "expression"}], " ", "*)"}], "\[IndentingNewLine]", 136 | RowBox[{"If", "[", 137 | RowBox[{ 138 | RowBox[{ 139 | RowBox[{"dep", "\[Equal]", "1"}], " ", "||", " ", 140 | RowBox[{ 141 | RowBox[{"RandomReal", "[", "]"}], " ", "<", " ", 142 | RowBox[{"N", "[", 143 | RowBox[{"1.0", "/", 144 | RowBox[{"(", 145 | RowBox[{ 146 | RowBox[{"2", "^", "dep"}], "-", "1"}], ")"}]}], "]"}]}]}], ",", 147 | " ", 148 | RowBox[{"(*", " ", "terminal", " ", "*)"}], "\[IndentingNewLine]", 149 | RowBox[{"If", "[", 150 | RowBox[{ 151 | RowBox[{ 152 | RowBox[{"RandomReal", "[", "]"}], ">", 153 | RowBox[{"N", "[", 154 | RowBox[{"1", "/", 155 | RowBox[{"(", 156 | RowBox[{"NUMVARS", "+", "1"}], ")"}]}], "]"}]}], ",", " ", 157 | "\[IndentingNewLine]", 158 | RowBox[{"Return", "[", 159 | RowBox[{"RandomChoice", "[", "vars", "]"}], "]"}], ",", " ", 160 | RowBox[{"(*", " ", "variable", " ", "*)"}], "\[IndentingNewLine]", 161 | RowBox[{"RandomReal", "[", 162 | RowBox[{"{", 163 | RowBox[{ 164 | RowBox[{"-", "1"}], ",", "1"}], "}"}], "]"}]}], "]"}], ",", " ", 165 | RowBox[{"(*", " ", "number", " ", "*)"}], "\[IndentingNewLine]", 166 | RowBox[{"If", "[", 167 | RowBox[{ 168 | RowBox[{ 169 | RowBox[{"RandomReal", "[", "]"}], "<", 170 | RowBox[{"N", "[", 171 | RowBox[{"1.0", "/", "5"}], "]"}]}], ",", " ", 172 | "\[IndentingNewLine]", 173 | RowBox[{"Return", "[", 174 | RowBox[{"-", " ", 175 | RowBox[{"randexpr", "[", 176 | RowBox[{"dep", "-", "1"}], "]"}]}], "]"}], ",", " ", 177 | RowBox[{"(*", " ", 178 | RowBox[{"unary", " ", "operation"}], " ", "*)"}], 179 | "\[IndentingNewLine]", 180 | RowBox[{"Return", "[", 181 | RowBox[{"Apply", "[", 182 | RowBox[{ 183 | RowBox[{"RandomChoice", "[", 184 | RowBox[{"{", 185 | RowBox[{ 186 | RowBox[{ 187 | RowBox[{"(", 188 | RowBox[{"#1", "+", "#2"}], ")"}], "&"}], ",", " ", 189 | RowBox[{ 190 | RowBox[{"(", 191 | RowBox[{"#1", "-", "#2"}], ")"}], "&"}], ",", 192 | RowBox[{ 193 | RowBox[{"(", 194 | RowBox[{"#1", "*", "#2"}], ")"}], "&"}]}], " ", 195 | RowBox[{"(*", 196 | RowBox[{",", " ", 197 | RowBox[{ 198 | RowBox[{"(", 199 | RowBox[{"#1", "/", "#2"}], ")"}], "&"}]}], "*)"}], "}"}], 200 | "]"}], ",", 201 | RowBox[{"{", 202 | RowBox[{ 203 | RowBox[{"randexpr", "[", 204 | RowBox[{"dep", "-", "1"}], "]"}], ",", 205 | RowBox[{"randexpr", "[", 206 | RowBox[{"dep", "-", "1"}], "]"}]}], "}"}]}], "]"}], "]"}]}], 207 | "]"}]}], "]"}]}]}], " ", 208 | RowBox[{"(*", " ", 209 | RowBox[{"binary", " ", "operations"}], " ", "*)"}], "\[IndentingNewLine]", 210 | "\[IndentingNewLine]", 211 | RowBox[{"(*", 212 | RowBox[{ 213 | RowBox[{"**", "**"}], "*", " ", "TRAINING", " ", "SET"}], " ", 214 | "******)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 215 | RowBox[{ 216 | RowBox[{"targetexpr", "=", 217 | RowBox[{"randexpr", "[", "DEPTH", "]"}]}], ";", " ", 218 | RowBox[{"(*", " ", 219 | RowBox[{ 220 | "target", " ", "is", " ", "a", " ", "random", " ", "expression"}], " ", 221 | "*)"}], "\[IndentingNewLine]", 222 | RowBox[{"Print", "[", 223 | RowBox[{"\"\\"", ",", " ", "targetexpr"}], "]"}], 224 | ";"}], "\[IndentingNewLine]", 225 | RowBox[{ 226 | RowBox[{"targetfunct", " ", "=", " ", 227 | RowBox[{"Function", "[", 228 | RowBox[{ 229 | RowBox[{"Evaluate", "[", "vars", "]"}], ",", 230 | RowBox[{"Evaluate", "[", "targetexpr", "]"}]}], "]"}]}], ";", " ", 231 | RowBox[{"(*", " ", 232 | RowBox[{ 233 | "convert", " ", "target", " ", "into", " ", "a", " ", "function"}], " ", 234 | "*)"}], "\[IndentingNewLine]", 235 | RowBox[{"traininginputs", " ", "=", " ", 236 | RowBox[{"Table", "[", 237 | RowBox[{ 238 | RowBox[{"RandomReal", "[", 239 | RowBox[{ 240 | RowBox[{"{", 241 | RowBox[{ 242 | RowBox[{"-", "1"}], ",", "1"}], "}"}], ",", "NUMVARS"}], "]"}], 243 | ",", 244 | RowBox[{"{", "NUMCASES", "}"}]}], "]"}]}], ";", " ", 245 | RowBox[{"(*", " ", 246 | RowBox[{ 247 | "generate", " ", "training", " ", "inputs", " ", "uniformly", " ", "at", 248 | " ", "random"}], " ", "*)"}], "\[IndentingNewLine]", 249 | RowBox[{"trainingoutputs", " ", "=", 250 | RowBox[{"MapThread", "[", 251 | RowBox[{"targetfunct", ",", "traininginputs"}], "]"}]}], ";", " ", 252 | RowBox[{"(*", " ", 253 | RowBox[{"compute", " ", "target", " ", "outputs"}], " ", "*)"}], 254 | "\[IndentingNewLine]", "\[IndentingNewLine]", 255 | RowBox[{"(*", 256 | RowBox[{ 257 | RowBox[{ 258 | RowBox[{ 259 | RowBox[{ 260 | RowBox[{"**", "**"}], "**"}], "**"}], "**"}], "**"}], 261 | "*************)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 262 | RowBox[{ 263 | RowBox[{"fitness", "[", "individual_", "]"}], ":=", " ", 264 | RowBox[{"Module", "[", 265 | RowBox[{ 266 | RowBox[{"{", 267 | RowBox[{"indfunct", ",", " ", "indoutputs"}], "}"}], ",", 268 | RowBox[{"(*", " ", 269 | RowBox[{ 270 | RowBox[{"fitness", " ", 271 | RowBox[{"(", "error", ")"}], " ", "function"}], ",", " ", 272 | RowBox[{"lower", " ", "is", " ", "better"}]}], " ", "*)"}], 273 | "\[IndentingNewLine]", 274 | RowBox[{ 275 | RowBox[{"indfunct", "=", 276 | RowBox[{"Function", "[", 277 | RowBox[{ 278 | RowBox[{"Evaluate", "[", "vars", "]"}], ",", 279 | RowBox[{"Evaluate", "[", "individual", "]"}]}], "]"}]}], ";", " ", 280 | RowBox[{"(*", " ", 281 | RowBox[{ 282 | "convert", " ", "individual", " ", "into", " ", "a", " ", 283 | "function"}], " ", "*)"}], "\[IndentingNewLine]", 284 | RowBox[{"indoutputs", " ", "=", 285 | RowBox[{"MapThread", "[", 286 | RowBox[{"indfunct", ",", "traininginputs"}], "]"}]}], ";", " ", 287 | RowBox[{"(*", " ", 288 | RowBox[{"compute", " ", "individual", " ", "outputs"}], " ", "*)"}], 289 | "\[IndentingNewLine]", 290 | RowBox[{"EuclideanDistance", "[", 291 | RowBox[{"indoutputs", ",", "trainingoutputs"}], "]"}]}]}], "]"}]}], 292 | " ", ";", " ", 293 | RowBox[{"(*", " ", 294 | RowBox[{ 295 | "finess", " ", "is", " ", "distance", " ", "between", " ", "output", " ", 296 | "vectors"}], " ", "*)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 297 | RowBox[{ 298 | RowBox[{"crossover", "[", 299 | RowBox[{"p1_", ",", "p2_"}], "]"}], ":=", 300 | RowBox[{"Module", "[", 301 | RowBox[{ 302 | RowBox[{"{", "coeff", "}"}], ",", 303 | RowBox[{"(*", " ", 304 | RowBox[{ 305 | "corssover", " ", "combines", " ", "linearly", " ", "parents"}], " ", 306 | "*)"}], "\[IndentingNewLine]", 307 | RowBox[{ 308 | RowBox[{"coeff", "=", 309 | RowBox[{"RandomReal", "[", "]"}]}], ";", "\[IndentingNewLine]", 310 | RowBox[{ 311 | RowBox[{"(", 312 | RowBox[{"p1", " ", "*", " ", "coeff"}], ")"}], "+", 313 | RowBox[{"(", 314 | RowBox[{"p2", " ", "*", " ", 315 | RowBox[{"(", 316 | RowBox[{"1", "-", "coeff"}], ")"}]}], ")"}]}]}]}], "]"}]}], ";"}], 317 | "\[IndentingNewLine]", "\[IndentingNewLine]", 318 | RowBox[{ 319 | RowBox[{ 320 | RowBox[{"mutation", "[", "p_", "]"}], ":=", " ", 321 | RowBox[{"p", " ", "+", 322 | RowBox[{"MUTSTEP", " ", "*", " ", 323 | RowBox[{"(", 324 | RowBox[{ 325 | RowBox[{"randexpr", "[", "DEPTH", "]"}], "-", 326 | RowBox[{"randexpr", "[", "DEPTH", "]"}]}], ")"}]}]}]}], ";"}], " ", 327 | RowBox[{"(*", " ", 328 | RowBox[{ 329 | RowBox[{ 330 | "mutation", " ", "perturbs", " ", "parent", " ", "with", " ", "zero"}], 331 | "-", 332 | RowBox[{"average", " ", "random", " ", "function"}]}], " ", "*)"}], 333 | "\[IndentingNewLine]", "\[IndentingNewLine]", 334 | RowBox[{"(*", 335 | RowBox[{ 336 | RowBox[{"**", "**"}], "*", " ", "EVOLVE"}], " ", "******)"}], 337 | "\[IndentingNewLine]", "\[IndentingNewLine]", 338 | RowBox[{ 339 | RowBox[{"pop", " ", "=", 340 | RowBox[{"Table", "[", 341 | RowBox[{ 342 | RowBox[{"randexpr", "[", "DEPTH", "]"}], ",", " ", 343 | RowBox[{"{", "POPSIZE", "}"}]}], "]"}]}], ";"}], " ", 344 | RowBox[{"(*", " ", 345 | RowBox[{"initialise", " ", "population"}], " ", "*)"}], 346 | "\[IndentingNewLine]", 347 | RowBox[{"For", "[", 348 | RowBox[{ 349 | RowBox[{"gen", "=", "0"}], ",", 350 | RowBox[{"gen", "<", 351 | RowBox[{"GENERATIONS", "+", "1"}]}], ",", 352 | RowBox[{"gen", "++"}], ",", "\[IndentingNewLine]", 353 | RowBox[{ 354 | RowBox[{"gradedpop", " ", "=", " ", 355 | RowBox[{"Table", "[", 356 | RowBox[{ 357 | RowBox[{"{", 358 | RowBox[{ 359 | RowBox[{"fitness", "[", 360 | RowBox[{"pop", "[", 361 | RowBox[{"[", "i", "]"}], "]"}], "]"}], ",", 362 | RowBox[{"pop", "[", 363 | RowBox[{"[", "i", "]"}], "]"}]}], "}"}], ",", 364 | RowBox[{"{", 365 | RowBox[{"i", ",", "POPSIZE"}], "}"}]}], "]"}]}], ";", 366 | RowBox[{"(*", " ", 367 | RowBox[{"evaluate", " ", "population", " ", "fitness"}], " ", "*)"}], 368 | "\[IndentingNewLine]", 369 | RowBox[{"sortedpop", " ", "=", " ", 370 | RowBox[{"Sort", "[", 371 | RowBox[{"gradedpop", ",", " ", 372 | RowBox[{ 373 | RowBox[{ 374 | RowBox[{"#1", "[", 375 | RowBox[{"[", "1", "]"}], "]"}], "<", 376 | RowBox[{"#2", "[", 377 | RowBox[{"[", "1", "]"}], "]"}]}], "&"}]}], "]"}]}], " ", ";", 378 | RowBox[{"(*", " ", 379 | RowBox[{"sort", " ", "population", " ", "on", " ", "fitness"}], " ", 380 | "*)"}], "\[IndentingNewLine]", 381 | RowBox[{"Print", "[", 382 | RowBox[{ 383 | "\"\\"", ",", "gen", ",", " ", "\"\< min fit: \>\"", ",", 384 | RowBox[{"sortedpop", "[", 385 | RowBox[{"[", 386 | RowBox[{"1", ",", "1"}], "]"}], "]"}], ",", "\"\< avg fit: \>\"", 387 | ",", " ", 388 | RowBox[{"N", "[", 389 | RowBox[{"Mean", "[", 390 | RowBox[{"sortedpop", "[", 391 | RowBox[{"[", 392 | RowBox[{"All", ",", "1"}], "]"}], "]"}], "]"}], "]"}]}], "]"}], 393 | ";", " ", 394 | RowBox[{"(*", " ", 395 | RowBox[{"print", " ", "stats"}], " ", "*)"}], "\[IndentingNewLine]", 396 | RowBox[{"parentpop", "=", 397 | RowBox[{"sortedpop", "[", 398 | RowBox[{"[", 399 | RowBox[{ 400 | RowBox[{"1", ";;", 401 | RowBox[{"Round", "[", 402 | RowBox[{"TRUNC", "*", "POPSIZE"}], "]"}]}], ",", "2"}], "]"}], 403 | "]"}]}], ";", " ", 404 | RowBox[{"(*", " ", 405 | RowBox[{"selected", " ", "parents"}], " ", "*)"}], 406 | "\[IndentingNewLine]", 407 | RowBox[{"If", "[", 408 | RowBox[{ 409 | RowBox[{"gen", "\[Equal]", "GENERATIONS"}], ",", " ", 410 | RowBox[{"Break", "[", "]"}]}], "]"}], ";", "\[IndentingNewLine]", 411 | RowBox[{"For", "[", 412 | RowBox[{ 413 | RowBox[{"i", "=", "1"}], ",", 414 | RowBox[{"i", "<", 415 | RowBox[{"POPSIZE", "+", "1"}]}], ",", 416 | RowBox[{"i", "++"}], ",", 417 | RowBox[{"(*", " ", 418 | RowBox[{"create", " ", "offspring", " ", "population"}], " ", "*)"}], 419 | "\[IndentingNewLine]", 420 | RowBox[{ 421 | RowBox[{"par", "=", 422 | RowBox[{"RandomSample", "[", 423 | RowBox[{"parentpop", ",", "2"}], "]"}]}], ";", " ", 424 | RowBox[{"(*", " ", 425 | RowBox[{"pick", " ", "two", " ", "random", " ", "parents"}], " ", 426 | "*)"}], "\[IndentingNewLine]", 427 | RowBox[{ 428 | RowBox[{"pop", "[", 429 | RowBox[{"[", "i", "]"}], "]"}], "=", 430 | RowBox[{"Simplify", "[", 431 | RowBox[{ 432 | RowBox[{"mutation", "[", 433 | RowBox[{"crossover", "[", 434 | RowBox[{ 435 | RowBox[{"par", "[", 436 | RowBox[{"[", "1", "]"}], "]"}], ",", 437 | RowBox[{"par", "[", 438 | RowBox[{"[", "2", "]"}], "]"}]}], "]"}], "]"}], ",", 439 | RowBox[{"TimeConstraint", "\[Rule]", "0.1"}]}], "]"}]}], ";"}]}], 440 | " ", 441 | RowBox[{"(*", " ", 442 | RowBox[{ 443 | "create", " ", "offspring", " ", "and", " ", "simplify", " ", "it"}], 444 | " ", "*)"}], "\[IndentingNewLine]", "]"}]}]}], " ", 445 | "\[IndentingNewLine]", "]"}], " ", "\[IndentingNewLine]", 446 | "\[IndentingNewLine]", 447 | RowBox[{ 448 | RowBox[{ 449 | "Print", "[", "\"\\"", "]"}], ";"}], 450 | "\[IndentingNewLine]", 451 | RowBox[{ 452 | RowBox[{"Print", "[", 453 | RowBox[{"sortedpop", "[", 454 | RowBox[{"[", "1", "]"}], "]"}], "]"}], ";", 455 | RowBox[{"(*", " ", 456 | RowBox[{"genotype", " ", "of", " ", "final", " ", "solution"}], " ", 457 | "*)"}], "\n", "\[IndentingNewLine]", 458 | RowBox[{"(*", 459 | RowBox[{ 460 | RowBox[{"**", "**"}], "*", " ", "GENERALISATION", " ", "TEST"}], " ", 461 | "******)"}], "\[IndentingNewLine]", "\[IndentingNewLine]", 462 | RowBox[{"Print", "[", 463 | RowBox[{"\"\\"", ",", " ", 464 | RowBox[{"sortedpop", "[", 465 | RowBox[{"[", 466 | RowBox[{"1", ",", "1"}], "]"}], "]"}]}], "]"}], ";", " ", 467 | RowBox[{"(*", " ", 468 | RowBox[{ 469 | "fitness", " ", "of", " ", "best", " ", "individual", " ", "is", " ", 470 | "training", " ", "error"}], " ", "*)"}], "\[IndentingNewLine]", 471 | RowBox[{"traininginputs", " ", "=", " ", 472 | RowBox[{"Table", "[", 473 | RowBox[{ 474 | RowBox[{"RandomReal", "[", 475 | RowBox[{ 476 | RowBox[{"{", 477 | RowBox[{ 478 | RowBox[{"-", "1"}], ",", "1"}], "}"}], ",", "NUMVARS"}], "]"}], 479 | ",", 480 | RowBox[{"{", "NUMCASES", "}"}]}], "]"}]}], ";", " ", 481 | RowBox[{"(*", " ", 482 | RowBox[{ 483 | "generate", " ", "test", " ", "inputs", " ", "uniformly", " ", "at", " ", 484 | "random"}], " ", "*)"}], "\[IndentingNewLine]", 485 | RowBox[{"trainingoutputs", " ", "=", 486 | RowBox[{"MapThread", "[", 487 | RowBox[{"targetfunct", ",", "traininginputs"}], "]"}]}], ";", " ", 488 | RowBox[{"(*", " ", 489 | RowBox[{ 490 | "compute", " ", "target", " ", "outputs", " ", "on", " ", "test", " ", 491 | "inputs"}], " ", "*)"}], "\[IndentingNewLine]", 492 | RowBox[{"Print", "[", 493 | RowBox[{"\"\\"", ",", " ", 494 | RowBox[{"fitness", "[", 495 | RowBox[{"sortedpop", "[", 496 | RowBox[{"[", "1", "]"}], "]"}], "]"}]}], "]"}], ";", " ", 497 | RowBox[{"(*", " ", 498 | RowBox[{ 499 | "fitness", " ", "of", " ", "best", " ", "individual", " ", "replacing", 500 | " ", "training", " ", "inputs", " ", "with", " ", "test", " ", "inputs", 501 | " ", "is", " ", "generalisation", " ", "error"}], " ", "*)"}], " ", 502 | "\[IndentingNewLine]", "\n", "\[IndentingNewLine]", 503 | "\[IndentingNewLine]"}]}]}]], "Input", 504 | CellChangeTimes->{{3.6178615652943487`*^9, 3.6178617291847224`*^9}, { 505 | 3.6178618197119*^9, 3.617861839921056*^9}, {3.617861875817109*^9, 506 | 3.6178618836335564`*^9}, {3.617861920657674*^9, 3.6178619238388557`*^9}, { 507 | 3.617861958067814*^9, 3.617862073114394*^9}, {3.6178621197200594`*^9, 508 | 3.6178621307036877`*^9}, {3.617868039043626*^9, 3.6178680597558107`*^9}, { 509 | 3.6178694054347787`*^9, 3.617869423575816*^9}, {3.617869461450983*^9, 510 | 3.6178695208423796`*^9}, {3.617869630694663*^9, 3.61786964637856*^9}, { 511 | 3.6178699531861086`*^9, 3.6178699587894287`*^9}, {3.6178700442033143`*^9, 512 | 3.617870047954529*^9}, {3.6178701085449944`*^9, 3.6178701236508584`*^9}, { 513 | 3.617870158892874*^9, 3.617870213116976*^9}, {3.6178702734854283`*^9, 514 | 3.617870280698841*^9}, {3.6178703213861685`*^9, 3.617870399202619*^9}, { 515 | 3.6178704598820896`*^9, 3.6178704654924107`*^9}, {3.6178705041936245`*^9, 516 | 3.617870647223805*^9}, {3.617870731268612*^9, 3.6178707669156513`*^9}, { 517 | 3.617870815976457*^9, 3.6178708616390686`*^9}, {3.6178708974981203`*^9, 518 | 3.6178709185403233`*^9}, {3.617871034366948*^9, 3.617871149225518*^9}, { 519 | 3.6178712674552803`*^9, 3.6178713464547987`*^9}, {3.617871523168906*^9, 520 | 3.6178715885946484`*^9}, {3.61787167096836*^9, 3.6178717393442707`*^9}, { 521 | 3.617871783411791*^9, 3.617871814753584*^9}, 3.6178718828814807`*^9, { 522 | 3.617872136417982*^9, 3.6178721377630587`*^9}, {3.617872183613682*^9, 523 | 3.6178723369254503`*^9}, {3.617872367564203*^9, 3.617872408882566*^9}, { 524 | 3.617872461381569*^9, 3.617872515057639*^9}, {3.617872563890432*^9, 525 | 3.617872577510211*^9}, {3.617872664548189*^9, 3.6178726902776613`*^9}, { 526 | 3.6178727467788925`*^9, 3.617872765191946*^9}, {3.617872887079918*^9, 527 | 3.617872922988971*^9}, {3.6178729867686195`*^9, 3.6178730460770116`*^9}, { 528 | 3.617873083594157*^9, 3.617873110882718*^9}, {3.6178731435085845`*^9, 529 | 3.6178731764354677`*^9}, {3.6178734047155247`*^9, 530 | 3.6178734711863265`*^9}, {3.6178735139207706`*^9, 3.6178735252114162`*^9}, 531 | 3.6178735939083457`*^9, {3.617873836581226*^9, 3.617873875315441*^9}, { 532 | 3.617873991919111*^9, 3.61787403684268*^9}, 3.617874067851454*^9, { 533 | 3.617874180164878*^9, 3.6178742249974422`*^9}, {3.617874342500163*^9, 534 | 3.6178743631953464`*^9}, {3.6178744872604427`*^9, 3.617874504737442*^9}, { 535 | 3.6178745381523533`*^9, 3.617874620217047*^9}, {3.6178747152614837`*^9, 536 | 3.6178747237439685`*^9}, {3.617874837673485*^9, 3.6178750503996525`*^9}, { 537 | 3.6178751101130676`*^9, 3.6178751913527145`*^9}, {3.6178752759735546`*^9, 538 | 3.6178752989958715`*^9}, {3.617875335828978*^9, 3.617875368636854*^9}, { 539 | 3.617875411729319*^9, 3.617875442433075*^9}, {3.617879011463212*^9, 540 | 3.617879195609745*^9}, {3.6178819079778833`*^9, 3.6178819110390587`*^9}, 541 | 3.6178819462950745`*^9, {3.617881990513604*^9, 3.617882022420429*^9}, { 542 | 3.617882100677905*^9, 3.6178821090443835`*^9}, {3.617882355376473*^9, 543 | 3.617882361839843*^9}, {3.6178824124697385`*^9, 3.6178824126157465`*^9}, { 544 | 3.6178827039704113`*^9, 3.6178827656639404`*^9}, {3.617882802911071*^9, 545 | 3.6178830055656614`*^9}, {3.617883057594638*^9, 3.617883175181363*^9}, { 546 | 3.6178832078292303`*^9, 3.617883249834633*^9}, {3.6178833009385557`*^9, 547 | 3.6178833341904583`*^9}, {3.6178833807461205`*^9, 3.617883407559654*^9}, { 548 | 3.6178834814318795`*^9, 3.6178836345376368`*^9}, {3.617883717898405*^9, 549 | 3.6178837260558715`*^9}, {3.617887204268814*^9, 3.6178872184616256`*^9}, { 550 | 3.617964322287301*^9, 3.617964363386652*^9}, {3.617964406623125*^9, 551 | 3.6179644973153124`*^9}, {3.617964536628561*^9, 3.6179645853763494`*^9}, { 552 | 3.617964622472471*^9, 3.6179646934075284`*^9}, {3.6179647445884557`*^9, 553 | 3.617964992780651*^9}, {3.6179650401463604`*^9, 3.617965217194487*^9}, { 554 | 3.6179655319794917`*^9, 3.6179655866446185`*^9}, 3.617965676869779*^9, { 555 | 3.6179657608995852`*^9, 3.617965770916158*^9}, {3.617966174974269*^9, 556 | 3.617966175252285*^9}, {3.617966274365954*^9, 3.6179663013154955`*^9}, { 557 | 3.6179663457940397`*^9, 3.6179664542822447`*^9}, {3.617966602652731*^9, 558 | 3.617966801752119*^9}, {3.6180341222011557`*^9, 3.6180342238509693`*^9}, { 559 | 3.618034345493927*^9, 3.618034409580593*^9}, {3.618034465318781*^9, 560 | 3.6180344797476063`*^9}, {3.618034550992681*^9, 3.618034577276184*^9}, { 561 | 3.6180346174674835`*^9, 3.61803462422787*^9}, {3.6180346794070263`*^9, 562 | 3.6180346928877974`*^9}, {3.618034763808853*^9, 3.6180348264484363`*^9}, { 563 | 3.6180348636055613`*^9, 3.6180349537467175`*^9}, {3.618035022994678*^9, 564 | 3.618035102339216*^9}, {3.6180351877701025`*^9, 3.6180352524588027`*^9}, { 565 | 3.618035975334149*^9, 3.6180359764272113`*^9}, {3.6180360359896183`*^9, 566 | 3.618036037915728*^9}, {3.618036073109741*^9, 3.618036319276821*^9}, { 567 | 3.618036567504019*^9, 3.618036595250606*^9}, {3.6180369486918216`*^9, 568 | 3.618036963043642*^9}, {3.6180370028369184`*^9, 3.618037031379551*^9}, { 569 | 3.6180373682778206`*^9, 3.6180373959254017`*^9}, {3.618037446047269*^9, 570 | 3.6180375567105985`*^9}, {3.618037692853385*^9, 3.61803770903131*^9}, { 571 | 3.618041309090222*^9, 3.6180413370228195`*^9}, {3.6180413689086437`*^9, 572 | 3.618041372990877*^9}, {3.6180414737396393`*^9, 3.6180414781608925`*^9}, { 573 | 3.6180430621504917`*^9, 3.618043246905059*^9}, {3.618043592282813*^9, 574 | 3.618043605181551*^9}, {3.6180436639419117`*^9, 3.6180436706382947`*^9}, { 575 | 3.618043744142499*^9, 3.618043974169656*^9}, {3.618044004674401*^9, 576 | 3.6180440523171253`*^9}, {3.618044186769816*^9, 3.6180441989665136`*^9}, { 577 | 3.618044242809021*^9, 3.6180442448711395`*^9}, 3.618044275764906*^9, { 578 | 3.6180443668421154`*^9, 3.6180445561509438`*^9}, {3.6180446610779448`*^9, 579 | 3.6180446957609286`*^9}, {3.6180447297278714`*^9, 580 | 3.6180447439396844`*^9}, {3.6180448383210826`*^9, 581 | 3.6180449241149893`*^9}, {3.6180451787395535`*^9, 3.618045194488454*^9}, { 582 | 3.618045301469573*^9, 3.6180453633171105`*^9}, 3.6180454108208275`*^9, 583 | 3.6180454434206924`*^9, {3.6180454758415465`*^9, 3.6180454837299976`*^9}, { 584 | 3.618045571944043*^9, 3.6180455723870687`*^9}, {3.6180456533366985`*^9, 585 | 3.6180456588450136`*^9}, {3.6180457165823164`*^9, 586 | 3.6180457518883357`*^9}, {3.618045831479888*^9, 3.6180459624753804`*^9}, { 587 | 3.618046173948476*^9, 3.6180462916792097`*^9}, {3.618046600880895*^9, 588 | 3.618046605127138*^9}, {3.618046711481221*^9, 3.6180467463592157`*^9}, { 589 | 3.6180468051775804`*^9, 3.618046807250699*^9}, {3.618046842480714*^9, 590 | 3.6180468462299285`*^9}, {3.6180468794818306`*^9, 3.618047070289744*^9}, 591 | 3.6180472879211917`*^9, {3.6180474943279977`*^9, 3.618047497470177*^9}, { 592 | 3.618047550962237*^9, 3.6180475514842663`*^9}, {3.6180476224493256`*^9, 593 | 3.6180476307948027`*^9}, {3.6180478533415318`*^9, 3.618048158580991*^9}, { 594 | 3.6180482178443804`*^9, 3.6180482931776896`*^9}, {3.6180483604965396`*^9, 595 | 3.618048361067572*^9}, {3.618057134677394*^9, 3.61805715943381*^9}, { 596 | 3.6180572179251556`*^9, 3.6180572199892735`*^9}, {3.6180572539092135`*^9, 597 | 3.61805726975912*^9}, {3.618057311706519*^9, 3.618057375522169*^9}, { 598 | 3.618057430659323*^9, 3.6180574959770594`*^9}, 3.6180575514592323`*^9, { 599 | 3.6180575903224554`*^9, 3.618057591406517*^9}, {3.618057628084615*^9, 600 | 3.61805765911539*^9}, 3.6180577290823917`*^9, {3.618057772019848*^9, 601 | 3.6180577725668793`*^9}, 3.6180578155543375`*^9, 3.6180580037341013`*^9, 602 | 3.6180580684518027`*^9, {3.6180584956922398`*^9, 3.6180585145533185`*^9}}] 603 | }, 604 | WindowSize->{1358, 652}, 605 | WindowMargins->{{0, Automatic}, {Automatic, 0}}, 606 | FrontEndVersion->"8.0 for Microsoft Windows (64-bit) (November 7, 2010)", 607 | StyleDefinitions->"Default.nb" 608 | ] 609 | (* End of Notebook Content *) 610 | 611 | (* Internal cache information *) 612 | (*CellTagsOutline 613 | CellTagsIndex->{} 614 | *) 615 | (*CellTagsIndex 616 | CellTagsIndex->{} 617 | *) 618 | (*NotebookFileOutline 619 | Notebook[{ 620 | Cell[557, 20, 26758, 581, 1732, "Input"] 621 | } 622 | ] 623 | *) 624 | 625 | (* End of internal cache information *) 626 | --------------------------------------------------------------------------------