本文整理汇总了Python中scipy.optimize._differentialevolution.DifferentialEvolutionSolver类的典型用法代码示例。如果您正苦于以下问题:Python DifferentialEvolutionSolver类的具体用法?Python DifferentialEvolutionSolver怎么用?Python DifferentialEvolutionSolver使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DifferentialEvolutionSolver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_deferred_updating
def test_deferred_updating(self):
# check setting of deferred updating, with default workers
bounds = [(0., 2.), (0., 2.)]
solver = DifferentialEvolutionSolver(rosen, bounds, updating='deferred')
assert_(solver._updating == 'deferred')
assert_(solver._mapwrapper._mapfunc is map)
solver.solve()
示例2: test_quadratic
def test_quadratic(self):
# test the quadratic function from object
solver = DifferentialEvolutionSolver(self.quadratic,
[(-100, 100)],
tol=0.02)
solver.solve()
assert_equal(np.argmin(solver.population_energies), 0)
示例3: test_maxfun_stops_solve
def test_maxfun_stops_solve(self):
# test that if the maximum number of function evaluations is exceeded
# during initialisation the solver stops
solver = DifferentialEvolutionSolver(rosen, self.bounds, maxfun=1,
polish=False)
result = solver.solve()
assert_equal(result.nfev, 2)
assert_equal(result.success, False)
assert_equal(result.message,
'Maximum number of function evaluations has '
'been exceeded.')
# test that if the maximum number of function evaluations is exceeded
# during the actual minimisation, then the solver stops.
# Have to turn polishing off, as this will still occur even if maxfun
# is reached. For popsize=5 and len(bounds)=2, then there are only 10
# function evaluations during initialisation.
solver = DifferentialEvolutionSolver(rosen,
self.bounds,
popsize=5,
polish=False,
maxfun=40)
result = solver.solve()
assert_equal(result.nfev, 41)
assert_equal(result.success, False)
assert_equal(result.message,
'Maximum number of function evaluations has '
'been exceeded.')
示例4: test_maxiter_stops_solve
def test_maxiter_stops_solve(self):
# test that if the maximum number of iterations is exceeded
# the solver stops.
solver = DifferentialEvolutionSolver(rosen, self.bounds, maxiter=1)
result = solver.solve()
assert_equal(result.success, False)
assert_equal(result.message,
'Maximum number of iterations has been exceeded.')
示例5: test_exp_runs
def test_exp_runs(self):
# test whether exponential mutation loop runs
solver = DifferentialEvolutionSolver(rosen,
self.bounds,
strategy='best1exp',
maxiter=1)
solver.solve()
示例6: test_maxiter_none_GH5731
def test_maxiter_none_GH5731(self):
# Pre 0.17 the previous default for maxiter and maxfun was None.
# the numerical defaults are now 1000 and np.inf. However, some scripts
# will still supply None for both of those, this will raise a TypeError
# in the solve method.
solver = DifferentialEvolutionSolver(rosen, self.bounds, maxiter=None,
maxfun=None)
solver.solve()
示例7: test_select_samples
def test_select_samples(self):
# select_samples should return 5 separate random numbers.
limits = np.arange(12., dtype='float64').reshape(2, 6)
bounds = list(zip(limits[0, :], limits[1, :]))
solver = DifferentialEvolutionSolver(None, bounds, popsize=1)
candidate = 0
r1, r2, r3, r4, r5 = solver._select_samples(candidate, 5)
assert_equal(
len(np.unique(np.array([candidate, r1, r2, r3, r4, r5]))), 6)
示例8: test_calculate_population_energies
def test_calculate_population_energies(self):
# if popsize is 3 then the overall generation has size (6,)
solver = DifferentialEvolutionSolver(rosen, self.bounds, popsize=3)
solver._calculate_population_energies(solver.population)
solver._promote_lowest_energy()
assert_equal(np.argmin(solver.population_energies), 0)
# initial calculation of the energies should require 6 nfev.
assert_equal(solver._nfev, 6)
示例9: test_constraint_solve
def test_constraint_solve(self):
def constr_f(x):
return np.array([x[0] + x[1]])
nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)
solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
constraints=(nlc))
# trust-constr warns if the constraint function is linear
with warns(UserWarning):
res = solver.solve()
assert constr_f(res.x) <= 1.9
assert res.success
示例10: setup_method
def setup_method(self):
self.old_seterr = np.seterr(invalid='raise')
self.limits = np.array([[0., 0.],
[2., 2.]])
self.bounds = [(0., 2.), (0., 2.)]
self.dummy_solver = DifferentialEvolutionSolver(self.quadratic,
[(0, 100)])
# dummy_solver2 will be used to test mutation strategies
self.dummy_solver2 = DifferentialEvolutionSolver(self.quadratic,
[(0, 1)],
popsize=7,
mutation=0.5)
# create a population that's only 7 members long
# [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
population = np.atleast_2d(np.arange(0.1, 0.8, 0.1)).T
self.dummy_solver2.population = population
示例11: test_constraint_violation_fn
def test_constraint_violation_fn(self):
def constr_f(x):
return [x[0] + x[1]]
def constr_f2(x):
return [x[0]**2 + x[1], x[0] - x[1]]
nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)
solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
constraints=(nlc))
cv = solver._constraint_violation_fn([1.0, 1.0])
assert_almost_equal(cv, 0.1)
nlc2 = NonlinearConstraint(constr_f2, -np.inf, 1.8)
solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
constraints=(nlc, nlc2))
# for multiple constraints the constraint violations should
# be concatenated.
cv = solver._constraint_violation_fn([1.2, 1.])
assert_almost_equal(cv, [0.3, 0.64, 0])
cv = solver._constraint_violation_fn([2., 2.])
assert_almost_equal(cv, [2.1, 4.2, 0])
# should accept valid values
cv = solver._constraint_violation_fn([0.5, 0.5])
assert_almost_equal(cv, [0., 0., 0.])
示例12: test_population_initiation
def test_population_initiation(self):
# test the different modes of population initiation
# init must be either 'latinhypercube' or 'random'
# raising ValueError is something else is passed in
assert_raises(ValueError,
DifferentialEvolutionSolver,
*(rosen, self.bounds),
**{'init': 'rubbish'})
solver = DifferentialEvolutionSolver(rosen, self.bounds)
# check that population initiation:
# 1) resets _nfev to 0
# 2) all population energies are np.inf
solver.init_population_random()
assert_equal(solver._nfev, 0)
assert_(np.all(np.isinf(solver.population_energies)))
solver.init_population_lhs()
assert_equal(solver._nfev, 0)
assert_(np.all(np.isinf(solver.population_energies)))
示例13: test_population_initiation
def test_population_initiation(self):
# test the different modes of population initiation
# init must be either 'latinhypercube' or 'random'
# raising ValueError is something else is passed in
assert_raises(ValueError,
DifferentialEvolutionSolver,
*(rosen, self.bounds),
**{'init': 'rubbish'})
solver = DifferentialEvolutionSolver(rosen, self.bounds)
# check that population initiation:
# 1) resets _nfev to 0
# 2) all population energies are np.inf
solver.init_population_random()
assert_equal(solver._nfev, 0)
assert_(np.all(np.isinf(solver.population_energies)))
solver.init_population_lhs()
assert_equal(solver._nfev, 0)
assert_(np.all(np.isinf(solver.population_energies)))
# we should be able to initialise with our own array
population = np.linspace(-1, 3, 10).reshape(5, 2)
solver = DifferentialEvolutionSolver(rosen, self.bounds,
init=population,
strategy='best2bin',
atol=0.01, seed=1, popsize=5)
assert_equal(solver._nfev, 0)
assert_(np.all(np.isinf(solver.population_energies)))
assert_(solver.num_population_members == 5)
assert_(solver.population_shape == (5, 2))
# check that the population was initialised correctly
unscaled_population = np.clip(solver._unscale_parameters(population),
0, 1)
assert_almost_equal(solver.population[:5], unscaled_population)
# population values need to be clipped to bounds
assert_almost_equal(np.min(solver.population[:5]), 0)
assert_almost_equal(np.max(solver.population[:5]), 1)
# shouldn't be able to initialise with an array if it's the wrong shape
# this would have too many parameters
population = np.linspace(-1, 3, 15).reshape(5, 3)
assert_raises(ValueError,
DifferentialEvolutionSolver,
*(rosen, self.bounds),
**{'init': population})
示例14: test_impossible_constraint
def test_impossible_constraint(self):
def constr_f(x):
return np.array([x[0] + x[1]])
nlc = NonlinearConstraint(constr_f, -np.inf, -1)
solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
constraints=(nlc), popsize=3)
# a UserWarning is issued because the 'trust-constr' polishing is
# attempted on the least infeasible solution found.
with warns(UserWarning):
res = solver.solve()
assert_allclose(res.x, [0, 0], atol=1e-6)
assert res.maxcv > 0
assert not res.success
# test _promote_lowest_energy works when none of the population is
# feasible. In this case the solution with the lowest constraint
# violation should be promoted.
solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
constraints=(nlc), polish=False)
next(solver)
assert not solver.feasible.all()
assert not np.isfinite(solver.population_energies).all()
# now swap two of the entries in the population
l = 20
cv = solver.constraint_violation[0]
solver.population_energies[[0, l]] = solver.population_energies[[l, 0]]
solver.population[[0, l], :] = solver.population[[l, 0], :]
solver.constraint_violation[[0, l], :] = (
solver.constraint_violation[[l, 0], :])
solver._promote_lowest_energy()
assert_equal(solver.constraint_violation[0], cv)
示例15: test_constraint_population_feasibilities
def test_constraint_population_feasibilities(self):
def constr_f(x):
return [x[0] + x[1]]
def constr_f2(x):
return [x[0]**2 + x[1], x[0] - x[1]]
nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)
solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
constraints=(nlc))
# are population feasibilities correct
# [0.5, 0.5] corresponds to scaled values of [1., 1.]
feas, cv = solver._calculate_population_feasibilities(
np.array([[0.5, 0.5], [1., 1.]]))
assert_equal(feas, [False, False])
assert_almost_equal(cv, np.array([[0.1], [2.1]]))
assert cv.shape == (2, 1)
nlc2 = NonlinearConstraint(constr_f2, -np.inf, 1.8)
solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
constraints=(nlc, nlc2))
feas, cv = solver._calculate_population_feasibilities(
np.array([[0.5, 0.5], [0.6, 0.5]]))
assert_equal(feas, [False, False])
assert_almost_equal(cv, np.array([[0.1, 0.2, 0], [0.3, 0.64, 0]]))
feas, cv = solver._calculate_population_feasibilities(
np.array([[0.5, 0.5], [1., 1.]]))
assert_equal(feas, [False, False])
assert_almost_equal(cv, np.array([[0.1, 0.2, 0], [2.1, 4.2, 0]]))
assert cv.shape == (2, 3)
feas, cv = solver._calculate_population_feasibilities(
np.array([[0.25, 0.25], [1., 1.]]))
assert_equal(feas, [True, False])
assert_almost_equal(cv, np.array([[0.0, 0.0, 0.], [2.1, 4.2, 0]]))
assert cv.shape == (2, 3)