OptiQ / api /routes /validate.py
AhmedSamir1598's picture
first baseline for project OptiQ. Contains research resources, first baseline using GNNs + QC, and benchmarks against current industry standards, while addressing the challenges that prevents better practices to be used in industry.
55e3496
"""
Validation Endpoint — Returns all hackathon validation answers as structured JSON.
GET /api/validate
GET /api/validate/{system}
Answers:
1. Time to full realization (scaling roadmap)
2. Number of dependent variables
3. Global energy impact percentage
4. Egypt-specific impact
5. Solution energy consumption
6. Solution CO2 emissions
7. Before/after computational efficiency
8. Net energy and CO2 after solution
"""
from __future__ import annotations
import time
from fastapi import APIRouter
from config import CFG
from src.grid.loader import load_network
from src.grid.power_flow import get_baseline, evaluate_topology
from src.quantum.qaoa_reconfig import solve_sa
from src.evaluation.metrics import (
compute_impact,
compute_solution_footprint,
compute_net_benefit,
compute_egypt_impact,
compute_business_model,
count_dependent_variables,
)
router = APIRouter()
@router.get("/validate/{system}")
@router.get("/validate")
def get_validation(system: str = "case33bw"):
"""Return all hackathon validation answers for the given system.
This endpoint runs a quick optimisation and computes all metrics needed
to answer the hackathon judges' questions.
"""
# Load network and compute baseline
net = load_network(system)
baseline = get_baseline(net)
if not baseline.get("converged"):
return {"error": "Baseline power flow did not converge."}
# Run quantum SA optimisation (lightweight)
t0 = time.perf_counter()
sa_result = solve_sa(net, n_iter=300, n_restarts=3, top_k=3)
optimisation_time = time.perf_counter() - t0
if "error" in sa_result:
return {"error": f"Optimisation failed: {sa_result['error']}"}
ev = evaluate_topology(net, sa_result["best_open_lines"])
if not ev.get("converged"):
return {"error": "Optimised topology did not converge."}
# Compute all metrics
impact = compute_impact(baseline, ev)
footprint = compute_solution_footprint(optimisation_time)
net_benefit = compute_net_benefit(impact, footprint)
egypt = compute_egypt_impact(impact["loss_reduction_pct"])
variables = count_dependent_variables()
business = compute_business_model(impact)
# Published comparison
published = {
"case33bw": {
"base_loss_kw": 202.67,
"optimal_loss_kw": 139.55,
"optimal_reduction_pct": 31.15,
"source": "Baran & Wu 1989, reproduced by PSO, GA, MILP",
}
}
return {
"system": system,
# Q1: How can the solution be implemented?
"implementation_plan": egypt["implementation_plan"],
# Q2: Is it one-time or recurring? Per what?
"usage_model": business["usage_model"],
# Q3: Pricing and revenue
"business_model": {
"savings_per_feeder": business["savings_per_feeder"],
"pricing_models": business["pricing_models"],
"revenue_projections": business["revenue_projections"],
},
# Q4: Comparison against existing solutions
"competitive_analysis": business["comparison_to_alternatives"],
# Q5: Number of dependent variables
"dependent_variables": variables,
# Q6: Global energy impact
"global_impact": egypt["global"],
# Q7: Egypt-specific impact
"egypt_impact": egypt["egypt"],
"cairo_impact": egypt["cairo"],
# Q8: Waste elimination (not solution-vs-consumption)
"waste_elimination": {
"baseline_waste_kwh_year": net_benefit["baseline_waste_kwh_year"],
"optimized_waste_kwh_year": net_benefit["optimized_waste_kwh_year"],
"waste_eliminated_kwh_year": net_benefit["waste_eliminated_kwh_year"],
"waste_eliminated_pct": net_benefit["waste_eliminated_pct"],
"solution_overhead_pct": net_benefit["solution_overhead_pct_of_savings"],
},
# Q9: Solution energy footprint
"solution_footprint": footprint,
# Q10: CO2 trustworthiness
"trustworthiness": net_benefit["trustworthiness"],
# Q11: Before/after comparison
"before_after": {
"baseline_loss_kw": impact["baseline_loss_kw"],
"optimized_loss_kw": impact["optimized_loss_kw"],
"loss_reduction_kw": impact["loss_reduction_kw"],
"loss_reduction_pct": impact["loss_reduction_pct"],
"baseline_min_voltage": impact["baseline_min_voltage"],
"optimized_min_voltage": impact["optimized_min_voltage"],
"voltage_violations_fixed": impact["voltage_violations_fixed"],
"computation_time_sec": round(optimisation_time, 3),
},
# Comparison with published
"published_comparison": published.get(system, {}),
# Annual impact (single feeder)
"annual_impact_single_feeder": {
"energy_saved_mwh": impact["energy_saved_mwh_year"],
"co2_saved_tonnes": impact["co2_saved_tonnes_year"],
"cost_saved_usd": impact["cost_saved_usd_year"],
"trees_equivalent": impact["equivalent_trees_planted"],
"cars_removed": impact["equivalent_cars_removed"],
},
}