-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpression.py
209 lines (172 loc) · 4.94 KB
/
expression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from abc import ABC, abstractmethod
import random
import warnings
import torch
import numpy as np
import sympy as sp
from sympy import lambdify, sympify
from bfgs import bfgs
import signal
# def handle_timeout(signum, frame):
# raise TimeoutError
# signal.signal(signal.SIGALRM, handle_timeout)
# Module-level constants
PLACEHOLDER = "PH"
OPT_SEQ = {
"x_1": "x_1",
"x_2": "x_2",
"c": "c",
"Abs": "abs",
"+": "add",
"*": "mul",
"/": "div",
"sqrt": "sqrt",
"exp": "exp",
"log": "log",
"**": "pow",
"sin": "sin",
"cos": "cos",
"tan": "tan",
"asin": "asin",
"acos": "acos",
"atan": "atan",
"sinh": "sinh",
"cosh": "cosh",
"tanh": "tanh",
"coth": "coth",
"-3": "-3",
"-2": "-2",
"-1": "-1",
"0": "0",
"1": "1",
"2": "2",
"3": "3",
"4": "4",
"5": "5",
}
class Node:
UNARY_OPERATORS = {
"sin",
"cos",
"tan",
"exp",
"sqrt",
"log",
"Abs",
"asin",
"acos",
"atan",
"sinh",
"cosh",
"tanh",
"coth",
}
BINARY_OPERATORS = {"+", "*", "/", "**"}
def __init__(self, parent=None):
self.val = PLACEHOLDER
self.parent = parent
def __str__(self):
return self.val
@property
def binary(self) -> bool:
"""Check if the node is a binary operation."""
if self.val in Node.UNARY_OPERATORS:
return False
elif self.val in Node.BINARY_OPERATORS:
return True
return None
def set_value(self, val: str) -> list:
"""
Set the value of the node and expand the placeholders accordingly.
Returns:
- A list of the newly created placeholder nodes.
"""
assert isinstance(val, str), "Node value must be a string."
self.val = val
if self.binary is False:
self.child = Node(self)
return [self.child]
elif self.binary is True:
self.left = Node(self)
self.right = Node(self)
return [self.right, self.left] # LIFO: right first
return []
class Expression(ABC):
pass
class TargetExpression(Expression):
def __init__(
self,
point_set: torch.FloatTensor,
expr: str = None,
skeleton: str = None,
opt_sequence: list = None,
):
"""
Initialize the Expression with a default value or an existing expression.
"""
assert point_set.shape[2] == 3, "Point set must be 3D."
self._point_set = point_set # 1 x N x 3
self._expr = expr
self._skeleton = skeleton
self._opt_sequence = opt_sequence
@property
def point_set(self) -> torch.FloatTensor:
return self._point_set
@property
def expr(self) -> str:
return self._expr
@property
def skeleton(self) -> str:
return self._skeleton
@property
def opt_sequence(self) -> list:
return self._opt_sequence
class AgentExpression(Expression):
def __init__(self, point_set: torch.FloatTensor):
"""
Initialize the Expression with a default value or an existing expression.
"""
# Initialize the expression
self.tree = Node()
self._expr = None
# reference point set
assert point_set.shape[2] == 3, "Point set must be 3D."
self._ref_point_set = point_set # 1 x N x 3
self._point_set = None
# record the history of agent operations
self._opt_sequence = []
# used to get the nodes to work on for current operation
self._to_be_expanded = [self.tree]
self.expr = None
@property
def skeleton(self) -> str:
return self._update_skeleton(self.tree)
@property
def opt_sequence(self) -> list:
return self._opt_sequence
def add_opt(self, opt: str) -> int:
"""
Add operation to the expression.
Returns:
- A flag indicating the status of the expression:
- 0: operation successfully added.
- 1: no more placeholder.
"""
if not self._to_be_expanded:
raise ValueError("No placeholder found in the expression.")
node = self._to_be_expanded.pop()
# Set the value of the node and add the newly created placeholders
h_list = node.set_value(opt)
self._to_be_expanded.extend(h_list)
# Add the operation to the history
self._opt_sequence.append(OPT_SEQ[opt])
return 0 if self._to_be_expanded else 1
def _update_skeleton(self, node: Node):
"""Update the string representation of the expression."""
if node.binary is False:
return f"{node.val}({self._update_skeleton(node.child)})"
if node.binary is True:
return f"({self._update_skeleton(node.left)} {node.val} {self._update_skeleton(node.right)})"
return node.val
if __name__ == "__main__":
pass