Genetic Programming
1. Problem
Design and implement a genetic programming system to evolve some perceptrons that match well with a given training set. A training set is a collection of tuples of the form (x1, …, xn, l), where xi’s are real numbers and l is either 1 (positive example) or 0 (negative example). So for your genetic programming system, a ‘program’ is just a tuple (w1, … , wn, θ) of numbers (weights and the threshold). Answer the following questions:
-
What’s your fitness function?
-
What’s your crossover operator?
-
What’s your copy operator?
-
What’s your mutation operator, if you use any?
-
What’s the size of the initial generation, and how are programs generated?
-
When do you stop the evolution? Evolve it up to a fixed iteration, when it satisfies a condition on the fitness function, or a combination of the two?
-
What’s the output of your system for the training set of the below one? Its training-set.csv link
2. Solution
I use Python with Numpy and Pandas.
2.1 Fitness function
This returns the accuracy from 0 to 1 by the number of correctly predicted labels/the number of total labels when X
represents the training set as a matrix, y
is a vector having a corresponding label set.
2.2 Crossover operator
This randomly picks two genes from copied top 20% genes and generates a new gene with randomly picking a part of gene from those two until it makes the full gene.
2.3 Copy operator
It sorts genes by the accuracy and copies top 20% (the first 20% indices) genes.
2.4 Mutation operator
It randomly picks one gene and multiplies 1.4 or 0.6 those are randomly chosen number by me.
2.5 The size of the initial generation
2.6 The condition of evolution stop
A combination of a fixed iteration and the fitness score. To be specific, the number of iteration is 100 and the score is 0.98 (98% accuracy).
2.7 Output
accuracy, [Weights, -Theta]
0.8367346938775511
[ 2.71525264 1.11828279 2.68820513 0.27321884 2.52802301 1.8593701
-3.34377527 -2.8875624 1.48904056 -2.05926884]
accuracy, [Weights, -Theta]
0.9387755102040817
[ 1.18220027 -0.46205443 2.75175233 -1.35567167 2.50248339 -0.82743507
-3.70812714 -2.44350959 1.72111065 0.50576898]
accuracy, [Weights, -Theta]
0.9591836734693877
[ 0.33976931 -0.46205443 2.75175233 -0.33115215 2.50248339 -0.82743507
-3.70812714 -2.44350959 1.72111065 0.50576898]
accuracy, [Weights, -Theta]
0.9795918367346939
[-0.84338978 -1.46514636 2.75175233 -1.35567167 3.94876468 1.8593701
-3.70812714 -2.44350959 1.48904056 0.50576898]
accuracy, [Weights, -Theta]
0.9795918367346939
[-0.84338978 -1.46514636 2.75175233 -1.35567167 3.94876468 1.8593701
-3.70812714 -2.44350959 1.48904056 0.50576898]
accuracy, [Weights, -Theta]
1.0
[-0.84338978 -0.46205443 1.18068324 -0.33115215 3.94876468 1.75438933
-3.70812714 -2.44350959 0.73136723 0.50576898]
Final accuracy, [Weights, -Theta]
1.0
[-0.84338978 -0.46205443 1.18068324 -0.33115215 3.94876468 1.75438933
-3.70812714 -2.44350959 0.73136723 0.50576898]
Thus, the final set trains the following perceptron with 100% accuracy.
[w0, w1, w2, w3, w4, w5, w6, w7, w8, w9] =
[-0.84338978 -0.46205443 1.18068324 -0.33115215 3.94876468 1.75438933 -3.70812714 -2.44350959 0.73136723 0.50576898]
when x9 = 1 and w9 = -θ
You can check all code here GP.py link Click!.