-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm3.py
256 lines (217 loc) · 10 KB
/
m3.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import re
from torch_geometric.nn import GCNConv, global_mean_pool
import matplotlib.pyplot as plt
from matplotlib.widgets import Button, TextBox
import networkx as nx
class EquationEncoder:
def __init__(self, max_length=20):
self.max_length = max_length
self.char_to_idx = {
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4,
'5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
'x': 10, '^': 11, '+': 12, '-': 13, '=': 14,
' ': 15
}
self.idx_to_char = {v: k for k, v in self.char_to_idx.items()}
def encode_equation(self, equation):
"""Convert equation string to tensor."""
encoded = torch.zeros(self.max_length, dtype=torch.long)
for i, char in enumerate(equation[:self.max_length]):
if char in self.char_to_idx:
encoded[i] = self.char_to_idx[char]
return encoded
def decode_equation(self, encoded):
"""Convert tensor back to equation string."""
return ''.join(self.idx_to_char[idx.item()] for idx in encoded if idx.item() in self.idx_to_char)
class EquationType:
LINEAR = 'linear'
QUADRATIC = 'quadratic'
class EquationClassifier(nn.Module):
def __init__(self, input_size, hidden_size):
super().__init__()
self.embedding = nn.Embedding(16, hidden_size) # 16 possible characters
self.lstm = nn.LSTM(hidden_size, hidden_size, batch_first=True)
self.fc = nn.Linear(hidden_size, 2) # 2 classes: linear or quadratic
def forward(self, x):
x = self.embedding(x)
lstm_out, _ = self.lstm(x)
return self.fc(lstm_out[:, -1])
class EquationSolver(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.embedding = nn.Embedding(16, hidden_size)
self.lstm = nn.LSTM(hidden_size, hidden_size, num_layers=2, batch_first=True)
self.fc_layers = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, output_size)
)
def forward(self, x):
x = self.embedding(x)
lstm_out, _ = self.lstm(x)
return self.fc_layers(lstm_out[:, -1])
class MLMathSolver:
def __init__(self):
self.encoder = EquationEncoder()
self.classifier = EquationClassifier(input_size=20, hidden_size=128)
self.linear_solver = EquationSolver(input_size=20, hidden_size=128, output_size=1)
self.quadratic_solver = EquationSolver(input_size=20, hidden_size=128, output_size=2)
# Load pre-trained models if available
try:
self.classifier.load_state_dict(torch.load('classifier_model.pth'))
self.linear_solver.load_state_dict(torch.load('linear_solver_model.pth'))
self.quadratic_solver.load_state_dict(torch.load('quadratic_solver_model.pth'))
except FileNotFoundError:
print("Pre-trained models not found. Models will need training.")
def parse_equation(self, equation_str):
"""Parse equation and identify its type."""
# Clean the equation string
equation_str = equation_str.replace(" ", "").lower()
# Check if equation is quadratic
is_quadratic = 'x^2' in equation_str or 'x²' in equation_str
# Extract coefficients
if is_quadratic:
# Match quadratic equation pattern ax^2 + bx + c = 0
pattern = r'([-+]?\d*)?x\^2([-+]?\d*)?x?([-+]?\d+)?=0'
match = re.match(pattern, equation_str)
if match:
a = float(match.group(1) or 1)
b = float(match.group(2) or 0)
c = float(match.group(3) or 0)
return EquationType.QUADRATIC, (a, b, c)
else:
# Match linear equation pattern ax + b = c
pattern = r'([-+]?\d*)?x([-+]?\d+)?=(\d+)'
match = re.match(pattern, equation_str)
if match:
a = float(match.group(1) or 1)
b = float(match.group(2) or 0)
c = float(match.group(3))
return EquationType.LINEAR, (a, b, c)
raise ValueError("Invalid equation format")
def solve_equation(self, equation_str):
"""Solve the equation using appropriate ML model."""
# Encode equation
encoded_eq = self.encoder.encode_equation(equation_str)
encoded_eq = encoded_eq.unsqueeze(0) # Add batch dimension
# Classify equation type
with torch.no_grad():
eq_type_logits = self.classifier(encoded_eq)
eq_type_pred = torch.argmax(eq_type_logits, dim=1).item()
# Solve based on type
if eq_type_pred == 0: # Linear
with torch.no_grad():
solution = self.linear_solver(encoded_eq)
return [solution.item()]
else: # Quadratic
with torch.no_grad():
solutions = self.quadratic_solver(encoded_eq)
return solutions.tolist()[0]
def train_models(self, training_data):
"""Train the models with provided data."""
# Training logic here
pass
class SolutionVisualizer:
def __init__(self, figsize=(12, 8)):
self.figsize = figsize
self.fig = None
self.solver = MLMathSolver()
def setup_figure(self):
"""Set up the interactive figure."""
self.fig = plt.figure(figsize=self.figsize)
gs = self.fig.add_gridspec(2, 2)
# Create axes
self.ax_equation = self.fig.add_subplot(gs[0, 0])
self.ax_graph = self.fig.add_subplot(gs[0, 1])
self.ax_solution = self.fig.add_subplot(gs[1, :])
# Create input box
ax_input = plt.axes([0.1, 0.02, 0.3, 0.075])
self.text_input = TextBox(ax_input, 'Equation:', initial='x + 5 = 10')
self.text_input.on_submit(self.solve_equation)
# Create solve button
ax_solve = plt.axes([0.45, 0.02, 0.1, 0.075])
self.btn_solve = Button(ax_solve, 'Solve')
self.btn_solve.on_clicked(lambda _: self.solve_equation(self.text_input.text))
self.fig.suptitle('ML Math Equation Solver', fontsize=14)
def solve_equation(self, equation_str):
"""Handle equation solving and visualization."""
try:
# Clear previous plots
for ax in [self.ax_equation, self.ax_graph, self.ax_solution]:
ax.clear()
# Parse and solve equation
eq_type, coefficients = self.solver.parse_equation(equation_str)
solutions = self.solver.solve_equation(equation_str)
# Display equation
self.ax_equation.text(0.5, 0.5, f"Equation: {equation_str}",
fontsize=12, ha='center')
self.ax_equation.axis('off')
# Plot graph
x = np.linspace(-10, 10, 200)
if eq_type == EquationType.LINEAR:
a, b, c = coefficients
y = a * x + b
self.ax_graph.plot(x, y)
self.ax_graph.axhline(y=0, color='k', linestyle='-', alpha=0.3)
self.ax_graph.axvline(x=0, color='k', linestyle='-', alpha=0.3)
self.ax_graph.grid(True, alpha=0.3)
self.ax_graph.set_title('Linear Function')
else: # Quadratic
a, b, c = coefficients
y = a * x**2 + b * x + c
self.ax_graph.plot(x, y)
self.ax_graph.axhline(y=0, color='k', linestyle='-', alpha=0.3)
self.ax_graph.axvline(x=0, color='k', linestyle='-', alpha=0.3)
self.ax_graph.grid(True, alpha=0.3)
self.ax_graph.set_title('Quadratic Function')
# Display solutions
solution_text = f"Solutions: {solutions}"
self.ax_solution.text(0.5, 0.5, solution_text,
fontsize=12, ha='center')
self.ax_solution.axis('off')
plt.draw()
except ValueError as e:
self.ax_solution.text(0.5, 0.5, f"Error: {str(e)}",
fontsize=12, ha='center', color='red')
self.ax_solution.axis('off')
plt.draw()
def generate_training_data(num_samples=1000):
"""Generate training data for the models."""
linear_equations = []
quadratic_equations = []
for _ in range(num_samples):
# Generate linear equations
a = np.random.uniform(-10, 10)
b = np.random.uniform(-10, 10)
c = np.random.uniform(-10, 10)
linear_eq = f"{a}x + {b} = {c}"
solution = (c - b) / a
linear_equations.append((linear_eq, solution))
# Generate quadratic equations
a = np.random.uniform(-10, 10)
b = np.random.uniform(-10, 10)
c = np.random.uniform(-10, 10)
quadratic_eq = f"{a}x^2 + {b}x + {c} = 0"
# Calculate solutions using quadratic formula
discriminant = b**2 - 4*a*c
if discriminant >= 0:
x1 = (-b + np.sqrt(discriminant)) / (2*a)
x2 = (-b - np.sqrt(discriminant)) / (2*a)
quadratic_equations.append((quadratic_eq, (x1, x2)))
return linear_equations, quadratic_equations
def main():
# Generate training data
linear_data, quadratic_data = generate_training_data()
# Create and train solver
solver = MLMathSolver()
solver.train_models({'linear': linear_data, 'quadratic': quadratic_data})
# Create and display visualizer
visualizer = SolutionVisualizer()
visualizer.setup_figure()
plt.show()
if __name__ == "__main__":
main()