File size: 7,402 Bytes
8ddfad6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Example usage of the Document Classification OpenEnv Environment
Demonstrates various ways to interact with the environment
"""

import numpy as np
from environment import DocumentClassificationEnv
from grading import BaselineAgent, AgentGrader


def example_basic_usage():
    """Example 1: Basic environment usage"""
    print("=" * 70)
    print("Example 1: Basic Environment Usage")
    print("=" * 70)
    
    # Create environment
    env = DocumentClassificationEnv(task_difficulty="easy")
    print(f"✓ Created environment: {env.task_difficulty}")
    
    # Reset and get initial observation
    obs, info = env.reset()
    print(f"✓ Reset environment")
    print(f"  Initial document: {obs['document_id']}")
    print(f"  Content preview: {obs['content'][:60]}...")
    
    # Take a few steps
    total_reward = 0
    for step in range(5):
        action = env.action_space.sample()  # Random action
        obs, reward, done, truncated, info = env.step(action)
        total_reward += reward
        
        print(f"\nStep {step + 1}:")
        print(f"  Action: {env.CATEGORY_MAPS['easy'][action]}")
        print(f"  Reward: {reward:.3f}")
        print(f"  Episode Accuracy: {info['episode_accuracy']:.3f}")
        
        if done:
            print(f"  Episode Complete!")
            break
    
    print(f"\nTotal Reward: {total_reward:.3f}")


def example_state_inspection():
    """Example 2: Inspecting environment state"""
    print("\n" + "=" * 70)
    print("Example 2: Environment State Inspection")
    print("=" * 70)
    
    env = DocumentClassificationEnv(task_difficulty="medium")
    obs, _ = env.reset()
    
    # Take a few steps
    for _ in range(3):
        action = env.action_space.sample()
        obs, reward, done, _, info = env.step(action)
    
    # Get and inspect state
    state = env.state()
    print("Environment State:")
    print(f"  Current document index: {state['current_document_index']}")
    print(f"  Total documents: {state['total_documents']}")
    print(f"  Episode reward: {state['episode_reward_total']:.3f}")
    print(f"  Accuracy so far: {state['episode_accuracy']:.3f}")
    print(f"  Avg processing time: {state['average_processing_time_ms']:.2f}ms")
    
    print(f"\nCurrent Observation:")
    print(f"  Document ID: {state['current_observation']['document_id']}")
    print(f"  Word count: {state['current_observation']['word_count'][0]}")
    print(f"  Has urgency: {bool(state['current_observation']['has_urgency_markers'][0])}")


def example_baseline_agent():
    """Example 3: Using the baseline agent"""
    print("\n" + "=" * 70)
    print("Example 3: Baseline Agent Performance")
    print("=" * 70)
    
    for difficulty in ["easy", "medium", "hard"]:
        print(f"\n{difficulty.upper()} Task:")
        
        env = DocumentClassificationEnv(task_difficulty=difficulty)
        agent = BaselineAgent(difficulty)
        
        obs, _ = env.reset()
        
        total_reward = 0
        correct = 0
        total = 0
        
        for step in range(20):  # Run 20 steps
            action = agent.decide(obs)
            obs, reward, done, truncated, info = env.step(action)
            
            total_reward += reward
            if info['is_correct']:
                correct += 1
            total += 1
            
            if done:
                break
        
        accuracy = correct / total if total > 0 else 0
        print(f"  Accuracy: {accuracy:.3f} ({correct}/{total})")
        print(f"  Total Reward: {total_reward:.3f}")
        print(f"  Avg Reward: {total_reward / total:.3f}")


def example_agent_grading():
    """Example 4: Grading an agent"""
    print("\n" + "=" * 70)
    print("Example 4: Agent Grading System")
    print("=" * 70)
    
    difficulty = "easy"
    
    # Create grader
    grader = AgentGrader(difficulty)
    
    # Create a simple custom agent
    def custom_agent(obs):
        """A simple custom agent that prefers 'Support' category"""
        # 70% of the time, choose Support (index 2 in easy)
        if np.random.random() < 0.7:
            return 2  # Support
        else:
            return np.random.randint(0, 5)  # Random action
    
    # Grade the custom agent
    score, metrics = grader.grade_agent(custom_agent, verbose=True)
    
    print(f"Final Score: {score:.4f}")


def example_difficulty_comparison():
    """Example 5: Comparing difficulty levels"""
    print("\n" + "=" * 70)
    print("Example 5: Difficulty Level Comparison")
    print("=" * 70)
    
    difficulties = ["easy", "medium", "hard"]
    
    print(f"\n{'Difficulty':<10} {'Categories':<12} {'Documents':<12} {'Time Limit':<15} {'Features':<10}")
    print("-" * 60)
    
    for diff in difficulties:
        env = DocumentClassificationEnv(task_difficulty=diff)
        config = env.task_config
        
        time_limit = config.get("time_limit", "None")
        time_str = f"{time_limit}s" if time_limit else "None"
        
        print(f"{diff:<10} {config['num_categories']:<12} {config['num_documents']:<12} {time_str:<15} {config['feature_dim']:<10}")


def example_seed_reproducibility():
    """Example 6: Demonstrating reproducibility with seeds"""
    print("\n" + "=" * 70)
    print("Example 6: Seed-based Reproducibility")
    print("=" * 70)
    
    # Create two environments with the same seed
    env1 = DocumentClassificationEnv(task_difficulty="easy", seed=42)
    env2 = DocumentClassificationEnv(task_difficulty="easy", seed=42)
    
    obs1, _ = env1.reset(seed=42)
    obs2, _ = env2.reset(seed=42)
    
    print("Comparing two environments with seed=42:")
    print(f"  Env1 doc ID: {obs1['document_id']}")
    print(f"  Env2 doc ID: {obs2['document_id']}")
    print(f"  Match: {obs1['document_id'] == obs2['document_id']}")
    
    print(f"\n  Env1 content: {obs1['content'][:40]}...")
    print(f"  Env2 content: {obs2['content'][:40]}...")
    print(f"  Match: {obs1['content'] == obs2['content']}")
    
    print(f"\n  Feature vectors equal: {np.allclose(obs1['features'], obs2['features'])}")


def example_episode_summary():
    """Example 7: Getting episode summary"""
    print("\n" + "=" * 70)
    print("Example 7: Episode Summary")
    print("=" * 70)
    
    env = DocumentClassificationEnv(task_difficulty="easy")
    obs, _ = env.reset()
    
    # Run a full episode
    done = False
    while not done:
        action = env.action_space.sample()
        obs, reward, done, truncated, info = env.step(action)
    
    # Get summary from info
    summary = info.get("episode_summary", {})
    
    print("Episode Summary:")
    for key, value in summary.items():
        if isinstance(value, float):
            print(f"  {key}: {value:.4f}")
        else:
            print(f"  {key}: {value}")


def main():
    """Run all examples"""
    try:
        example_basic_usage()
        example_state_inspection()
        example_baseline_agent()
        example_agent_grading()
        example_difficulty_comparison()
        example_seed_reproducibility()
        example_episode_summary()
        
        print("\n" + "=" * 70)
        print("✓ All examples completed successfully!")
        print("=" * 70)
        
    except Exception as e:
        print(f"\n✗ Error running examples: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()