_dual_annealing.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. # Dual Annealing implementation.
  2. # Copyright (c) 2018 Sylvain Gubian <sylvain.gubian@pmi.com>,
  3. # Yang Xiang <yang.xiang@pmi.com>
  4. # Author: Sylvain Gubian, Yang Xiang, PMP S.A.
  5. """
  6. A Dual Annealing global optimization algorithm
  7. """
  8. import numpy as np
  9. from scipy.optimize import OptimizeResult
  10. from scipy.optimize import minimize, Bounds
  11. from scipy.special import gammaln
  12. from scipy._lib._util import check_random_state, _transition_to_rng
  13. from scipy.optimize._constraints import new_bounds_to_old
  14. __all__ = ['dual_annealing']
  15. class VisitingDistribution:
  16. """
  17. Class used to generate new coordinates based on the distorted
  18. Cauchy-Lorentz distribution. Depending on the steps within the strategy
  19. chain, the class implements the strategy for generating new location
  20. changes.
  21. Parameters
  22. ----------
  23. lb : array_like
  24. A 1-D NumPy ndarray containing lower bounds of the generated
  25. components. Neither NaN or inf are allowed.
  26. ub : array_like
  27. A 1-D NumPy ndarray containing upper bounds for the generated
  28. components. Neither NaN or inf are allowed.
  29. visiting_param : float
  30. Parameter for visiting distribution. Default value is 2.62.
  31. Higher values give the visiting distribution a heavier tail, this
  32. makes the algorithm jump to a more distant region.
  33. The value range is (1, 3]. Its value is fixed for the life of the
  34. object.
  35. rng_gen : {`~numpy.random.Generator`}
  36. A `~numpy.random.Generator` object for generating new locations.
  37. (can be a `~numpy.random.RandomState` object until SPEC007 transition
  38. is fully complete).
  39. """
  40. TAIL_LIMIT = 1.e8
  41. MIN_VISIT_BOUND = 1.e-10
  42. def __init__(self, lb, ub, visiting_param, rng_gen):
  43. # if you wish to make _visiting_param adjustable during the life of
  44. # the object then _factor2, _factor3, _factor5, _d1, _factor6 will
  45. # have to be dynamically calculated in `visit_fn`. They're factored
  46. # out here so they don't need to be recalculated all the time.
  47. self._visiting_param = visiting_param
  48. self.rng_gen = rng_gen
  49. self.lower = lb
  50. self.upper = ub
  51. self.bound_range = ub - lb
  52. # these are invariant numbers unless visiting_param changes
  53. self._factor2 = np.exp((4.0 - self._visiting_param) * np.log(
  54. self._visiting_param - 1.0))
  55. self._factor3 = np.exp((2.0 - self._visiting_param) * np.log(2.0)
  56. / (self._visiting_param - 1.0))
  57. self._factor4_p = np.sqrt(np.pi) * self._factor2 / (self._factor3 * (
  58. 3.0 - self._visiting_param))
  59. self._factor5 = 1.0 / (self._visiting_param - 1.0) - 0.5
  60. self._d1 = 2.0 - self._factor5
  61. self._factor6 = np.pi * (1.0 - self._factor5) / np.sin(
  62. np.pi * (1.0 - self._factor5)) / np.exp(gammaln(self._d1))
  63. def visiting(self, x, step, temperature):
  64. """ Based on the step in the strategy chain, new coordinates are
  65. generated by changing all components is the same time or only
  66. one of them, the new values are computed with visit_fn method
  67. """
  68. dim = x.size
  69. if step < dim:
  70. # Changing all coordinates with a new visiting value
  71. visits = self.visit_fn(temperature, dim)
  72. upper_sample, lower_sample = self.rng_gen.uniform(size=2)
  73. visits[visits > self.TAIL_LIMIT] = self.TAIL_LIMIT * upper_sample
  74. visits[visits < -self.TAIL_LIMIT] = -self.TAIL_LIMIT * lower_sample
  75. x_visit = visits + x
  76. a = x_visit - self.lower
  77. b = np.fmod(a, self.bound_range) + self.bound_range
  78. x_visit = np.fmod(b, self.bound_range) + self.lower
  79. x_visit[np.fabs(
  80. x_visit - self.lower) < self.MIN_VISIT_BOUND] += 1.e-10
  81. else:
  82. # Changing only one coordinate at a time based on strategy
  83. # chain step
  84. x_visit = np.copy(x)
  85. visit = self.visit_fn(temperature, 1)[0]
  86. if visit > self.TAIL_LIMIT:
  87. visit = self.TAIL_LIMIT * self.rng_gen.uniform()
  88. elif visit < -self.TAIL_LIMIT:
  89. visit = -self.TAIL_LIMIT * self.rng_gen.uniform()
  90. index = step - dim
  91. x_visit[index] = visit + x[index]
  92. a = x_visit[index] - self.lower[index]
  93. b = np.fmod(a, self.bound_range[index]) + self.bound_range[index]
  94. x_visit[index] = np.fmod(b, self.bound_range[
  95. index]) + self.lower[index]
  96. if np.fabs(x_visit[index] - self.lower[
  97. index]) < self.MIN_VISIT_BOUND:
  98. x_visit[index] += self.MIN_VISIT_BOUND
  99. return x_visit
  100. def visit_fn(self, temperature, dim):
  101. """ Formula Visita from p. 405 of reference [2] """
  102. x, y = self.rng_gen.normal(size=(dim, 2)).T
  103. factor1 = np.exp(np.log(temperature) / (self._visiting_param - 1.0))
  104. factor4 = self._factor4_p * factor1
  105. # sigmax
  106. x *= np.exp(-(self._visiting_param - 1.0) * np.log(
  107. self._factor6 / factor4) / (3.0 - self._visiting_param))
  108. den = np.exp((self._visiting_param - 1.0) * np.log(np.fabs(y)) /
  109. (3.0 - self._visiting_param))
  110. return x / den
  111. class EnergyState:
  112. """
  113. Class used to record the energy state. At any time, it knows what is the
  114. currently used coordinates and the most recent best location.
  115. Parameters
  116. ----------
  117. lower : array_like
  118. A 1-D NumPy ndarray containing lower bounds for generating an initial
  119. random components in the `reset` method.
  120. upper : array_like
  121. A 1-D NumPy ndarray containing upper bounds for generating an initial
  122. random components in the `reset` method
  123. components. Neither NaN or inf are allowed.
  124. callback : callable, ``callback(x, f, context)``, optional
  125. A callback function which will be called for all minima found.
  126. ``x`` and ``f`` are the coordinates and function value of the
  127. latest minimum found, and `context` has value in [0, 1, 2]
  128. """
  129. # Maximum number of trials for generating a valid starting point
  130. MAX_REINIT_COUNT = 1000
  131. def __init__(self, lower, upper, callback=None):
  132. self.ebest = None
  133. self.current_energy = None
  134. self.current_location = None
  135. self.xbest = None
  136. self.lower = lower
  137. self.upper = upper
  138. self.callback = callback
  139. def reset(self, func_wrapper, rng_gen, x0=None):
  140. """
  141. Initialize current location is the search domain. If `x0` is not
  142. provided, a random location within the bounds is generated.
  143. """
  144. if x0 is None:
  145. self.current_location = rng_gen.uniform(self.lower, self.upper,
  146. size=len(self.lower))
  147. else:
  148. self.current_location = np.copy(x0)
  149. init_error = True
  150. reinit_counter = 0
  151. while init_error:
  152. self.current_energy = func_wrapper.fun(self.current_location)
  153. if self.current_energy is None:
  154. raise ValueError('Objective function is returning None')
  155. if not np.isfinite(self.current_energy):
  156. if reinit_counter >= EnergyState.MAX_REINIT_COUNT:
  157. init_error = False
  158. message = (
  159. 'Stopping algorithm because function '
  160. 'create NaN or (+/-) infinity values even with '
  161. 'trying new random parameters'
  162. )
  163. raise ValueError(message)
  164. self.current_location = rng_gen.uniform(self.lower,
  165. self.upper,
  166. size=self.lower.size)
  167. reinit_counter += 1
  168. else:
  169. init_error = False
  170. # If first time reset, initialize ebest and xbest
  171. if self.ebest is None and self.xbest is None:
  172. self.ebest = self.current_energy
  173. self.xbest = np.copy(self.current_location)
  174. # Otherwise, we keep them in case of reannealing reset
  175. def update_best(self, e, x, context):
  176. self.ebest = e
  177. self.xbest = np.copy(x)
  178. if self.callback is not None:
  179. val = self.callback(x, e, context)
  180. if val is not None:
  181. if val:
  182. return ('Callback function requested to stop early by '
  183. 'returning True')
  184. def update_current(self, e, x):
  185. self.current_energy = e
  186. self.current_location = np.copy(x)
  187. class StrategyChain:
  188. """
  189. Class that implements within a Markov chain the strategy for location
  190. acceptance and local search decision making.
  191. Parameters
  192. ----------
  193. acceptance_param : float
  194. Parameter for acceptance distribution. It is used to control the
  195. probability of acceptance. The lower the acceptance parameter, the
  196. smaller the probability of acceptance. Default value is -5.0 with
  197. a range (-1e4, -5].
  198. visit_dist : VisitingDistribution
  199. Instance of `VisitingDistribution` class.
  200. func_wrapper : ObjectiveFunWrapper
  201. Instance of `ObjectiveFunWrapper` class.
  202. minimizer_wrapper: LocalSearchWrapper
  203. Instance of `LocalSearchWrapper` class.
  204. rand_gen : {None, int, `numpy.random.Generator`,
  205. `numpy.random.RandomState`}, optional
  206. If `seed` is None (or `np.random`), the `numpy.random.RandomState`
  207. singleton is used.
  208. If `seed` is an int, a new ``RandomState`` instance is used,
  209. seeded with `seed`.
  210. If `seed` is already a ``Generator`` or ``RandomState`` instance then
  211. that instance is used.
  212. energy_state: EnergyState
  213. Instance of `EnergyState` class.
  214. """
  215. def __init__(self, acceptance_param, visit_dist, func_wrapper,
  216. minimizer_wrapper, rand_gen, energy_state):
  217. # Local strategy chain minimum energy and location
  218. self.emin = energy_state.current_energy
  219. self.xmin = np.array(energy_state.current_location)
  220. # Global optimizer state
  221. self.energy_state = energy_state
  222. # Acceptance parameter
  223. self.acceptance_param = acceptance_param
  224. # Visiting distribution instance
  225. self.visit_dist = visit_dist
  226. # Wrapper to objective function
  227. self.func_wrapper = func_wrapper
  228. # Wrapper to the local minimizer
  229. self.minimizer_wrapper = minimizer_wrapper
  230. self.not_improved_idx = 0
  231. self.not_improved_max_idx = 1000
  232. self._rand_gen = rand_gen
  233. self.temperature_step = 0
  234. self.K = 100 * len(energy_state.current_location)
  235. def accept_reject(self, j, e, x_visit):
  236. r = self._rand_gen.uniform()
  237. pqv_temp = 1.0 - ((1.0 - self.acceptance_param) *
  238. (e - self.energy_state.current_energy) / self.temperature_step)
  239. if pqv_temp <= 0.:
  240. pqv = 0.
  241. else:
  242. pqv = np.exp(np.log(pqv_temp) / (
  243. 1. - self.acceptance_param))
  244. if r <= pqv:
  245. # We accept the new location and update state
  246. self.energy_state.update_current(e, x_visit)
  247. self.xmin = np.copy(self.energy_state.current_location)
  248. # No improvement for a long time
  249. if self.not_improved_idx >= self.not_improved_max_idx:
  250. if j == 0 or self.energy_state.current_energy < self.emin:
  251. self.emin = self.energy_state.current_energy
  252. self.xmin = np.copy(self.energy_state.current_location)
  253. def run(self, step, temperature):
  254. self.temperature_step = temperature / float(step + 1)
  255. self.not_improved_idx += 1
  256. for j in range(self.energy_state.current_location.size * 2):
  257. if j == 0:
  258. if step == 0:
  259. self.energy_state_improved = True
  260. else:
  261. self.energy_state_improved = False
  262. x_visit = self.visit_dist.visiting(
  263. self.energy_state.current_location, j, temperature)
  264. # Calling the objective function
  265. e = self.func_wrapper.fun(x_visit)
  266. if e < self.energy_state.current_energy:
  267. # We have got a better energy value
  268. self.energy_state.update_current(e, x_visit)
  269. if e < self.energy_state.ebest:
  270. val = self.energy_state.update_best(e, x_visit, 0)
  271. if val is not None:
  272. if val:
  273. return val
  274. self.energy_state_improved = True
  275. self.not_improved_idx = 0
  276. else:
  277. # We have not improved but do we accept the new location?
  278. self.accept_reject(j, e, x_visit)
  279. if self.func_wrapper.nfev >= self.func_wrapper.maxfun:
  280. return ('Maximum number of function call reached '
  281. 'during annealing')
  282. # End of StrategyChain loop
  283. def local_search(self):
  284. # Decision making for performing a local search
  285. # based on strategy chain results
  286. # If energy has been improved or no improvement since too long,
  287. # performing a local search with the best strategy chain location
  288. if self.energy_state_improved:
  289. # Global energy has improved, let's see if LS improves further
  290. e, x = self.minimizer_wrapper.local_search(self.energy_state.xbest,
  291. self.energy_state.ebest)
  292. if e < self.energy_state.ebest:
  293. self.not_improved_idx = 0
  294. val = self.energy_state.update_best(e, x, 1)
  295. if val is not None:
  296. if val:
  297. return val
  298. self.energy_state.update_current(e, x)
  299. if self.func_wrapper.nfev >= self.func_wrapper.maxfun:
  300. return ('Maximum number of function call reached '
  301. 'during local search')
  302. # Check probability of a need to perform a LS even if no improvement
  303. do_ls = False
  304. if self.K < 90 * len(self.energy_state.current_location):
  305. pls = np.exp(self.K * (
  306. self.energy_state.ebest - self.energy_state.current_energy) /
  307. self.temperature_step)
  308. if pls >= self._rand_gen.uniform():
  309. do_ls = True
  310. # Global energy not improved, let's see what LS gives
  311. # on the best strategy chain location
  312. if self.not_improved_idx >= self.not_improved_max_idx:
  313. do_ls = True
  314. if do_ls:
  315. e, x = self.minimizer_wrapper.local_search(self.xmin, self.emin)
  316. self.xmin = np.copy(x)
  317. self.emin = e
  318. self.not_improved_idx = 0
  319. self.not_improved_max_idx = self.energy_state.current_location.size
  320. if e < self.energy_state.ebest:
  321. val = self.energy_state.update_best(
  322. self.emin, self.xmin, 2)
  323. if val is not None:
  324. if val:
  325. return val
  326. self.energy_state.update_current(e, x)
  327. if self.func_wrapper.nfev >= self.func_wrapper.maxfun:
  328. return ('Maximum number of function call reached '
  329. 'during dual annealing')
  330. class ObjectiveFunWrapper:
  331. def __init__(self, func, maxfun=1e7, *args):
  332. self.func = func
  333. self.args = args
  334. # Number of objective function evaluations
  335. self.nfev = 0
  336. # Number of gradient function evaluation if used
  337. self.ngev = 0
  338. # Number of hessian of the objective function if used
  339. self.nhev = 0
  340. self.maxfun = maxfun
  341. def fun(self, x):
  342. self.nfev += 1
  343. return self.func(x, *self.args)
  344. class LocalSearchWrapper:
  345. """
  346. Class used to wrap around the minimizer used for local search
  347. Default local minimizer is SciPy minimizer L-BFGS-B
  348. """
  349. LS_MAXITER_RATIO = 6
  350. LS_MAXITER_MIN = 100
  351. LS_MAXITER_MAX = 1000
  352. def __init__(self, search_bounds, func_wrapper, *args, **kwargs):
  353. self.func_wrapper = func_wrapper
  354. self.kwargs = kwargs
  355. self.jac = self.kwargs.get('jac', None)
  356. self.hess = self.kwargs.get('hess', None)
  357. self.hessp = self.kwargs.get('hessp', None)
  358. self.kwargs.pop("args", None)
  359. self.minimizer = minimize
  360. bounds_list = list(zip(*search_bounds))
  361. self.lower = np.array(bounds_list[0])
  362. self.upper = np.array(bounds_list[1])
  363. # If no minimizer specified, use SciPy minimize with 'L-BFGS-B' method
  364. if not self.kwargs:
  365. n = len(self.lower)
  366. ls_max_iter = min(max(n * self.LS_MAXITER_RATIO,
  367. self.LS_MAXITER_MIN),
  368. self.LS_MAXITER_MAX)
  369. self.kwargs['method'] = 'L-BFGS-B'
  370. self.kwargs['options'] = {
  371. 'maxiter': ls_max_iter,
  372. }
  373. self.kwargs['bounds'] = list(zip(self.lower, self.upper))
  374. else:
  375. if callable(self.jac):
  376. def wrapped_jac(x):
  377. return self.jac(x, *args)
  378. self.kwargs['jac'] = wrapped_jac
  379. if callable(self.hess):
  380. def wrapped_hess(x):
  381. return self.hess(x, *args)
  382. self.kwargs['hess'] = wrapped_hess
  383. if callable(self.hessp):
  384. def wrapped_hessp(x, p):
  385. return self.hessp(x, p, *args)
  386. self.kwargs['hessp'] = wrapped_hessp
  387. def local_search(self, x, e):
  388. # Run local search from the given x location where energy value is e
  389. x_tmp = np.copy(x)
  390. mres = self.minimizer(self.func_wrapper.fun, x, **self.kwargs)
  391. if 'njev' in mres:
  392. self.func_wrapper.ngev += mres.njev
  393. if 'nhev' in mres:
  394. self.func_wrapper.nhev += mres.nhev
  395. # Check if is valid value
  396. is_finite = np.all(np.isfinite(mres.x)) and np.isfinite(mres.fun)
  397. in_bounds = np.all(mres.x >= self.lower) and np.all(
  398. mres.x <= self.upper)
  399. is_valid = is_finite and in_bounds
  400. # Use the new point only if it is valid and return a better results
  401. if is_valid and mres.fun < e:
  402. return mres.fun, mres.x
  403. else:
  404. return e, x_tmp
  405. @_transition_to_rng("seed", position_num=10)
  406. def dual_annealing(func, bounds, args=(), maxiter=1000,
  407. minimizer_kwargs=None, initial_temp=5230.,
  408. restart_temp_ratio=2.e-5, visit=2.62, accept=-5.0,
  409. maxfun=1e7, rng=None, no_local_search=False,
  410. callback=None, x0=None):
  411. """
  412. Find the global minimum of a function using Dual Annealing.
  413. Parameters
  414. ----------
  415. func : callable
  416. The objective function to be minimized. Must be in the form
  417. ``f(x, *args)``, where ``x`` is the argument in the form of a 1-D array
  418. and ``args`` is a tuple of any additional fixed parameters needed to
  419. completely specify the function.
  420. bounds : sequence or `Bounds`
  421. Bounds for variables. There are two ways to specify the bounds:
  422. 1. Instance of `Bounds` class.
  423. 2. Sequence of ``(min, max)`` pairs for each element in `x`.
  424. args : tuple, optional
  425. Any additional fixed parameters needed to completely specify the
  426. objective function.
  427. maxiter : int, optional
  428. The maximum number of global search iterations. Default value is 1000.
  429. minimizer_kwargs : dict, optional
  430. Keyword arguments to be passed to the local minimizer
  431. (`minimize`). An important option could be ``method`` for the minimizer
  432. method to use.
  433. If no keyword arguments are provided, the local minimizer defaults to
  434. 'L-BFGS-B' and uses the already supplied bounds. If `minimizer_kwargs`
  435. is specified, then the dict must contain all parameters required to
  436. control the local minimization. `args` is ignored in this dict, as it is
  437. passed automatically. `bounds` is not automatically passed on to the
  438. local minimizer as the method may not support them.
  439. initial_temp : float, optional
  440. The initial temperature, use higher values to facilitates a wider
  441. search of the energy landscape, allowing dual_annealing to escape
  442. local minima that it is trapped in. Default value is 5230. Range is
  443. (0.01, 5.e4].
  444. restart_temp_ratio : float, optional
  445. During the annealing process, temperature is decreasing, when it
  446. reaches ``initial_temp * restart_temp_ratio``, the reannealing process
  447. is triggered. Default value of the ratio is 2e-5. Range is (0, 1).
  448. visit : float, optional
  449. Parameter for visiting distribution. Default value is 2.62. Higher
  450. values give the visiting distribution a heavier tail, this makes
  451. the algorithm jump to a more distant region. The value range is (1, 3].
  452. accept : float, optional
  453. Parameter for acceptance distribution. It is used to control the
  454. probability of acceptance. The lower the acceptance parameter, the
  455. smaller the probability of acceptance. Default value is -5.0 with
  456. a range (-1e4, -5].
  457. maxfun : int, optional
  458. Soft limit for the number of objective function calls. If the
  459. algorithm is in the middle of a local search, this number will be
  460. exceeded, the algorithm will stop just after the local search is
  461. done. Default value is 1e7.
  462. rng : `numpy.random.Generator`, optional
  463. Pseudorandom number generator state. When `rng` is None, a new
  464. `numpy.random.Generator` is created using entropy from the
  465. operating system. Types other than `numpy.random.Generator` are
  466. passed to `numpy.random.default_rng` to instantiate a `Generator`.
  467. Specify `rng` for repeatable minimizations. The random numbers
  468. generated only affect the visiting distribution function
  469. and new coordinates generation.
  470. no_local_search : bool, optional
  471. If `no_local_search` is set to True, a traditional Generalized
  472. Simulated Annealing will be performed with no local search
  473. strategy applied.
  474. callback : callable, optional
  475. A callback function with signature ``callback(x, f, context)``,
  476. which will be called for all minima found.
  477. ``x`` and ``f`` are the coordinates and function value of the
  478. latest minimum found, and ``context`` has one of the following
  479. values:
  480. - ``0``: minimum detected in the annealing process.
  481. - ``1``: detection occurred in the local search process.
  482. - ``2``: detection done in the dual annealing process.
  483. If the callback implementation returns True, the algorithm will stop.
  484. x0 : ndarray, shape(n,), optional
  485. Coordinates of a single N-D starting point.
  486. Returns
  487. -------
  488. res : OptimizeResult
  489. The optimization result represented as a `OptimizeResult` object.
  490. Important attributes are: ``x`` the solution array, ``fun`` the value
  491. of the function at the solution, and ``message`` which describes the
  492. cause of the termination.
  493. See `OptimizeResult` for a description of other attributes.
  494. Notes
  495. -----
  496. This function implements the Dual Annealing optimization. This stochastic
  497. approach derived from [3]_ combines the generalization of CSA (Classical
  498. Simulated Annealing) and FSA (Fast Simulated Annealing) [1]_ [2]_ coupled
  499. to a strategy for applying a local search on accepted locations [4]_.
  500. An alternative implementation of this same algorithm is described in [5]_
  501. and benchmarks are presented in [6]_. This approach introduces an advanced
  502. method to refine the solution found by the generalized annealing
  503. process. This algorithm uses a distorted Cauchy-Lorentz visiting
  504. distribution, with its shape controlled by the parameter :math:`q_{v}`
  505. .. math::
  506. g_{q_{v}}(\\Delta x(t)) \\propto \\frac{ \\
  507. \\left[T_{q_{v}}(t) \\right]^{-\\frac{D}{3-q_{v}}}}{ \\
  508. \\left[{1+(q_{v}-1)\\frac{(\\Delta x(t))^{2}} { \\
  509. \\left[T_{q_{v}}(t)\\right]^{\\frac{2}{3-q_{v}}}}}\\right]^{ \\
  510. \\frac{1}{q_{v}-1}+\\frac{D-1}{2}}}
  511. Where :math:`t` is the artificial time. This visiting distribution is used
  512. to generate a trial jump distance :math:`\\Delta x(t)` of variable
  513. :math:`x(t)` under artificial temperature :math:`T_{q_{v}}(t)`.
  514. From the starting point, after calling the visiting distribution
  515. function, the acceptance probability is computed as follows:
  516. .. math::
  517. p_{q_{a}} = \\min{\\{1,\\left[1-(1-q_{a}) \\beta \\Delta E \\right]^{ \\
  518. \\frac{1}{1-q_{a}}}\\}}
  519. Where :math:`q_{a}` is a acceptance parameter. For :math:`q_{a}<1`, zero
  520. acceptance probability is assigned to the cases where
  521. .. math::
  522. [1-(1-q_{a}) \\beta \\Delta E] < 0
  523. The artificial temperature :math:`T_{q_{v}}(t)` is decreased according to
  524. .. math::
  525. T_{q_{v}}(t) = T_{q_{v}}(1) \\frac{2^{q_{v}-1}-1}{\\left( \\
  526. 1 + t\\right)^{q_{v}-1}-1}
  527. Where :math:`q_{v}` is the visiting parameter.
  528. .. versionadded:: 1.2.0
  529. References
  530. ----------
  531. .. [1] Tsallis C. Possible generalization of Boltzmann-Gibbs
  532. statistics. Journal of Statistical Physics, 52, 479-487 (1988).
  533. .. [2] Tsallis C, Stariolo DA. Generalized Simulated Annealing.
  534. Physica A, 233, 395-406 (1996).
  535. .. [3] Xiang Y, Sun DY, Fan W, Gong XG. Generalized Simulated
  536. Annealing Algorithm and Its Application to the Thomson Model.
  537. Physics Letters A, 233, 216-220 (1997).
  538. .. [4] Xiang Y, Gong XG. Efficiency of Generalized Simulated
  539. Annealing. Physical Review E, 62, 4473 (2000).
  540. .. [5] Xiang Y, Gubian S, Suomela B, Hoeng J. Generalized
  541. Simulated Annealing for Efficient Global Optimization: the GenSA
  542. Package for R. The R Journal, Volume 5/1 (2013).
  543. .. [6] Mullen, K. Continuous Global Optimization in R. Journal of
  544. Statistical Software, 60(6), 1 - 45, (2014).
  545. :doi:`10.18637/jss.v060.i06`
  546. Examples
  547. --------
  548. The following example is a 10-D problem, with many local minima.
  549. The function involved is called Rastrigin
  550. (https://en.wikipedia.org/wiki/Rastrigin_function)
  551. >>> import numpy as np
  552. >>> from scipy.optimize import dual_annealing
  553. >>> func = lambda x: np.sum(x*x - 10*np.cos(2*np.pi*x)) + 10*np.size(x)
  554. >>> lw = [-5.12] * 10
  555. >>> up = [5.12] * 10
  556. >>> ret = dual_annealing(func, bounds=list(zip(lw, up)))
  557. >>> ret.x
  558. array([-4.26437714e-09, -3.91699361e-09, -1.86149218e-09, -3.97165720e-09,
  559. -6.29151648e-09, -6.53145322e-09, -3.93616815e-09, -6.55623025e-09,
  560. -6.05775280e-09, -5.00668935e-09]) # random
  561. >>> ret.fun
  562. 0.000000
  563. """
  564. if isinstance(bounds, Bounds):
  565. bounds = new_bounds_to_old(bounds.lb, bounds.ub, len(bounds.lb))
  566. if x0 is not None and not len(x0) == len(bounds):
  567. raise ValueError('Bounds size does not match x0')
  568. lu = list(zip(*bounds))
  569. lower = np.array(lu[0])
  570. upper = np.array(lu[1])
  571. # Check that restart temperature ratio is correct
  572. if restart_temp_ratio <= 0. or restart_temp_ratio >= 1.:
  573. raise ValueError('Restart temperature ratio has to be in range (0, 1)')
  574. # Checking bounds are valid
  575. if (np.any(np.isinf(lower)) or np.any(np.isinf(upper)) or np.any(
  576. np.isnan(lower)) or np.any(np.isnan(upper))):
  577. raise ValueError('Some bounds values are inf values or nan values')
  578. # Checking that bounds are consistent
  579. if not np.all(lower < upper):
  580. raise ValueError('Bounds are not consistent min < max')
  581. # Checking that bounds are the same length
  582. if not len(lower) == len(upper):
  583. raise ValueError('Bounds do not have the same dimensions')
  584. # Wrapper for the objective function
  585. func_wrapper = ObjectiveFunWrapper(func, maxfun, *args)
  586. # minimizer_kwargs has to be a dict, not None
  587. minimizer_kwargs = minimizer_kwargs or {}
  588. minimizer_wrapper = LocalSearchWrapper(
  589. bounds, func_wrapper, *args, **minimizer_kwargs)
  590. # Initialization of random Generator for reproducible runs if rng provided
  591. rng_gen = check_random_state(rng)
  592. # Initialization of the energy state
  593. energy_state = EnergyState(lower, upper, callback)
  594. energy_state.reset(func_wrapper, rng_gen, x0)
  595. # Minimum value of annealing temperature reached to perform
  596. # re-annealing
  597. temperature_restart = initial_temp * restart_temp_ratio
  598. # VisitingDistribution instance
  599. visit_dist = VisitingDistribution(lower, upper, visit, rng_gen)
  600. # Strategy chain instance
  601. strategy_chain = StrategyChain(accept, visit_dist, func_wrapper,
  602. minimizer_wrapper, rng_gen, energy_state)
  603. need_to_stop = False
  604. iteration = 0
  605. message = []
  606. # OptimizeResult object to be returned
  607. optimize_res = OptimizeResult()
  608. optimize_res.success = True
  609. optimize_res.status = 0
  610. t1 = np.exp((visit - 1) * np.log(2.0)) - 1.0
  611. # Run the search loop
  612. while not need_to_stop:
  613. for i in range(maxiter):
  614. # Compute temperature for this step
  615. s = float(i) + 2.0
  616. t2 = np.exp((visit - 1) * np.log(s)) - 1.0
  617. temperature = initial_temp * t1 / t2
  618. if iteration >= maxiter:
  619. message.append("Maximum number of iteration reached")
  620. need_to_stop = True
  621. break
  622. # Need a re-annealing process?
  623. if temperature < temperature_restart:
  624. energy_state.reset(func_wrapper, rng_gen)
  625. break
  626. # starting strategy chain
  627. val = strategy_chain.run(i, temperature)
  628. if val is not None:
  629. message.append(val)
  630. need_to_stop = True
  631. optimize_res.success = False
  632. break
  633. # Possible local search at the end of the strategy chain
  634. if not no_local_search:
  635. val = strategy_chain.local_search()
  636. if val is not None:
  637. message.append(val)
  638. need_to_stop = True
  639. optimize_res.success = False
  640. break
  641. iteration += 1
  642. # Setting the OptimizeResult values
  643. optimize_res.x = energy_state.xbest
  644. optimize_res.fun = energy_state.ebest
  645. optimize_res.nit = iteration
  646. optimize_res.nfev = func_wrapper.nfev
  647. optimize_res.njev = func_wrapper.ngev
  648. optimize_res.nhev = func_wrapper.nhev
  649. optimize_res.message = message
  650. return optimize_res