Advertisement
Guest User

Untitled

a guest
May 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Apr 12 13:15:12 2016
  4.  
  5. @author: Samir Kanaan
  6. """
  7.  
  8.  
  9. # This class encloses ten oil wells, each with an extract() operation that
  10. # returns the amount of barrels produced in that extraction.
  11.  
  12. import operator
  13. import random
  14.  
  15. import numpy
  16.  
  17. from deap import base
  18. from deap import benchmarks
  19. from deap import creator
  20. from deap import tools
  21.  
  22. class OilWells(object):
  23.     # Each oil well is a function y = ax**2+bx+c
  24.     def __init__(self):
  25.         self.a = [ 0.005, 0.001, -0.1, 0.002,    0, -0.05,  -0.1, 0.01, 0.001,  0.02]
  26.         self.b = [  -1.5, -0.59, 0.05,  -0.2, -0.1,  0.05, -0.05, -1.6, 0.001,  -1.8]
  27.         self.c = [    40,    28,   42,    10,   12,    30,    45,   38,     3,    22]
  28.        
  29.         # Current extraction number in each well
  30.         self.x = [0]*self.numberOfWells()
  31.    
  32.     # Simply return the number of wells
  33.     def numberOfWells(self):
  34.         return len(self.a)
  35.        
  36.     # Reset the state of the wells to allow a new simulation
  37.     def reset(self):
  38.         self.x = [0]*self.numberOfWells()
  39.        
  40.  
  41.     # Make an extraction from a given well (0..9). Min is 0 barrels
  42.     def extract(self, well):
  43.         try:
  44.             x = self.x[well]
  45.             y = self.a[well]*x**2 + self.b[well]*x + self.c[well]
  46.             self.x[well] += 1
  47.             return max(0,round(y))
  48.         except:
  49.             print('ERROR: invalid well number(%d)' % well)
  50.  
  51.  
  52.  
  53. creator.create("OilWellsMax", OilWells, weights=(1.0,), well = None, barrels = None)
  54. creator.create("Well", list, well=creator.OilWellsMax)
  55.  
  56.  
  57. def generate( well):
  58.     part = creator.OilWellsMax()
  59.     part.barrels = part.extract( well)
  60.     part.well = well
  61.     return part
  62.  
  63.        
  64. toolbox = base.Toolbox()
  65. toolbox.register("evaluate", generate, well = random.randint(0,9) )
  66. toolbox.register("individual", tools.initRepeat, creator.Well, toolbox.evaluate,  n = 100)
  67. toolbox.register("population", tools.initRepeat, list, toolbox.individual)
  68.  
  69.  
  70.  
  71.  
  72. def main():
  73.     pop = toolbox.population(n = 10)
  74.    
  75.     stats = tools.Statistics(lambda ind: ind.well.barrels)
  76.     stats.register("max", numpy.max)
  77.    
  78.    
  79.     logbook = tools.Logbook()
  80.     logbook.header = ["gen", "evals"] + stats.fields
  81.  
  82.     GEN = 1000
  83.     best = None
  84.  
  85.     for g in range(GEN):
  86.         for part in pop:
  87.             #part.barrels = toolbox.evaluate(part).barrels
  88.             if not part.best or part.best.well < part.well:
  89.                 part.best = creator.Well(part)
  90.                 part.best.well.barrels = part.well.barrels
  91.             if not best or best.well < part.well:
  92.                 best = creator.Well(part)
  93.                 best.well.barrels = part.well.barrels
  94.         for part in pop:
  95.             toolbox.update(part, best)
  96.  
  97.         # Gather all the fitnesses in one list and print the stats
  98.         logbook.record(gen=g, evals=len(pop), **stats.compile(pop))
  99.         print(logbook.stream)
  100.    
  101.     return pop, logbook, best
  102.  
  103. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement