UCBPolicy.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. """Policy class implementation which is used for selecting operator using Upper Confidence Bound
  2. """
  3. # main imports
  4. import logging
  5. import random
  6. import math
  7. import numpy as np
  8. # module imports
  9. from .Policy import Policy
  10. class UCBPolicy(Policy):
  11. """UCB policy class which is used for applying UCB strategy when selecting and applying operator
  12. Attributes:
  13. operators: {[Operator]} -- list of selected operators for the algorithm
  14. C: {float} -- tradeoff between EvE parameter for UCB
  15. exp_rate: {float} -- exploration rate (probability to choose randomly next operator)
  16. rewards: {[float]} -- list of summed rewards obtained for each operator
  17. occurrences: {[int]} -- number of use (selected) of each operator
  18. """
  19. def __init__(self, _operators, _C=100., _exp_rate=0.5):
  20. self.operators = _operators
  21. self.rewards = [0. for o in self.operators]
  22. self.occurrences = [0 for o in self.operators]
  23. self.C = _C
  24. self.exp_rate = _exp_rate
  25. def select(self):
  26. """Select randomly the next operator to use
  27. Returns:
  28. {Operator}: the selected operator
  29. """
  30. indices = [i for i, o in enumerate(self.occurrences) if o == 0]
  31. # random choice following exploration rate
  32. if np.random.uniform(0, 1) <= self.exp_rate:
  33. index = random.choice(range(len(self.operators)))
  34. return self.operators[index]
  35. elif len(indices) == 0:
  36. # if operator have at least be used one time
  37. ucbValues = []
  38. nVisits = sum(self.occurrences)
  39. for i in range(len(self.operators)):
  40. ucbValue = self.rewards[i] + self.C * math.sqrt(
  41. math.log(nVisits) / (self.occurrences[i] + 0.1))
  42. ucbValues.append(ucbValue)
  43. return self.operators[ucbValues.index(max(ucbValues))]
  44. else:
  45. return self.operators[random.choice(indices)]
  46. def apply(self, _solution):
  47. """
  48. Apply specific operator chosen to create new solution, computes its fitness and returns solution
  49. Args:
  50. _solution: {Solution} -- the solution to use for generating new solution
  51. Returns:
  52. {Solution} -- new generated solution
  53. """
  54. operator = self.select()
  55. logging.info("---- Applying %s on %s" %
  56. (type(operator).__name__, _solution))
  57. # apply operator on solution
  58. newSolution = operator.apply(_solution)
  59. # compute fitness of new solution
  60. newSolution.evaluate(self.algo.evaluator)
  61. # compute fitness improvment rate
  62. if self.algo.maximise:
  63. fir = (newSolution.fitness() -
  64. _solution.fitness()) / _solution.fitness()
  65. else:
  66. fir = (_solution.fitness() -
  67. newSolution.fitness()) / _solution.fitness()
  68. operator_index = self.operators.index(operator)
  69. if fir > 0:
  70. self.rewards[operator_index] += fir
  71. self.occurrences[operator_index] += 1
  72. logging.info("---- Obtaining %s" % (_solution))
  73. return newSolution