Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- agents.py +35 -0
- app.py +26 -0
- emotion.py +6 -0
- requirements.txt +1 -0
- shared_memory.py +10 -0
agents.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import math
|
| 3 |
+
import operator
|
| 4 |
+
|
| 5 |
+
ops = [('+', operator.add), ('-', operator.sub), ('*', operator.mul), ('/', lambda a, b: a / b if b != 0 else 1)]
|
| 6 |
+
|
| 7 |
+
def random_expr():
|
| 8 |
+
return f"{random.randint(1, 10)} {random.choice(['+', '-', '*', '/'])} {random.randint(1, 10)}"
|
| 9 |
+
|
| 10 |
+
def eval_expr(expr):
|
| 11 |
+
try:
|
| 12 |
+
return eval(expr)
|
| 13 |
+
except:
|
| 14 |
+
return float('inf')
|
| 15 |
+
|
| 16 |
+
def mutate(expr):
|
| 17 |
+
parts = expr.split()
|
| 18 |
+
idx = random.randint(0, 2)
|
| 19 |
+
if idx % 2 == 0:
|
| 20 |
+
parts[idx] = str(random.randint(1, 10))
|
| 21 |
+
else:
|
| 22 |
+
parts[idx] = random.choice(['+', '-', '*', '/'])
|
| 23 |
+
return ' '.join(parts)
|
| 24 |
+
|
| 25 |
+
def evolve_agent(name, target, emotion_mod, memory):
|
| 26 |
+
population = [random_expr() for _ in range(5)]
|
| 27 |
+
scored = [(expr, -abs(eval_expr(expr) - target)) for expr in population]
|
| 28 |
+
best_expr, best_score = max(scored, key=lambda x: x[1])
|
| 29 |
+
for _ in range(3 + emotion_mod):
|
| 30 |
+
mutated = mutate(best_expr)
|
| 31 |
+
score = -abs(eval_expr(mutated) - target)
|
| 32 |
+
if score > best_score:
|
| 33 |
+
best_expr, best_score = mutated, score
|
| 34 |
+
memory.store(name, best_expr, best_score)
|
| 35 |
+
return {'agent': name, 'expression': best_expr, 'score': best_score}
|
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from agents import evolve_agent
|
| 3 |
+
from emotion import get_emotion_level
|
| 4 |
+
from shared_memory import MemoryStore
|
| 5 |
+
|
| 6 |
+
memory = MemoryStore()
|
| 7 |
+
|
| 8 |
+
def evolve_step(target_value: float, emotion_input: str):
|
| 9 |
+
emotion_mod = get_emotion_level(emotion_input)
|
| 10 |
+
result_a = evolve_agent('Agent A', target_value, emotion_mod, memory)
|
| 11 |
+
result_b = evolve_agent('Agent B', target_value, emotion_mod, memory)
|
| 12 |
+
best = max([result_a, result_b], key=lambda x: x['score'])
|
| 13 |
+
return f"Best result: {best['agent']} => Expression: {best['expression']} (Score: {best['score']})"
|
| 14 |
+
|
| 15 |
+
demo = gr.Interface(
|
| 16 |
+
fn=evolve_step,
|
| 17 |
+
inputs=[
|
| 18 |
+
gr.Number(label="Target Value"),
|
| 19 |
+
gr.Radio(["Calm", "Frustrated", "Curious"], label="User Emotion")
|
| 20 |
+
],
|
| 21 |
+
outputs="text",
|
| 22 |
+
title="Multi-Agent Evolution Sandbox",
|
| 23 |
+
description="Two agents evolve Python expressions to match a target number. Emotions influence their exploration."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
demo.launch()
|
emotion.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def get_emotion_level(emotion):
|
| 2 |
+
return {
|
| 3 |
+
"Calm": 1,
|
| 4 |
+
"Frustrated": 5,
|
| 5 |
+
"Curious": 3
|
| 6 |
+
}.get(emotion, 1)
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
gradio
|
shared_memory.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
class MemoryStore:
|
| 2 |
+
def __init__(self):
|
| 3 |
+
self.records = {}
|
| 4 |
+
|
| 5 |
+
def store(self, agent, expr, score):
|
| 6 |
+
if agent not in self.records or self.records[agent]['score'] < score:
|
| 7 |
+
self.records[agent] = {'expression': expr, 'score': score}
|
| 8 |
+
|
| 9 |
+
def get(self, agent):
|
| 10 |
+
return self.records.get(agent, None)
|