Skip to content

Commit 36a51f0

Browse files
committed
Adding score tracking + color labeling
1 parent 80ea273 commit 36a51f0

6 files changed

Lines changed: 38 additions & 5 deletions

File tree

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ OPENAI_API_KEY=
77

88
# Database URL
99
DATABASE_URL=
10+
11+
# Competition ID (update accordingly)
12+
COMPETITION_ID=IQC2025S2

brain/alpha_class.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import datetime
2+
import os
23
import sqlite3
34
import uuid
45
from dataclasses import asdict, dataclass, field, replace
56
from typing import Optional
67

8+
from brain.api import BrainAPI
9+
710

811
@dataclass
912
class Alpha:
@@ -39,6 +42,8 @@ class Alpha:
3942
failing_tests: Optional[list[str]] = field(default_factory=list)
4043
long_count: Optional[int] = None
4144
short_count: Optional[int] = None
45+
# Score change
46+
score: Optional[float] = None
4247

4348
# timestamp (let DB default if you don’t set it)
4449
created_at: Optional[str] = field(
@@ -120,6 +125,19 @@ def replace(self, **kwargs) -> "Alpha":
120125
"""
121126
return replace(self, **kwargs, alpha_id=str(uuid.uuid4()), is_temporary=True)
122127

128+
def update_score(self) -> float:
129+
"""Check and return the score of the Alpha."""
130+
results = BrainAPI.performance_comparison(
131+
self.alpha_id, competition=os.environ.get("COMPETITION_ID")
132+
)
133+
if "score" not in results or "after" not in results["score"]:
134+
return None
135+
136+
self.score = results["score"].get("after", 0) - results["score"].get(
137+
"before",
138+
)
139+
return self.score
140+
123141
@classmethod
124142
def create_alpha(
125143
cls,

brain/database.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ def __init__(self, db_url: str = None):
2222
self.conn.set_autocommit(True)
2323
self.cursor = self.conn.cursor()
2424

25-
def insert_alpha(self, alpha: Alpha) -> int:
26-
"""Insert a Alpha class instance into the table."""
25+
def upsert_alpha(self, alpha: Alpha) -> int:
26+
"""Insert a Alpha class instance into the table (update if id already exists)."""
2727
record = alpha.as_dict()
2828
cols = ", ".join(record)
2929
bangs = ", ".join("%s" for _ in record)
30-
sql = f"INSERT INTO alphas ({cols}) VALUES ({bangs})"
30+
update_cols = ", ".join(f"{k} = EXCLUDED.{k}" for k in record if k != "alpha_id")
31+
sql = (
32+
f"INSERT INTO alphas ({cols}) VALUES ({bangs}) ON CONFLICT (alpha_id)"
33+
f" DO UPDATE SET {update_cols}"
34+
)
3135
self.cursor.execute(sql, tuple(record.values()))
3236

3337
def find_by_alpha(

brain/genetic_algorithm.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ def _update_alphas_storage(
8686
return
8787

8888
alpha = Alpha.from_stats(stats)
89+
if alpha.fitness is not None and alpha.fitness > 1.0 and len(alpha.failing_tests) < 2:
90+
score = alpha.update_score()
91+
if score > 100 and len(alpha.failing_tests) == 0:
92+
BrainAPI.set_alpha_properties(alpha.alpha_id, color="YELLOW")
93+
8994
try:
90-
Database().insert_alpha(alpha)
95+
Database().upsert_alpha(alpha)
9196
except Exception as e:
9297
print(f"Error during database insertion: {e}")
9398
pass

brain/search_algorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import random
22

3-
from brain.agent import agent
43
from brain.agent_config import DEFAULT_CONFIG
4+
from brain.agents.executor import agent
55
from brain.alpha_storage import Storage
66
from brain.database import Database
77
from brain.genetic_algorithm import genetic_algorithm
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- add an integer "score" column (allows negative/positive values)
2+
ALTER TABLE alphas
3+
ADD COLUMN IF NOT EXISTS score REAL;

0 commit comments

Comments
 (0)