Spaces:
Sleeping
Sleeping
[TEST] Add stress tests and edge case coverage for optimisation engine
Browse files- tests/test_optimization_stress.py +1528 -0
tests/test_optimization_stress.py
ADDED
|
@@ -0,0 +1,1528 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Stress test & edge case coverage for optimisation/optimisation.py
|
| 3 |
+
|
| 4 |
+
Run with:
|
| 5 |
+
python tests/test_optimisation_stress.py
|
| 6 |
+
"""
|
| 7 |
+
import sys, traceback, json
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
sys.path.insert(0, str(Path(__file__).parent.parent))
|
| 11 |
+
from optimisation.optimisation import run_optimisation
|
| 12 |
+
|
| 13 |
+
# ββ Helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
|
| 15 |
+
PASS = "\033[92mPASS\033[0m"
|
| 16 |
+
FAIL = "\033[91mFAIL\033[0m"
|
| 17 |
+
WARN = "\033[93mWARN\033[0m"
|
| 18 |
+
|
| 19 |
+
results = []
|
| 20 |
+
|
| 21 |
+
def run_case(name, expected_success, **kwargs):
|
| 22 |
+
try:
|
| 23 |
+
ok, res = run_optimisation(**kwargs)
|
| 24 |
+
if ok:
|
| 25 |
+
total = res.get("total_removed", 0)
|
| 26 |
+
gain = res.get("resource_actual_gain", {})
|
| 27 |
+
if expected_success:
|
| 28 |
+
status = PASS
|
| 29 |
+
detail = f"total={total:.4f} gain_keys={list(gain.keys())[:3]}"
|
| 30 |
+
else:
|
| 31 |
+
status = WARN
|
| 32 |
+
detail = f"Expected failure but got success β total={total:.4f}"
|
| 33 |
+
else:
|
| 34 |
+
msg = res.get("message", "")
|
| 35 |
+
if not expected_success:
|
| 36 |
+
status = PASS
|
| 37 |
+
detail = f"Correctly failed: {msg[:80]}"
|
| 38 |
+
else:
|
| 39 |
+
status = FAIL
|
| 40 |
+
detail = f"Unexpected failure: {msg[:80]}"
|
| 41 |
+
print(f" [{status}] {name}\n {detail}")
|
| 42 |
+
results.append((name, status, detail))
|
| 43 |
+
except Exception as e:
|
| 44 |
+
tb = traceback.format_exc().strip().split("\n")[-1]
|
| 45 |
+
print(f" [{FAIL}] {name}\n EXCEPTION: {tb}")
|
| 46 |
+
results.append((name, FAIL, f"EXCEPTION: {tb}"))
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# ββ Realistic base fixtures ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 50 |
+
|
| 51 |
+
CAPS = {
|
| 52 |
+
"Land": 100.0,
|
| 53 |
+
"Water": 50.0,
|
| 54 |
+
"Rock": 80.0,
|
| 55 |
+
"Energy": 200.0,
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
COSTS = {
|
| 59 |
+
"Afforestation": {"Land": 0.5, "Water": 0.2},
|
| 60 |
+
"Agroforestry": {"Land": 0.3, "Water": 0.1},
|
| 61 |
+
"Enhanced Weathering": {"Rock": 0.4, "Energy": 0.3},
|
| 62 |
+
"BioCCS": {"Energy": 0.6, "Water": 0.05},
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
def base_constraints(cap_type="percent", cap_value=100):
|
| 66 |
+
return {m: {"active": True, "cap_type": cap_type, "cap_value": cap_value} for m in COSTS}
|
| 67 |
+
|
| 68 |
+
def inactive_constraints():
|
| 69 |
+
return {m: {"active": False, "cap_type": "percent", "cap_value": 100} for m in COSTS}
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
+
# SECTION 1 β Baseline & sanity
|
| 74 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
+
print("\nββ SECTION 1 Β· Baseline & sanity βββββββββββββββββββββββββββββββββββββββββ")
|
| 76 |
+
|
| 77 |
+
run_case("1.1 Normal run",
|
| 78 |
+
expected_success=True,
|
| 79 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS)
|
| 80 |
+
|
| 81 |
+
run_case("1.2 Single method, single resource",
|
| 82 |
+
expected_success=True,
|
| 83 |
+
resource_caps={"Land": 100.0},
|
| 84 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 85 |
+
method_costs={"Afforestation": {"Land": 0.5}})
|
| 86 |
+
|
| 87 |
+
run_case("1.3 All methods at 100% absolute cap",
|
| 88 |
+
expected_success=True,
|
| 89 |
+
resource_caps=CAPS, method_costs=COSTS,
|
| 90 |
+
method_constraints=base_constraints("absolute", 1000.0))
|
| 91 |
+
|
| 92 |
+
run_case("1.4 Very large caps (numerical scale)",
|
| 93 |
+
expected_success=True,
|
| 94 |
+
resource_caps={k: v * 1e9 for k, v in CAPS.items()},
|
| 95 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 96 |
+
|
| 97 |
+
run_case("1.5 Very small caps (near-zero precision)",
|
| 98 |
+
expected_success=True,
|
| 99 |
+
resource_caps={k: v * 1e-6 for k, v in CAPS.items()},
|
| 100 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 101 |
+
|
| 102 |
+
run_case("1.6 All resources = 0",
|
| 103 |
+
expected_success=True,
|
| 104 |
+
resource_caps={k: 0.0 for k in CAPS},
|
| 105 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 109 |
+
# SECTION 2 β Method constraint edge cases
|
| 110 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 111 |
+
print("\nββ SECTION 2 Β· Method constraints ββββββββββββββββββββββββββββββββββββββββ")
|
| 112 |
+
|
| 113 |
+
run_case("2.1 All methods inactive",
|
| 114 |
+
expected_success=False,
|
| 115 |
+
resource_caps=CAPS, method_constraints=inactive_constraints(), method_costs=COSTS)
|
| 116 |
+
|
| 117 |
+
run_case("2.2 One method active, others inactive",
|
| 118 |
+
expected_success=True,
|
| 119 |
+
resource_caps=CAPS,
|
| 120 |
+
method_constraints={
|
| 121 |
+
"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 122 |
+
"Agroforestry": {"active": False, "cap_type": "percent", "cap_value": 100},
|
| 123 |
+
"Enhanced Weathering": {"active": False, "cap_type": "percent", "cap_value": 100},
|
| 124 |
+
"BioCCS": {"active": False, "cap_type": "percent", "cap_value": 100},
|
| 125 |
+
}, method_costs=COSTS)
|
| 126 |
+
|
| 127 |
+
run_case("2.3 Percent cap = 0 on all methods",
|
| 128 |
+
expected_success=True,
|
| 129 |
+
resource_caps=CAPS,
|
| 130 |
+
method_constraints=base_constraints("percent", 0),
|
| 131 |
+
method_costs=COSTS)
|
| 132 |
+
|
| 133 |
+
run_case("2.4 Absolute cap = 0 on all methods",
|
| 134 |
+
expected_success=True,
|
| 135 |
+
resource_caps=CAPS,
|
| 136 |
+
method_constraints=base_constraints("absolute", 0.0),
|
| 137 |
+
method_costs=COSTS)
|
| 138 |
+
|
| 139 |
+
run_case("2.5 Absolute cap very tight (1e-10)",
|
| 140 |
+
expected_success=True,
|
| 141 |
+
resource_caps=CAPS,
|
| 142 |
+
method_constraints=base_constraints("absolute", 1e-10),
|
| 143 |
+
method_costs=COSTS)
|
| 144 |
+
|
| 145 |
+
run_case("2.6 Mixed cap types",
|
| 146 |
+
expected_success=True,
|
| 147 |
+
resource_caps=CAPS, method_costs=COSTS,
|
| 148 |
+
method_constraints={
|
| 149 |
+
"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 50},
|
| 150 |
+
"Agroforestry": {"active": True, "cap_type": "absolute", "cap_value": 30.0},
|
| 151 |
+
"Enhanced Weathering": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 152 |
+
"BioCCS": {"active": True, "cap_type": "absolute", "cap_value": 1e-3},
|
| 153 |
+
})
|
| 154 |
+
|
| 155 |
+
run_case("2.7 Percent cap = 100 forces one method to take everything",
|
| 156 |
+
expected_success=True,
|
| 157 |
+
resource_caps={"Land": 100.0},
|
| 158 |
+
method_constraints={
|
| 159 |
+
"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 160 |
+
"Agroforestry": {"active": True, "cap_type": "percent", "cap_value": 5},
|
| 161 |
+
},
|
| 162 |
+
method_costs={"Afforestation": {"Land": 0.5}, "Agroforestry": {"Land": 0.3}})
|
| 163 |
+
|
| 164 |
+
run_case("2.8 Method missing from constraints dict",
|
| 165 |
+
expected_success=True,
|
| 166 |
+
resource_caps=CAPS,
|
| 167 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 168 |
+
method_costs=COSTS)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 172 |
+
# SECTION 3 β Coefficient edge cases
|
| 173 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 174 |
+
print("\nββ SECTION 3 Β· Coefficient edge cases ββββββββββββββββββββββββββββββββββββ")
|
| 175 |
+
|
| 176 |
+
run_case("3.1 Method with all-zero coefficients",
|
| 177 |
+
expected_success=True,
|
| 178 |
+
resource_caps=CAPS, method_constraints=base_constraints(),
|
| 179 |
+
method_costs={**COSTS, "Ghost": {"Land": 0.0, "Water": 0.0}})
|
| 180 |
+
|
| 181 |
+
run_case("3.2 Method with no resources in costs (empty dict)",
|
| 182 |
+
expected_success=True,
|
| 183 |
+
resource_caps=CAPS, method_constraints=base_constraints(),
|
| 184 |
+
method_costs={**COSTS, "Empty": {}})
|
| 185 |
+
|
| 186 |
+
run_case("3.3 All coefficients very large",
|
| 187 |
+
expected_success=True,
|
| 188 |
+
resource_caps=CAPS, method_constraints=base_constraints(),
|
| 189 |
+
method_costs={"Afforestation": {"Land": 1e6, "Water": 1e6}})
|
| 190 |
+
|
| 191 |
+
run_case("3.4 Coefficient = 0 for one resource, positive for another",
|
| 192 |
+
expected_success=True,
|
| 193 |
+
resource_caps=CAPS, method_constraints=base_constraints(),
|
| 194 |
+
method_costs={"Afforestation": {"Land": 0.0, "Water": 0.2}})
|
| 195 |
+
|
| 196 |
+
run_case("3.5 Resource in costs but not in resource_caps",
|
| 197 |
+
expected_success=True,
|
| 198 |
+
resource_caps={"Land": 100.0},
|
| 199 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 200 |
+
method_costs={"Afforestation": {"Land": 0.5, "MissingResource": 0.1}})
|
| 201 |
+
|
| 202 |
+
run_case("3.6 method_costs empty dict",
|
| 203 |
+
expected_success=False,
|
| 204 |
+
resource_caps=CAPS,
|
| 205 |
+
method_constraints=base_constraints(),
|
| 206 |
+
method_costs={})
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 210 |
+
# SECTION 4 β enabled_methods
|
| 211 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 212 |
+
print("\nββ SECTION 4 Β· enabled_methods ββββββββββββββββββββββββββββββββββββββββββββ")
|
| 213 |
+
|
| 214 |
+
run_case("4.1 Disable one resource for one method",
|
| 215 |
+
expected_success=True,
|
| 216 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 217 |
+
enabled_methods={"Land": {"Afforestation": False}})
|
| 218 |
+
|
| 219 |
+
run_case("4.2 Disable all resources for one method (effectively inactive)",
|
| 220 |
+
expected_success=True,
|
| 221 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 222 |
+
enabled_methods={"Land": {"Afforestation": False}, "Water": {"Afforestation": False}})
|
| 223 |
+
|
| 224 |
+
run_case("4.3 Disable all resources for all methods β all inactive",
|
| 225 |
+
expected_success=False,
|
| 226 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 227 |
+
enabled_methods={
|
| 228 |
+
"Land": {m: False for m in COSTS},
|
| 229 |
+
"Water": {m: False for m in COSTS},
|
| 230 |
+
"Rock": {m: False for m in COSTS},
|
| 231 |
+
"Energy": {m: False for m in COSTS},
|
| 232 |
+
})
|
| 233 |
+
|
| 234 |
+
run_case("4.4 enabled_methods = None (default)",
|
| 235 |
+
expected_success=True,
|
| 236 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 237 |
+
enabled_methods=None)
|
| 238 |
+
|
| 239 |
+
run_case("4.5 enabled_methods references non-existent method",
|
| 240 |
+
expected_success=True,
|
| 241 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 242 |
+
enabled_methods={"Land": {"NonExistentMethod": False}})
|
| 243 |
+
|
| 244 |
+
run_case("4.6 enabled_methods = True explicitly (already default)",
|
| 245 |
+
expected_success=True,
|
| 246 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 247 |
+
enabled_methods={"Land": {"Afforestation": True}})
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 251 |
+
# SECTION 5 β Custom resources (no overlap with standard)
|
| 252 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 253 |
+
print("\nββ SECTION 5 Β· Custom resources (independent) βββββββββββββββββββββββββββββ")
|
| 254 |
+
|
| 255 |
+
custom_independent = [{
|
| 256 |
+
"name": "Mine A",
|
| 257 |
+
"group": "NewGroup",
|
| 258 |
+
"amount": 50.0,
|
| 259 |
+
"unit": "t",
|
| 260 |
+
"methods": {
|
| 261 |
+
"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5},
|
| 262 |
+
}
|
| 263 |
+
}]
|
| 264 |
+
|
| 265 |
+
run_case("5.1 Custom resource, independent group",
|
| 266 |
+
expected_success=True,
|
| 267 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 268 |
+
custom_resources=custom_independent)
|
| 269 |
+
|
| 270 |
+
run_case("5.2 Custom resource, amount = 0",
|
| 271 |
+
expected_success=True,
|
| 272 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 273 |
+
custom_resources=[{
|
| 274 |
+
"name": "Empty Mine",
|
| 275 |
+
"group": "NewGroup",
|
| 276 |
+
"amount": 0.0,
|
| 277 |
+
"unit": "t",
|
| 278 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}},
|
| 279 |
+
}])
|
| 280 |
+
|
| 281 |
+
run_case("5.3 Custom resource, no methods listed",
|
| 282 |
+
expected_success=True,
|
| 283 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 284 |
+
custom_resources=[{
|
| 285 |
+
"name": "Orphan Batch",
|
| 286 |
+
"group": "NewGroup",
|
| 287 |
+
"amount": 50.0,
|
| 288 |
+
"unit": "t",
|
| 289 |
+
"methods": {},
|
| 290 |
+
}])
|
| 291 |
+
|
| 292 |
+
run_case("5.4 Custom resource, method not in method_costs",
|
| 293 |
+
expected_success=True,
|
| 294 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 295 |
+
custom_resources=[{
|
| 296 |
+
"name": "Ghost Mine",
|
| 297 |
+
"group": "NewGroup",
|
| 298 |
+
"amount": 50.0,
|
| 299 |
+
"unit": "t",
|
| 300 |
+
"methods": {"NonExistentMethod": {"min": 0.1, "median": 0.2, "max": 0.3}},
|
| 301 |
+
}])
|
| 302 |
+
|
| 303 |
+
run_case("5.5 Multiple custom resources, different groups",
|
| 304 |
+
expected_success=True,
|
| 305 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 306 |
+
custom_resources=[
|
| 307 |
+
{"name": "Mine A", "group": "GroupA", "amount": 30.0, "unit": "t",
|
| 308 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}}},
|
| 309 |
+
{"name": "Field B", "group": "GroupB", "amount": 60.0, "unit": "ha",
|
| 310 |
+
"methods": {"Afforestation": {"min": 0.4, "median": 0.5, "max": 0.6}}},
|
| 311 |
+
])
|
| 312 |
+
|
| 313 |
+
run_case("5.6 Custom resource = None",
|
| 314 |
+
expected_success=True,
|
| 315 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 316 |
+
custom_resources=None)
|
| 317 |
+
|
| 318 |
+
run_case("5.7 Custom resources = [] (empty list)",
|
| 319 |
+
expected_success=True,
|
| 320 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 321 |
+
custom_resources=[])
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 325 |
+
# SECTION 6 β Custom resources as substitutes (same group as standard)
|
| 326 |
+
# βββββββββββββββββββββββββββββββββοΏ½οΏ½οΏ½ββββββββββββββββββββββββββββββββββββββββββββ
|
| 327 |
+
print("\nββ SECTION 6 Β· Custom resources (same group as standard) ββββββββββββββββββ")
|
| 328 |
+
|
| 329 |
+
custom_substitute = [{
|
| 330 |
+
"name": "Mine A",
|
| 331 |
+
"group": "Rock",
|
| 332 |
+
"amount": 50.0,
|
| 333 |
+
"unit": "t",
|
| 334 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}},
|
| 335 |
+
}]
|
| 336 |
+
|
| 337 |
+
run_case("6.1 Custom batch same group as standard, std cap > 0",
|
| 338 |
+
expected_success=True,
|
| 339 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 340 |
+
custom_resources=custom_substitute)
|
| 341 |
+
|
| 342 |
+
run_case("6.2 Custom batch same group, std cap = 0",
|
| 343 |
+
expected_success=True,
|
| 344 |
+
resource_caps={**CAPS, "Rock": 0.0},
|
| 345 |
+
method_constraints=base_constraints(), method_costs=COSTS,
|
| 346 |
+
custom_resources=custom_substitute)
|
| 347 |
+
|
| 348 |
+
run_case("6.3 Custom batch same group, std cap = 0, batch cap = 0",
|
| 349 |
+
expected_success=True,
|
| 350 |
+
resource_caps={**CAPS, "Rock": 0.0},
|
| 351 |
+
method_constraints=base_constraints(), method_costs=COSTS,
|
| 352 |
+
custom_resources=[{
|
| 353 |
+
"name": "Mine A", "group": "Rock", "amount": 0.0, "unit": "t",
|
| 354 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}},
|
| 355 |
+
}])
|
| 356 |
+
|
| 357 |
+
run_case("6.4 Two custom batches, same group",
|
| 358 |
+
expected_success=True,
|
| 359 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 360 |
+
custom_resources=[
|
| 361 |
+
{"name": "Mine A", "group": "Rock", "amount": 30.0, "unit": "t",
|
| 362 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}}},
|
| 363 |
+
{"name": "Mine B", "group": "Rock", "amount": 20.0, "unit": "t",
|
| 364 |
+
"methods": {"Enhanced Weathering": {"min": 0.2, "median": 0.3, "max": 0.4}}},
|
| 365 |
+
])
|
| 366 |
+
|
| 367 |
+
run_case("6.5 Custom batch same group, different coefficient than standard",
|
| 368 |
+
expected_success=True,
|
| 369 |
+
resource_caps=CAPS, method_constraints=base_constraints(),
|
| 370 |
+
method_costs={**COSTS, "Enhanced Weathering": {"Rock": 0.4, "Energy": 0.3, "Mine A": 0.1}},
|
| 371 |
+
custom_resources=[{
|
| 372 |
+
"name": "Mine A", "group": "Rock", "amount": 50.0, "unit": "t",
|
| 373 |
+
"methods": {"Enhanced Weathering": {"min": 0.05, "median": 0.1, "max": 0.15}},
|
| 374 |
+
}])
|
| 375 |
+
|
| 376 |
+
run_case("6.6 Custom batch, std disabled via enabled_methods",
|
| 377 |
+
expected_success=True,
|
| 378 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 379 |
+
custom_resources=custom_substitute,
|
| 380 |
+
enabled_methods={"Rock": {"Enhanced Weathering": False}})
|
| 381 |
+
|
| 382 |
+
run_case("6.7 Two methods use same custom batch",
|
| 383 |
+
expected_success=True,
|
| 384 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 385 |
+
custom_resources=[{
|
| 386 |
+
"name": "Mine A", "group": "Rock", "amount": 50.0, "unit": "t",
|
| 387 |
+
"methods": {
|
| 388 |
+
"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5},
|
| 389 |
+
"BioCCS": {"min": 0.1, "median": 0.2, "max": 0.3},
|
| 390 |
+
},
|
| 391 |
+
}])
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 395 |
+
# SECTION 7 β Infeasible / degenerate LP
|
| 396 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 397 |
+
print("\nββ SECTION 7 Β· Infeasible / degenerate LP βββββββββββββββββββββββββββββββββ")
|
| 398 |
+
|
| 399 |
+
run_case("7.1 Empty resource_caps dict",
|
| 400 |
+
expected_success=True,
|
| 401 |
+
resource_caps={},
|
| 402 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 403 |
+
|
| 404 |
+
run_case("7.2 resource_caps = None",
|
| 405 |
+
expected_success=False,
|
| 406 |
+
resource_caps=None,
|
| 407 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 408 |
+
|
| 409 |
+
run_case("7.3 Single method, single resource, coeff = 0 β inactive",
|
| 410 |
+
expected_success=False,
|
| 411 |
+
resource_caps={"Land": 100.0},
|
| 412 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 413 |
+
method_costs={"Afforestation": {"Land": 0.0}})
|
| 414 |
+
|
| 415 |
+
run_case("7.4 All caps = 0, all coeffs > 0",
|
| 416 |
+
expected_success=True,
|
| 417 |
+
resource_caps={k: 0.0 for k in CAPS},
|
| 418 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 419 |
+
|
| 420 |
+
run_case("7.5 Percent caps sum to < 100 (feasible, just limits portfolio)",
|
| 421 |
+
expected_success=True,
|
| 422 |
+
resource_caps=CAPS, method_costs=COSTS,
|
| 423 |
+
method_constraints={m: {"active": True, "cap_type": "percent", "cap_value": 10} for m in COSTS})
|
| 424 |
+
|
| 425 |
+
run_case("7.6 method_costs has method not in method_constraints",
|
| 426 |
+
expected_success=True,
|
| 427 |
+
resource_caps=CAPS,
|
| 428 |
+
method_constraints={m: {"active": True, "cap_type": "percent", "cap_value": 100}
|
| 429 |
+
for m in list(COSTS.keys())[:2]},
|
| 430 |
+
method_costs=COSTS)
|
| 431 |
+
|
| 432 |
+
run_case("7.7 Negative cap_value absolute (should still run)",
|
| 433 |
+
expected_success=True,
|
| 434 |
+
resource_caps=CAPS, method_costs=COSTS,
|
| 435 |
+
method_constraints={m: {"active": True, "cap_type": "absolute", "cap_value": -1.0} for m in COSTS})
|
| 436 |
+
|
| 437 |
+
|
| 438 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 439 |
+
# SECTION 8 β Result integrity checks
|
| 440 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 441 |
+
print("\nββ SECTION 8 Β· Result integrity βββββββββββββββββββββββββββββββββββββββββββ")
|
| 442 |
+
|
| 443 |
+
def check_integrity(name, **kwargs):
|
| 444 |
+
try:
|
| 445 |
+
ok, res = run_optimisation(**kwargs)
|
| 446 |
+
if not ok:
|
| 447 |
+
print(f" [{WARN}] {name} β optimisation failed: {res.get('message','')[:60]}")
|
| 448 |
+
results.append((name, WARN, "opt failed"))
|
| 449 |
+
return
|
| 450 |
+
|
| 451 |
+
total = res["total_removed"]
|
| 452 |
+
method_usage = res["method_usage"]
|
| 453 |
+
resource_usage = res["resource_usage"]
|
| 454 |
+
resource_used_total = res["resource_used_total"]
|
| 455 |
+
resource_remaining = res["resource_remaining"]
|
| 456 |
+
|
| 457 |
+
errors = []
|
| 458 |
+
|
| 459 |
+
# total_removed >= 0
|
| 460 |
+
if total < -1e-9:
|
| 461 |
+
errors.append(f"total_removed < 0: {total}")
|
| 462 |
+
|
| 463 |
+
# sum(method_usage) β total_removed
|
| 464 |
+
method_sum = sum(method_usage.values())
|
| 465 |
+
if abs(method_sum - total) > 1e-6:
|
| 466 |
+
errors.append(f"method_usage sum {method_sum:.6f} β total {total:.6f}")
|
| 467 |
+
|
| 468 |
+
# Build full cap dict: standard + custom batches
|
| 469 |
+
full_caps = dict(kwargs["resource_caps"])
|
| 470 |
+
for b in (kwargs.get("custom_resources") or []):
|
| 471 |
+
full_caps[b["name"]] = float(b.get("amount", 0))
|
| 472 |
+
|
| 473 |
+
# resource_remaining = cap - used (within tolerance)
|
| 474 |
+
for r, remaining in resource_remaining.items():
|
| 475 |
+
cap = full_caps.get(r, 0)
|
| 476 |
+
used = resource_used_total.get(r, 0)
|
| 477 |
+
expected_remaining = cap - used
|
| 478 |
+
if abs(remaining - expected_remaining) > 1e-6:
|
| 479 |
+
errors.append(f"remaining[{r}] {remaining:.6f} β cap-used {expected_remaining:.6f}")
|
| 480 |
+
|
| 481 |
+
# no resource over-consumed
|
| 482 |
+
for r, used in resource_used_total.items():
|
| 483 |
+
cap = full_caps.get(r, 0)
|
| 484 |
+
if used > cap + 1e-6:
|
| 485 |
+
errors.append(f"resource {r} over-consumed: used={used:.6f} cap={cap}")
|
| 486 |
+
|
| 487 |
+
if errors:
|
| 488 |
+
print(f" [{FAIL}] {name}")
|
| 489 |
+
for e in errors:
|
| 490 |
+
print(f" β {e}")
|
| 491 |
+
results.append((name, FAIL, "; ".join(errors)))
|
| 492 |
+
else:
|
| 493 |
+
print(f" [{PASS}] {name} total={total:.4f} methods={len(method_usage)}")
|
| 494 |
+
results.append((name, PASS, f"total={total:.4f}"))
|
| 495 |
+
|
| 496 |
+
except Exception as e:
|
| 497 |
+
tb = traceback.format_exc().strip().split("\n")[-1]
|
| 498 |
+
print(f" [{FAIL}] {name} β EXCEPTION: {tb}")
|
| 499 |
+
results.append((name, FAIL, f"EXCEPTION: {tb}"))
|
| 500 |
+
|
| 501 |
+
check_integrity("8.1 Integrity β normal run",
|
| 502 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS)
|
| 503 |
+
|
| 504 |
+
check_integrity("8.2 Integrity β tight percent cap (10%)",
|
| 505 |
+
resource_caps=CAPS, method_costs=COSTS,
|
| 506 |
+
method_constraints={m: {"active": True, "cap_type": "percent", "cap_value": 10} for m in COSTS})
|
| 507 |
+
|
| 508 |
+
check_integrity("8.3 Integrity β absolute caps",
|
| 509 |
+
resource_caps=CAPS, method_costs=COSTS,
|
| 510 |
+
method_constraints=base_constraints("absolute", 20.0))
|
| 511 |
+
|
| 512 |
+
check_integrity("8.4 Integrity β with custom batch substitute",
|
| 513 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 514 |
+
custom_resources=[{
|
| 515 |
+
"name": "Mine A", "group": "Rock", "amount": 50.0, "unit": "t",
|
| 516 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}},
|
| 517 |
+
}])
|
| 518 |
+
|
| 519 |
+
check_integrity("8.5 Integrity β two batches same group",
|
| 520 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 521 |
+
custom_resources=[
|
| 522 |
+
{"name": "Mine A", "group": "Rock", "amount": 30.0, "unit": "t",
|
| 523 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}}},
|
| 524 |
+
{"name": "Mine B", "group": "Rock", "amount": 20.0, "unit": "t",
|
| 525 |
+
"methods": {"Enhanced Weathering": {"min": 0.2, "median": 0.3, "max": 0.4}}},
|
| 526 |
+
])
|
| 527 |
+
|
| 528 |
+
check_integrity("8.6 Integrity β std disabled for method with batch",
|
| 529 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS,
|
| 530 |
+
custom_resources=[{
|
| 531 |
+
"name": "Mine A", "group": "Rock", "amount": 50.0, "unit": "t",
|
| 532 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}},
|
| 533 |
+
}],
|
| 534 |
+
enabled_methods={"Rock": {"Enhanced Weathering": False}})
|
| 535 |
+
|
| 536 |
+
check_integrity("8.7 Integrity β std cap = 0, batch present",
|
| 537 |
+
resource_caps={**CAPS, "Rock": 0.0},
|
| 538 |
+
method_constraints=base_constraints(), method_costs=COSTS,
|
| 539 |
+
custom_resources=[{
|
| 540 |
+
"name": "Mine A", "group": "Rock", "amount": 50.0, "unit": "t",
|
| 541 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}},
|
| 542 |
+
}])
|
| 543 |
+
|
| 544 |
+
check_integrity("8.8 Integrity β large caps, many methods, mixed constraints",
|
| 545 |
+
resource_caps={k: v * 1000 for k, v in CAPS.items()},
|
| 546 |
+
method_costs=COSTS,
|
| 547 |
+
method_constraints={
|
| 548 |
+
"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 30},
|
| 549 |
+
"Agroforestry": {"active": True, "cap_type": "absolute", "cap_value": 5000},
|
| 550 |
+
"Enhanced Weathering": {"active": True, "cap_type": "percent", "cap_value": 60},
|
| 551 |
+
"BioCCS": {"active": False, "cap_type": "absolute", "cap_value": 100},
|
| 552 |
+
},
|
| 553 |
+
custom_resources=[
|
| 554 |
+
{"name": "Mine A", "group": "Rock", "amount": 1000.0, "unit": "t",
|
| 555 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}}},
|
| 556 |
+
])
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 560 |
+
# SECTION 9 β Perturbation / resource_actual_gain checks
|
| 561 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 562 |
+
print("\nββ SECTION 9 Β· Perturbation (resource_actual_gain) ββββββββββββββββββββββββ")
|
| 563 |
+
|
| 564 |
+
def check_gain(name, expect_binding_resources, **kwargs):
|
| 565 |
+
try:
|
| 566 |
+
ok, res = run_optimisation(**kwargs)
|
| 567 |
+
if not ok:
|
| 568 |
+
print(f" [{WARN}] {name} β failed")
|
| 569 |
+
results.append((name, WARN, "opt failed"))
|
| 570 |
+
return
|
| 571 |
+
gain = res.get("resource_actual_gain", {})
|
| 572 |
+
binding = {r: v for r, v in gain.items() if v > 1e-9}
|
| 573 |
+
non_negative = all(v >= -1e-9 for v in gain.values())
|
| 574 |
+
status = PASS if non_negative else FAIL
|
| 575 |
+
note = "negative gain detected" if not non_negative else f"binding={list(binding.keys())}"
|
| 576 |
+
print(f" [{status}] {name} {note}")
|
| 577 |
+
results.append((name, status, note))
|
| 578 |
+
except Exception as e:
|
| 579 |
+
tb = traceback.format_exc().strip().split("\n")[-1]
|
| 580 |
+
print(f" [{FAIL}] {name} β EXCEPTION: {tb}")
|
| 581 |
+
results.append((name, FAIL, f"EXCEPTION: {tb}"))
|
| 582 |
+
|
| 583 |
+
check_gain("9.1 Gain non-negative β normal", [],
|
| 584 |
+
resource_caps=CAPS, method_constraints=base_constraints(), method_costs=COSTS)
|
| 585 |
+
|
| 586 |
+
check_gain("9.2 Gain β single binding resource", ["Land"],
|
| 587 |
+
resource_caps={"Land": 1.0, "Water": 1e9, "Rock": 1e9, "Energy": 1e9},
|
| 588 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 589 |
+
|
| 590 |
+
check_gain("9.3 Gain β all caps zero (all gains = 0)", [],
|
| 591 |
+
resource_caps={k: 0.0 for k in CAPS},
|
| 592 |
+
method_constraints=base_constraints(), method_costs=COSTS)
|
| 593 |
+
|
| 594 |
+
check_gain("9.4 Gain β custom batch binding", [],
|
| 595 |
+
resource_caps={**CAPS, "Rock": 0.0},
|
| 596 |
+
method_constraints=base_constraints(), method_costs=COSTS,
|
| 597 |
+
custom_resources=[{
|
| 598 |
+
"name": "Mine A", "group": "Rock", "amount": 0.1, "unit": "t",
|
| 599 |
+
"methods": {"Enhanced Weathering": {"min": 0.3, "median": 0.4, "max": 0.5}},
|
| 600 |
+
}])
|
| 601 |
+
|
| 602 |
+
|
| 603 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 604 |
+
# SUMMARY
|
| 605 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 606 |
+
print("\nββ SUMMARY ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ")
|
| 607 |
+
total_cases = len(results)
|
| 608 |
+
passed = sum(1 for _, s, _ in results if "PASS" in s)
|
| 609 |
+
failed = sum(1 for _, s, _ in results if "FAIL" in s)
|
| 610 |
+
warned = sum(1 for _, s, _ in results if "WARN" in s)
|
| 611 |
+
print(f" Total: {total_cases} PASS: {passed} FAIL: {failed} WARN: {warned}")
|
| 612 |
+
|
| 613 |
+
if failed:
|
| 614 |
+
print("\n Failed cases:")
|
| 615 |
+
for name, status, detail in results:
|
| 616 |
+
if "FAIL" in status:
|
| 617 |
+
print(f" β’ {name}: {detail}")
|
| 618 |
+
if warned:
|
| 619 |
+
print("\n Warnings (unexpected behavior):")
|
| 620 |
+
for name, status, detail in results:
|
| 621 |
+
if "WARN" in status:
|
| 622 |
+
print(f" β’ {name}: {detail}")
|
| 623 |
+
print()
|
| 624 |
+
|
| 625 |
+
|
| 626 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 627 |
+
# SECTION 10 β "Other" method expansion (OR semantics)
|
| 628 |
+
# ββοΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 629 |
+
print("\nββ SECTION 10 Β· Other-method expansion βββββββββββββββββββββββββββββββββββ")
|
| 630 |
+
|
| 631 |
+
from utils.data_utils import is_other_method, get_other_method_variants, make_variant_name
|
| 632 |
+
|
| 633 |
+
# ββ 10.1 is_other_method detection ββββββββββββββββββββββββββββββββββββββββββ
|
| 634 |
+
def _check(name, condition, detail=""):
|
| 635 |
+
icon = PASS if condition else FAIL
|
| 636 |
+
print(f" [{icon}] {name}\n {detail or ('OK' if condition else 'FAILED')}")
|
| 637 |
+
results.append((name, icon, detail))
|
| 638 |
+
|
| 639 |
+
_check("10.1a is_other_method('Enhanced weathering - Other mineral')", is_other_method("Enhanced weathering - Other mineral"))
|
| 640 |
+
_check("10.1b is_other_method('Bio-char - Other biomass')", is_other_method("Bio-char - Other biomass"))
|
| 641 |
+
_check("10.1c is_other_method('Afforestation') is False", not is_other_method("Afforestation"))
|
| 642 |
+
_check("10.1d is_other_method('Mineral OAE / Ocean liming') is False", not is_other_method("Mineral OAE / Ocean liming"))
|
| 643 |
+
|
| 644 |
+
# ββ 10.2 get_other_method_variants ββββββββββββββββββββββββββββββββββββββββββ
|
| 645 |
+
_cr2 = [
|
| 646 |
+
{"name": "Ganite", "group": "Other mineral", "amount": 100.0, "unit": "Mt",
|
| 647 |
+
"methods": {"Enhanced weathering - Other mineral": {"min": 1.0, "median": 2.5, "max": 4.0}}},
|
| 648 |
+
{"name": "Dunite", "group": "Other mineral", "amount": 80.0, "unit": "Mt",
|
| 649 |
+
"methods": {"Enhanced weathering - Other mineral": {"min": 1.0, "median": 2.5, "max": 4.0}}},
|
| 650 |
+
]
|
| 651 |
+
v = get_other_method_variants("Enhanced weathering - Other mineral", _cr2)
|
| 652 |
+
_check("10.2a Two batches β 2 variants", len(v) == 2, f"got {len(v)}")
|
| 653 |
+
_check("10.2b Afforestation β 0 variants", len(get_other_method_variants("Afforestation", _cr2)) == 0)
|
| 654 |
+
_check("10.2c Bio-char Other biomass (no batch) β 0", len(get_other_method_variants("Bio-char - Other biomass", _cr2)) == 0)
|
| 655 |
+
_check("10.2d make_variant_name correct",
|
| 656 |
+
make_variant_name("Enhanced weathering - Other mineral", "Ganite")
|
| 657 |
+
== "Enhanced weathering - Other mineral: Ganite")
|
| 658 |
+
|
| 659 |
+
# ββ 10.3 LP: single Other method + single batch β bounded and correct ββββββββ
|
| 660 |
+
run_case("10.3 Other method + 1 batch β bounded",
|
| 661 |
+
expected_success=True,
|
| 662 |
+
resource_caps={"Energy": 200.0},
|
| 663 |
+
method_constraints={
|
| 664 |
+
"Enhanced weathering - Other mineral - Ganite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 665 |
+
},
|
| 666 |
+
method_costs={
|
| 667 |
+
"Enhanced weathering - Other mineral - Ganite": {"Ganite": 2.5, "Energy": 0.1},
|
| 668 |
+
},
|
| 669 |
+
custom_resources=[], # batch already promoted to standard resource
|
| 670 |
+
# Ganite cap comes from resource_caps
|
| 671 |
+
# Note: in real app resource_caps_for_lp includes Ganite; we simulate that here
|
| 672 |
+
)
|
| 673 |
+
# Simulated LP with Ganite in resource_caps (as the app does after expansion)
|
| 674 |
+
run_case("10.3b Other method + 1 batch + batch in resource_caps β correct usage",
|
| 675 |
+
expected_success=True,
|
| 676 |
+
resource_caps={"Energy": 200.0, "Ganite": 100.0},
|
| 677 |
+
method_constraints={
|
| 678 |
+
"EW-Ganite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 679 |
+
},
|
| 680 |
+
method_costs={
|
| 681 |
+
"EW-Ganite": {"Ganite": 2.5, "Energy": 0.1},
|
| 682 |
+
},
|
| 683 |
+
custom_resources=[],
|
| 684 |
+
)
|
| 685 |
+
|
| 686 |
+
# ββ 10.4 LP: two variants, each limited by its own batch ββββββββββββββββββββ
|
| 687 |
+
run_case("10.4 Two variants independent β each limited by own batch",
|
| 688 |
+
expected_success=True,
|
| 689 |
+
resource_caps={"Energy": 1e9, "Ganite": 10.0, "Dunite": 20.0},
|
| 690 |
+
method_constraints={
|
| 691 |
+
"EW-Ganite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 692 |
+
"EW-Dunite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 693 |
+
},
|
| 694 |
+
method_costs={
|
| 695 |
+
"EW-Ganite": {"Ganite": 2.5, "Energy": 0.0},
|
| 696 |
+
"EW-Dunite": {"Dunite": 2.5, "Energy": 0.0},
|
| 697 |
+
},
|
| 698 |
+
custom_resources=[],
|
| 699 |
+
)
|
| 700 |
+
|
| 701 |
+
def check_result_10_4():
|
| 702 |
+
ok, res = run_optimisation(
|
| 703 |
+
resource_caps={"Energy": 1e9, "Ganite": 10.0, "Dunite": 20.0},
|
| 704 |
+
method_constraints={
|
| 705 |
+
"EW-Ganite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 706 |
+
"EW-Dunite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 707 |
+
},
|
| 708 |
+
method_costs={
|
| 709 |
+
"EW-Ganite": {"Ganite": 2.5, "Energy": 0.0},
|
| 710 |
+
"EW-Dunite": {"Dunite": 2.5, "Energy": 0.0},
|
| 711 |
+
},
|
| 712 |
+
custom_resources=[],
|
| 713 |
+
)
|
| 714 |
+
if not ok:
|
| 715 |
+
_check("10.4b Variant allocations correct", False, "LP failed")
|
| 716 |
+
return
|
| 717 |
+
allocs = res["method_usage"]
|
| 718 |
+
ganite_co2 = allocs.get("EW-Ganite", 0)
|
| 719 |
+
dunite_co2 = allocs.get("EW-Dunite", 0)
|
| 720 |
+
expected_ganite = 10.0 / 2.5 # 4.0
|
| 721 |
+
expected_dunite = 20.0 / 2.5 # 8.0
|
| 722 |
+
_check("10.4b EW-Ganite uses Ganite cap fully",
|
| 723 |
+
abs(ganite_co2 - expected_ganite) < 1e-4,
|
| 724 |
+
f"got {ganite_co2:.4f}, expected {expected_ganite:.4f}")
|
| 725 |
+
_check("10.4c EW-Dunite uses Dunite cap fully",
|
| 726 |
+
abs(dunite_co2 - expected_dunite) < 1e-4,
|
| 727 |
+
f"got {dunite_co2:.4f}, expected {expected_dunite:.4f}")
|
| 728 |
+
_check("10.4d Variants independent (total = sum of individuals)",
|
| 729 |
+
abs(res["total_removed"] - (expected_ganite + expected_dunite)) < 1e-4,
|
| 730 |
+
f"total={res['total_removed']:.4f}, expected {expected_ganite + expected_dunite:.4f}")
|
| 731 |
+
check_result_10_4()
|
| 732 |
+
|
| 733 |
+
# ββ 10.5 LP: variant inactive β only the other runs βββββββββββββββββββββββββ
|
| 734 |
+
run_case("10.5 One variant inactive β only other runs",
|
| 735 |
+
expected_success=True,
|
| 736 |
+
resource_caps={"Ganite": 10.0, "Dunite": 20.0},
|
| 737 |
+
method_constraints={
|
| 738 |
+
"EW-Ganite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 739 |
+
"EW-Dunite": {"active": False, "cap_type": "percent", "cap_value": 100},
|
| 740 |
+
},
|
| 741 |
+
method_costs={
|
| 742 |
+
"EW-Ganite": {"Ganite": 2.5},
|
| 743 |
+
"EW-Dunite": {"Dunite": 2.5},
|
| 744 |
+
},
|
| 745 |
+
custom_resources=[],
|
| 746 |
+
)
|
| 747 |
+
|
| 748 |
+
def check_10_5():
|
| 749 |
+
ok, res = run_optimisation(
|
| 750 |
+
resource_caps={"Ganite": 10.0, "Dunite": 20.0},
|
| 751 |
+
method_constraints={
|
| 752 |
+
"EW-Ganite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 753 |
+
"EW-Dunite": {"active": False, "cap_type": "percent", "cap_value": 100},
|
| 754 |
+
},
|
| 755 |
+
method_costs={
|
| 756 |
+
"EW-Ganite": {"Ganite": 2.5},
|
| 757 |
+
"EW-Dunite": {"Dunite": 2.5},
|
| 758 |
+
},
|
| 759 |
+
custom_resources=[],
|
| 760 |
+
)
|
| 761 |
+
if not ok:
|
| 762 |
+
_check("10.5b Dunite inactive β only Ganite runs", False, "LP failed")
|
| 763 |
+
return
|
| 764 |
+
_check("10.5b Only EW-Ganite allocates",
|
| 765 |
+
res["method_usage"].get("EW-Dunite", 0) == 0.0,
|
| 766 |
+
f"Dunite alloc={res['method_usage'].get('EW-Dunite', 0)}")
|
| 767 |
+
check_10_5()
|
| 768 |
+
|
| 769 |
+
# ββ 10.6 LP: variant with percent cap βββββββββββββββββββββββββββββββββββββββ
|
| 770 |
+
# Note: percent cap = 50% with two methods: EW-Ganite β€ 50% of total(all methods).
|
| 771 |
+
# With equal-cost methods, each gets 50% β each at its resource cap.
|
| 772 |
+
def check_10_6():
|
| 773 |
+
# Two balanced variants: each limited to 50% of total portfolio.
|
| 774 |
+
# With symmetric caps (Ganite=100, Dunite=100, coeff=2.5), optimal is 50/50 split.
|
| 775 |
+
ok, res = run_optimisation(
|
| 776 |
+
resource_caps={"Ganite": 100.0, "Dunite": 100.0},
|
| 777 |
+
method_constraints={
|
| 778 |
+
"EW-Ganite": {"active": True, "cap_type": "percent", "cap_value": 50},
|
| 779 |
+
"EW-Dunite": {"active": True, "cap_type": "percent", "cap_value": 50},
|
| 780 |
+
},
|
| 781 |
+
method_costs={
|
| 782 |
+
"EW-Ganite": {"Ganite": 2.5},
|
| 783 |
+
"EW-Dunite": {"Dunite": 2.5},
|
| 784 |
+
},
|
| 785 |
+
custom_resources=[],
|
| 786 |
+
)
|
| 787 |
+
if not ok:
|
| 788 |
+
_check("10.6 Variant percent cap (50%)", False, f"LP failed: {res.get('message', '')}")
|
| 789 |
+
return
|
| 790 |
+
alloc_g = res["method_usage"].get("EW-Ganite", 0)
|
| 791 |
+
alloc_d = res["method_usage"].get("EW-Dunite", 0)
|
| 792 |
+
total = alloc_g + alloc_d
|
| 793 |
+
# Each should be β€ 50% of total
|
| 794 |
+
_check("10.6 Variant 50% percent cap β EW-Ganite β€ 50% of total",
|
| 795 |
+
total > 0 and alloc_g <= total * 0.5 + 1e-4,
|
| 796 |
+
f"EW-Ganite={alloc_g:.4f}, total={total:.4f}")
|
| 797 |
+
_check("10.6b Variant 50% percent cap β EW-Dunite β€ 50% of total",
|
| 798 |
+
total > 0 and alloc_d <= total * 0.5 + 1e-4,
|
| 799 |
+
f"EW-Dunite={alloc_d:.4f}, total={total:.4f}")
|
| 800 |
+
check_10_6()
|
| 801 |
+
|
| 802 |
+
# ββ 10.7 LP: no unbounded when batch in resource_caps (regression) βββββββββββ
|
| 803 |
+
def check_10_7():
|
| 804 |
+
# This is the exact scenario that caused the unbounded LP bug.
|
| 805 |
+
# Without the fix, custom_resources_for_lp containing the batch would
|
| 806 |
+
# put it in custom_batch_names β excluded from standard_resources β no constraint.
|
| 807 |
+
ok, res = run_optimisation(
|
| 808 |
+
resource_caps={"Ganite": 50.0, "Dunite": 30.0},
|
| 809 |
+
method_constraints={
|
| 810 |
+
"EW-Ganite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 811 |
+
"EW-Dunite": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 812 |
+
},
|
| 813 |
+
method_costs={
|
| 814 |
+
"EW-Ganite": {"Ganite": 1.0},
|
| 815 |
+
"EW-Dunite": {"Dunite": 1.0},
|
| 816 |
+
},
|
| 817 |
+
custom_resources=[], # batches NOT in custom_resources β treated as standard
|
| 818 |
+
)
|
| 819 |
+
bounded = ok and res.get("total_removed", 0) < 1e8
|
| 820 |
+
_check("10.7 No unbounded LP when batches are standard resources",
|
| 821 |
+
bounded,
|
| 822 |
+
f"total={res.get('total_removed', 'n/a')} ok={ok}")
|
| 823 |
+
check_10_7()
|
| 824 |
+
|
| 825 |
+
|
| 826 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 827 |
+
# SECTION 11 β Usage threshold & tiny coefficients
|
| 828 |
+
# ββββββββββββοΏ½οΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 829 |
+
print("\nββ SECTION 11 Β· Usage threshold & tiny coefficients ββββββββββββββββββββββ")
|
| 830 |
+
|
| 831 |
+
def check_11_1():
|
| 832 |
+
# Coefficient = 1e-11 (previously filtered by 1e-9 threshold β showed as 0 used).
|
| 833 |
+
# Use Energy as the binding resource (prevents LP from going to 1e11) and
|
| 834 |
+
# NonArableLand as the tiny-coeff resource to verify it's tracked in usage.
|
| 835 |
+
ok, res = run_optimisation(
|
| 836 |
+
resource_caps={"NonArableLand": 1.0, "Energy": 100.0},
|
| 837 |
+
method_constraints={"DACCS": {"active": True, "cap_type": "absolute", "cap_value": 100.0}},
|
| 838 |
+
method_costs={"DACCS": {"NonArableLand": 1e-11, "Energy": 1.0}},
|
| 839 |
+
custom_resources=[],
|
| 840 |
+
)
|
| 841 |
+
if not ok:
|
| 842 |
+
_check("11.1 Tiny coeff 1e-11 not filtered", False, f"LP failed: {res.get('message', '')}")
|
| 843 |
+
return
|
| 844 |
+
usage = res.get("resource_usage", {}).get("NonArableLand", {}).get("DACCS", 0)
|
| 845 |
+
# DACCS produces 100 MtCO2 (bounded by Energy cap), usage = 100 * 1e-11 = 1e-9
|
| 846 |
+
_check("11.1 Tiny coeff 1e-11 tracked in resource_usage",
|
| 847 |
+
usage > 0,
|
| 848 |
+
f"usage={usage:.2e} (should be > 0)")
|
| 849 |
+
check_11_1()
|
| 850 |
+
|
| 851 |
+
def check_11_2():
|
| 852 |
+
# Coefficient exactly 0 β method should be neutralized
|
| 853 |
+
ok, res = run_optimisation(
|
| 854 |
+
resource_caps={"Land": 100.0},
|
| 855 |
+
method_constraints={"Method": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 856 |
+
method_costs={"Method": {"Land": 0.0}},
|
| 857 |
+
custom_resources=[],
|
| 858 |
+
)
|
| 859 |
+
_check("11.2 Coeff=0 β method neutralized (no result)",
|
| 860 |
+
not ok or res.get("total_removed", 0) == 0,
|
| 861 |
+
f"total={res.get('total_removed', 0):.4f}")
|
| 862 |
+
check_11_2()
|
| 863 |
+
|
| 864 |
+
def check_11_3():
|
| 865 |
+
# resource_remaining should be positive when tiny amount used
|
| 866 |
+
ok, res = run_optimisation(
|
| 867 |
+
resource_caps={"NonArableLand": 1.0, "Energy": 100.0},
|
| 868 |
+
method_constraints={"DACCS": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 869 |
+
method_costs={"DACCS": {"NonArableLand": 1e-11, "Energy": 1.0}},
|
| 870 |
+
custom_resources=[],
|
| 871 |
+
)
|
| 872 |
+
if not ok:
|
| 873 |
+
_check("11.3 resource_remaining correct for tiny usage", False, "LP failed")
|
| 874 |
+
return
|
| 875 |
+
remaining = res.get("resource_remaining", {}).get("NonArableLand", None)
|
| 876 |
+
_check("11.3 resource_remaining β₯ 0 for tiny coeff",
|
| 877 |
+
remaining is not None and remaining >= 0,
|
| 878 |
+
f"remaining={remaining}")
|
| 879 |
+
check_11_3()
|
| 880 |
+
|
| 881 |
+
|
| 882 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 883 |
+
# SECTION 12 β cap_type validation
|
| 884 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 885 |
+
print("\nββ SECTION 12 Β· cap_type validation ββββββββββββββββββββββββββββββββββββββ")
|
| 886 |
+
|
| 887 |
+
run_case("12.1 cap_type='percent' valid",
|
| 888 |
+
expected_success=True,
|
| 889 |
+
resource_caps=CAPS, method_constraints=base_constraints("percent", 100), method_costs=COSTS)
|
| 890 |
+
|
| 891 |
+
run_case("12.2 cap_type='absolute' valid",
|
| 892 |
+
expected_success=True,
|
| 893 |
+
resource_caps=CAPS, method_constraints=base_constraints("absolute", 500), method_costs=COSTS)
|
| 894 |
+
|
| 895 |
+
run_case("12.3 cap_type='relative' invalid β ValueError caught",
|
| 896 |
+
expected_success=False,
|
| 897 |
+
resource_caps=CAPS,
|
| 898 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "relative", "cap_value": 50}},
|
| 899 |
+
method_costs={"Afforestation": {"Land": 0.5}})
|
| 900 |
+
|
| 901 |
+
run_case("12.4 cap_type=None β ValueError caught",
|
| 902 |
+
expected_success=False,
|
| 903 |
+
resource_caps=CAPS,
|
| 904 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": None, "cap_value": 50}},
|
| 905 |
+
method_costs={"Afforestation": {"Land": 0.5}})
|
| 906 |
+
|
| 907 |
+
|
| 908 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 909 |
+
# SECTION 13 β Production data smoke test
|
| 910 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 911 |
+
print("\nββ SECTION 13 Β· Production data smoke test βββββββββββββββββββββββββββββββ")
|
| 912 |
+
|
| 913 |
+
import json, pandas as pd
|
| 914 |
+
from utils.data_utils import strip_unit_suffix
|
| 915 |
+
try:
|
| 916 |
+
with open("data/cost_sliders.json") as f:
|
| 917 |
+
cost_sliders_raw = json.load(f)
|
| 918 |
+
df_csv = pd.read_csv("data/inputs.csv")
|
| 919 |
+
|
| 920 |
+
# Build method_costs from medians, stripping unit suffixes so resource names
|
| 921 |
+
# match resource_caps keys (e.g. "Electrical energy (TWh/MtCOβ)" β "Electrical energy").
|
| 922 |
+
prod_costs = {}
|
| 923 |
+
for method, resources in cost_sliders_raw.items():
|
| 924 |
+
prod_costs[method] = {strip_unit_suffix(r): conf.get("median", 0.0) for r, conf in resources.items()}
|
| 925 |
+
|
| 926 |
+
# Realistic resource caps (generous)
|
| 927 |
+
prod_caps = {
|
| 928 |
+
"Arable land": 50.0, "Non-arable land": 200.0, "Other land": 100.0,
|
| 929 |
+
"Forest biomass": 500.0, "Non-forest biomass": 300.0, "Other biomass": 200.0,
|
| 930 |
+
"Electrical energy": 5e6, "Thermal energy high grade": 2e6,
|
| 931 |
+
"Thermal energy low grade": 3e6, "Thermal energy other grade": 1e6,
|
| 932 |
+
"Geologic storage": 100.0, "Other chemical": 50.0,
|
| 933 |
+
"Basalt mineral": 1000.0, "Olivine mineral": 500.0, "Wollastonite mineral": 200.0,
|
| 934 |
+
"Other mineral": 300.0, "Salt water": 1000.0, "Other water": 500.0,
|
| 935 |
+
"Shoreline": 10.0,
|
| 936 |
+
}
|
| 937 |
+
|
| 938 |
+
method_list = list(prod_costs.keys())
|
| 939 |
+
prod_constraints = {m: {"active": True, "cap_type": "percent", "cap_value": 100} for m in method_list}
|
| 940 |
+
|
| 941 |
+
run_case("13.1 Production data β all methods active",
|
| 942 |
+
expected_success=True,
|
| 943 |
+
resource_caps=prod_caps, method_constraints=prod_constraints, method_costs=prod_costs)
|
| 944 |
+
|
| 945 |
+
# 13.2 Only DACCS methods
|
| 946 |
+
daccs = {m: c for m, c in prod_costs.items() if "DACCS" in m}
|
| 947 |
+
daccs_constraints = {m: {"active": True, "cap_type": "percent", "cap_value": 100} for m in daccs}
|
| 948 |
+
run_case("13.2 Production data β DACCS methods only",
|
| 949 |
+
expected_success=True,
|
| 950 |
+
resource_caps=prod_caps, method_constraints=daccs_constraints, method_costs=daccs)
|
| 951 |
+
|
| 952 |
+
# 13.3 Tight energy cap β some DACCS infeasible
|
| 953 |
+
tight_caps = dict(prod_caps)
|
| 954 |
+
tight_caps["Electrical energy"] = 1.0
|
| 955 |
+
tight_caps["Thermal energy high grade"] = 1.0
|
| 956 |
+
tight_caps["Thermal energy low grade"] = 1.0
|
| 957 |
+
run_case("13.3 Production data β tight energy caps",
|
| 958 |
+
expected_success=True,
|
| 959 |
+
resource_caps=tight_caps, method_constraints=prod_constraints, method_costs=prod_costs)
|
| 960 |
+
|
| 961 |
+
# 13.4 All caps = 0 β zero removal
|
| 962 |
+
def check_13_4():
|
| 963 |
+
ok, res = run_optimisation(
|
| 964 |
+
resource_caps={k: 0.0 for k in prod_caps},
|
| 965 |
+
method_constraints=prod_constraints, method_costs=prod_costs)
|
| 966 |
+
_check("13.4 All caps=0 β zero removal",
|
| 967 |
+
ok and res.get("total_removed", -1) == 0.0,
|
| 968 |
+
f"total={res.get('total_removed', 'n/a')}")
|
| 969 |
+
check_13_4()
|
| 970 |
+
|
| 971 |
+
# 13.5 Absolute cap = 5 MtCO2 per method
|
| 972 |
+
abs_constraints = {m: {"active": True, "cap_type": "absolute", "cap_value": 5.0} for m in method_list}
|
| 973 |
+
run_case("13.5 Production data β 5 MtCO2 absolute cap per method",
|
| 974 |
+
expected_success=True,
|
| 975 |
+
resource_caps=prod_caps, method_constraints=abs_constraints, method_costs=prod_costs)
|
| 976 |
+
|
| 977 |
+
# 13.6 Mixed cap types
|
| 978 |
+
mixed_constraints = {}
|
| 979 |
+
for i, m in enumerate(method_list):
|
| 980 |
+
if i % 2 == 0:
|
| 981 |
+
mixed_constraints[m] = {"active": True, "cap_type": "percent", "cap_value": 80}
|
| 982 |
+
else:
|
| 983 |
+
mixed_constraints[m] = {"active": True, "cap_type": "absolute", "cap_value": 10.0}
|
| 984 |
+
run_case("13.6 Production data β mixed percent/absolute caps",
|
| 985 |
+
expected_success=True,
|
| 986 |
+
resource_caps=prod_caps, method_constraints=mixed_constraints, method_costs=prod_costs)
|
| 987 |
+
|
| 988 |
+
except Exception as e:
|
| 989 |
+
_check("13.x Production data load", False, str(e))
|
| 990 |
+
|
| 991 |
+
|
| 992 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 993 |
+
# SECTION 14 β smart_display_format (no floating-point noise)
|
| 994 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 995 |
+
print("\nββ SECTION 14 Β· smart_display_format βββββββββββββββββββββββββββββββββββββ")
|
| 996 |
+
|
| 997 |
+
import sys; sys.path.insert(0, ".")
|
| 998 |
+
from utils.ui_helpers import smart_display_format
|
| 999 |
+
|
| 1000 |
+
_fmt_cases = [
|
| 1001 |
+
# (input, expected_output, description)
|
| 1002 |
+
(462961.1, "462961.1", "Large value from CSV β no float noise"),
|
| 1003 |
+
(462961.09999999997671693, "462961.1","Same float stored differently β same display"),
|
| 1004 |
+
(1e-11, "0.00000000001", "Tiny coefficient β fixed decimal not sci notation"),
|
| 1005 |
+
(1.1e-11, "0.000000000011", "Tiny coefficient with mantissa"),
|
| 1006 |
+
(2.5, "2.5", "Typical coefficient"),
|
| 1007 |
+
(0.5, "0.5", "Half"),
|
| 1008 |
+
(100.0, "100.0", "Integer-like float"),
|
| 1009 |
+
(1000.0, "1000.0", "Larger integer-like float"),
|
| 1010 |
+
(0.001, "0.001", "Small decimal"),
|
| 1011 |
+
(0.1234567890123456, "0.1234567890123456", "High-precision decimal"),
|
| 1012 |
+
(1.0, "1.0", "One"),
|
| 1013 |
+
(0.0, "", "Zero β empty string"),
|
| 1014 |
+
(5e-15, "", "Below 1e-14 threshold β empty string"),
|
| 1015 |
+
(9.99e-15, "", "Just below threshold β empty string"),
|
| 1016 |
+
(1e-14, "0.00000000000001", "Exactly at threshold β shown"),
|
| 1017 |
+
(2e-14, "0.00000000000002", "Just above threshold β shown"),
|
| 1018 |
+
(-0.5, "-0.5", "Negative value"),
|
| 1019 |
+
(-1e-11, "-0.00000000001", "Negative tiny coefficient"),
|
| 1020 |
+
]
|
| 1021 |
+
|
| 1022 |
+
for val, expected, desc in _fmt_cases:
|
| 1023 |
+
result = smart_display_format(val)
|
| 1024 |
+
ok = result == expected
|
| 1025 |
+
_check(f"14 Β· {desc}",
|
| 1026 |
+
ok,
|
| 1027 |
+
f"got {result!r}, expected {expected!r}")
|
| 1028 |
+
|
| 1029 |
+
|
| 1030 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1031 |
+
# SECTION 15 β Batch shared between Other-method variant and non-Other method
|
| 1032 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1033 |
+
print("\nββ SECTION 15 Β· Shared batch: Other variant vs non-Other method ββββββββββ")
|
| 1034 |
+
|
| 1035 |
+
# Scenario: "Limestone" batch assigned to both:
|
| 1036 |
+
# - "EW-Other mineral" (Other β expanded to variant EW-Other mineral: Limestone)
|
| 1037 |
+
# - "Mineral OAE" (non-Other β should keep access to Limestone via type-A)
|
| 1038 |
+
#
|
| 1039 |
+
# EW-Other mineral coeff = 2.5 Mt/MtCOβ β 4.2/2.5 = 1.68 MtCOβ
|
| 1040 |
+
# Mineral OAE coeff = 1.743 Mt/MtCOβ β 4.2/1.743 = 2.41 MtCOβ (more efficient)
|
| 1041 |
+
#
|
| 1042 |
+
# Without the fix, Limestone is removed from custom_resources_for_lp but NOT
|
| 1043 |
+
# injected into Mineral OAE's applied_costs β Mineral OAE has no Limestone
|
| 1044 |
+
# constraint β LP allocates all Limestone to EW-Other mineral: Limestone β 1.68.
|
| 1045 |
+
# With the fix, Mineral OAE gets Limestone injected β LP prefers Mineral OAE β 2.41.
|
| 1046 |
+
|
| 1047 |
+
_limestone_batch = {
|
| 1048 |
+
"name": "Limestone",
|
| 1049 |
+
"group": "Other mineral",
|
| 1050 |
+
"amount": 4.2,
|
| 1051 |
+
"unit": "Mt",
|
| 1052 |
+
"methods": {
|
| 1053 |
+
"EW-Other mineral": {"min": 0.5, "median": 2.5, "max": 25.0},
|
| 1054 |
+
"Mineral OAE": {"min": 1.4, "median": 1.743, "max": 5.0},
|
| 1055 |
+
},
|
| 1056 |
+
}
|
| 1057 |
+
|
| 1058 |
+
# Simulate what tab4 expansion does to applied_costs / resource_caps before
|
| 1059 |
+
# calling run_optimisation. After fix, Mineral OAE gets Limestone in its costs.
|
| 1060 |
+
|
| 1061 |
+
def _simulate_expansion(base_costs, custom_res):
|
| 1062 |
+
"""Replicate the tab4 "Other" expansion logic (the same code path)."""
|
| 1063 |
+
import sys, os
|
| 1064 |
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
| 1065 |
+
from utils.data_utils import is_other_method, get_other_method_variants, make_variant_name
|
| 1066 |
+
from config.config import hidden_resource_names as hidden
|
| 1067 |
+
|
| 1068 |
+
applied = {m: dict(c) for m, c in base_costs.items()}
|
| 1069 |
+
resource_caps_lp = {"Other mineral": 0.0} # standard pool = 0
|
| 1070 |
+
expanded_batch_names: set = set()
|
| 1071 |
+
|
| 1072 |
+
for m in list(applied.keys()):
|
| 1073 |
+
if not is_other_method(m):
|
| 1074 |
+
continue
|
| 1075 |
+
batches = get_other_method_variants(m, custom_res)
|
| 1076 |
+
if not batches:
|
| 1077 |
+
continue
|
| 1078 |
+
all_names = {b["name"] for b in batches}
|
| 1079 |
+
for b in batches:
|
| 1080 |
+
vname = make_variant_name(m, b["name"])
|
| 1081 |
+
vcosts = {r: c for r, c in applied[m].items() if r not in hidden and r not in all_names}
|
| 1082 |
+
vcosts[b["name"]] = b["methods"][m]["median"]
|
| 1083 |
+
applied[vname] = vcosts
|
| 1084 |
+
resource_caps_lp[b["name"]] = float(b["amount"])
|
| 1085 |
+
expanded_batch_names.add(b["name"])
|
| 1086 |
+
del applied[m]
|
| 1087 |
+
|
| 1088 |
+
cr_for_lp = [b for b in custom_res if b["name"] not in expanded_batch_names]
|
| 1089 |
+
|
| 1090 |
+
# THE FIX: inject batch coeff into non-Other methods
|
| 1091 |
+
for b in custom_res:
|
| 1092 |
+
if b["name"] not in expanded_batch_names:
|
| 1093 |
+
continue
|
| 1094 |
+
for mname, coeff_d in b.get("methods", {}).items():
|
| 1095 |
+
if mname not in applied:
|
| 1096 |
+
continue
|
| 1097 |
+
applied[mname][b["name"]] = coeff_d.get("median", 0.0)
|
| 1098 |
+
|
| 1099 |
+
return applied, resource_caps_lp, cr_for_lp
|
| 1100 |
+
|
| 1101 |
+
|
| 1102 |
+
def check_15_1():
|
| 1103 |
+
"""Mineral OAE should win over EW-Other mineral for Limestone (lower coeff = more efficient)."""
|
| 1104 |
+
base_costs = {
|
| 1105 |
+
"EW-Other mineral": {"Other mineral": 0.0}, # standard pool empty
|
| 1106 |
+
"Mineral OAE": {},
|
| 1107 |
+
}
|
| 1108 |
+
applied, caps, cr_lp = _simulate_expansion(base_costs, [_limestone_batch])
|
| 1109 |
+
|
| 1110 |
+
# After expansion: variant "EW-Other mineral: Limestone" and "Mineral OAE" (with Limestone injected)
|
| 1111 |
+
_check("15.1a EW-Other mineral removed from applied_costs after expansion",
|
| 1112 |
+
"EW-Other mineral" not in applied)
|
| 1113 |
+
variant_name = "EW-Other mineral: Limestone"
|
| 1114 |
+
_check("15.1b Variant created in applied_costs",
|
| 1115 |
+
variant_name in applied, f"keys={list(applied.keys())}")
|
| 1116 |
+
_check("15.1c Mineral OAE has Limestone injected",
|
| 1117 |
+
"Limestone" in applied.get("Mineral OAE", {}),
|
| 1118 |
+
f"Mineral OAE costs={applied.get('Mineral OAE', {})}")
|
| 1119 |
+
_check("15.1d Limestone coeff for Mineral OAE = 1.743",
|
| 1120 |
+
abs(applied.get("Mineral OAE", {}).get("Limestone", 0) - 1.743) < 1e-9,
|
| 1121 |
+
f"got {applied.get('Mineral OAE', {}).get('Limestone', 'missing')}")
|
| 1122 |
+
_check("15.1e Limestone in resource_caps_lp with correct amount",
|
| 1123 |
+
abs(caps.get("Limestone", 0) - 4.2) < 1e-9,
|
| 1124 |
+
f"got {caps.get('Limestone', 'missing')}")
|
| 1125 |
+
_check("15.1f Limestone not in custom_resources_for_lp (no double constraint)",
|
| 1126 |
+
all(b["name"] != "Limestone" for b in cr_lp),
|
| 1127 |
+
f"cr_lp names={[b['name'] for b in cr_lp]}")
|
| 1128 |
+
check_15_1()
|
| 1129 |
+
|
| 1130 |
+
def check_15_2():
|
| 1131 |
+
"""LP should allocate Limestone to Mineral OAE (coeff 1.743 < 2.5) β more efficient."""
|
| 1132 |
+
# Simulate the state after expansion (with fix applied)
|
| 1133 |
+
ok, res = run_optimisation(
|
| 1134 |
+
resource_caps={"Limestone": 4.2},
|
| 1135 |
+
method_constraints={
|
| 1136 |
+
"EW-Other mineral: Limestone": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1137 |
+
"Mineral OAE": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1138 |
+
},
|
| 1139 |
+
method_costs={
|
| 1140 |
+
"EW-Other mineral: Limestone": {"Limestone": 2.5},
|
| 1141 |
+
"Mineral OAE": {"Limestone": 1.743},
|
| 1142 |
+
},
|
| 1143 |
+
custom_resources=[],
|
| 1144 |
+
)
|
| 1145 |
+
if not ok:
|
| 1146 |
+
_check("15.2 LP: Mineral OAE preferred over EW-Other mineral", False, f"LP failed: {res.get('message', '')}")
|
| 1147 |
+
return
|
| 1148 |
+
alloc_oae = res["method_usage"].get("Mineral OAE", 0)
|
| 1149 |
+
alloc_ew = res["method_usage"].get("EW-Other mineral: Limestone", 0)
|
| 1150 |
+
expected_oae = 4.2 / 1.743 # β 2.409
|
| 1151 |
+
_check("15.2a Mineral OAE gets all Limestone (more efficient)",
|
| 1152 |
+
abs(alloc_oae - expected_oae) < 1e-3,
|
| 1153 |
+
f"Mineral OAE={alloc_oae:.4f}, expectedβ{expected_oae:.4f}")
|
| 1154 |
+
_check("15.2b EW-Other mineral: Limestone gets 0 (LP correctly rejects it)",
|
| 1155 |
+
alloc_ew < 1e-6,
|
| 1156 |
+
f"EW alloc={alloc_ew:.6f}")
|
| 1157 |
+
_check("15.2c Total β 2.409 MtCOβ (Mineral OAE wins)",
|
| 1158 |
+
abs(res["total_removed"] - expected_oae) < 1e-3,
|
| 1159 |
+
f"total={res['total_removed']:.4f}")
|
| 1160 |
+
check_15_2()
|
| 1161 |
+
|
| 1162 |
+
def check_15_3():
|
| 1163 |
+
"""Regression: without the fix, EW-Other mineral: Limestone would win (wrong)."""
|
| 1164 |
+
# Simulate WITHOUT fix: Mineral OAE has NO Limestone in costs
|
| 1165 |
+
ok, res = run_optimisation(
|
| 1166 |
+
resource_caps={"Limestone": 4.2},
|
| 1167 |
+
method_constraints={
|
| 1168 |
+
"EW-Other mineral: Limestone": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1169 |
+
"Mineral OAE": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1170 |
+
},
|
| 1171 |
+
method_costs={
|
| 1172 |
+
"EW-Other mineral: Limestone": {"Limestone": 2.5},
|
| 1173 |
+
"Mineral OAE": {}, # NO Limestone β old buggy state
|
| 1174 |
+
},
|
| 1175 |
+
custom_resources=[],
|
| 1176 |
+
)
|
| 1177 |
+
if not ok:
|
| 1178 |
+
_check("15.3 Regression check (old buggy behaviour)", False, f"LP failed")
|
| 1179 |
+
return
|
| 1180 |
+
alloc_ew = res["method_usage"].get("EW-Other mineral: Limestone", 0)
|
| 1181 |
+
# Without fix: Mineral OAE has no Limestone β EW wins (wrong, less efficient)
|
| 1182 |
+
_check("15.3 Without fix EW-Other gets Limestone (confirms the bug existed)",
|
| 1183 |
+
alloc_ew > 1.0, # EW-Other would get 4.2/2.5=1.68
|
| 1184 |
+
f"EW alloc={alloc_ew:.4f} (expected ~1.68 in buggy state)")
|
| 1185 |
+
check_15_3()
|
| 1186 |
+
|
| 1187 |
+
def check_15_4():
|
| 1188 |
+
"""Multiple non-Other methods sharing the same expanded batch all get the coeff injected."""
|
| 1189 |
+
# Limestone shared between EW-Other mineral, Mineral OAE, AND Electrochemical OAE
|
| 1190 |
+
limestone_multi = {
|
| 1191 |
+
"name": "Limestone",
|
| 1192 |
+
"group": "Other mineral",
|
| 1193 |
+
"amount": 4.2,
|
| 1194 |
+
"unit": "Mt",
|
| 1195 |
+
"methods": {
|
| 1196 |
+
"EW-Other mineral": {"min": 0.5, "median": 2.5, "max": 25.0},
|
| 1197 |
+
"Mineral OAE": {"min": 1.4, "median": 1.743, "max": 5.0},
|
| 1198 |
+
"Electrochemical OAE": {"min": 2.0, "median": 2.95, "max": 3.5},
|
| 1199 |
+
},
|
| 1200 |
+
}
|
| 1201 |
+
base_costs = {
|
| 1202 |
+
"EW-Other mineral": {"Other mineral": 0.0},
|
| 1203 |
+
"Mineral OAE": {},
|
| 1204 |
+
"Electrochemical OAE": {},
|
| 1205 |
+
}
|
| 1206 |
+
applied, caps, cr_lp = _simulate_expansion(base_costs, [limestone_multi])
|
| 1207 |
+
_check("15.4a Mineral OAE gets Limestone injected",
|
| 1208 |
+
"Limestone" in applied.get("Mineral OAE", {}))
|
| 1209 |
+
_check("15.4b Electrochemical OAE gets Limestone injected",
|
| 1210 |
+
"Limestone" in applied.get("Electrochemical OAE", {}))
|
| 1211 |
+
_check("15.4c Electrochemical OAE coeff = 2.95",
|
| 1212 |
+
abs(applied.get("Electrochemical OAE", {}).get("Limestone", 0) - 2.95) < 1e-9,
|
| 1213 |
+
f"got {applied.get('Electrochemical OAE', {}).get('Limestone', 'missing')}")
|
| 1214 |
+
check_15_4()
|
| 1215 |
+
|
| 1216 |
+
run_case("15.5 Three-way competition for Limestone β Mineral OAE wins (coeff 1.743 lowest)",
|
| 1217 |
+
expected_success=True,
|
| 1218 |
+
resource_caps={"Limestone": 4.2},
|
| 1219 |
+
method_constraints={
|
| 1220 |
+
"EW-Other: Limestone": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1221 |
+
"Mineral OAE": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1222 |
+
"Electrochem OAE": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1223 |
+
},
|
| 1224 |
+
method_costs={
|
| 1225 |
+
"EW-Other: Limestone": {"Limestone": 2.5}, # 1.68 MtCOβ/4.2Mt
|
| 1226 |
+
"Mineral OAE": {"Limestone": 1.743}, # 2.41 MtCOβ/4.2Mt β best
|
| 1227 |
+
"Electrochem OAE": {"Limestone": 2.95}, # 1.42 MtCOβ/4.2Mt
|
| 1228 |
+
},
|
| 1229 |
+
custom_resources=[],
|
| 1230 |
+
)
|
| 1231 |
+
|
| 1232 |
+
|
| 1233 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1234 |
+
# SECTION 16 β Multi-resource method with simultaneous custom-batch substitutes
|
| 1235 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1236 |
+
print("\nββ SECTION 16 Β· Multi-resource method (High-temp DACCS) β Leontief across roles ββ")
|
| 1237 |
+
|
| 1238 |
+
# Real coefficients from data/inputs.csv (median column), for
|
| 1239 |
+
# "High-temp DACCS (solid-sorbent/liquid-solvent)" β a genuine multi-resource
|
| 1240 |
+
# (Leontief/AND) method: every unit of CO2 removed needs ALL FIVE resources
|
| 1241 |
+
# simultaneously, they are NOT substitutes for each other.
|
| 1242 |
+
DACCS_METHOD = "High-temp DACCS"
|
| 1243 |
+
E_C = 423787.8 # Electrical energy, MWh/MtCO2
|
| 1244 |
+
L_C = 0.000223 # Non-arable land, Mha/MtCO2
|
| 1245 |
+
T_C = 1750000.0 # Thermal energy high grade, MWh/MtCO2
|
| 1246 |
+
S_C = 1.0 # Geologic storage, Mt/MtCO2
|
| 1247 |
+
W_C = 0.0047 # Other water, bcm/MtCO2
|
| 1248 |
+
|
| 1249 |
+
DACCS_COSTS = {
|
| 1250 |
+
DACCS_METHOD: {
|
| 1251 |
+
"Electrical energy": E_C,
|
| 1252 |
+
"Non-arable land": L_C,
|
| 1253 |
+
"Thermal energy high grade": T_C,
|
| 1254 |
+
"Geologic storage": S_C,
|
| 1255 |
+
"Other water": W_C,
|
| 1256 |
+
}
|
| 1257 |
+
}
|
| 1258 |
+
DACCS_CONSTRAINTS = {DACCS_METHOD: {"active": True, "cap_type": "percent", "cap_value": 100}}
|
| 1259 |
+
|
| 1260 |
+
# All 5 standard caps set to "10 MtCO2-equivalent" β clean baseline of 10.0
|
| 1261 |
+
TEN_MT_CAPS = {
|
| 1262 |
+
"Electrical energy": 10 * E_C,
|
| 1263 |
+
"Non-arable land": 10 * L_C,
|
| 1264 |
+
"Thermal energy high grade": 10 * T_C,
|
| 1265 |
+
"Geologic storage": 10 * S_C,
|
| 1266 |
+
"Other water": 10 * W_C,
|
| 1267 |
+
}
|
| 1268 |
+
|
| 1269 |
+
energy_a = {
|
| 1270 |
+
"name": "Energy A", "group": "Electrical energy", "amount": 10 * E_C, "unit": "MWh",
|
| 1271 |
+
"methods": {DACCS_METHOD: {"min": E_C, "median": E_C, "max": E_C}},
|
| 1272 |
+
}
|
| 1273 |
+
land_a = {
|
| 1274 |
+
"name": "Land A", "group": "Non-arable land", "amount": 10 * L_C, "unit": "Mha",
|
| 1275 |
+
"methods": {DACCS_METHOD: {"min": L_C, "median": L_C, "max": L_C}},
|
| 1276 |
+
}
|
| 1277 |
+
|
| 1278 |
+
def check_16_x(name, custom_resources, caps_override, expected_total):
|
| 1279 |
+
ok, res = run_optimisation(
|
| 1280 |
+
resource_caps={**TEN_MT_CAPS, **caps_override},
|
| 1281 |
+
method_constraints=DACCS_CONSTRAINTS,
|
| 1282 |
+
method_costs=DACCS_COSTS,
|
| 1283 |
+
custom_resources=custom_resources,
|
| 1284 |
+
)
|
| 1285 |
+
if not ok:
|
| 1286 |
+
_check(name, False, f"LP failed: {res.get('message', '')}")
|
| 1287 |
+
return None
|
| 1288 |
+
total = res["total_removed"]
|
| 1289 |
+
_check(name, abs(total - expected_total) < 1e-3,
|
| 1290 |
+
f"got {total:.4f}, expected {expected_total:.4f}")
|
| 1291 |
+
return res
|
| 1292 |
+
|
| 1293 |
+
# 16.1 Baseline: standard only, no batches β all 5 resources bind at 10 MtCO2
|
| 1294 |
+
check_16_x("16.1 Baseline β standard only, no batches", [], {}, 10.0)
|
| 1295 |
+
|
| 1296 |
+
# 16.2 Energy A alone + all standards: Energy role doubles to 20-equiv, but
|
| 1297 |
+
# Land/Thermal/Storage/Water stay at 10-equiv β total stays 10 (Leontief holds:
|
| 1298 |
+
# extra electricity alone doesn't unlock more removal).
|
| 1299 |
+
check_16_x("16.2 Energy A alone + all standards β still bound by Land/Thermal/Water",
|
| 1300 |
+
[energy_a], {}, 10.0)
|
| 1301 |
+
|
| 1302 |
+
# 16.3 Land A alone + all standards: symmetric case.
|
| 1303 |
+
check_16_x("16.3 Land A alone + all standards β still bound by Energy/Thermal/Water",
|
| 1304 |
+
[land_a], {}, 10.0)
|
| 1305 |
+
|
| 1306 |
+
# 16.4 Energy A AND Land A together + unchanged Thermal/Storage/Water: both
|
| 1307 |
+
# substituted roles double capacity, but Thermal/Storage/Water (untouched)
|
| 1308 |
+
# remain the binding constraint at 10 β total should STILL be 10, not 20.
|
| 1309 |
+
res_16_4 = check_16_x(
|
| 1310 |
+
"16.4 Energy A + Land A together + standards β still bound by Thermal/Water",
|
| 1311 |
+
[energy_a, land_a], {}, 10.0)
|
| 1312 |
+
|
| 1313 |
+
# 16.4b Regression guard: the standard resource must still show non-zero
|
| 1314 |
+
# usage (no D2 misfire zeroing it out). NOTE: batch usage (Energy A / Land A)
|
| 1315 |
+
# is NOT asserted here β this exact test is degenerate (standard alone already
|
| 1316 |
+
# covers the full 10, so the solver is free to leave the batches at 0; that's
|
| 1317 |
+
# a valid optimum, not a bug). See 16.4bis for a non-degenerate mixed-usage check.
|
| 1318 |
+
if res_16_4:
|
| 1319 |
+
usage = res_16_4["resource_usage"]
|
| 1320 |
+
_check("16.4b Electrical energy (standard) used > 0",
|
| 1321 |
+
usage.get("Electrical energy", {}).get(DACCS_METHOD, 0) > 1e-6,
|
| 1322 |
+
f"usage={usage.get('Electrical energy', {})}")
|
| 1323 |
+
_check("16.4d Non-arable land (standard) used > 0",
|
| 1324 |
+
usage.get("Non-arable land", {}).get(DACCS_METHOD, 0) > 1e-6,
|
| 1325 |
+
f"usage={usage.get('Non-arable land', {})}")
|
| 1326 |
+
|
| 1327 |
+
# 16.4bis Same total (10), but standard Energy/Land caps are DELIBERATELY too
|
| 1328 |
+
# small (6-equiv) to cover it alone β the LP is forced (not just allowed) to
|
| 1329 |
+
# draw part of its Energy and Land needs from the batches. This removes the
|
| 1330 |
+
# degeneracy of 16.4 (where standard alone already covered the full 10, so the
|
| 1331 |
+
# solver was free to leave the batches at 0 β that's not a bug, just an
|
| 1332 |
+
# arbitrary vertex choice when nothing distinguishes the two sources).
|
| 1333 |
+
res_16_4bis = check_16_x(
|
| 1334 |
+
"16.4bis Standard Energy/Land capped below demand β forces real mixed usage",
|
| 1335 |
+
[energy_a, land_a],
|
| 1336 |
+
{"Electrical energy": 6 * E_C, "Non-arable land": 6 * L_C}, 10.0)
|
| 1337 |
+
if res_16_4bis:
|
| 1338 |
+
usage = res_16_4bis["resource_usage"]
|
| 1339 |
+
_check("16.4bis-b Electrical energy (standard) used β 6 (its own cap, fully used)",
|
| 1340 |
+
abs(usage.get("Electrical energy", {}).get(DACCS_METHOD, 0) - 6 * E_C) < 1,
|
| 1341 |
+
f"usage={usage.get('Electrical energy', {})}")
|
| 1342 |
+
_check("16.4bis-c Energy A (batch) used β 4 MtCO2-equiv (the missing part)",
|
| 1343 |
+
abs(usage.get("Energy A", {}).get(DACCS_METHOD, 0) - 4 * E_C) < 1,
|
| 1344 |
+
f"usage={usage.get('Energy A', {})}")
|
| 1345 |
+
_check("16.4bis-d Non-arable land (standard) used β 6-equiv (its own cap, fully used)",
|
| 1346 |
+
abs(usage.get("Non-arable land", {}).get(DACCS_METHOD, 0) - 6 * L_C) < 1e-6,
|
| 1347 |
+
f"usage={usage.get('Non-arable land', {})}")
|
| 1348 |
+
_check("16.4bis-e Land A (batch) used β 4 MtCO2-equiv (the missing part)",
|
| 1349 |
+
abs(usage.get("Land A", {}).get(DACCS_METHOD, 0) - 4 * L_C) < 1e-6,
|
| 1350 |
+
f"usage={usage.get('Land A', {})}")
|
| 1351 |
+
|
| 1352 |
+
# 16.5 Energy A + Land A together, with Thermal/Storage/Water made generous
|
| 1353 |
+
# (non-binding) β now Energy (20-equiv) and Land (20-equiv) ARE the bottleneck
|
| 1354 |
+
# β total should rise to 20 (pooling genuinely unlocks extra capacity when it
|
| 1355 |
+
# actually is the limiting resource).
|
| 1356 |
+
check_16_x(
|
| 1357 |
+
"16.5 Energy A + Land A, Thermal/Storage/Water non-binding β total rises to 20",
|
| 1358 |
+
[energy_a, land_a],
|
| 1359 |
+
{"Thermal energy high grade": 1000 * T_C, "Geologic storage": 1000 * S_C, "Other water": 1000 * W_C},
|
| 1360 |
+
20.0)
|
| 1361 |
+
|
| 1362 |
+
# 16.6 Standard Electrical energy AND Non-arable land forced to 0 (exclusivity
|
| 1363 |
+
# for BOTH roles at once), Thermal/Storage/Water generous β total should be
|
| 1364 |
+
# bound solely by Energy A's own 10-equiv amount and Land A's own 10-equiv
|
| 1365 |
+
# amount (both maxed, no cross-contamination between the two D2 branches).
|
| 1366 |
+
check_16_x(
|
| 1367 |
+
"16.6 Standard Energy=0 & Land=0, batches only β bound by batch amounts (10)",
|
| 1368 |
+
[energy_a, land_a],
|
| 1369 |
+
{"Electrical energy": 0.0, "Non-arable land": 0.0,
|
| 1370 |
+
"Thermal energy high grade": 1000 * T_C, "Geologic storage": 1000 * S_C, "Other water": 1000 * W_C},
|
| 1371 |
+
10.0)
|
| 1372 |
+
|
| 1373 |
+
|
| 1374 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1375 |
+
# SECTION 17 β Family-resource pooling (OR semantics for same-group resources)
|
| 1376 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1377 |
+
print("\nββ SECTION 17 Β· Family-resource pooling (resource_to_group) ββββββββββββββ")
|
| 1378 |
+
|
| 1379 |
+
FAM_GROUPS = {"Arable land": "Land", "Other land": "Land", "Non-arable land": "Land"}
|
| 1380 |
+
|
| 1381 |
+
# ββ 17.1 Baseline (no resource_to_group) β two same-family resources are
|
| 1382 |
+
# still required at once (AND): total bounded by whichever is more binding. ββ
|
| 1383 |
+
def check_17_1():
|
| 1384 |
+
ok, res = run_optimisation(
|
| 1385 |
+
resource_caps={"Arable land": 10.0, "Other land": 5.0},
|
| 1386 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 1387 |
+
method_costs={"Afforestation": {"Arable land": 1.0, "Other land": 1.0}},
|
| 1388 |
+
custom_resources=[],
|
| 1389 |
+
)
|
| 1390 |
+
_check("17.1 No resource_to_group β AND (bounded by the tighter resource, 5.0)",
|
| 1391 |
+
ok and abs(res["total_removed"] - 5.0) < 1e-6,
|
| 1392 |
+
f"total={res.get('total_removed') if ok else res}")
|
| 1393 |
+
check_17_1()
|
| 1394 |
+
|
| 1395 |
+
# ββ 17.2 Same setup, pooled β OR/blend: total can exceed either resource
|
| 1396 |
+
# alone, up to the sum (equal coefficients). βββββββββββββββββββββββββββββββββ
|
| 1397 |
+
def check_17_2():
|
| 1398 |
+
ok, res = run_optimisation(
|
| 1399 |
+
resource_caps={"Arable land": 10.0, "Other land": 5.0},
|
| 1400 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 1401 |
+
method_costs={"Afforestation": {"Arable land": 1.0, "Other land": 1.0}},
|
| 1402 |
+
custom_resources=[],
|
| 1403 |
+
resource_to_group=FAM_GROUPS,
|
| 1404 |
+
)
|
| 1405 |
+
_check("17.2 Pooled β OR (total = sum of both caps, 15.0)",
|
| 1406 |
+
ok and abs(res["total_removed"] - 15.0) < 1e-6,
|
| 1407 |
+
f"total={res.get('total_removed') if ok else res}")
|
| 1408 |
+
if ok:
|
| 1409 |
+
usage = res["resource_usage"]
|
| 1410 |
+
arable_used = usage.get("Arable land", {}).get("Afforestation", 0)
|
| 1411 |
+
other_used = usage.get("Other land", {}).get("Afforestation", 0)
|
| 1412 |
+
_check("17.2b Both pool members fully used (10 + 5)",
|
| 1413 |
+
abs(arable_used - 10.0) < 1e-6 and abs(other_used - 5.0) < 1e-6,
|
| 1414 |
+
f"Arable land={arable_used}, Other land={other_used}")
|
| 1415 |
+
check_17_2()
|
| 1416 |
+
|
| 1417 |
+
# ββ 17.3 Unequal coefficients β pooled capacity = sum of each member's own
|
| 1418 |
+
# ceiling (cap / coefficient), regardless of which is cheaper. ββββββββββββββ
|
| 1419 |
+
def check_17_3():
|
| 1420 |
+
ok, res = run_optimisation(
|
| 1421 |
+
resource_caps={"Arable land": 10.0, "Other land": 10.0},
|
| 1422 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 1423 |
+
method_costs={"Afforestation": {"Arable land": 0.5, "Other land": 2.0}},
|
| 1424 |
+
custom_resources=[],
|
| 1425 |
+
resource_to_group=FAM_GROUPS,
|
| 1426 |
+
)
|
| 1427 |
+
expected = 10.0 / 0.5 + 10.0 / 2.0 # 20 + 5 = 25
|
| 1428 |
+
_check("17.3 Pooled, unequal coefficients β total = 25",
|
| 1429 |
+
ok and abs(res["total_removed"] - expected) < 1e-4,
|
| 1430 |
+
f"total={res.get('total_removed') if ok else res}, expected={expected}")
|
| 1431 |
+
check_17_3()
|
| 1432 |
+
|
| 1433 |
+
# ββ 17.4 A custom batch attached to one pooled standard resource joins the
|
| 1434 |
+
# same pool (batch + its reference resource + the sibling family resource all
|
| 1435 |
+
# complement each other). ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1436 |
+
def check_17_4():
|
| 1437 |
+
ok, res = run_optimisation(
|
| 1438 |
+
resource_caps={"Arable land": 10.0, "Other land": 5.0},
|
| 1439 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 1440 |
+
method_costs={"Afforestation": {"Arable land": 1.0, "Other land": 1.0}},
|
| 1441 |
+
custom_resources=[{
|
| 1442 |
+
"name": "Reserve land", "group": "Arable land", "amount": 5.0, "unit": "Mha",
|
| 1443 |
+
"methods": {"Afforestation": {"min": 1.0, "median": 1.0, "max": 1.0}},
|
| 1444 |
+
}],
|
| 1445 |
+
resource_to_group=FAM_GROUPS,
|
| 1446 |
+
)
|
| 1447 |
+
_check("17.4 Pool + custom batch on a pool member β total = 20 (10+5+5)",
|
| 1448 |
+
ok and abs(res["total_removed"] - 20.0) < 1e-4,
|
| 1449 |
+
f"total={res.get('total_removed') if ok else res}")
|
| 1450 |
+
check_17_4()
|
| 1451 |
+
|
| 1452 |
+
# ββ 17.5 One pool member at zero cap β fully sourced from the other, no crash. ββ
|
| 1453 |
+
def check_17_5():
|
| 1454 |
+
ok, res = run_optimisation(
|
| 1455 |
+
resource_caps={"Arable land": 0.0, "Other land": 8.0},
|
| 1456 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 1457 |
+
method_costs={"Afforestation": {"Arable land": 1.0, "Other land": 1.0}},
|
| 1458 |
+
custom_resources=[],
|
| 1459 |
+
resource_to_group=FAM_GROUPS,
|
| 1460 |
+
)
|
| 1461 |
+
_check("17.5 One pool member at 0 β total = 8 (fully from the other)",
|
| 1462 |
+
ok and abs(res["total_removed"] - 8.0) < 1e-6,
|
| 1463 |
+
f"total={res.get('total_removed') if ok else res}")
|
| 1464 |
+
check_17_5()
|
| 1465 |
+
|
| 1466 |
+
# ββ 17.6 A method with only ONE resource from the family (no sibling) is
|
| 1467 |
+
# never pooled, even when resource_to_group is supplied β singleton case is
|
| 1468 |
+
# unaffected. βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 1469 |
+
def check_17_6():
|
| 1470 |
+
ok, res = run_optimisation(
|
| 1471 |
+
resource_caps={"Arable land": 6.0},
|
| 1472 |
+
method_constraints={"Agroforestry": {"active": True, "cap_type": "percent", "cap_value": 100}},
|
| 1473 |
+
method_costs={"Agroforestry": {"Arable land": 2.0}},
|
| 1474 |
+
custom_resources=[],
|
| 1475 |
+
resource_to_group=FAM_GROUPS,
|
| 1476 |
+
)
|
| 1477 |
+
_check("17.6 Single family member (no sibling) β unaffected (total = 3.0)",
|
| 1478 |
+
ok and abs(res["total_removed"] - 3.0) < 1e-6,
|
| 1479 |
+
f"total={res.get('total_removed') if ok else res}")
|
| 1480 |
+
check_17_6()
|
| 1481 |
+
|
| 1482 |
+
# ββ 17.7 Percent cap on a pooled method is computed against its pooled (OR)
|
| 1483 |
+
# standalone potential, not the AND-bounded one. ββββββββββββββββββββββββββββ
|
| 1484 |
+
def check_17_7():
|
| 1485 |
+
ok, res = run_optimisation(
|
| 1486 |
+
resource_caps={"Arable land": 10.0, "Other land": 5.0},
|
| 1487 |
+
method_constraints={"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 50}},
|
| 1488 |
+
method_costs={"Afforestation": {"Arable land": 1.0, "Other land": 1.0}},
|
| 1489 |
+
custom_resources=[],
|
| 1490 |
+
resource_to_group=FAM_GROUPS,
|
| 1491 |
+
)
|
| 1492 |
+
_check("17.7 50% cap of pooled potential (15) β total = 7.5",
|
| 1493 |
+
ok and abs(res["total_removed"] - 7.5) < 1e-4,
|
| 1494 |
+
f"total={res.get('total_removed') if ok else res}")
|
| 1495 |
+
check_17_7()
|
| 1496 |
+
|
| 1497 |
+
# ββ 17.8 Two methods sharing a pooled resource with a non-pooled method that
|
| 1498 |
+
# also draws on one of the family members β the non-pooled method's own usage
|
| 1499 |
+
# must not be swallowed by the pool. βββββββββββββββββββββββββββββοΏ½οΏ½οΏ½ββββββββββ
|
| 1500 |
+
def check_17_8():
|
| 1501 |
+
ok, res = run_optimisation(
|
| 1502 |
+
resource_caps={"Arable land": 10.0, "Other land": 5.0},
|
| 1503 |
+
method_constraints={
|
| 1504 |
+
"Afforestation": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1505 |
+
"Cropland management": {"active": True, "cap_type": "percent", "cap_value": 100},
|
| 1506 |
+
},
|
| 1507 |
+
method_costs={
|
| 1508 |
+
"Afforestation": {"Arable land": 1.0, "Other land": 1.0},
|
| 1509 |
+
"Cropland management": {"Arable land": 1.0},
|
| 1510 |
+
},
|
| 1511 |
+
custom_resources=[],
|
| 1512 |
+
resource_to_group=FAM_GROUPS,
|
| 1513 |
+
)
|
| 1514 |
+
# Both methods compete for total CO2; Arable land (10) is shared between
|
| 1515 |
+
# Afforestation's pool and Cropland's direct use, Other land (5) only
|
| 1516 |
+
# available to Afforestation's pool. Total achievable = 15 either way
|
| 1517 |
+
# (Cropland can use Arable land directly, or Afforestation can via the pool)
|
| 1518 |
+
# β what matters here is that it solves without error and fully uses both land pools.
|
| 1519 |
+
total_arable = 0.0
|
| 1520 |
+
total_other = 0.0
|
| 1521 |
+
if ok:
|
| 1522 |
+
usage = res["resource_usage"]
|
| 1523 |
+
total_arable = sum(usage.get("Arable land", {}).values())
|
| 1524 |
+
total_other = sum(usage.get("Other land", {}).values())
|
| 1525 |
+
_check("17.8 Pool alongside a non-pooled method sharing a family member β solves, fully uses both",
|
| 1526 |
+
ok and abs(total_arable - 10.0) < 1e-4 and abs(total_other - 5.0) < 1e-4,
|
| 1527 |
+
f"ok={ok} arable_used={total_arable} other_used={total_other} total={res.get('total_removed') if ok else res}")
|
| 1528 |
+
check_17_8()
|