Hongfeng Wang Pusan, Korea. Introduction General framwork of GA An example of GA programming 2.

19
Hongfeng Wang Pusan, Korea How to program a GA

Transcript of Hongfeng Wang Pusan, Korea. Introduction General framwork of GA An example of GA programming 2.

Page 1: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

Hongfeng Wang

Pusan, Korea

How to program a GA

Page 2: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

Contents

Introduction

General framwork of GA

An example of GA programming

2

Page 3: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

Introduction

GA is a population-based, iterative stochastic optimization method. Population-based: GA employs a population of chromosomes to

search for the optimum in the solution space in parallel. Each chromosome corresponds to a solution.

Iterative: GA is running during a iterative course. The algorithm result can be obtained by the best chromosome in the population at the last iteration (generation) until this iteration is terminated.

Stochastic: All GA operators (crossover, mutation and selection) are designed in a probability mechanism.

3

Page 4: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

Introduction

Then, GA is called as a meta-heuristic method GA cannot ensure the optimal solution is achieved. It just can achieve

a high quality solution within an acceptive compute time. GA uses a non-determinate way when its chromosome are searching

for optimum.

4

Page 5: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

General framwork of GA

5

begin parameterize(popsize, pc, pm); t = 0; initializePopulation(P(0)); evaluatePopulation(P(0)); repeat P’(t) = selectForReproduction(P(t)); P’’(t) = crossover(P’(t)); mutate(P’’(t)); evaluatePopulation(P’’(t)); P(t+1) = selectForProceed(P(t)+P’’(t)); t = t+1; until a stop condition is metend

Page 6: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Test function One-Max function: It is a 100-bit binary function, which aims to

maximize the number of ones in a binary string.

Algorithm design Binary encoding scheme Tournament selection method where the tournament size is set to 2 One-point crossover where the probability pc is set to 0.8 Bit-wise mutation where the probability pm is set to 0.01

6

Page 7: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Chromosome Define a class to express a chromosome Attributes: code and fitness Methods: initialize(), evaluate(), clone() and mutate();

7

Page 8: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class Chromosome{int[] code;

int fitness;

public Chromosome(int length){

code=new int[length];

fitness=0;

// this is a constructive method for class Chromosome, which aims to initialize the // values of two attributes (code and fitness) in Chromosome

}

void initialize(){ … }

void evaluate(){ … }

void clone(Chromosome chr){ … }

void mutate(double pm){ … }

void toprint(){ … }

}

8

Page 9: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class Chromosome{…

void initialize(){

for(int i=0;i<code.length;i++){

if(Math.random()<0.5){ code[i]=0; }

else{ code[i]=1; }

}

}

void evaluate(){

fitness=0;

for(int i=0;i<code.length;i++) {fitness+=code[i]; }

}

}

9

Page 10: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class Chromosome{…

void clone(Chromosome chr){

for(int i=0;i<code.length;i++){

code[i]=chr.code[i];

}

fitness=chr.fitness

}

void mutate(double pm){

for(int i=0;i<code.length;i++) {

if(Math.random()<pm){ code[i]=1-code[i]; }

}

}

void toprint(){ … }

}

10

Page 11: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

GA a main class to execute GA Attributes: length, popsize, max_gen, elite_num, pc, pm, old_pop,

new_pop, pool_pop, good_chr, best_chr Methods: initializePop(), selectChr(), crossover(), sort() and run();

11

Page 12: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class GA{int length=100,popsize=100,max_gen=200,elite_num=0;

double pc=0.8, pm=0.01;

Chromosome[] old_pop,new_pop,pool_pop;

Chromosome good_chr,best_chr;

public GA(){

// this is a constructive method for a class (GA), which aims to initialize the values of // all attributes (code and fitness) in GA

}

void initializePop(){ … }

void run(){ … }

void sort(Chromosome[] pop){ … }

int selectChr(Chromosome[] pop){ … }

void crossover(Chromosome chr1, Chromosome chr2){ … }

}

12

Page 13: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class GA{…

public GA(){old_pop=new Chromosome[popsize];

new_pop=new Chromosome[popsize];

for(int i=0;i<popsize;i++){

old_pop[i]=new Chromosome(length);

new_pop[i]=new Chromosome(length);

}

pool_pop=new Chromosome[popsize+elite_num];

for(int i=0;i<pool_pop.length;i++){

pool_pop [i]=new Chromosome(length);

}

good_chr=new Chromosome(length);

best_chr=new Chromosome(length);

}

}

13

Page 14: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class GA{…

void initializePop(){

for(int i=0;i<popsize;i++){

old_pop[i].initialize();

old_pop[i].evaluate();

}

}

}

14

Page 15: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class GA{…

void run(){initializePop(); sort(old_pop);

good_chr.clone(old_pop[0]); best_chr.clone(good_chr);

for(int gen=1;gen<max_gen;gen++){

for(int i=0;i<popsize;i++){ new_pop[i].clone(old_pop[selectChr(old_pop)]); }

for(int i=0;i<popsize/2;i++){ if(Math.random()<pc){ crossover(new_pop[2*i], new_pop[2*i+1]); } }

for(int i=0;i<popsize;i++){ new_pop[i].mutate(pm); }

for(int i=0;i<popsize;i++){ new_pop[i].evaluate(); }

for(int i=0;i<popsize;i++){ pool_pop[i].clone(new_pop[i]); }

for(int i=0;i<elite_num;i++){ pool_pop[popsize+i].clone(old_pop[i]); }

sort(pool_pop);

for(int i=0;i<popsize;i++){ old_pop[i].clone(pool_pop[i]); }

good_chr.clone(old_pop[0]);

if(good_chr.fitness>best_chr.fitness){ best_chr.clone(good_chr); }

}

best_chr.toprint();

}

} 15

Page 16: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class GA{…

void sort(Chromosome[] pop){…

// this method is used to sort a set of chromosomes in descending order

}

int selectChr(Chromosome[] pop){int n1=(int)(popsize*Math.random()),n2;

do{

n2 =(int)(popsize*Math.random());

}while(n1==n2);

if(pop[n1].fitness>pop[n2].fitness){ return n1; }

else{ return n2; }

}

}

16

Page 17: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class GA{…

void crossover(Chromosome chr1, Chromosome chr2){Chromosome chi1=new Chromosome(length), chi2=new Chromosome(length);

int n=(int)(length*Math.random());

for(int i=0;i<n;i++){

chi1.code[i]=chr1.code[i];

chi2.code[i]=chr2.code[i];

}

for(int i=n;i<length;i++){

chi1.code[i]=chr2.code[i];

chi2.code[i]=chr1.code[i];

}

chr1.clone(chi1);

chr2.clone(chi2);

}

}

17

Page 18: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

An example of GA coding

Class GA{…

public void static main(String[] args){

GA ga=new GA();

ga.run();

}

}

18

Page 19: Hongfeng Wang Pusan, Korea.  Introduction  General framwork of GA  An example of GA programming 2.

Production System Analysis Lab.

Pusan National University, Busan, Korea

부산대학교 산업공학과

생산시스템분석연구실