"""Exhaustive six-slot scheduling example; synthetic carbon intensities.""" import json,itertools SLUG="carbon-deadline-scheduling" def run(): forecast=[500,400,100,80,300,600] actual=[500,400,450,420,120,80] choices=list(itertools.combinations(range(6),3)) cost=lambda s,c:sum(c[i] for i in s) selected=min(choices,key=lambda s:(cost(s,forecast),s)) oracle=min(choices,key=lambda s:(cost(s,actual),s)) baseline=(0,1,2) eps=max(abs(a-b) for a,b in zip(forecast,actual)) regret=cost(selected,actual)-cost(oracle,actual) assert selected==(2,3,4) and oracle==(1,4,5) assert (cost(selected,actual),cost(oracle,actual),cost(baseline,actual))==(990,600,1350) assert regret<=6*eps # Perfect forecast optimization agrees with sorted3; exhaustive all120 permutations. cases=0 for c in itertools.permutations([1,2,3,4,5]): brute=min(sum(c[i] for i in s) for s in itertools.combinations(range(5),2)) assert brute==sum(sorted(c)[:2]);cases+=1 ds=list(range(3,7)) best=[min(cost(s,actual) for s in itertools.combinations(range(d),3)) for d in ds] assert all(a>=b for a,b in zip(best,best[1:])) return {"slug":SLUG,"checks":[{"name":"Forecast policy and hindsight optimum by exhaustive enumeration","passed":True,"cases":20,"detail":"All feasible 3-of-6 schedules; actual emissions 990 g versus oracle 600 g and immediate execution 1,350 g."},{"name":"Equal-energy sorting optimum cross-checked against enumeration","passed":True,"cases":cases,"detail":"Every permutation of five distinct costs, selecting two slots."},{"name":"Deadline relaxation and bounded-error regret","passed":True,"cases":4,"detail":"Larger feasible sets cannot worsen the offline optimum; regret 390 g <= 6 * 520 g."}],"figures":[{"id":"forecast","title":"The lowest forecast is not necessarily the lowest realized intensity","x_label":"One-hour slot (zero-based)","y_label":"Synthetic intensity (g CO2e/kWh)","series":[{"name":"Forecast","x":list(range(6)),"y":forecast},{"name":"Realized","x":list(range(6)),"y":actual}],"caption":"Each of three unit jobs consumes 1 kWh in one slot. At most one job fits per slot, all are available initially, and all share the final deadline. These are synthetic signals.","assumptions":["No idle, migration, start-up or embodied emissions in this narrow example","All jobs identical and independent"],"kind":"Constructed analytical model; not a measured benchmark"},{"id":"deadline","title":"Deadline slack expands the feasible set","x_label":"Number of allowed one-hour slots","y_label":"Hindsight minimum operating emissions (g CO2e)","series":[{"name":"Best 3-slot schedule","x":ds,"y":best}],"caption":"Exhaustive search with realized costs. This is an offline feasibility envelope, not achievable online savings.","assumptions":["Three unit jobs; capacity one each slot","One kWh per job; no switching overhead"],"kind":"Constructed analytical model; not a measured benchmark"}],"result_summary":{"forecast_choice":selected,"oracle_choice":oracle,"forecast_selected_actual_g":990,"oracle_g":600,"immediate_g":1350,"regret_g":regret,"max_signal_error":eps,"regret_upper_bound_g":6*eps,"deadline_optima":best}} if __name__=="__main__":print(json.dumps(run(),indent=2))