Advertisement
Guest User

solver.py

a guest
Sep 11th, 2017
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. import json
  2. import random
  3. import numpy as np
  4. from deap import algorithms
  5. from deap import base
  6. from deap import creator
  7. from deap import tools
  8.  
  9. class Evaluator(object):
  10.     def __init__(self, items, capacity):
  11.         self.items = items
  12.         self.capacity = capacity
  13.     def __call__(self, candidate):
  14.         total_weight = 0
  15.         total_profit = 0
  16.  
  17.         for i in xrange(len(candidate)):
  18.             if candidate[i]:
  19.                 total_weight += self.items[i]['weight']
  20.                 total_profit += self.items[i]['profit']
  21.  
  22.         if total_weight > self.capacity:
  23.             overload = total_weight - self.capacity
  24.         else:
  25.             overload = 0
  26.  
  27.         return (overload, total_profit)
  28.  
  29. with open('items.json') as f:
  30.     items = json.load(f)
  31.  
  32. with open('capacity.json') as f:
  33.     capacity = json.load(f)
  34.  
  35. evaluator = Evaluator(items, capacity)
  36.  
  37. # Objectives are decreasing overload and increasing total profit, in that order
  38. creator.create('FitnessMulti', base.Fitness, weights=(-1, 1))
  39. creator.create('Individual', list, fitness=creator.FitnessMulti)
  40.  
  41. toolbox = base.Toolbox()
  42. toolbox.register('random_bit', lambda: random.choice([False, True]))
  43. toolbox.register('individual',
  44.                  tools.initRepeat,
  45.                  creator.Individual,
  46.                  toolbox.random_bit,
  47.                  n = len(items))
  48. toolbox.register('population', tools.initRepeat, list, toolbox.individual)
  49. toolbox.register('evaluate', evaluator)
  50. toolbox.register('mate', tools.cxTwoPoint)
  51. toolbox.register('mutate', tools.mutFlipBit, indpb=0.05)
  52. toolbox.register('select', tools.selNSGA2)
  53.  
  54. # random.seed(42)
  55.  
  56. NGEN = 500
  57. CXPB = 0.5
  58. MUTPB = 0.2
  59.  
  60. pop = toolbox.population(n=100)
  61. hof = tools.ParetoFront()
  62.  
  63. pop, log = algorithms.eaSimple(pop, toolbox,
  64.                                CXPB,
  65.                                MUTPB,
  66.                                NGEN,
  67.                                halloffame=hof,
  68.                                verbose=True)
  69.  
  70. total_profit = 0
  71. total_weight = 0
  72. best_solution = hof[0]
  73.  
  74. print 'Items selected:'
  75.  
  76. for i in xrange(len(best_solution)):
  77.     if best_solution[i]:
  78.         print i + 1
  79.         total_profit += items[i]['profit']
  80.         total_weight += items[i]['weight']
  81.  
  82. print
  83. print 'Total weight: %d' % total_weight
  84. print 'Capacity: %d' % capacity
  85. print 'Total profit: %d' % total_profit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement