Page Optimising a Symbolic Problem of Sequences of Instructions.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

Chapter 16 Java Virtual Machine. To compile a java program in Simple.java, enter javac Simple.java javac outputs Simple.class, a file that contains bytecode.
1 Lecture 10 Intermediate Representations. 2 front end »produces an intermediate representation (IR) for the program. optimizer »transforms the code in.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
SPIM and MIPS programming
Ch. 8 Functions.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Genetic Programming 김용덕 Page 2 Contents What is Genetic Programming? Difference between GP and GA Flowchart for GP Structures in GP.
Instruction Set Architecture & Design
Intermediate code generation. Code Generation Create linear representation of program Result can be machine code, assembly code, code for an abstract.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
COMP305. Part II. Genetic Algorithms. Genetic Algorithms.
CS 536 Spring Code generation I Lecture 20.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
COMP305. Part II. Genetic Algorithms. Genetic Algorithms.
Prof. Fateman CS 164 Lecture 201 Virtual Machine Structure Lecture 20.
5/6/99 Ashish Sabharwal1 JVM Architecture n Local storage area –Randomly accessible –Just like standard RAM –Stores variables (eg. an array) –Have to specify.
Topic 15 Implementing and Using Stacks
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 1: An Overview of Computers and Programming Languages C++ Programming:
CS0007: Introduction to Computer Programming Introduction to Arrays.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Genetic Programming.
Slides are based on Negnevitsky, Pearson Education, Lecture 10 Evolutionary Computation: Evolution strategies and genetic programming n Evolution.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)
Genetic Algorithm.
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Lecture 17 Today’s Lecture –Instruction formats Little versus big endian Internal storage in the CPU: stacks vs. registers Number of operands and instruction.
Soft Computing Lecture 18 Foundations of genetic algorithms (GA). Using of GA.
SOFT COMPUTING (Optimization Techniques using GA) Dr. N.Uma Maheswari Professor/CSE PSNA CET.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Programmer's view on Computer Architecture by Istvan Haller.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Zorica Stanimirović Faculty of Mathematics, University of Belgrade
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
4-1 4 Interpretation  Overview  Virtual machine interpretation  Case study: SVM  Case study: SVM interpreter in Java Programming Languages 3 © 2012.
Genetic Algorithms Michael J. Watts
December 8, 2003Other ISA's1 Other ISAs Next, we discuss some alternative instruction set designs. – Different ways of specifying memory addresses – Different.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
What is Genetic Programming? Genetic programming is a model of programming which uses the ideas (and some of the terminology) of biological evolution to.
Flow of Control Part 1: Selection
Prof. Fateman CS 164 Lecture 281 Virtual Machine Structure Lecture 28.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
CMSC 150 PROGRAM EXECUTION CS 150: Wed 1 Feb 2012.
M. Mateen Yaqoob The University of Lahore Spring 2014.
Wednesday, March 2 Homework #2 is posted Homework #2 is posted Due Friday, March 4 (BOC) Due Friday, March 4 (BOC) Program #5 is posted Program #5 is posted.
GENETIC ALGORITHM Basic Algorithm begin set time t = 0;
PHY 201 (Blum)1 Stacks Based in part on material from Chapters 4 & 5 in Computer Architecture by Nicholas Carter.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Example of formula (defun roots (a b c) (list
Evolution strategies and genetic programming
CS216: Program and Data Representation
Algorithm Analysis CSE 2011 Winter September 2018.
Stacks Chapter 4.
CS 11 C track: lecture 8 Last week: hash tables, C preprocessor
11/10/2018.
Instructions - Type and Format
CSCE Fall 2013 Prof. Jennifer L. Welch.
Programming Funamental slides
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
Other ISAs Next, we’ll first we look at a longer example program, starting with some C code and translating it into our assembly language. Then we discuss.
CSCE Fall 2012 Prof. Jennifer L. Welch.
Addressing mode summary
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Presentation transcript:

Page Optimising a Symbolic Problem of Sequences of Instructions

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

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)

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

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

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

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).

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.

Page

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

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

Page Virtual Machine Symbolics #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.

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

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

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;

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);

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);

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]);

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

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.

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.

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); }

Page

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

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

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

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

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

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)

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

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

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

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

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

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

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

Page