Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import random
  2.  
  3. import numpy as np
  4.  
  5. MUTATION_PROB = 0.1
  6. GENE_FROM_FATHER_PROB = 0.5
  7. MUTATION_EPSILON = 0.5
  8. POP_SIZE = 100
  9.  
  10. class Chromosome:
  11.  
  12. def __init__(self, genotype):
  13. self.genotype = genotype
  14. self.phenotype = None
  15.  
  16. def XO(self, chrom):
  17. off = []
  18. for i in range(len(self.genotype)):
  19. prob = random.random()
  20. if prob < GENE_FROM_FATHER_PROB:
  21. off.append(self.genotype[i])
  22. else:
  23. off.append(chrom.genotype[i])
  24. return Chromosome(off)
  25.  
  26. def mutation(self, epsilon):
  27. for i in range(len(self.genotype)):
  28. prob = random.random()
  29. if prob < MUTATION_PROB:
  30. self.genotype[i] += epsilon
  31.  
  32.  
  33.  
  34. def predict(self, features):
  35. coefs = np.asarray(self.genotype)
  36. results = np.dot(features, coefs)
  37. return self.sigmoid(results)
  38.  
  39. def sigmoid(self, x):
  40. val = 1 / (1 + np.exp(-x))
  41. eps = 1e-9
  42. val = np.maximum(val, eps)
  43. val = np.minimum(val, 1 - eps)
  44. return val
  45.  
  46. def __cost(self, features, results):
  47. #Cost = (labels * log(predictions) + (1 - labels) * log(1 - predictions)) / len(labels)
  48. predictions = self.predict(features)
  49.  
  50. class1Cost = -results * np.log(predictions)
  51. class2Cost = (1-results) * np.log(1-predictions)
  52. cost = class1Cost - class2Cost
  53.  
  54.  
  55. cost = cost.sum() / len(features)
  56. return cost
  57.  
  58. def eval(self, features, coefs):
  59. self.phenotype = self.__cost(features, coefs)
  60.  
  61.  
  62. class Population:
  63.  
  64. def __init__(self):
  65. self.chroms = None
  66. self.best = None
  67. self.coefs = None
  68.  
  69. def init(self, popSize, geneCount):
  70. self.chroms = []
  71. for chrom in range(popSize):
  72. genotype = [((random.random() * 100) - 50) for gene in range(geneCount)]
  73. self.chroms.append(Chromosome(genotype))
  74.  
  75. def select(self):
  76. self.chroms = sorted(self.chroms, key=lambda x:x.phenotype)
  77. return random.choice(self.chroms[:len(self.chroms)//4 + 1])
  78.  
  79. def replaceWorst(self, chrom):
  80. worstPos = 0
  81. worstPheno = self.chroms[0].phenotype
  82.  
  83. for i in range(1, len(self.chroms)):
  84. if self.chroms[i].phenotype > worstPheno:
  85. worstPos, worstPheno = i, self.chroms[i].phenotype
  86.  
  87. self.chroms[worstPos] = chrom
  88.  
  89.  
  90. def train(self,features, results, learningRate=0.1, epochCount=100000,feedbackEpoch = 1000):
  91. self.init(POP_SIZE, len(features[0]))
  92. self.eval(features, results)
  93.  
  94. for epoch in range(epochCount):
  95. self.best = self.getBest()
  96.  
  97. if epoch % feedbackEpoch == 0:
  98. print("Epoch: " + str(epoch) + " cost: " + str(self.best.phenotype))
  99.  
  100.  
  101. mother = self.select()
  102. father = self.select()
  103. off = father.XO(mother)
  104. off.mutation(learningRate)
  105. off.eval(features, results)
  106.  
  107. self.replaceWorst(off)
  108.  
  109. self.coefs = self.best.genotype
  110.  
  111.  
  112.  
  113. def getBest(self):
  114. bestPos = 0
  115. bestPheno = self.chroms[0].phenotype
  116.  
  117. for i in range(1, len(self.chroms)):
  118. if self.chroms[i].phenotype < bestPheno:
  119. bestPos, bestPheno = i, self.chroms[i].phenotype
  120.  
  121. return self.chroms[bestPos]
  122.  
  123. def eval(self, features, results):
  124. for chrom in self.chroms:
  125. chrom.eval(features, results)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement