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)
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.')
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()
def test_exp_runs(self): # test whether exponential mutation loop runs solver = DifferentialEvolutionSolver(rosen, self.bounds, strategy='best1exp', maxiter=1) solver.solve()
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()
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.')
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
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)
def test_convergence(self): solver = DifferentialEvolutionSolver(rosen, self.bounds, tol=0.2, polish=False) solver.solve() assert_(solver.convergence < 0.2)
def test_quadratic(self): # test the quadratic function from object solver = DifferentialEvolutionSolver(self.quadratic, [(-100, 100)], tol=0.02) solver.solve()
def test_best_solution_retrieval(self): # test that the getter property method for the best solution works. solver = DifferentialEvolutionSolver(self.quadratic, [(-2, 2)]) result = solver.solve() assert_almost_equal(result.x, solver.x)
def test_differential_evolution(self): # test that the Jmin of DifferentialEvolutionSolver # is the same as the function evaluation solver = DifferentialEvolutionSolver(self.quadratic, [(-2, 2)]) result = solver.solve() assert_almost_equal(result.fun, self.quadratic(result.x))
def test_converged(self): solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)]) solver.solve() assert_(solver.converged())