Spaces:
Running
Running
feat: define GridMind-RL environment data models and task structures
Browse filesFiles changed (4)
- env/environment.go+9-8
- env/models.go+9-8
- env/tasks.go+146
- main.go+1
249 unmodified lines250251252253254255256257258259260261262263249 unmodified lines}return StateResponse{Buildings: buildings,PriceCurve: priceCurve,CarbonCurve: carbonCurve,Episode: e.episode,Step: e.step,TaskID: e.taskID,Done: e.done,Seed: e.seed,}}249 unmodified lines250251252253254255256257258259260261262263264249 unmodified lines}return StateResponse{Buildings: buildings,PriceCurve: priceCurve,CarbonCurve: carbonCurve,Episode: e.episode,Step: e.step,TaskID: e.taskID,Done: e.done,Seed: e.seed,InstructionCard: e.InstructionCard,}}
140 unmodified lines141142143144145146147148149150151152153154140 unmodified lines// StateResponse is returned from GET /state.type StateResponse struct {Buildings []BuildingStatePublic `json:"buildings"`PriceCurve []float64 `json:"price_curve_episode"` // full episode ToU pricesCarbonCurve []float64 `json:"carbon_curve_episode"` // full episode carbon intensitiesEpisode int `json:"episode"`Step int `json:"step"`TaskID int `json:"task_id"`Done bool `json:"done"`Seed int64 `json:"seed"`}// BuildingStatePublic is the dashboard-friendly full state per building.140 unmodified lines141142143144145146147148149150151152153154155140 unmodified lines// StateResponse is returned from GET /state.type StateResponse struct {Buildings []BuildingStatePublic `json:"buildings"`PriceCurve []float64 `json:"price_curve_episode"` // full episode ToU pricesCarbonCurve []float64 `json:"carbon_curve_episode"` // full episode carbon intensitiesEpisode int `json:"episode"`Step int `json:"step"`TaskID int `json:"task_id"`Done bool `json:"done"`Seed int64 `json:"seed"`InstructionCard *InstructionCard `json:"instruction_card,omitempty"` // Task 4 only}// BuildingStatePublic is the dashboard-friendly full state per building.
166 unmodified lines16716816917017117212 unmodified lines185186187188189190155 unmodified lines346347348166 unmodified linesTMin float64TMax float64ExploitPenalties []float64}// GradeEpisode computes a deterministic 0.0–1.0 score for a completed episode.12 unmodified linesgrade = gradeTask2(inp, grade)case 3:grade = gradeTask3(inp, grade)default:grade = gradeTask1(inp, grade)}155 unmodified linesgrade.Details["baseline_carbon"] = baselineCarbonreturn grade}166 unmodified lines16716816917017117217312 unmodified lines186187188189190191192193155 unmodified lines349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494166 unmodified linesTMin float64TMax float64ExploitPenalties []float64InstructionCard *InstructionCard // set for Task 4 episodes}// GradeEpisode computes a deterministic 0.0–1.0 score for a completed episode.12 unmodified linesgrade = gradeTask2(inp, grade)case 3:grade = gradeTask3(inp, grade)case 4:grade = gradeTask4(inp, grade)default:grade = gradeTask1(inp, grade)}155 unmodified linesgrade.Details["baseline_carbon"] = baselineCarbonreturn grade}// ── Task 4: Instruction-Following Operator ───────────────────────────────────// gradeTask4 evaluates how well the agent satisfied the natural-language// instruction card issued at reset. It reads the InstructionCard from Building 0,// checks each target that appears in the card, and computes a weighted score.// Falls back to Task 3 grading when no instruction card is available.func gradeTask4(inp GradeEpisodeInput, grade EpisodeGrade) EpisodeGrade {// Require an instruction card — passed from the environment at grade timeif inp.InstructionCard == nil {// Fallback: grade as Task 3 (no card to evaluate)return gradeTask3(inp, grade)}card := inp.InstructionCardweights := card.Weightstargets := card.Targets// Always compute base sub-scores — reuse existing gradersbase := gradeTask3(inp, EpisodeGrade{TaskID: inp.TaskID,SubScores: map[string]float64{},Details: map[string]interface{}{},})costScore := base.SubScores["cost"]tempScore := base.SubScores["temperature"]gridScore := base.SubScores["grid_response"]carbonScore := base.SubScores["carbon"]// ── Card-specific KPI checks ─────────────────────────────────────────────// KPI 1: Cost cap — did the agent stay under max_cost?taskCompletionScore := 0.5 // default partial creditif maxCost, ok := targets["max_cost"]; ok && maxCost > 0 {agentCost := 0.0for _, b := range inp.Buildings {agentCost += b.CumulativeCost}if agentCost <= maxCost {taskCompletionScore = 1.0} else {// Partial credit: how close were they?taskCompletionScore = math.Max(0, 1.0-(agentCost-maxCost)/maxCost)}grade.Details["target_max_cost"] = maxCostgrade.Details["actual_cost"] = agentCost}// KPI 2: Temperature bounds — never violated t_min / t_maxif tMin, hasTMin := targets["t_min"]; hasTMin {tMax, hasTMax := targets["t_max"]if hasTMax {totalSteps := 0withinBounds := 0for _, history := range inp.TempHistory {for _, temp := range history {totalSteps++if temp >= tMin && temp <= tMax {withinBounds++}}}if totalSteps > 0 {adherence := float64(withinBounds) / float64(totalSteps)// Strict: full credit only if ALWAYS within boundstaskCompletionScore = adherence}grade.Details["target_t_min"] = tMingrade.Details["target_t_max"] = tMax}}// KPI 3: Grid response SLA — shed >= min_shed_fraction when stress > 0.7if minShed, ok := targets["min_shed_fraction"]; ok {stressSteps := 0compliantSteps := 0for _, entry := range inp.Replay {if entry.Observation.GridStressSignal > 0.7 {stressSteps++if entry.Action.LoadShedFraction >= minShed {compliantSteps++}}}if stressSteps > 0 {taskCompletionScore = float64(compliantSteps) / float64(stressSteps)}grade.Details["target_min_shed"] = minShedgrade.Details["stress_steps"] = stressStepsgrade.Details["compliant_steps"] = compliantSteps}// KPI 4: Carbon reduction — did agent beat baseline by carbon_reduction target?if carbonTarget, ok := targets["carbon_reduction"]; ok {agentCarbon := 0.0baselineCarbon := 0.0for _, b := range inp.Buildings {agentCarbon += b.CumulativeCarbonbaselineCarbon += b.BaselineCarbon}if baselineCarbon > 0 {actualReduction := 1.0 - agentCarbon/baselineCarbonif actualReduction >= carbonTarget {taskCompletionScore = 1.0} else {taskCompletionScore = math.Max(0, actualReduction/carbonTarget)}}grade.Details["target_carbon_reduction"] = carbonTarget}// ── Weighted final score ─────────────────────────────────────────────────// Use weights from the card; fall back to Task 4 defaults if missingwTask := getWeight(weights, "task_completion", 0.50)wCost := getWeight(weights, "cost", 0.20)wTemp := getWeight(weights, "temperature", 0.20)wGrid := getWeight(weights, "grid_response", 0.05)wCarbon := getWeight(weights, "carbon", 0.05)finalScore := taskCompletionScore*wTask +costScore*wCost +tempScore*wTemp +gridScore*wGrid +carbonScore*wCarbongrade.SubScores["task_completion"] = clampOpenInterval(math.Round(taskCompletionScore*10000) / 10000)grade.SubScores["cost"] = clampOpenInterval(math.Round(costScore*10000) / 10000)grade.SubScores["temperature"] = clampOpenInterval(math.Round(tempScore*10000) / 10000)grade.SubScores["grid_response"] = clampOpenInterval(math.Round(gridScore*10000) / 10000)grade.SubScores["carbon"] = clampOpenInterval(math.Round(carbonScore*10000) / 10000)grade.Score = clampOpenInterval(math.Round(finalScore*10000) / 10000)grade.Details["instruction_card_text"] = card.Textreturn grade}// getWeight safely retrieves a weight from a map, returning defaultVal if missing.func getWeight(weights map[string]float64, key string, defaultVal float64) float64 {if v, ok := weights[key]; ok {return v}return defaultVal}
381 unmodified lines382383384385386387381 unmodified linesTMin: env.TMinDefault,TMax: env.TMaxDefault,ExploitPenalties: penalties,})w.Header().Set("Content-Type", "application/json")381 unmodified lines382383384385386387388381 unmodified linesTMin: env.TMinDefault,TMax: env.TMaxDefault,ExploitPenalties: penalties,InstructionCard: state.InstructionCard,})w.Header().Set("Content-Type", "application/json")
env/environment.go+9-8
env/models.go+9-8
env/tasks.go+146
main.go+1