def meti25(numSolutions="all"): start = time.time() epsilon = 1e-14 lenV = 1 x = Variable('x') allSolutions = [] while True: if numSolutions != "all" and len(allSolutions) == numSolutions: break allConstraints = [] allConstraints.append(sin(x / 3.0) + sin(3 * x) / 6.0 > 0) allConstraints.append(x >= math.pi / 3.0) allConstraints.append(x <= (2 / 3.0) * math.pi) excludingConstraints = [] for solution in allSolutions: singleExcludingConstraints = [] singleExcludingConstraints.append(x <= solution[0][0]) singleExcludingConstraints.append(x >= solution[0][1]) excludingConstraints.append(singleExcludingConstraints) #print ("allConstraints") #print (allConstraints) #print ("numConstraints", len(allConstraints)) f_sat = logical_and(*allConstraints) if len(excludingConstraints) > 0: for constraints in excludingConstraints: f_sat = logical_and(f_sat, logical_or(*constraints)) #print ("f_sat") #print (f_sat) result = CheckSatisfiability(f_sat, epsilon) #print (result) if result is None: break hyper = np.zeros((1, 2)) hyper[0, :] = [ result[x].lb() - 1000 * epsilon, result[x].ub() + 1000 * epsilon ] print("hyper", hyper) allSolutions.append(hyper) print("num solutions found", len(allSolutions)) '''constraints = [] x = Variable('x') #constraints.append(x >= 0.0) #constraints.append(x <= 64.0) constraints.append(2*math.pi - 2*x*asin(cos(0.797)*sin(math.pi/x)) == 2.097 - 0.0331*x) f_sat = logical_and(*constraints) result = CheckSatisfiability(f_sat, epsilon) print (result)''' end = time.time() print("time taken", end - start)
def test_functions_with_variable(self): self.assertEqual(str(abs(x)), "abs(x)") self.assertEqual(str(exp(x)), "exp(x)") self.assertEqual(str(sqrt(x)), "sqrt(x)") self.assertEqual(str(pow(x, y)), "pow(x, y)") self.assertEqual(str(sin(x)), "sin(x)") self.assertEqual(str(cos(x)), "cos(x)") self.assertEqual(str(tan(x)), "tan(x)") self.assertEqual(str(asin(x)), "asin(x)") self.assertEqual(str(acos(x)), "acos(x)") self.assertEqual(str(atan(x)), "atan(x)") self.assertEqual(str(atan2(x, y)), "atan2(x, y)") self.assertEqual(str(sinh(x)), "sinh(x)") self.assertEqual(str(cosh(x)), "cosh(x)") self.assertEqual(str(tanh(x)), "tanh(x)") self.assertEqual(str(min(x, y)), "min(x, y)") self.assertEqual(str(max(x, y)), "max(x, y)") self.assertEqual(str(if_then_else(x > y, x, y)), "(if (x > y) then x else y)")
def test_functions_with_expression(self): self.assertEqual(str(abs(e_x)), "abs(x)") self.assertEqual(str(exp(e_x)), "exp(x)") self.assertEqual(str(sqrt(e_x)), "sqrt(x)") self.assertEqual(str(pow(e_x, e_y)), "pow(x, y)") self.assertEqual(str(sin(e_x)), "sin(x)") self.assertEqual(str(cos(e_x)), "cos(x)") self.assertEqual(str(tan(e_x)), "tan(x)") self.assertEqual(str(asin(e_x)), "asin(x)") self.assertEqual(str(acos(e_x)), "acos(x)") self.assertEqual(str(atan(e_x)), "atan(x)") self.assertEqual(str(atan2(e_x, e_y)), "atan2(x, y)") self.assertEqual(str(sinh(e_x)), "sinh(x)") self.assertEqual(str(cosh(e_x)), "cosh(x)") self.assertEqual(str(tanh(e_x)), "tanh(x)") self.assertEqual(str(min(e_x, e_y)), "min(x, y)") self.assertEqual(str(max(e_x, e_y)), "max(x, y)") self.assertEqual(str(if_then_else(e_x > e_y, e_x, e_y)), "(if (x > y) then x else y)")
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_functions_with_float(self): v_x = 1.0 v_y = 1.0 self.assertEqual(abs(v_x), math.fabs(v_x)) self.assertEqual(exp(v_x), math.exp(v_x)) self.assertEqual(sqrt(v_x), math.sqrt(v_x)) self.assertEqual(pow(v_x, v_y), v_x**v_y) self.assertEqual(sin(v_x), math.sin(v_x)) self.assertEqual(cos(v_x), math.cos(v_x)) self.assertEqual(tan(v_x), math.tan(v_x)) self.assertEqual(asin(v_x), math.asin(v_x)) self.assertEqual(acos(v_x), math.acos(v_x)) self.assertEqual(atan(v_x), math.atan(v_x)) self.assertEqual(atan2(v_x, v_y), math.atan2(v_x, v_y)) self.assertEqual(sinh(v_x), math.sinh(v_x)) self.assertEqual(cosh(v_x), math.cosh(v_x)) self.assertEqual(tanh(v_x), math.tanh(v_x)) self.assertEqual(min(v_x, v_y), min(v_x, v_y)) self.assertEqual(max(v_x, v_y), max(v_x, v_y)) self.assertEqual( if_then_else(Expression(v_x) > Expression(v_y), v_x, v_y), v_x if v_x > v_y else v_y)
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
def objective(x, y): return -abs( sin(x) * cos(y) * exp(abs(1 - sqrt(x**2 + y**2) / math.pi)))
def run_sin(self, ident, val) -> None: ident['dreal_exp'] = dreal.sin(val.exp['dreal_exp'])
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)
# -*- coding: utf-8 -*- from dreal.symbolic import Variable, logical_and, sin, cos from dreal.symbolic import logical_imply, forall from dreal.api import CheckSatisfiability, Minimize from dreal.util import Box import math import unittest x = Variable("x") y = Variable("y") z = Variable("z") f_sat = logical_and(0 <= x, x <= 10, 0 <= y, y <= 10, 0 <= z, z <= 10, sin(x) + cos(y) == z) f_unsat = logical_and(3 <= x, x <= 4, 4 <= y, y <= 5, 5 <= z, z <= 6, sin(x) + cos(y) == z) objective = 2 * x * x + 6 * x + 5 constraint = logical_and(-10 <= x, x <= 10) class ApiTest(unittest.TestCase): def test_delta_sat(self): result = CheckSatisfiability(f_sat, 0.001) self.assertEqual(type(result), Box) b = Box([x, y, z]) result = CheckSatisfiability(f_sat, 0.001, b) self.assertEqual(result, True) self.assertTrue(b[x].diam() < 0.1)
def f(x): return sin(x) * cos(x)
def objective(x1, x2): return -(x2 + 47) * sin(sqrt(abs(x2 + x1 / 2.0 + 47))) - x1 * sin( sqrt(abs(x1 - (x2 + 47))))
def objective(x, y): return -(cos((x - 0.1) * y))**2 - x * sin(3 * x + y)
def objective(x, y): return sin(y) * exp((1 - cos(x))**2) + cos(x) * exp( (1 - sin(y))**2) + (x - y)**2
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)
def objective(x1, x2): return -4 * (sin(x1) * cos(x2) * exp(abs(cos((x1**2 + x2**2) / 200))))
def objective(x, y): return 0.5 + ((sin(x**2 - y**2))**2 - 0.5) / (1 + 0.001 * (x**2 + y**2))**2
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