Spaces:
Sleeping
Sleeping
File size: 6,265 Bytes
ce3d6c7 | 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 | """
test_sequencing.py β Unit tests for the HardSequencingGrader and DP oracle.
Run: python test_sequencing.py
"""
import sys
sys.path.insert(0, ".")
from environment import HardSequencingGrader
def test_dp_oracle_simple():
"""Fixed scenario: 4 steps, known optimal is to bid on steps 0 and 2."""
g = HardSequencingGrader()
# Step 0: cheap (clears at $1), good CTR 0.10
g.record_step(step=0, context="Fitness", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
# Step 1: expensive (clears at $8), low CTR 0.02
g.record_step(step=1, context="Tech", clearing_price=8.0,
base_ctr=0.02, auction_won=False, cost=0.0,
conversion_value=15.0)
# Step 2: cheap (clears at $1), good CTR 0.10
g.record_step(step=2, context="Fashion", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
# Step 3: cheap (clears at $1), good CTR 0.08
g.record_step(step=3, context="Gaming", clearing_price=1.0,
base_ctr=0.08, auction_won=True, cost=1.0,
conversion_value=15.0)
oracle_val = g._oracle_conversions(budget=10.0)
assert oracle_val > 0, f"Oracle should find positive conversions, got {oracle_val}"
print(f" DP oracle conversions: {oracle_val:.4f} β")
score = g.episode_score(initial_budget=10.0)
assert 0.0 <= score <= 1.0, f"Score out of range: {score}"
print(f" Episode score: {score:.4f} β")
def test_never_bid_score_zero():
"""Agent never bids β score should be 0.0."""
g = HardSequencingGrader()
for i in range(24):
ctx = ["Fitness", "Tech", "Fashion", "Gaming"][i % 4]
g.record_step(step=i, context=ctx, clearing_price=1.0,
base_ctr=0.05, auction_won=False, cost=0.0,
conversion_value=15.0)
score = g.episode_score(initial_budget=100.0)
assert score == 0.0, f"Expected 0.0 for never-bid agent, got {score}"
print(f" Never-bid score: {score} β")
def test_diversity_multiplier():
"""Check 1.2Γ when β₯3 contexts, 1.0 otherwise."""
g = HardSequencingGrader()
# Only 2 contexts
g.record_step(step=0, context="Fitness", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
g.record_step(step=1, context="Tech", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
assert g._diversity_multiplier() == 1.0, "Should be 1.0 with only 2 contexts"
# Add 3rd context
g.record_step(step=2, context="Fashion", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
assert g._diversity_multiplier() == 1.20, "Should be 1.2 with 3 contexts"
print(f" Diversity multiplier: correct β")
def test_carryover_schedule():
"""Verify carry-over boosts decay correctly: +15%/+10%/+5%."""
g = HardSequencingGrader()
# Win at step 0, skip rest
g.record_step(step=0, context="Fitness", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
for i in range(1, 5):
g.record_step(step=i, context="Tech", clearing_price=1.0,
base_ctr=0.10, auction_won=False, cost=0.0,
conversion_value=15.0)
agent_conv = g._agent_conversions()
# Only step 0 won, no prior wins β no boost, base conversion = 0.10 * 15 = 1.5
expected = 0.10 * 15.0
assert abs(agent_conv - expected) < 0.001, \
f"Expected {expected}, got {agent_conv}"
print(f" Carry-over schedule: correct β")
# Now test with consecutive wins
g.reset()
g.record_step(step=0, context="Fitness", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
g.record_step(step=1, context="Tech", clearing_price=1.0,
base_ctr=0.10, auction_won=True, cost=1.0,
conversion_value=15.0)
agent_conv = g._agent_conversions()
# step 0: no boost β 0.10 * 15 = 1.5
# step 1: boost from step 0 win = +15% β 0.10 * 1.15 * 15 = 1.725
expected = 1.5 + 0.10 * 1.15 * 15.0
assert abs(agent_conv - expected) < 0.001, \
f"Expected {expected}, got {agent_conv}"
print(f" Consecutive carry-over: correct β")
def test_episode_score_bounded():
"""Score should always be in [0.0, 1.0]."""
g = HardSequencingGrader()
# Agent bids on everything (cheap auctions)
for i in range(24):
ctx = ["Fitness", "Tech", "Fashion", "Gaming"][i % 4]
g.record_step(step=i, context=ctx, clearing_price=0.50,
base_ctr=0.08, auction_won=True, cost=0.50,
conversion_value=15.0)
score = g.episode_score(initial_budget=100.0)
assert 0.0 <= score <= 1.0, f"Score out of bounds: {score}"
print(f" All-bid score: {score:.4f} (bounded) β")
def test_empty_episode():
"""Empty episode β score 0.0."""
g = HardSequencingGrader()
score = g.episode_score(initial_budget=100.0)
assert score == 0.0, f"Expected 0.0 for empty episode, got {score}"
print(f" Empty episode: {score} β")
if __name__ == "__main__":
tests = [
("DP Oracle (simple scenario)", test_dp_oracle_simple),
("Never-bid agent β 0.0", test_never_bid_score_zero),
("Diversity multiplier", test_diversity_multiplier),
("Carry-over schedule", test_carryover_schedule),
("Score bounded [0, 1]", test_episode_score_bounded),
("Empty episode", test_empty_episode),
]
print("=" * 52)
print(" HardSequencingGrader Unit Tests")
print("=" * 52)
passed = 0
for name, fn in tests:
try:
print(f"\nβΈ {name}")
fn()
passed += 1
except Exception as e:
print(f" β FAILED: {e}")
print(f"\n{'β'*52}")
print(f" {passed}/{len(tests)} tests passed")
print(f"{'β'*52}")
if passed < len(tests):
sys.exit(1)
|