Algorithm.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import logging
  2. # Generic algorithm class
  3. class Algorithm():
  4. def __init__(self, _initalizer, _evaluator, _updators, _policy, _validator, _maximise=True):
  5. """
  6. Initialize all usefull parameters for problem to solve
  7. """
  8. self.initializer = _initalizer
  9. self.evaluator = _evaluator
  10. self.updators = _updators
  11. self.validator = _validator
  12. self.policy = _policy
  13. self.currentSolution = self.initializer()
  14. # evaluate current solution
  15. self.currentSolution.evaluate(self.evaluator)
  16. # keep in memory best known solution (current solution)
  17. self.bestSolution = self.currentSolution
  18. # other parameters
  19. self.maxEvalutations = 0 # by default
  20. self.numberOfEvaluations = 0
  21. self.maximise = _maximise
  22. def reinit(self):
  23. """
  24. Reinit the whole variable
  25. """
  26. self.currentSolution = self.initializer
  27. self.bestSolution = self.currentSolution
  28. self.numberOfEvaluations = 0
  29. def evaluate(self, solution):
  30. """
  31. Returns:
  32. fitness score of solution which is not already evaluated or changed
  33. Note:
  34. if multi-objective problem this method can be updated using array of `evaluator`
  35. """
  36. return solution.evaluate(self.evaluator)
  37. def update(self, solution):
  38. """
  39. Apply update function to solution using specific `policy`
  40. Check if solution is valid after modification and returns it
  41. Returns:
  42. updated solution
  43. """
  44. sol = self.policy.apply(solution)
  45. if(sol.isValid(self.validator)):
  46. return sol
  47. else:
  48. print("New solution is not valid", sol)
  49. return solution
  50. def isBetter(self, solution):
  51. """
  52. Check if solution is better than best found
  53. Returns:
  54. `True` if better
  55. """
  56. # depending of problem to solve (maximizing or minimizing)
  57. if self.maximise:
  58. if self.evaluate(solution) > self.bestSolution.fitness():
  59. return True
  60. else:
  61. if self.evaluate(solution) < self.bestSolution.fitness():
  62. return True
  63. # by default
  64. return False
  65. def run(self, _evaluations):
  66. """
  67. Run the specific algorithm following number of evaluations to find optima
  68. """
  69. self.maxEvalutations = _evaluations
  70. logging.info("Run %s with %s evaluations" % (self.__str__(), _evaluations))
  71. def progress(self):
  72. logging.info("-- Evaluation n°%s/%s, %s%%" % (self.numberOfEvaluations, self.maxEvalutations, "{0:.2f}".format((self.numberOfEvaluations) / self.maxEvalutations * 100.)))
  73. def information(self):
  74. logging.info("-- Found %s with score of %s" % (self.bestSolution, self.bestSolution.fitness()))
  75. def __str__(self):
  76. return "%s using %s" % (type(self).__name__, type(self.bestSolution).__name__)