ILSSurrogate.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """Iterated Local Search Algorithm implementation using surrogate as fitness approximation
  2. """
  3. # main imports
  4. import os
  5. import logging
  6. import joblib
  7. # module imports
  8. from macop.algorithms.Algorithm import Algorithm
  9. from .LSSurrogate import LocalSearchSurrogate
  10. from sklearn.linear_model import (LinearRegression, Lasso, Lars, LassoLars,
  11. LassoCV, ElasticNet)
  12. from wsao.sao.problems.nd3dproblem import ND3DProblem
  13. from wsao.sao.surrogates.walsh import WalshSurrogate
  14. from wsao.sao.algos.fitter import FitterAlgo
  15. from wsao.sao.utils.analysis import SamplerAnalysis, FitterAnalysis, OptimizerAnalysis
  16. class ILSSurrogate(Algorithm):
  17. """Iterated Local Search used to avoid local optima and increave EvE (Exploration vs Exploitation) compromise using surrogate
  18. Attributes:
  19. initalizer: {function} -- basic function strategy to initialize solution
  20. evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)
  21. operators: {[Operator]} -- list of operator to use when launching algorithm
  22. policy: {Policy} -- Policy class implementation strategy to select operators
  23. validator: {function} -- basic function to check if solution is valid or not under some constraints
  24. maximise: {bool} -- specify kind of optimization problem
  25. currentSolution: {Solution} -- current solution managed for current evaluation
  26. bestSolution: {Solution} -- best solution found so far during running algorithm
  27. ls_iteration: {int} -- number of evaluation for each local search algorithm
  28. surrogate_file: {str} -- Surrogate model file to load (model trained using https://gitlab.com/florianlprt/wsao)
  29. start_train_surrogate: {int} -- number of evaluation expected before start training and use surrogate
  30. surrogate: {Surrogate} -- Surrogate model instance loaded
  31. ls_train_surrogate: {int} -- Specify if we need to retrain our surrogate model (every Local Search)
  32. solutions_file: {str} -- Path where real evaluated solutions are saved in order to train surrogate again
  33. callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm
  34. """
  35. def __init__(self,
  36. _initalizer,
  37. _evaluator,
  38. _operators,
  39. _policy,
  40. _validator,
  41. _surrogate_file_path,
  42. _start_train_surrogate,
  43. _ls_train_surrogate,
  44. _solutions_file,
  45. _maximise=True,
  46. _parent=None):
  47. # set real evaluator as default
  48. super().__init__(_initalizer, _evaluator, _operators, _policy,
  49. _validator, _maximise, _parent)
  50. self.n_local_search = 0
  51. self.surrogate_file_path = _surrogate_file_path
  52. self.start_train_surrogate = _start_train_surrogate
  53. self.surrogate_evaluator = None
  54. self.ls_train_surrogate = _ls_train_surrogate
  55. self.solutions_file = _solutions_file
  56. def train_surrogate(self):
  57. """etrain if necessary the whole surrogate fitness approximation function
  58. """
  59. # Following https://gitlab.com/florianlprt/wsao, we re-train the model
  60. # ---------------------------------------------------------------------------
  61. # cli_restart.py problem=nd3d,size=30,filename="data/statistics_extended_svdn" \
  62. # model=lasso,alpha=1e-5 \
  63. # surrogate=walsh,order=3 \
  64. # algo=fitter,algo_restarts=10,samplefile=stats_extended.csv \
  65. # sample=1000,step=10 \
  66. # analysis=fitter,logfile=out_fit.csv
  67. problem = ND3DProblem(size=len(self.bestSolution.data)) # problem size based on best solution size (need to improve...)
  68. model = Lasso(alpha=1e-5)
  69. surrogate = WalshSurrogate(order=3, size=problem.size, model=model)
  70. analysis = FitterAnalysis(logfile="train_surrogate.log", problem=problem)
  71. algo = FitterAlgo(problem=problem, surrogate=surrogate, analysis=analysis, seed=problem.seed)
  72. print("Start fitting again the surrogate model")
  73. for r in range(10):
  74. print("Iteration n°{0}: for fitting surrogate".format(r))
  75. algo.run(samplefile=self.solutions_file, sample=100, step=10)
  76. joblib.dump(algo, self.surrogate_file_path)
  77. def load_surrogate(self):
  78. """Load algorithm with surrogate model and create lambda evaluator function
  79. """
  80. # need to first train surrogate if not exist
  81. if not os.path.exists(self.surrogate_file_path):
  82. self.train_surrogate()
  83. self.surrogate = joblib.load(self.surrogate_file_path)
  84. # update evaluator function
  85. self.surrogate_evaluator = lambda s: self.surrogate.surrogate.predict([s.data])[0]
  86. def add_to_surrogate(self, solution):
  87. # save real evaluated solution into specific file for surrogate
  88. with open(self.solutions_file, 'a') as f:
  89. line = ""
  90. for index, e in enumerate(solution.data):
  91. line += str(e)
  92. if index < len(solution.data) - 1:
  93. line += ","
  94. line += ";"
  95. line += str(solution.score)
  96. f.write(line + "\n")
  97. def run(self, _evaluations, _ls_evaluations=100):
  98. """
  99. Run the iterated local search algorithm using local search (EvE compromise)
  100. Args:
  101. _evaluations: {int} -- number of global evaluations for ILS
  102. _ls_evaluations: {int} -- number of Local search evaluations (default: 100)
  103. Returns:
  104. {Solution} -- best solution found
  105. """
  106. # by default use of mother method to initialize variables
  107. super().run(_evaluations)
  108. # enable resuming for ILS
  109. self.resume()
  110. if self.start_train_surrogate < self.getGlobalEvaluation():
  111. self.load_surrogate()
  112. # initialize current solution
  113. self.initRun()
  114. # local search algorithm implementation
  115. while not self.stop():
  116. # set current evaluator based on used or not of surrogate function
  117. current_evaluator = self.surrogate_evaluator if self.start_train_surrogate < self.getGlobalEvaluation() else self.evaluator
  118. # create new local search instance
  119. # passing global evaluation param from ILS
  120. ls = LocalSearchSurrogate(self.initializer,
  121. current_evaluator,
  122. self.operators,
  123. self.policy,
  124. self.validator,
  125. self.maximise,
  126. _parent=self)
  127. # add same callbacks
  128. for callback in self.callbacks:
  129. ls.addCallback(callback)
  130. # create and search solution from local search
  131. newSolution = ls.run(_ls_evaluations)
  132. # if better solution than currently, replace it (solution saved in training pool, only if surrogate process is in a second process step)
  133. if self.isBetter(newSolution) and self.start_train_surrogate < self.getGlobalEvaluation():
  134. # if better solution found from local search, retrained the found solution and test again
  135. # without use of surrogate
  136. fitness_score = self.evaluator(newSolution)
  137. self.increaseEvaluation()
  138. newSolution.score = fitness_score
  139. # if solution is really better after real evaluation, then we replace
  140. if self.isBetter(newSolution):
  141. self.bestSolution = newSolution
  142. self.add_to_surrogate(newSolution)
  143. # check if necessary or not to train again surrogate
  144. if self.n_local_search % self.ls_train_surrogate == 0 and self.start_train_surrogate < self.getGlobalEvaluation():
  145. # train again surrogate on real evaluated solutions file
  146. self.train_surrogate()
  147. # reload new surrogate function
  148. self.load_surrogate()
  149. # increase number of local search done
  150. self.n_local_search += 1
  151. self.information()
  152. logging.info("End of %s, best solution found %s" %
  153. (type(self).__name__, self.bestSolution))
  154. self.end()
  155. return self.bestSolution