8
8
from scipy .optimize import OptimizeResult , minimize
9
9
from scipy .optimize ._optimize import _status_message , _wrap_callback
10
10
from scipy ._lib ._util import (check_random_state , MapWrapper , _FunctionWrapper ,
11
- rng_integers )
11
+ rng_integers , _transition_to_rng )
12
12
13
13
from scipy .optimize ._constraints import (Bounds , new_bounds_to_old ,
14
14
NonlinearConstraint , LinearConstraint )
20
20
_MACHEPS = np .finfo (np .float64 ).eps
21
21
22
22
23
+ @_transition_to_rng ("seed" , position_num = 9 )
23
24
def differential_evolution (func , bounds , args = (), strategy = 'best1bin' ,
24
25
maxiter = 1000 , popsize = 15 , tol = 0.01 ,
25
- mutation = (0.5 , 1 ), recombination = 0.7 , seed = None ,
26
+ mutation = (0.5 , 1 ), recombination = 0.7 , rng = None ,
26
27
callback = None , disp = False , polish = True ,
27
28
init = 'latinhypercube' , atol = 0 , updating = 'immediate' ,
28
29
workers = 1 , constraints = (), x0 = None , * ,
@@ -124,14 +125,30 @@ def differential_evolution(func, bounds, args=(), strategy='best1bin',
124
125
denoted by CR. Increasing this value allows a larger number of mutants
125
126
to progress into the next generation, but at the risk of population
126
127
stability.
127
- seed : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
128
- If `seed` is None (or `np.random`), the `numpy.random.RandomState`
129
- singleton is used.
130
- If `seed` is an int, a new ``RandomState`` instance is used,
131
- seeded with `seed`.
132
- If `seed` is already a ``Generator`` or ``RandomState`` instance then
133
- that instance is used.
134
- Specify `seed` for repeatable minimizations.
128
+ rng : {None, int, `numpy.random.Generator`}, optional
129
+ If `rng` is passed by keyword, types other than `numpy.random.Generator` are
130
+ passed to `numpy.random.default_rng` to instantiate a ``Generator``.
131
+ If `rng` is already a ``Generator`` instance, then the provided instance is
132
+ used. Specify `rng` for repeatable minimizations.
133
+
134
+ If this argument is passed by position or `seed` is passed by keyword,
135
+ legacy behavior for the argument `seed` applies:
136
+
137
+ - If `seed` is None (or `numpy.random`), the `numpy.random.RandomState`
138
+ singleton is used.
139
+ - If `seed` is an int, a new ``RandomState`` instance is used,
140
+ seeded with `seed`.
141
+ - If `seed` is already a ``Generator`` or ``RandomState`` instance then
142
+ that instance is used.
143
+
144
+ .. versionchanged:: 1.15.0
145
+ As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
146
+ transition from use of `numpy.random.RandomState` to
147
+ `numpy.random.Generator` this keyword was changed from `seed` to `rng`.
148
+ For an interim period, both keywords will continue to work (only specify
149
+ one of them). After the interim period using the `seed` keyword will emit
150
+ warnings. The behavior of the `seed` and `rng` keywords is outlined above.
151
+
135
152
disp : bool, optional
136
153
Prints the evaluated `func` at every iteration.
137
154
callback : callable, optional
@@ -424,7 +441,7 @@ def differential_evolution(func, bounds, args=(), strategy='best1bin',
424
441
425
442
>>> bounds = Bounds([0., 0.], [2., 2.])
426
443
>>> result = differential_evolution(rosen, bounds, constraints=lc,
427
- ... seed =1)
444
+ ... rng =1)
428
445
>>> result.x, result.fun
429
446
(array([0.96632622, 0.93367155]), 0.0011352416852625719)
430
447
@@ -436,7 +453,7 @@ def differential_evolution(func, bounds, args=(), strategy='best1bin',
436
453
... arg2 = 0.5 * (np.cos(2. * np.pi * x[0]) + np.cos(2. * np.pi * x[1]))
437
454
... return -20. * np.exp(arg1) - np.exp(arg2) + 20. + np.e
438
455
>>> bounds = [(-5, 5), (-5, 5)]
439
- >>> result = differential_evolution(ackley, bounds, seed =1)
456
+ >>> result = differential_evolution(ackley, bounds, rng =1)
440
457
>>> result.x, result.fun
441
458
(array([0., 0.]), 4.440892098500626e-16)
442
459
@@ -445,7 +462,7 @@ def differential_evolution(func, bounds, args=(), strategy='best1bin',
445
462
function evaluations.
446
463
447
464
>>> result = differential_evolution(
448
- ... ackley, bounds, vectorized=True, updating='deferred', seed =1
465
+ ... ackley, bounds, vectorized=True, updating='deferred', rng =1
449
466
... )
450
467
>>> result.x, result.fun
451
468
(array([0., 0.]), 4.440892098500626e-16)
@@ -491,7 +508,7 @@ def differential_evolution(func, bounds, args=(), strategy='best1bin',
491
508
popsize = popsize , tol = tol ,
492
509
mutation = mutation ,
493
510
recombination = recombination ,
494
- seed = seed , polish = polish ,
511
+ rng = rng , polish = polish ,
495
512
callback = callback ,
496
513
disp = disp , init = init , atol = atol ,
497
514
updating = updating ,
@@ -594,14 +611,33 @@ class DifferentialEvolutionSolver:
594
611
denoted by CR. Increasing this value allows a larger number of mutants
595
612
to progress into the next generation, but at the risk of population
596
613
stability.
597
- seed : {None, int, `numpy.random.Generator`, `numpy.random.RandomState`}, optional
598
- If `seed` is None (or `np.random`), the `numpy.random.RandomState`
599
- singleton is used.
600
- If `seed` is an int, a new ``RandomState`` instance is used,
601
- seeded with `seed`.
602
- If `seed` is already a ``Generator`` or ``RandomState`` instance then
603
- that instance is used.
604
- Specify `seed` for repeatable minimizations.
614
+
615
+ rng : {None, int, `numpy.random.Generator`}, optional
616
+
617
+ ..versionchanged:: 1.15.0
618
+ As part of the `SPEC-007 <https://scientific-python.org/specs/spec-0007/>`_
619
+ transition from use of `numpy.random.RandomState` to
620
+ `numpy.random.Generator` this keyword was changed from `seed` to `rng`.
621
+ For an interim period both keywords will continue to work (only specify
622
+ one of them). After the interim period using the `seed` keyword will emit
623
+ warnings. The behavior of the `seed` and `rng` keywords is outlined below.
624
+
625
+ If `rng` is passed by keyword, types other than `numpy.random.Generator` are
626
+ passed to `numpy.random.default_rng` to instantiate a `Generator`.
627
+ If `rng` is already a `Generator` instance, then the provided instance is
628
+ used.
629
+
630
+ If this argument is passed by position or `seed` is passed by keyword, the
631
+ behavior is:
632
+
633
+ - If `seed` is None (or `np.random`), the `numpy.random.RandomState`
634
+ singleton is used.
635
+ - If `seed` is an int, a new `RandomState` instance is used,
636
+ seeded with `seed`.
637
+ - If `seed` is already a `Generator` or `RandomState` instance then
638
+ that instance is used.
639
+
640
+ Specify `seed`/`rng` for repeatable minimizations.
605
641
disp : bool, optional
606
642
Prints the evaluated `func` at every iteration.
607
643
callback : callable, optional
@@ -745,7 +781,7 @@ class DifferentialEvolutionSolver:
745
781
746
782
def __init__ (self , func , bounds , args = (),
747
783
strategy = 'best1bin' , maxiter = 1000 , popsize = 15 ,
748
- tol = 0.01 , mutation = (0.5 , 1 ), recombination = 0.7 , seed = None ,
784
+ tol = 0.01 , mutation = (0.5 , 1 ), recombination = 0.7 , rng = None ,
749
785
maxfun = np .inf , callback = None , disp = False , polish = True ,
750
786
init = 'latinhypercube' , atol = 0 , updating = 'immediate' ,
751
787
workers = 1 , constraints = (), x0 = None , * , integrality = None ,
@@ -864,7 +900,7 @@ def maplike_for_vectorized_func(func, x):
864
900
865
901
self .parameter_count = np .size (self .limits , 1 )
866
902
867
- self .random_number_generator = check_random_state (seed )
903
+ self .random_number_generator = check_random_state (rng )
868
904
869
905
# Which parameters are going to be integers?
870
906
if np .any (integrality ):
0 commit comments