Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import numpy as np
- MUTATION_PROB = 0.1
- GENE_FROM_FATHER_PROB = 0.5
- MUTATION_EPSILON = 0.5
- POP_SIZE = 100
- class Chromosome:
- def __init__(self, genotype):
- self.genotype = genotype
- self.phenotype = None
- def XO(self, chrom):
- off = []
- for i in range(len(self.genotype)):
- prob = random.random()
- if prob < GENE_FROM_FATHER_PROB:
- off.append(self.genotype[i])
- else:
- off.append(chrom.genotype[i])
- return Chromosome(off)
- def mutation(self, epsilon):
- for i in range(len(self.genotype)):
- prob = random.random()
- if prob < MUTATION_PROB:
- self.genotype[i] += epsilon
- def predict(self, features):
- coefs = np.asarray(self.genotype)
- results = np.dot(features, coefs)
- return self.sigmoid(results)
- def sigmoid(self, x):
- val = 1 / (1 + np.exp(-x))
- eps = 1e-9
- val = np.maximum(val, eps)
- val = np.minimum(val, 1 - eps)
- return val
- def __cost(self, features, results):
- #Cost = (labels * log(predictions) + (1 - labels) * log(1 - predictions)) / len(labels)
- predictions = self.predict(features)
- class1Cost = -results * np.log(predictions)
- class2Cost = (1-results) * np.log(1-predictions)
- cost = class1Cost - class2Cost
- cost = cost.sum() / len(features)
- return cost
- def eval(self, features, coefs):
- self.phenotype = self.__cost(features, coefs)
- class Population:
- def __init__(self):
- self.chroms = None
- self.best = None
- self.coefs = None
- def init(self, popSize, geneCount):
- self.chroms = []
- for chrom in range(popSize):
- genotype = [((random.random() * 100) - 50) for gene in range(geneCount)]
- self.chroms.append(Chromosome(genotype))
- def select(self):
- self.chroms = sorted(self.chroms, key=lambda x:x.phenotype)
- return random.choice(self.chroms[:len(self.chroms)//4 + 1])
- def replaceWorst(self, chrom):
- worstPos = 0
- worstPheno = self.chroms[0].phenotype
- for i in range(1, len(self.chroms)):
- if self.chroms[i].phenotype > worstPheno:
- worstPos, worstPheno = i, self.chroms[i].phenotype
- self.chroms[worstPos] = chrom
- def train(self,features, results, learningRate=0.1, epochCount=100000,feedbackEpoch = 1000):
- self.init(POP_SIZE, len(features[0]))
- self.eval(features, results)
- for epoch in range(epochCount):
- self.best = self.getBest()
- if epoch % feedbackEpoch == 0:
- print("Epoch: " + str(epoch) + " cost: " + str(self.best.phenotype))
- mother = self.select()
- father = self.select()
- off = father.XO(mother)
- off.mutation(learningRate)
- off.eval(features, results)
- self.replaceWorst(off)
- self.coefs = self.best.genotype
- def getBest(self):
- bestPos = 0
- bestPheno = self.chroms[0].phenotype
- for i in range(1, len(self.chroms)):
- if self.chroms[i].phenotype < bestPheno:
- bestPos, bestPheno = i, self.chroms[i].phenotype
- return self.chroms[bestPos]
- def eval(self, features, results):
- for chrom in self.chroms:
- chrom.eval(features, results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement