Presentation is loading. Please wait.

Presentation is loading. Please wait.

Page Optimising a Symbolic Problem of Sequences of Instructions.

Similar presentations


Presentation on theme: "Page Optimising a Symbolic Problem of Sequences of Instructions."— Presentation transcript:

1 Page Optimising a Symbolic Problem of Sequences of Instructions

2 Page 2 A Program that programs itself So far, we have seen GAs used for optimisation problems... What about using GAs for developing programs that program themselves? e.g. Can we write a GA that implements the following formula using a limited set of instructions? x 3 + y 3 + z

3 Page 3 Sample Problem Virtual Computer Consider a Virtual Computer Stack architecturea Stack (or zero address) architecture has no registers, only a stack for which instructions can manipulate the values on the stack. simple instruction sethas a simple instruction set (6 instructions that use operands on the stack)

4 Page 4 Sample Problem Simple Instruction Set Simple Instruction Set InstructionDescription DUPDuplicate the top of the stack (stack: A => A A) SWAPSwap the top two elements of the stack (stack: A B => B A) MULMultiply the top two elements of the stack (stack: 2 3 => 6) ADDAdd the top two elements of the stack (stack: 2 3 => 5) OVERDuplicate the second item on the stack (stack: A B => B A B) NOPNo operation

5 Page 5 Sample Problem Simple Instruction Set Simple Instruction Set functionsSimple and can be used to solve various functions. Example: Compute the square of the top element of the stack, assuming the top of the stack contains our input value: DUPDUP MULMUL This sequence duplicates the top of the stack and then multiplies the two elements together. (X * X), or X 2

6 Page 6 Solution Encoding We encode the solution of the problem, not the problem itself (because the problem is used as the fitness measure for our chromosomes). chromosome Our chromosome represents a string of instructions in a contiguous block. We assign numerical values to each of the instructions. DUP = 0 SWAP = 1 MUL = 2 ADD = 3 OVER = 4 NOP = 5 contiguous string stream of instructions Our encoding is a contiguous string of bytes representing the stream of instructions. Chromosome = program

7 Page Fitness Evaluation 7 executing the string of instructions Evaluating the fitness of a given chromosome is a simple process of executing the string of instructions that are contained in the chromosome. We prepare a simple stack of some depth. Load the initial values onto the stack. Execute each instruction sequentially until the program reaches the end instruction. fitness The fitness values is then calculated as the difference between the resulting value on the stack and what was expected per our objective function (predefined for the test).

8 PageRecombination 8 For this problem, crossover and mutation are used. crossover operator The crossover operator breaks an instruction stream at a single point and swaps the tails of each parent. mutation operator The mutation operator is a random reassignment of the gene with a new instruction. gene Because the chromosome represents the program, each gene is a single instruction.

9 Page

10 Virtual Machine Implementation 10 Stack-based operations: SPEEK, SPUSH, SPOP Assert macros to identify violations in the program DUP MU L DUP Stack depth Integer stack stackPointer

11 Page Virtual Machine Symbolics 11 //Instruction set #define DUP 0x00 #define SWAP 0x01 #define MUL 0x02 #define ADD 0x03 #define OVER 0x04 #define NOP 0x05 #define MAX_INSTRUCTION (NOP+1) //Error signals #define NONE0 #define STACK_VIOLATION1 #define MATH_VIOLATION2

12 Page Virtual Machine Symbolics 12 15 #define STACK_DEPTH 15 int stack[STACK_DEPTH]; int stackPointer; //Error functions #define ASSERT_STACK_ELEMENTS(x) \ if (stackPointer < x) { error = STACK_VIOLATION ; break; } #define ASSERT_STACK_NOT_FULL \ if (stackPointer == STACK_DEPTH) { \ error = STACK_VIOLATION ; break; } //Stack operators #define SPUSH(x) (stack[stackPointer++] = x) #define SPOP (stack[--stackPointer]) #define SPEEK (stack[stackPointer-1]) stackPointer x y z Tells how many elements are there in the stack.

