_dual_annealing.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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
  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. rand_gen : {`~numpy.random.RandomState`, `~numpy.random.Generator`}
  36. A `~numpy.random.RandomState`, `~numpy.random.Generator` object
  37. for using the current state of the created random generator container.
  38. """
  39. TAIL_LIMIT = 1.e8
  40. MIN_VISIT_BOUND = 1.e-10
  41. def __init__(self, lb, ub, visiting_param, rand_gen):
  42. # if you wish to make _visiting_param adjustable during the life of
  43. # the object then _factor2, _factor3, _factor5, _d1, _factor6 will
  44. # have to be dynamically calculated in `visit_fn`. They're factored
  45. # out here so they don't need to be recalculated all the time.
  46. self._visiting_param = visiting_param
  47. self.rand_gen = rand_gen
  48. self.lower = lb
  49. self.upper = ub
  50. self.bound_range = ub - lb
  51. # these are invariant numbers unless visiting_param changes
  52. self._factor2 = np.exp((4.0 - self._visiting_param) * np.log(
  53. self._visiting_param - 1.0))
  54. self._factor3 = np.exp((2.0 - self._visiting_param) * np.log(2.0)
  55. / (self._visiting_param - 1.0))
  56. self._factor4_p = np.sqrt(np.pi) * self._factor2 / (self._factor3 * (
  57. 3.0 - self._visiting_param))
  58. self._factor5 = 1.0 / (self._visiting_param - 1.0) - 0.5
  59. self._d1 = 2.0 - self._factor5
  60. self._factor6 = np.pi * (1.0 - self._factor5) / np.sin(
  61. np.pi * (1.0 - self._factor5)) / np.exp(gammaln(self._d1))
  62. def visiting(self, x, step, temperature):
  63. """ Based on the step in the strategy chain, new coordinates are
  64. generated by changing all components is the same time or only
  65. one of them, the new values are computed with visit_fn method
  66. """
  67. dim = x.size
  68. if step < dim:
  69. # Changing all coordinates with a new visiting value
  70. visits = self.visit_fn(temperature, dim)
  71. upper_sample, lower_sample = self.rand_gen.uniform(size=2)
  72. visits[visits > self.TAIL_LIMIT] = self.TAIL_LIMIT * upper_sample
  73. visits[visits < -self.TAIL_LIMIT] = -self.TAIL_LIMIT * lower_sample
  74. x_visit = visits + x
  75. a = x_visit - self.lower
  76. b = np.fmod(a, self.bound_range) + self.bound_range
  77. x_visit = np.fmod(b, self.bound_range) + self.lower
  78. x_visit[np.fabs(
  79. x_visit - self.lower) < self.MIN_VISIT_BOUND] += 1.e-10
  80. else:
  81. # Changing only one coordinate at a time based on strategy
  82. # chain step
  83. x_visit = np.copy(x)
  84. visit = self.visit_fn(temperature, 1)[0]
  85. if visit > self.TAIL_LIMIT:
  86. visit = self.TAIL_LIMIT * self.rand_gen.uniform()
  87. elif visit < -self.TAIL_LIMIT:
  88. visit = -self.TAIL_LIMIT * self.rand_gen.uniform()
  89. index = step - dim
  90. x_visit[index] = visit + x[index]
  91. a = x_visit[index] - self.lower[index]
  92. b = np.fmod(a, self.bound_range[index]) + self.bound_range[index]
  93. x_visit[index] = np.fmod(b, self.bound_range[
  94. index]) + self.lower[index]
  95. if np.fabs(x_visit[index] - self.lower[
  96. index]) < self.MIN_VISIT_BOUND:
  97. x_visit[index] += self.MIN_VISIT_BOUND
  98. return x_visit
  99. def visit_fn(self, temperature, dim):
  100. """ Formula Visita from p. 405 of reference [2] """
  101. x, y = self.rand_gen.normal(size=(dim, 2)).T
  102. factor1 = np.exp(np.log(temperature) / (self._visiting_param - 1.0))
  103. factor4 = self._factor4_p * factor1
  104. # sigmax
  105. x *= np.exp(-(self._visiting_param - 1.0) * np.log(
  106. self._factor6 / factor4) / (3.0 - self._visiting_param))
  107. den = np.exp((self._visiting_param - 1.0) * np.log(np.fabs(y)) /
  108. (3.0 - self._visiting_param))
  109. return x / den
  110. class EnergyState:
  111. """
  112. Class used to record the energy state. At any time, it knows what is the
  113. currently used coordinates and the most recent best location.
  114. Parameters
  115. ----------
  116. lower : array_like
  117. A 1-D NumPy ndarray containing lower bounds for generating an initial
  118. random components in the `reset` method.
  119. upper : array_like
  120. A 1-D NumPy ndarray containing upper bounds for generating an initial
  121. random components in the `reset` method
  122. components. Neither NaN or inf are allowed.
  123. callback : callable, ``callback(x, f, context)``, optional
  124. A callback function which will be called for all minima found.
  125. ``x`` and ``f`` are the coordinates and function value of the
  126. latest minimum found, and `context` has value in [0, 1, 2]
  127. """
  128. # Maximum number of trials for generating a valid starting point
  129. MAX_REINIT_COUNT = 1000
  130. def __init__(self, lower, upper, callback=None):
  131. self.ebest = None
  132. self.current_energy = None
  133. self.current_location = None
  134. self.xbest = None
  135. self.lower = lower
  136. self.upper = upper
  137. self.callback = callback
  138. def reset(self, func_wrapper, rand_gen, x0=None):
  139. """
  140. Initialize current location is the search domain. If `x0` is not
  141. provided, a random location within the bounds is generated.
  142. """
  143. if x0 is None:
  144. self.current_location = rand_gen.uniform(self.lower, self.upper,
  145. size=len(self.lower))
  146. else:
  147. self.current_location = np.copy(x0)
  148. init_error = True
  149. reinit_counter = 0
  150. while init_error:
  151. self.current_energy = func_wrapper.fun(self.current_location)
  152. if self.current_energy is None:
  153. raise ValueError('Objective function is returning None')
  154. if (not np.isfinite(self.current_energy) or np.isnan(
  155. 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 = rand_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, **kwargs):
  353. self.func_wrapper = func_wrapper
  354. self.kwargs = kwargs
  355. self.minimizer = minimize
  356. bounds_list = list(zip(*search_bounds))
  357. self.lower = np.array(bounds_list[0])
  358. self.upper = np.array(bounds_list[1])
  359. # If no minimizer specified, use SciPy minimize with 'L-BFGS-B' method
  360. if not self.kwargs:
  361. n = len(self.lower)
  362. ls_max_iter = min(max(n * self.LS_MAXITER_RATIO,
  363. self.LS_MAXITER_MIN),
  364. self.LS_MAXITER_MAX)
  365. self.kwargs['method'] = 'L-BFGS-B'
  366. self.kwargs['options'] = {
  367. 'maxiter': ls_max_iter,
  368. }
  369. self.kwargs['bounds'] = list(zip(self.lower, self.upper))
  370. def local_search(self, x, e):
  371. # Run local search from the given x location where energy value is e
  372. x_tmp = np.copy(x)
  373. mres = self.minimizer(self.func_wrapper.fun, x, **self.kwargs)
  374. if 'njev' in mres:
  375. self.func_wrapper.ngev += mres.njev
  376. if 'nhev' in mres:
  377. self.func_wrapper.nhev += mres.nhev
  378. # Check if is valid value
  379. is_finite = np.all(np.isfinite(mres.x)) and np.isfinite(mres.fun)
  380. in_bounds = np.all(mres.x >= self.lower) and np.all(
  381. mres.x <= self.upper)
  382. is_valid = is_finite and in_bounds
  383. # Use the new point only if it is valid and return a better results
  384. if is_valid and mres.fun < e:
  385. return mres.fun, mres.x
  386. else:
  387. return e, x_tmp
  388. def dual_annealing(func, bounds, args=(), maxiter=1000,
  389. minimizer_kwargs=None, initial_temp=5230.,
  390. restart_temp_ratio=2.e-5, visit=2.62, accept=-5.0,
  391. maxfun=1e7, seed=None, no_local_search=False,
  392. callback=None, x0=None):
  393. """
  394. Find the global minimum of a function using Dual Annealing.
  395. Parameters
  396. ----------
  397. func : callable
  398. The objective function to be minimized. Must be in the form
  399. ``f(x, *args)``, where ``x`` is the argument in the form of a 1-D array
  400. and ``args`` is a tuple of any additional fixed parameters needed to
  401. completely specify the function.
  402. bounds : sequence or `Bounds`
  403. Bounds for variables. There are two ways to specify the bounds:
  404. 1. Instance of `Bounds` class.
  405. 2. Sequence of ``(min, max)`` pairs for each element in `x`.
  406. args : tuple, optional
  407. Any additional fixed parameters needed to completely specify the
  408. objective function.
  409. maxiter : int, optional
  410. The maximum number of global search iterations. Default value is 1000.
  411. minimizer_kwargs : dict, optional
  412. Extra keyword arguments to be passed to the local minimizer
  413. (`minimize`). Some important options could be:
  414. ``method`` for the minimizer method to use and ``args`` for
  415. objective function additional arguments.
  416. initial_temp : float, optional
  417. The initial temperature, use higher values to facilitates a wider
  418. search of the energy landscape, allowing dual_annealing to escape
  419. local minima that it is trapped in. Default value is 5230. Range is
  420. (0.01, 5.e4].
  421. restart_temp_ratio : float, optional
  422. During the annealing process, temperature is decreasing, when it
  423. reaches ``initial_temp * restart_temp_ratio``, the reannealing process
  424. is triggered. Default value of the ratio is 2e-5. Range is (0, 1).
  425. visit : float, optional
  426. Parameter for visiting distribution. Default value is 2.62. Higher
  427. values give the visiting distribution a heavier tail, this makes
  428. the algorithm jump to a more distant region. The value range is (1, 3].
  429. accept : float, optional
  430. Parameter for acceptance distribution. It is used to control the
  431. probability of acceptance. The lower the acceptance parameter, the
  432. smaller the probability of acceptance. Default value is -5.0 with
  433. a range (-1e4, -5].
  434. maxfun : int, optional
  435. Soft limit for the number of objective function calls. If the
  436. algorithm is in the middle of a local search, this number will be
  437. exceeded, the algorithm will stop just after the local search is
  438. done. Default value is 1e7.
  439. seed : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
  440. If `seed` is None (or `np.random`), the `numpy.random.RandomState`
  441. singleton is used.
  442. If `seed` is an int, a new ``RandomState`` instance is used,
  443. seeded with `seed`.
  444. If `seed` is already a ``Generator`` or ``RandomState`` instance then
  445. that instance is used.
  446. Specify `seed` for repeatable minimizations. The random numbers
  447. generated with this seed only affect the visiting distribution function
  448. and new coordinates generation.
  449. no_local_search : bool, optional
  450. If `no_local_search` is set to True, a traditional Generalized
  451. Simulated Annealing will be performed with no local search
  452. strategy applied.
  453. callback : callable, optional
  454. A callback function with signature ``callback(x, f, context)``,
  455. which will be called for all minima found.
  456. ``x`` and ``f`` are the coordinates and function value of the
  457. latest minimum found, and ``context`` has value in [0, 1, 2], with the
  458. following meaning:
  459. - 0: minimum detected in the annealing process.
  460. - 1: detection occurred in the local search process.
  461. - 2: detection done in the dual annealing process.
  462. If the callback implementation returns True, the algorithm will stop.
  463. x0 : ndarray, shape(n,), optional
  464. Coordinates of a single N-D starting point.
  465. Returns
  466. -------
  467. res : OptimizeResult
  468. The optimization result represented as a `OptimizeResult` object.
  469. Important attributes are: ``x`` the solution array, ``fun`` the value
  470. of the function at the solution, and ``message`` which describes the
  471. cause of the termination.
  472. See `OptimizeResult` for a description of other attributes.
  473. Notes
  474. -----
  475. This function implements the Dual Annealing optimization. This stochastic
  476. approach derived from [3]_ combines the generalization of CSA (Classical
  477. Simulated Annealing) and FSA (Fast Simulated Annealing) [1]_ [2]_ coupled
  478. to a strategy for applying a local search on accepted locations [4]_.
  479. An alternative implementation of this same algorithm is described in [5]_
  480. and benchmarks are presented in [6]_. This approach introduces an advanced
  481. method to refine the solution found by the generalized annealing
  482. process. This algorithm uses a distorted Cauchy-Lorentz visiting
  483. distribution, with its shape controlled by the parameter :math:`q_{v}`
  484. .. math::
  485. g_{q_{v}}(\\Delta x(t)) \\propto \\frac{ \\
  486. \\left[T_{q_{v}}(t) \\right]^{-\\frac{D}{3-q_{v}}}}{ \\
  487. \\left[{1+(q_{v}-1)\\frac{(\\Delta x(t))^{2}} { \\
  488. \\left[T_{q_{v}}(t)\\right]^{\\frac{2}{3-q_{v}}}}}\\right]^{ \\
  489. \\frac{1}{q_{v}-1}+\\frac{D-1}{2}}}
  490. Where :math:`t` is the artificial time. This visiting distribution is used
  491. to generate a trial jump distance :math:`\\Delta x(t)` of variable
  492. :math:`x(t)` under artificial temperature :math:`T_{q_{v}}(t)`.
  493. From the starting point, after calling the visiting distribution
  494. function, the acceptance probability is computed as follows:
  495. .. math::
  496. p_{q_{a}} = \\min{\\{1,\\left[1-(1-q_{a}) \\beta \\Delta E \\right]^{ \\
  497. \\frac{1}{1-q_{a}}}\\}}
  498. Where :math:`q_{a}` is a acceptance parameter. For :math:`q_{a}<1`, zero
  499. acceptance probability is assigned to the cases where
  500. .. math::
  501. [1-(1-q_{a}) \\beta \\Delta E] < 0
  502. The artificial temperature :math:`T_{q_{v}}(t)` is decreased according to
  503. .. math::
  504. T_{q_{v}}(t) = T_{q_{v}}(1) \\frac{2^{q_{v}-1}-1}{\\left( \\
  505. 1 + t\\right)^{q_{v}-1}-1}
  506. Where :math:`q_{v}` is the visiting parameter.
  507. .. versionadded:: 1.2.0
  508. References
  509. ----------
  510. .. [1] Tsallis C. Possible generalization of Boltzmann-Gibbs
  511. statistics. Journal of Statistical Physics, 52, 479-487 (1998).
  512. .. [2] Tsallis C, Stariolo DA. Generalized Simulated Annealing.
  513. Physica A, 233, 395-406 (1996).
  514. .. [3] Xiang Y, Sun DY, Fan W, Gong XG. Generalized Simulated
  515. Annealing Algorithm and Its Application to the Thomson Model.
  516. Physics Letters A, 233, 216-220 (1997).
  517. .. [4] Xiang Y, Gong XG. Efficiency of Generalized Simulated
  518. Annealing. Physical Review E, 62, 4473 (2000).
  519. .. [5] Xiang Y, Gubian S, Suomela B, Hoeng J. Generalized
  520. Simulated Annealing for Efficient Global Optimization: the GenSA
  521. Package for R. The R Journal, Volume 5/1 (2013).
  522. .. [6] Mullen, K. Continuous Global Optimization in R. Journal of
  523. Statistical Software, 60(6), 1 - 45, (2014).
  524. :doi:`10.18637/jss.v060.i06`
  525. Examples
  526. --------
  527. The following example is a 10-D problem, with many local minima.
  528. The function involved is called Rastrigin
  529. (https://en.wikipedia.org/wiki/Rastrigin_function)
  530. >>> import numpy as np
  531. >>> from scipy.optimize import dual_annealing
  532. >>> func = lambda x: np.sum(x*x - 10*np.cos(2*np.pi*x)) + 10*np.size(x)
  533. >>> lw = [-5.12] * 10
  534. >>> up = [5.12] * 10
  535. >>> ret = dual_annealing(func, bounds=list(zip(lw, up)))
  536. >>> ret.x
  537. array([-4.26437714e-09, -3.91699361e-09, -1.86149218e-09, -3.97165720e-09,
  538. -6.29151648e-09, -6.53145322e-09, -3.93616815e-09, -6.55623025e-09,
  539. -6.05775280e-09, -5.00668935e-09]) # random
  540. >>> ret.fun
  541. 0.000000
  542. """
  543. if isinstance(bounds, Bounds):
  544. bounds = new_bounds_to_old(bounds.lb, bounds.ub, len(bounds.lb))
  545. # noqa: E501
  546. if x0 is not None and not len(x0) == len(bounds):
  547. raise ValueError('Bounds size does not match x0')
  548. lu = list(zip(*bounds))
  549. lower = np.array(lu[0])
  550. upper = np.array(lu[1])
  551. # Check that restart temperature ratio is correct
  552. if restart_temp_ratio <= 0. or restart_temp_ratio >= 1.:
  553. raise ValueError('Restart temperature ratio has to be in range (0, 1)')
  554. # Checking bounds are valid
  555. if (np.any(np.isinf(lower)) or np.any(np.isinf(upper)) or np.any(
  556. np.isnan(lower)) or np.any(np.isnan(upper))):
  557. raise ValueError('Some bounds values are inf values or nan values')
  558. # Checking that bounds are consistent
  559. if not np.all(lower < upper):
  560. raise ValueError('Bounds are not consistent min < max')
  561. # Checking that bounds are the same length
  562. if not len(lower) == len(upper):
  563. raise ValueError('Bounds do not have the same dimensions')
  564. # Wrapper for the objective function
  565. func_wrapper = ObjectiveFunWrapper(func, maxfun, *args)
  566. # minimizer_kwargs has to be a dict, not None
  567. minimizer_kwargs = minimizer_kwargs or {}
  568. minimizer_wrapper = LocalSearchWrapper(
  569. bounds, func_wrapper, **minimizer_kwargs)
  570. # Initialization of random Generator for reproducible runs if seed provided
  571. rand_state = check_random_state(seed)
  572. # Initialization of the energy state
  573. energy_state = EnergyState(lower, upper, callback)
  574. energy_state.reset(func_wrapper, rand_state, x0)
  575. # Minimum value of annealing temperature reached to perform
  576. # re-annealing
  577. temperature_restart = initial_temp * restart_temp_ratio
  578. # VisitingDistribution instance
  579. visit_dist = VisitingDistribution(lower, upper, visit, rand_state)
  580. # Strategy chain instance
  581. strategy_chain = StrategyChain(accept, visit_dist, func_wrapper,
  582. minimizer_wrapper, rand_state, energy_state)
  583. need_to_stop = False
  584. iteration = 0
  585. message = []
  586. # OptimizeResult object to be returned
  587. optimize_res = OptimizeResult()
  588. optimize_res.success = True
  589. optimize_res.status = 0
  590. t1 = np.exp((visit - 1) * np.log(2.0)) - 1.0
  591. # Run the search loop
  592. while not need_to_stop:
  593. for i in range(maxiter):
  594. # Compute temperature for this step
  595. s = float(i) + 2.0
  596. t2 = np.exp((visit - 1) * np.log(s)) - 1.0
  597. temperature = initial_temp * t1 / t2
  598. if iteration >= maxiter:
  599. message.append("Maximum number of iteration reached")
  600. need_to_stop = True
  601. break
  602. # Need a re-annealing process?
  603. if temperature < temperature_restart:
  604. energy_state.reset(func_wrapper, rand_state)
  605. break
  606. # starting strategy chain
  607. val = strategy_chain.run(i, temperature)
  608. if val is not None:
  609. message.append(val)
  610. need_to_stop = True
  611. optimize_res.success = False
  612. break
  613. # Possible local search at the end of the strategy chain
  614. if not no_local_search:
  615. val = strategy_chain.local_search()
  616. if val is not None:
  617. message.append(val)
  618. need_to_stop = True
  619. optimize_res.success = False
  620. break
  621. iteration += 1
  622. # Setting the OptimizeResult values
  623. optimize_res.x = energy_state.xbest
  624. optimize_res.fun = energy_state.ebest
  625. optimize_res.nit = iteration
  626. optimize_res.nfev = func_wrapper.nfev
  627. optimize_res.njev = func_wrapper.ngev
  628. optimize_res.nhev = func_wrapper.nhev
  629. optimize_res.message = message
  630. return optimize_res