ILSSurrogate.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. # initialize current solution
  109. self.initRun()
  110. # enable resuming for ILS
  111. self.resume()
  112. if self.start_train_surrogate > self.getGlobalEvaluation():
  113. # get `self.start_train_surrogate` number of real evaluations and save it into surrogate dataset file
  114. # using randomly generated solutions (in order to cover seearch space)
  115. while self.start_train_surrogate > self.getGlobalEvaluation():
  116. newSolution = self.initializer()
  117. # evaluate new solution
  118. newSolution.evaluate(self.evaluator)
  119. # add it to surrogate pool
  120. self.add_to_surrogate(newSolution)
  121. self.increaseEvaluation()
  122. # train surrogate on real evaluated solutions file
  123. self.train_surrogate()
  124. self.load_surrogate()
  125. # local search algorithm implementation
  126. while not self.stop():
  127. # set current evaluator based on used or not of surrogate function
  128. current_evaluator = self.surrogate_evaluator if self.start_train_surrogate <= self.getGlobalEvaluation() else self.evaluator
  129. # create new local search instance
  130. # passing global evaluation param from ILS
  131. ls = LocalSearchSurrogate(self.initializer,
  132. current_evaluator,
  133. self.operators,
  134. self.policy,
  135. self.validator,
  136. self.maximise,
  137. _parent=self)
  138. # add same callbacks
  139. for callback in self.callbacks:
  140. ls.addCallback(callback)
  141. # create and search solution from local search
  142. newSolution = ls.run(_ls_evaluations)
  143. # if better solution than currently, replace it (solution saved in training pool, only if surrogate process is in a second process step)
  144. # Update : always add new solution into surrogate pool, not only if solution is better
  145. #if self.isBetter(newSolution) and self.start_train_surrogate < self.getGlobalEvaluation():
  146. if self.start_train_surrogate <= self.getGlobalEvaluation():
  147. # if better solution found from local search, retrained the found solution and test again
  148. # without use of surrogate
  149. fitness_score = self.evaluator(newSolution)
  150. # self.increaseEvaluation() # dot not add evaluation
  151. newSolution.score = fitness_score
  152. # if solution is really better after real evaluation, then we replace
  153. if self.isBetter(newSolution):
  154. self.bestSolution = newSolution
  155. self.add_to_surrogate(newSolution)
  156. self.progress()
  157. # check if necessary or not to train again surrogate
  158. if self.n_local_search % self.ls_train_surrogate == 0 and self.start_train_surrogate <= self.getGlobalEvaluation():
  159. # train again surrogate on real evaluated solutions file
  160. self.train_surrogate()
  161. # reload new surrogate function
  162. self.load_surrogate()
  163. # increase number of local search done
  164. self.n_local_search += 1
  165. self.information()
  166. logging.info("End of %s, best solution found %s" %
  167. (type(self).__name__, self.bestSolution))
  168. self.end()
  169. return self.bestSolution
  170. def addCallback(self, _callback):
  171. """Add new callback to algorithm specifying usefull parameters
  172. Args:
  173. _callback: {Callback} -- specific Callback instance
  174. """
  175. # specify current main algorithm reference
  176. if self.parent is not None:
  177. _callback.setAlgo(self.parent)
  178. else:
  179. _callback.setAlgo(self)
  180. # set as new
  181. self.callbacks.append(_callback)