Parv Pareek commited on
Commit
d342897
·
1 Parent(s): 6c66cc1
app.py CHANGED
@@ -1,15 +1,22 @@
1
- from fastapi import FastAPI
 
2
  from env.core import CacheEnv
3
 
4
  app = FastAPI()
5
  env = CacheEnv()
6
 
 
 
 
 
 
 
7
  @app.post("/reset")
8
- def reset():
9
- state = env.reset()
10
  return {
11
  "state": state,
12
- "task_id": state.get("task_id")
13
  }
14
  @app.post("/step")
15
  def step(action: dict):
 
1
+ from fastapi import Body, FastAPI
2
+ from pydantic import BaseModel, ConfigDict
3
  from env.core import CacheEnv
4
 
5
  app = FastAPI()
6
  env = CacheEnv()
7
 
8
+
9
+ class ResetBody(BaseModel):
10
+ model_config = ConfigDict(extra="ignore")
11
+ task_id: str | None = None
12
+
13
+
14
  @app.post("/reset")
15
+ def reset(body: ResetBody = Body(default_factory=ResetBody)):
16
+ state = env.reset(task_id=body.task_id)
17
  return {
18
  "state": state,
19
+ "task_id": state.get("task_id"),
20
  }
21
  @app.post("/step")
22
  def step(action: dict):
cache_invalidation_env.egg-info/PKG-INFO ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Metadata-Version: 2.4
2
+ Name: cache-invalidation-env
3
+ Version: 0.1.0
4
+ Summary: Cache invalidation decision environment for OpenEnv
5
+ Requires-Python: >=3.10
6
+ Requires-Dist: openenv-core[core]>=0.2.2
7
+ Requires-Dist: fastapi>=0.100.0
8
+ Requires-Dist: uvicorn[standard]>=0.22.0
9
+ Requires-Dist: pydantic>=2.0.0
10
+ Requires-Dist: requests>=2.28.0
11
+ Requires-Dist: openai>=1.0.0
12
+ Requires-Dist: python-dotenv>=1.0.0
cache_invalidation_env.egg-info/SOURCES.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ README.md
2
+ pyproject.toml
3
+ cache_invalidation_env.egg-info/PKG-INFO
4
+ cache_invalidation_env.egg-info/SOURCES.txt
5
+ cache_invalidation_env.egg-info/dependency_links.txt
6
+ cache_invalidation_env.egg-info/entry_points.txt
7
+ cache_invalidation_env.egg-info/requires.txt
8
+ cache_invalidation_env.egg-info/top_level.txt
9
+ env/__init__.py
10
+ env/core.py
11
+ env/generator.py
12
+ env/grader.py
13
+ env/models.py
14
+ env/tasks.py
15
+ server/__init__.py
16
+ server/app.py
cache_invalidation_env.egg-info/dependency_links.txt ADDED
@@ -0,0 +1 @@
 
 
1
+
cache_invalidation_env.egg-info/entry_points.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [console_scripts]
2
+ server = server.app:main
cache_invalidation_env.egg-info/requires.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ openenv-core[core]>=0.2.2
2
+ fastapi>=0.100.0
3
+ uvicorn[standard]>=0.22.0
4
+ pydantic>=2.0.0
5
+ requests>=2.28.0
6
+ openai>=1.0.0
7
+ python-dotenv>=1.0.0
cache_invalidation_env.egg-info/top_level.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ env
2
+ server
env/core.py CHANGED
@@ -2,16 +2,17 @@ import random
2
  from env.generator import generate_env
3
  from env.grader import compute_step_reward
4
  from env.tasks import sample_task
5
- from env.grader import normalize_episode_score
6
-
7
  class CacheEnv:
8
 
9
  def __init__(self):
10
  self.reset()
11
 
12
- def reset(self):
13
  self.history = []
14
- self.task_id = sample_task()
 
 
 
15
  items, hidden, current_time = generate_env(self.task_id)
16
 