13 Page Virtual Machine Implementation 13 Stack depth Integer stack stackPointer Function: interpretStackMachine x y z Stack-based operations: SPEEK, SPUSH, SPOP Assert macros to identify violations in the program One element higher than the topmost element

14 Page Virtual Machine Implementation 14 Instruction: DUP stackPointer Function: interpretStackMachine x y z ASSERT_STACK_ELEMENTS(1); ASSERT_STACK_NOT_FULL; SPUSH(SPEEK); Stack-based operations: SPEEK, SPUSH, SPOP Assert macros to identify violations in the program

15 Page Virtual Machine Implementation 15 Stack-based operations: SPEEK, SPUSH, SPOP Assert macros to identify violations in the program Instruction: SWAP stackPointer Function: interpretStackMachine x y z ASSERT_STACK_ELEMENTS(2); a = stack[stackPointer-1]; stack[stackPointer-1] = stack[stackPointer-2]; stack[stackPointer-2] = a;

16 Page Virtual Machine Implementation 16 Stack-based operations: SPEEK, SPUSH, SPOP Assert macros to identify violations in the program Instruction: MUL stackPointer Function: interpretStackMachine x y z ASSERT_STACK_ELEMENTS(2); a = SPOP; b = SPOP; SPUSH(a * b);

17 Page Virtual Machine Implementation 17 Stack-based operations: SPEEK, SPUSH, SPOP Assert macros to identify violations in the program Instruction: ADD stackPointer Function: interpretStackMachine x y z ASSERT_STACK_ELEMENTS(2); a = SPOP; b = SPOP; SPUSH(a + b);

18 Page Virtual Machine Implementation 18 Stack-based operations: SPEEK, SPUSH, SPOP Assert macros to identify violations in the program Instruction: OVER stackPointer Function: interpretStackMachine x y z ASSERT_STACK_ELEMENTS(2); SPUSH(stack[stackPointer-2]);

19 Page Virtual Machine Implementation 19 The function operates on the arguments according to the program fed to it. Inputs: stackPointer Function: interpretStackMachine x y z ProgramProgram Length of the programLength of the program Arguments (operands)Arguments (operands) Length of the argumentsLength of the arguments For testing, fitness calculations

20 Page Virtual Machine Implementation 20 The function operates on the arguments based on the program fed to it. Sequence of Operations: stackPointer Function: interpretStackMachine x y z 1.Load the passed arguments (operands) on the stack. This is done in a reverse fashion. That is, element 0 will be placed at the top of the stack.

21 Page Virtual Machine Implementation 21 Sequence of Operations: stackPointer Function: interpretStackMachine x y z 1.Load the passed arguments (operands) on the stack. This is done in a reverse fashion. That is, element 0 will be placed at the top of the stack. 2.Execute each of the instructions until the end of the program is reached. Return an error if one is encountered to allow another program to be generated.

22 Page Source Code 22 int interpretSTM(const int *program, int progLength, const int *args, int argsLength){ int pc = 0; int i, error = NONE; int a, b; stackPointer = 0; /* Load the arguments onto the stack */ for (i = argsLength-1 ; i >= 0 ; i--) { SPUSH(args[i]); } /* Execute the program */ while ((error == NONE) && (pc < progLength)) { switch(program[pc++]) { case DUP: ASSERT_STACK_ELEMENTS(1); ASSERT_STACK_NOT_FULL; SPUSH(SPEEK); break; case SWAP: ASSERT_STACK_ELEMENTS(2); a = stack[stackPointer-1]; stack[stackPointer-1] = stack[stackPointer-2]; stack[stackPointer-2] = a; break; case MUL: ASSERT_STACK_ELEMENTS(2); a = SPOP; b = SPOP; SPUSH(a * b); break; case ADD: ASSERT_STACK_ELEMENTS(2); a = SPOP; b = SPOP; SPUSH(a + b); break; case OVER: ASSERT_STACK_ELEMENTS(2); SPUSH(stack[stackPointer-2]); break; } /* Switch opcode */ } /* Loop */ return(error); }

23 Page

24 GA 24 stackPointer Declaration of Populations x y z population typedef struct population { float fitness; int progSize; int program[MAX_PROGRAM]; POPULATION_TYPE } POPULATION_TYPE; POPULATION_TYPE2 POPULATION_TYPE populations[2][MAX_CHROMS]; int curPop; Old and New Populations 3000

25 Page Initialising a chromosome 25 We assign each of the genes a random instruction. void initMember( pop, index ){ int progIndex; populations[pop][index].fitness = 0.0; populations[pop][index].progSize = MAX_PROGRAM-1; progIndex = 0; while (progIndex < MAX_PROGRAM) { populations[pop][index].program[progIndex++] = getRand(MAX_INSTRUCTION); } see init.c

26 Page Initialising the population 26 Initialize all of the chromosomes in the population (potential * solutions to the given problem). void initPopulation( void ){ int index; for (index = 0 ; index < MAX_CHROMS ; index++) { initMember(curPop, index); } see init.c

27 PageGA 27 stackPointer During the process of Recombination x y z POPULATION_TYPE2 POPULATION_TYPE populations[2][MAX_CHROMS]; curPop !curPop Where the Selection of Parents is performed Where the children are placed Also referred to as the nextPop see main.c

28 PageGA 28 Basic Flow 1.Seed the random number generator. 2.Initialise the population with random chromosomes. 3.Calculate fitness values for the chromosomes. (performFitnessCheck). 4.Use sum of fitness for probabilistic selection of parents. 5.Apply recombination operations = crossover + mutation. 6.Switch between the old and the new populations of chromosomes. 7.Calculate fitness values. 8.Check for premature convergence. 9.Check if the desired solution has been found already (by way of best fitness) 10.If (solution is found), show best chromosome, else go back to step 4. Same as conventional GAs

29 Page Basic Flow 29 srand(time(NULL)); initPopulation(); performFitnessCheck( fp ); while (generation < MAX_GENERATIONS) { curCrossovers = curMutations = 0; performSelection(); /* Switch the populations */ curPop = (curPop == 0) ? 1 : 0; performFitnessCheck( fp ) check for premature convergence, if so, break check if the desired solution has been found already, if so, break } show the best chromosome (solution)

30 Page performFitnessCheck 30 1.Define the arguments (operands) for testing. target equation = correct answer 2.Calculate the evaluation of the target equation = correct answer. 3.Feed the same arguments to the interpretStackMachine to evaluate the fitness. 10 times three-tiered measure Go back to Step 1: the fitness is calculated (10 times) based on a three-tiered measure: TIER1 Value = 1 If the program exited gracefully (no math error or program error), we give it a TIER1 Value = 1 see fitness.c TIER2 Value = 20 If only one element was left on the stack, then TIER2 Value = 20 TIER3 Value = 50 If the top of the stack contains the correct value, then TIER3 Value = 50 To ensure correctness of program generated

31 Page Check for premature convergence 31 Check diversity of population after ¼ of the generations has completed. If the population has prematurely converged, exit to allow the user to restart. if ( generation > (MAX_GENERATIONS * 0.25) ) { if ((avgFitness / maxFitness) > 0.98) { printf("converged\n"); break; } see main.c

32 Page Sample Runs 32 Equation: X 8 Resulting program evolved: DUP MUL DUP MUL DUP MUL

33 Page Sample Runs 33 Equation: (x*2) + (y*2) + z Resulting program evolved: ADD DUP ADD SWAP ADD

34 Page Sample Runs 34 Equation: (x*y)+(y*y)+z Resulting program evolved: OVER ADD MUL ADD

35 Page Sample Runs 35 Equation: x 3 +y 2 +z Resulting program evolved: DUP DUP MUL MUL SWAP DUP MUL SWAP ADD SWAP SWAP ADD

36 Page Plot of fitness over time 36 time (1 = 100 generations)

37 Page


Download ppt "Page Optimising a Symbolic Problem of Sequences of Instructions."

Similar presentations


Ads by Google