ILSSurrogate.py 8.9 KB

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