def test_lorentz_cone(self): config = Config() config.use_local_optimization = True config.precision = 0.0001 result = Minimize( x2, logical_and(-5 <= x0, x0 <= 5, -5 <= x1, x1 <= 5, 0 <= x2, x2 <= 5, 1 >= (x0 - 1)**2 + (x1 - 1)**2, x2**2 >= x0**2 + x1**2), config) self.assertTrue(result) self.assertAlmostEqual(result[x2].mid(), 0.414212, places=3)
def test_03_Aluffi_Pentini(self): (vars, domain) = make_domain([("x1", -10, 10), ("x2", -10, 10)]) def objective(x1, x2): return 0.25 * x1**4 - 0.5 * x1**2 + 0.1 * x1 + 0.5 * x2**2 sol = Minimize(objective(*vars), domain, self.config) self.global_min = -0.3523 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_19_Zettl(self): (vars, domain) = make_domain([("x", -5, 10), ("y", -5, 10)]) def objective(x1, x2): return (x1**2 + x2**2 - 2 * x1)**2 + 0.25 * x1 sol = Minimize(objective(*vars), domain, self.config) self.global_min = -0.003791 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_07_Brent(self): (vars, domain) = make_domain([("x", -10, 10), ("y", -10, 10)]) def objective(x, y): return (x + 10)**2 + (y + 10)**2 + exp(-1 * x**2 - y**2) sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_08_Bukin6(self): (vars, domain) = make_domain([("x", -15, 15), ("y", -3, 3)]) def objective(x, y): return 100 * sqrt(abs(y - 0.01 * x**2)) + 0.01 * abs(x + 10) sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_06_Booth(self): (vars, domain) = make_domain([("x", -10, 10), ("y", -10, 10)]) def objective(x, y): return (x + 2 * y - 7)**2 + (2 * x + y - 5)**2 sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_16_Testtube_Holder(self): (vars, domain) = make_domain([("x", -10, 10), ("y", -10, 10)]) def objective(x1, x2): return -4 * (sin(x1) * cos(x2) * exp(abs(cos((x1**2 + x2**2) / 200)))) sol = Minimize(objective(*vars), domain, self.config) self.global_min = -10.872300 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_12_Holder_Table2(self): (vars, domain) = make_domain([("x", -10, 10), ("y", -10, 10)]) def objective(x, y): return -abs( sin(x) * cos(y) * exp(abs(1 - sqrt(x**2 + y**2) / math.pi))) sol = Minimize(objective(*vars), domain, self.config) self.global_min = -19.2085 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_11_EggHolder(self): (vars, domain) = make_domain([("x", -512, 512), ("y", -512, 512)]) def objective(x1, x2): return -(x2 + 47) * sin(sqrt(abs(x2 + x1 / 2.0 + 47))) - x1 * sin( sqrt(abs(x1 - (x2 + 47)))) sol = Minimize(objective(*vars), domain, self.config) self.global_min = -959.6407 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 5)
def test_10_Easom(self): (vars, domain) = make_domain([("x", -100, 100), ("y", -100, 100)]) def objective(x, y): return -cos(x) * cos(y) * exp(-((x - math.pi)**2 + (y - math.pi)**2)) sol = Minimize(objective(*vars), domain, self.config) self.global_min = -1 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_05_Bohachevsky1(self): (vars, domain) = make_domain([("x", -100, 100), ("y", -100, 100)]) def objective(x, y): return x**2 + 2 * y**2 - 0.3 * cos(3 * math.pi * x) - 0.4 * cos( 4 * math.pi * y) + 0.7 sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_04_Beale(self): (vars, domain) = make_domain([("x", -4.5, 4.5), ("y", -4.5, 4.5)]) def objective(x, y): return ((1.5 - x + x * y)**2 + (2.25 - x + x * (y**2))**2 + (2.625 - x + x * (y**3))**2) sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_18_W_Wavy(self): (vars, domain) = make_domain([("x", -3, 3), ("y", -3, 3)]) def objective(x1, x2): return 1 - 0.5 * ((cos(10 * x1) * exp(-x1**2 / 2)) + (cos(10 * x2) * exp(-x2**2 / 2))) sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0.0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_15_Schaffer_F6(self): (vars, domain) = make_domain([("x", -100, 100), ("y", -100, 100)]) def objective(x, y): return 0.5 + ((sin(x**2 - y**2))**2 - 0.5) / (1 + 0.001 * (x**2 + y**2))**2 sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_17_Trefethen(self): (vars, domain) = make_domain([("x", -10, 10), ("y", -10, 10)]) def objective(x1, x2): return exp(sin(50 * x1)) + sin(60 * exp(x2)) + sin( 70 * sin(x1)) + sin(sin(80 * x2)) - sin( 10 * (x1 + x2)) + 0.25 * (x1**2 + x2**2) sol = Minimize(objective(*vars), domain, self.config) self.global_min = -3.30686865 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_09_Cross_in_Tray(self): (vars, domain) = make_domain([("x", -10, 10), ("y", -10, 10)]) def objective(x, y): return -0.0001 * (abs( sin(x) * sin(y) * exp(abs(100 - sqrt(x**2 + y**2) / math.pi))) + 1)**0.1 sol = Minimize(objective(*vars), domain, self.config) self.global_min = -2.06261 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_01_Ackley_2D(self): (vars, domain) = make_domain([("x", -5, 5), ("y", -5, 5)]) def objective(x, y): return (-20 * exp(-0.02 * sqrt(0.5 * (x**2 + y**2))) - exp(0.5 * (cos(2 * math.pi * x) + cos(2 * math.pi * y))) + math.e + 20) sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0.0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_13_Levi_N13(self): (vars, domain) = make_domain([("x", -10, 10), ("y", -10, 10)]) def objective(x, y): return (sin( 3 * math.pi * x))**2 + (x - 1)**2 * (1 + (sin(3 * math.pi * y))**2) + ( y - 1)**2 * (1 + (sin(2 * math.pi * y))**2) sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_02_Rosenbrock_Disk(self): (vars, domain) = make_domain([("x", -1.5, 1.5), ("y", -1.5, 1.5)]) [x, y] = vars def objective(x, y): return (1 - x)**2 + 100 * (y - x**2)**2 constraints = logical_and(domain, x**2 + y**2 < 2) sol = Minimize(objective(*vars), constraints, self.config) self.global_min = 0.0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_05_Simionescu(self): (vars, domain) = make_domain([("x", -1.25, 1.25), ("y", -1.25, 1.25)]) [x, y] = vars def objective(x, y): return 0.1 * x * y constraints = logical_and( domain, x**2 + y**2 <= (1 + 0.2 * cos(8 * atan(x / y)))**2) sol = Minimize(objective(*vars), constraints, self.config) self.global_min = -0.072625 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 10)
def test_03_Mishra_Bird(self): (vars, domain) = make_domain([("x", -10, 0.0), ("y", -6.5, 0.0)]) [x, y] = vars def objective(x, y): return sin(y) * exp((1 - cos(x))**2) + cos(x) * exp( (1 - sin(y))**2) + (x - y)**2 constraints = logical_and(domain, (x + 5)**2 + (y + 5)**2 < 25) sol = Minimize(objective(*vars), constraints, self.config) self.global_min = -106.7645367 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_14_Ripple1(self): (vars, domain) = make_domain([("x1", 0, 1.0), ("x2", 0, 1.0)]) def objective(x1, x2): t1 = -1 * exp(-2 * log(2) * ((x1 - 0.1) / 0.8)**2) * ( sin(5 * math.pi * x1)**6 + 0.1 * cos(500 * math.pi * x1)**2) t2 = -1 * exp(-2 * log(2) * ((x2 - 0.1) / 0.8)**2) * ( sin(5 * math.pi * x2)**6 + 0.1 * cos(500 * math.pi * x2)**2) return t1 + t2 sol = Minimize(objective(*vars), domain, self.config) self.global_min = -2.2 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_04_Townsend(self): (vars, domain) = make_domain([("x", -2.25, 2.5), ("y", -2.5, 1.75)]) [x, y] = vars def objective(x, y): return -(cos((x - 0.1) * y))**2 - x * sin(3 * x + y) constraints = logical_and( domain, x**2 + y**2 < (2 * cos(atan2(x, y)) - 0.5 * cos(2 * atan2(x, y)) - 0.25 * cos(3 * atan2(x, y)) - 0.125 * cos(4 * atan2(x, y)))**2 + (2 * sin(atan2(x, y)))**2) sol = Minimize(objective(*vars), constraints, self.config) self.global_min = -2.0239884 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_02_Ackley_4D(self): (vars, domain) = make_domain([ ("x1", -5, 5), ("x2", -5, 5), ("x3", -5, 5), ("x4", -5, 5), ]) def objective(x1, x2, x3, x4): return (-20 * exp(-0.02 * sqrt(1.0 / 4.0 * (x1**2 + x2**2 + x3**2 + x4**2))) - exp(1.0 / 4.0 * (cos(2 * math.pi * x1) + cos(2 * math.pi * x2) + cos(2 * math.pi * x3) + cos(2 * math.pi * x4))) + math.e + 20) sol = Minimize(objective(*vars), domain, self.config) self.global_min = 0.0 self.assertIsNotNone(sol) self.found_min = compute_min(objective, sol, vars) self.assertAlmostEqual(self.found_min, self.global_min, delta=self.config.precision * 2)
def test_minimize1(self): result = Minimize(objective, constraint, 0.00001) self.assertTrue(result) self.assertAlmostEqual(result[x].mid(), -1.5, places=2)
def test_minimize2(self): b = Box([x]) result = Minimize(objective, constraint, 0.00001, b) self.assertTrue(result) self.assertAlmostEqual(b[x].mid(), -1.5, places=2)
def test_minimize3(self): result = Minimize(x, logical_and(0 <= x, x <= 1, 0 <= p, p <= 1, p <= x), 0.00001) self.assertTrue(result) self.assertAlmostEqual(result[x].mid(), 0, places=2) self.assertAlmostEqual(result[p].mid(), 0, places=2)