17
  self.state = {
 
2
  from env.generator import generate_env
3
  from env.grader import compute_step_reward
4
  from env.tasks import sample_task
 
 
5
  class CacheEnv:
6
 
7
  def __init__(self):
8
  self.reset()
9
 
10
+ def reset(self, task_id=None):
11
  self.history = []
12
+ if task_id in ("easy", "medium", "hard"):
13
+ self.task_id = task_id
14
+ else:
15
+ self.task_id = sample_task()
16
  items, hidden, current_time = generate_env(self.task_id)
17
 
18
  self.state = {
env/grader.py CHANGED
@@ -1,3 +1,11 @@
 
 
 
 
 
 
 
 
1
  def compute_step_reward(action_type, is_stale):
2
  reward = 0
3
 
@@ -15,7 +23,7 @@ def compute_step_reward(action_type, is_stale):
15
  def normalize_episode_score(total_reward, max_steps=10):
16
  # expected max ≈ 1.0 per step
17
  score = total_reward / max_steps
18
- return max(0.0, min(1.0, score))
19
 
20
 
21
 
@@ -31,7 +39,7 @@ def evaluate_episode(history):
31
  total_steps = len(history)
32
 
33
  if total_steps == 0:
34
- return 0.0
35
 
36
  correct_decisions = 0
37
  unnecessary_invalidations = 0
@@ -72,4 +80,4 @@ def evaluate_episode(history):
72
  0.2 * stability
73
  )
74
 
75
- return max(0.0, min(1.0, score))
 
1
+ # Submission validators require final scores strictly in (0, 1), not at the endpoints.
2
+ _SCORE_EPS = 1e-4
3
+
4
+
5
+ def clamp_strict_unit_interval(x: float) -> float:
6
+ return float(min(1.0 - _SCORE_EPS, max(_SCORE_EPS, x)))
7
+
8
+
9
  def compute_step_reward(action_type, is_stale):
10
  reward = 0
11
 
 
23
  def normalize_episode_score(total_reward, max_steps=10):
24
  # expected max ≈ 1.0 per step
25
  score = total_reward / max_steps
26
+ return clamp_strict_unit_interval(max(0.0, min(1.0, score)))
27
 
28
 
29
 
 
39
  total_steps = len(history)
40
 
41
  if total_steps == 0:
42
+ return clamp_strict_unit_interval(0.0)
43
 
44
  correct_decisions = 0
45
  unnecessary_invalidations = 0
 
80
  0.2 * stability
81
  )
82
 
83
+ return clamp_strict_unit_interval(max(0.0, min(1.0, score)))
inference.py CHANGED
@@ -8,6 +8,8 @@ from typing import List, Optional
8
  import requests
9
  from openai import OpenAI
10
 
 
 
11
  # Load .env from repo root so HF_TOKEN / API_BASE_URL work when you run: python inference.py
12
  try:
13
  from dotenv import load_dotenv
@@ -233,6 +235,7 @@ def run() -> None:
233
  success = False
234
  print(f"[RUN] fatal: {exc}", file=sys.stderr)
235
  finally:
 
236
  log_end(
237
  success=success,
238
  steps=steps_taken,
 
8
  import requests
9
  from openai import OpenAI
10
 
11
+ from env.grader import clamp_strict_unit_interval
12
+
13
  # Load .env from repo root so HF_TOKEN / API_BASE_URL work when you run: python inference.py
14
  try:
15
  from dotenv import load_dotenv
 
235
  success = False
236
  print(f"[RUN] fatal: {exc}", file=sys.stderr)
237
  finally:
238
+ episode_score = clamp_strict_unit_interval(episode_score)
239
  log_end(
240
  success=success,
241
  steps=steps_taken,
openenv.yaml CHANGED
@@ -1,6 +1,11 @@
1
  name: cache_invalidation_env
2
  description: Decision-making environment for cache invalidation under uncertainty
3
 
 
 
 
 
 
4
  actions:
5
  type: object
6
  properties:
 
1
  name: cache_invalidation_env
2
  description: Decision-making environment for cache invalidation under uncertainty
3
 
4
+ tasks:
5
+ - easy
6
+ - medium
7
+ - hard
8
+
9
  actions:
10
  type: object
11
  properties: