Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ class SimEngine:
|
|
| 16 |
self.nodes = {}
|
| 17 |
self.running = False
|
| 18 |
self.mode = 'inference'
|
| 19 |
-
self.problem_type = 'mult'
|
| 20 |
self.batch_queue = collections.deque()
|
| 21 |
self.logs = []
|
| 22 |
self.iteration = 0
|
|
@@ -24,11 +24,11 @@ class SimEngine:
|
|
| 24 |
self.reset()
|
| 25 |
|
| 26 |
def reset(self):
|
| 27 |
-
# A & B are
|
| 28 |
self.nodes = {
|
| 29 |
'A': {'x': 2.0, 'anchored': True, 'k': 1.0, 'f': 0.0},
|
| 30 |
'B': {'x': 3.0, 'anchored': True, 'k': 1.0, 'f': 0.0},
|
| 31 |
-
'C': {'x':
|
| 32 |
}
|
| 33 |
self.batch_queue.clear()
|
| 34 |
self.logs = []
|
|
@@ -44,26 +44,29 @@ class SimEngine:
|
|
| 44 |
self.nodes['B']['x'] = float(b)
|
| 45 |
|
| 46 |
if self.mode == 'training':
|
| 47 |
-
# In Training, everything is locked to learn the K-factor
|
| 48 |
self.nodes['C']['x'] = float(c_target)
|
| 49 |
self.nodes['C']['anchored'] = True
|
| 50 |
-
|
|
|
|
| 51 |
else:
|
| 52 |
-
# In Inference, C is free to drift to the predicted answer
|
| 53 |
self.nodes['C']['anchored'] = False
|
| 54 |
-
|
| 55 |
-
self.
|
| 56 |
-
self.add_log(f"INFER: {a} * {b} = ?")
|
| 57 |
|
| 58 |
def physics_step(self):
|
| 59 |
na, nb, nc = self.nodes['A'], self.nodes['B'], self.nodes['C']
|
| 60 |
|
| 61 |
-
# Calculate
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
self.current_error = prediction - nc['x']
|
| 66 |
|
|
|
|
| 67 |
if abs(self.current_error) < 0.01:
|
| 68 |
if self.batch_queue:
|
| 69 |
p = self.batch_queue.popleft()
|
|
@@ -75,18 +78,17 @@ class SimEngine:
|
|
| 75 |
return False
|
| 76 |
|
| 77 |
if self.mode == 'inference':
|
| 78 |
-
#
|
| 79 |
-
# If error is positive (pred > C), we must increase C
|
| 80 |
move = self.current_error * 0.1
|
| 81 |
nc['x'] += move
|
| 82 |
nc['f'] = move
|
| 83 |
|
| 84 |
elif self.mode == 'training':
|
| 85 |
-
#
|
| 86 |
-
#
|
| 87 |
-
target_k = nc['x'] / (
|
| 88 |
k_error = target_k - nc['k']
|
| 89 |
-
nc['k'] += k_error * 0.05 # Learning
|
| 90 |
nc['f'] = k_error
|
| 91 |
|
| 92 |
self.iteration += 1
|
|
@@ -97,7 +99,7 @@ engine = SimEngine()
|
|
| 97 |
def run_loop():
|
| 98 |
while True:
|
| 99 |
if engine.running: engine.physics_step()
|
| 100 |
-
time.sleep(0.
|
| 101 |
|
| 102 |
threading.Thread(target=run_loop, daemon=True).start()
|
| 103 |
|
|
@@ -106,21 +108,25 @@ async def get_ui(): return FileResponse("index.html")
|
|
| 106 |
|
| 107 |
@app.get("/state")
|
| 108 |
async def get_state():
|
| 109 |
-
return {'nodes': engine.nodes, 'error': engine.current_error, 'iter': engine.iteration, 'logs': engine.logs}
|
| 110 |
|
| 111 |
@app.post("/config")
|
| 112 |
async def config(data: dict):
|
| 113 |
engine.mode = data['mode']
|
|
|
|
| 114 |
engine.running = False
|
|
|
|
| 115 |
return {"ok": True}
|
| 116 |
|
| 117 |
-
@app.post("/
|
| 118 |
-
async def
|
| 119 |
engine.batch_queue.clear()
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
p = engine.batch_queue.popleft()
|
| 125 |
engine.set_problem(p['a'], p['b'], p['c'])
|
| 126 |
engine.running = True
|
|
|
|
| 16 |
self.nodes = {}
|
| 17 |
self.running = False
|
| 18 |
self.mode = 'inference'
|
| 19 |
+
self.problem_type = 'add' # Restored: 'add' or 'mult'
|
| 20 |
self.batch_queue = collections.deque()
|
| 21 |
self.logs = []
|
| 22 |
self.iteration = 0
|
|
|
|
| 24 |
self.reset()
|
| 25 |
|
| 26 |
def reset(self):
|
| 27 |
+
# A & B are Inputs, C is Output
|
| 28 |
self.nodes = {
|
| 29 |
'A': {'x': 2.0, 'anchored': True, 'k': 1.0, 'f': 0.0},
|
| 30 |
'B': {'x': 3.0, 'anchored': True, 'k': 1.0, 'f': 0.0},
|
| 31 |
+
'C': {'x': 5.0, 'anchored': False, 'k': 1.0, 'f': 0.0}
|
| 32 |
}
|
| 33 |
self.batch_queue.clear()
|
| 34 |
self.logs = []
|
|
|
|
| 44 |
self.nodes['B']['x'] = float(b)
|
| 45 |
|
| 46 |
if self.mode == 'training':
|
|
|
|
| 47 |
self.nodes['C']['x'] = float(c_target)
|
| 48 |
self.nodes['C']['anchored'] = True
|
| 49 |
+
symbol = "+" if self.problem_type == 'add' else "*"
|
| 50 |
+
self.add_log(f"TRAIN: {a} {symbol} {b} = {c_target}")
|
| 51 |
else:
|
|
|
|
| 52 |
self.nodes['C']['anchored'] = False
|
| 53 |
+
self.nodes['C']['x'] = random.uniform(-5, 5) # Random start for C
|
| 54 |
+
self.add_log(f"INFER: {a} {self.problem_type} {b} = ?")
|
|
|
|
| 55 |
|
| 56 |
def physics_step(self):
|
| 57 |
na, nb, nc = self.nodes['A'], self.nodes['B'], self.nodes['C']
|
| 58 |
|
| 59 |
+
# 1. Calculate Base Logic Value
|
| 60 |
+
if self.problem_type == 'add':
|
| 61 |
+
base_val = na['x'] + nb['x']
|
| 62 |
+
else:
|
| 63 |
+
base_val = na['x'] * nb['x']
|
| 64 |
+
|
| 65 |
+
# 2. Apply Learned Stiffness (K) to get Prediction
|
| 66 |
+
prediction = base_val * nc['k']
|
| 67 |
self.current_error = prediction - nc['x']
|
| 68 |
|
| 69 |
+
# Convergence Check
|
| 70 |
if abs(self.current_error) < 0.01:
|
| 71 |
if self.batch_queue:
|
| 72 |
p = self.batch_queue.popleft()
|
|
|
|
| 78 |
return False
|
| 79 |
|
| 80 |
if self.mode == 'inference':
|
| 81 |
+
# Solve for C: Drift C to meet the prediction
|
|
|
|
| 82 |
move = self.current_error * 0.1
|
| 83 |
nc['x'] += move
|
| 84 |
nc['f'] = move
|
| 85 |
|
| 86 |
elif self.mode == 'training':
|
| 87 |
+
# Solve for K: Adapt internal stiffness to match clamped C
|
| 88 |
+
# Target K is the ratio needed to make base_val * K = nc['x']
|
| 89 |
+
target_k = nc['x'] / (base_val + 1e-9)
|
| 90 |
k_error = target_k - nc['k']
|
| 91 |
+
nc['k'] += k_error * 0.05 # Learning rate
|
| 92 |
nc['f'] = k_error
|
| 93 |
|
| 94 |
self.iteration += 1
|
|
|
|
| 99 |
def run_loop():
|
| 100 |
while True:
|
| 101 |
if engine.running: engine.physics_step()
|
| 102 |
+
time.sleep(0.04)
|
| 103 |
|
| 104 |
threading.Thread(target=run_loop, daemon=True).start()
|
| 105 |
|
|
|
|
| 108 |
|
| 109 |
@app.get("/state")
|
| 110 |
async def get_state():
|
| 111 |
+
return {'nodes': engine.nodes, 'error': engine.current_error, 'iter': engine.iteration, 'logs': engine.logs, 'type': engine.problem_type}
|
| 112 |
|
| 113 |
@app.post("/config")
|
| 114 |
async def config(data: dict):
|
| 115 |
engine.mode = data['mode']
|
| 116 |
+
engine.problem_type = data['type']
|
| 117 |
engine.running = False
|
| 118 |
+
engine.add_log(f"Config: {engine.mode} | Logic: {engine.problem_type}")
|
| 119 |
return {"ok": True}
|
| 120 |
|
| 121 |
+
@app.post("/generate_batch")
|
| 122 |
+
async def gen_batch(data: dict):
|
| 123 |
engine.batch_queue.clear()
|
| 124 |
+
count = int(data['count'])
|
| 125 |
+
for _ in range(count):
|
| 126 |
+
a = random.randint(1, 15)
|
| 127 |
+
b = random.randint(1, 15)
|
| 128 |
+
c = (a + b) if engine.problem_type == 'add' else (a * b)
|
| 129 |
+
engine.batch_queue.append({'a': a, 'b': b, 'c': c})
|
| 130 |
p = engine.batch_queue.popleft()
|
| 131 |
engine.set_problem(p['a'], p['b'], p['c'])
|
| 132 |
engine.running = True
|