mathai 0.3.2__py3-none-any.whl → 0.3.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mathai/search.py
CHANGED
@@ -2,110 +2,107 @@ from mathai import *
|
|
2
2
|
import copy
|
3
3
|
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
base_timeout=1, time_per_char=0.1, timeout_increase=0.5):
|
8
|
-
"""
|
9
|
-
Perform DFS simplification on a given equation using provided functions.
|
10
|
-
|
11
|
-
Args:
|
12
|
-
equation: The starting expression (TreeNode or parsed equation)
|
13
|
-
functions: List of simplification functions
|
14
|
-
true_expr: Expression representing True (immediate termination)
|
15
|
-
false_expr: Expression representing False (immediate termination)
|
16
|
-
max_timeout: Maximum timeout allowed for any function
|
17
|
-
max_small: Number of smallest expressions to track
|
18
|
-
base_timeout: Base timeout in seconds
|
19
|
-
time_per_char: Additional timeout per character of expression
|
20
|
-
timeout_increase: Factor to increase timeout for consecutive timeouts
|
21
|
-
|
22
|
-
Returns:
|
23
|
-
tuple(found_boolean, boolean_path, smallest_expressions)
|
24
|
-
"""
|
25
|
-
original_eq = simplify(equation)
|
26
|
-
smallest_four = []
|
27
|
-
|
28
|
-
stack = [(copy.deepcopy(original_eq), [copy.deepcopy(original_eq)])]
|
29
|
-
visited = set()
|
30
|
-
|
31
|
-
found_boolean = False
|
32
|
-
boolean_path = None
|
33
|
-
boolean_expr = None
|
34
|
-
|
35
|
-
executor = ThreadPoolExecutor(max_workers=3)
|
36
|
-
consecutive_timeouts = 0
|
37
|
-
|
38
|
-
while stack and not found_boolean:
|
39
|
-
current_eq, path = stack.pop()
|
40
|
-
expr_str = str(current_eq)
|
41
|
-
|
42
|
-
if expr_str in visited:
|
43
|
-
continue
|
44
|
-
visited.add(expr_str)
|
5
|
+
# Original expression
|
6
|
+
original_eq = simplify(parse("((P|~Q)&(P&~Q|P&R)&(~P&~R|~Q))<->P&~Q"))
|
45
7
|
|
46
|
-
|
47
|
-
|
8
|
+
# Simplification functions
|
9
|
+
lst = [logic1, logic3, logic2]
|
10
|
+
|
11
|
+
MAX_SMALL = 2
|
12
|
+
smallest_four = []
|
13
|
+
|
14
|
+
# Stack element: (current_expr, path_list)
|
15
|
+
stack = [(copy.deepcopy(original_eq), [copy.deepcopy(original_eq)])]
|
16
|
+
|
17
|
+
# Keep track of visited expressions to prevent cycles
|
18
|
+
visited = set()
|
19
|
+
|
20
|
+
# Boolean constants for immediate termination
|
21
|
+
TRUE_EXPR = tree_form("s_true")
|
22
|
+
FALSE_EXPR = tree_form("s_false")
|
23
|
+
|
24
|
+
found_boolean = False
|
25
|
+
boolean_path = None
|
26
|
+
boolean_expr = None
|
48
27
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
28
|
+
# Thread pool executor
|
29
|
+
executor = ThreadPoolExecutor(max_workers=3)
|
30
|
+
|
31
|
+
while stack and not found_boolean:
|
32
|
+
current_eq, path = stack.pop()
|
33
|
+
expr_str = str(current_eq)
|
34
|
+
|
35
|
+
if expr_str in visited:
|
36
|
+
continue
|
37
|
+
visited.add(expr_str)
|
38
|
+
|
39
|
+
# Thinking message
|
40
|
+
printeq(current_eq)
|
41
|
+
|
42
|
+
# Immediate termination for boolean constants
|
43
|
+
if current_eq == TRUE_EXPR or current_eq == FALSE_EXPR:
|
44
|
+
found_boolean = True
|
45
|
+
boolean_path = path
|
46
|
+
boolean_expr = current_eq
|
47
|
+
break
|
48
|
+
|
49
|
+
# Insert into smallest_four if qualifies
|
50
|
+
inserted = False
|
51
|
+
for j in range(len(smallest_four)):
|
52
|
+
if len(expr_str) < len(str(smallest_four[j][0])):
|
53
|
+
smallest_four.insert(j, (copy.deepcopy(current_eq), copy.deepcopy(path)))
|
54
|
+
inserted = True
|
54
55
|
break
|
56
|
+
if not inserted and len(smallest_four) < MAX_SMALL:
|
57
|
+
smallest_four.append((copy.deepcopy(current_eq), copy.deepcopy(path)))
|
58
|
+
if len(smallest_four) > MAX_SMALL:
|
59
|
+
smallest_four = smallest_four[:MAX_SMALL]
|
60
|
+
|
61
|
+
# First, try functions that reduce length
|
62
|
+
reduced_any = False
|
63
|
+
for fx in lst:
|
64
|
+
print(f"[Thinking] Executing {fx.__name__} on current expression:")
|
65
|
+
printeq(current_eq)
|
66
|
+
future = executor.submit(fx, current_eq)
|
67
|
+
try:
|
68
|
+
new_expr = future.result(timeout=5)
|
69
|
+
new_expr_str = str(new_expr)
|
70
|
+
# Only accept if shorter or equal
|
71
|
+
if len(new_expr_str) <= len(expr_str) and new_expr_str != expr_str:
|
72
|
+
reduced_any = True
|
73
|
+
stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
|
74
|
+
except TimeoutError:
|
75
|
+
print(f"[Thinking] {fx.__name__} timed out, skipping.")
|
76
|
+
continue
|
55
77
|
|
56
|
-
|
57
|
-
|
58
|
-
for
|
59
|
-
|
60
|
-
smallest_four.insert(j, (copy.deepcopy(current_eq), copy.deepcopy(path)))
|
61
|
-
inserted = True
|
62
|
-
break
|
63
|
-
if not inserted and len(smallest_four) < max_small:
|
64
|
-
smallest_four.append((copy.deepcopy(current_eq), copy.deepcopy(path)))
|
65
|
-
if len(smallest_four) > max_small:
|
66
|
-
smallest_four = smallest_four[:max_small]
|
67
|
-
|
68
|
-
# Calculate adaptive timeout with cap
|
69
|
-
timeout = (base_timeout + time_per_char * len(expr_str)) * (1 + timeout_increase * consecutive_timeouts)
|
70
|
-
if timeout > max_timeout:
|
71
|
-
timeout = max_timeout
|
72
|
-
|
73
|
-
# Try functions that reduce length first
|
74
|
-
reduced_any = False
|
75
|
-
for fx in functions:
|
76
|
-
print(f"[Thinking] Executing {fx.__name__} on current expression (timeout={timeout:.2f}s):")
|
78
|
+
# If no reducing function produced a shorter or equal expression, try a “growing” function
|
79
|
+
if not reduced_any:
|
80
|
+
for fx in lst:
|
81
|
+
print(f"[Thinking] Trying growing {fx.__name__} on current expression:")
|
77
82
|
printeq(current_eq)
|
78
83
|
future = executor.submit(fx, current_eq)
|
79
84
|
try:
|
80
|
-
new_expr = future.result(timeout=
|
85
|
+
new_expr = future.result(timeout=5)
|
81
86
|
new_expr_str = str(new_expr)
|
82
|
-
if
|
83
|
-
reduced_any = True
|
87
|
+
if new_expr_str != expr_str:
|
84
88
|
stack.append((new_expr, path + [copy.deepcopy(new_expr)]))
|
85
|
-
|
89
|
+
break # only take one growing function
|
86
90
|
except TimeoutError:
|
87
|
-
print(f"[Thinking] {fx.__name__} timed out, skipping.")
|
88
|
-
consecutive_timeouts += 1
|
91
|
+
print(f"[Thinking] {fx.__name__} (growing) timed out, skipping.")
|
89
92
|
continue
|
90
93
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
consecutive_timeouts += 1
|
107
|
-
continue
|
108
|
-
|
109
|
-
executor.shutdown(wait=True)
|
110
|
-
|
111
|
-
return found_boolean, boolean_path, smallest_four
|
94
|
+
# Shutdown executor
|
95
|
+
executor.shutdown(wait=True)
|
96
|
+
|
97
|
+
# Display final results
|
98
|
+
if found_boolean:
|
99
|
+
print("\nBoolean constant found! Full path to solution:\n")
|
100
|
+
for step in boolean_path:
|
101
|
+
printeq(step)
|
102
|
+
else:
|
103
|
+
print("\nDFS completed. Two smallest expressions with steps:\n")
|
104
|
+
for expr, path in smallest_four:
|
105
|
+
print("Path to final expression:")
|
106
|
+
for step in path:
|
107
|
+
printeq(step)
|
108
|
+
print("-"*50)
|
@@ -13,13 +13,13 @@ mathai/linear.py,sha256=wyiLpIxRDmD96xXktkgvc5gegIB3zblLB1EuV3TFdmU,5474
|
|
13
13
|
mathai/logic.py,sha256=UvHzRmKcO9AD51tRzHmpNSEhgW5gmaf4XPaQKFjGfC4,9653
|
14
14
|
mathai/parser.py,sha256=f7bemieFmp0sbup1NlraMLvZDVFvqKGFknEVtlFRMVk,6979
|
15
15
|
mathai/printeq.py,sha256=gIes-pstFOa6FcnpVIVvkjVKuWdsVdo11LlEnmHhakU,1303
|
16
|
-
mathai/search.py,sha256=
|
16
|
+
mathai/search.py,sha256=BmUKacCpptIoyUObB0hknAbgFs8EPQXXXXYBXT40Luc,3672
|
17
17
|
mathai/simplify.py,sha256=F37h-Z_rW35uVgx0G86vQErU2Ac6ZiTBN9KcC3RzDkg,15156
|
18
18
|
mathai/structure.py,sha256=4Ww2IAx62RcQSO7_17TZES-DjMWBpcFQtL939FBIHwY,4103
|
19
19
|
mathai/tool.py,sha256=UyccamiJy_CkFPakfufyPzdhtlEO6v2D7qwbXQ9V7Rg,2000
|
20
20
|
mathai/trig.py,sha256=5P0RNS4eetNds2l-wyA4BAKqJdFIUws_8RGS_dH7gp8,9348
|
21
21
|
mathai/univariate_inequality.py,sha256=_r-kkiS4Hr-jRN7f-EL_E4svAMFWJP1Ea50HJKKbjfk,14778
|
22
|
-
mathai-0.3.
|
23
|
-
mathai-0.3.
|
24
|
-
mathai-0.3.
|
25
|
-
mathai-0.3.
|
22
|
+
mathai-0.3.3.dist-info/METADATA,sha256=mlSi3WdczR0cSSetNJUPeJE7v9EAHJY-wTjKFbvRtsg,7021
|
23
|
+
mathai-0.3.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
24
|
+
mathai-0.3.3.dist-info/top_level.txt,sha256=ROP4l3OhGYw3ihkQGASr18xM9GsK4z3_6whV5AyXLwE,7
|
25
|
+
mathai-0.3.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|