Agentic Application in Power Grid Static Analysis: Automatic Code Generation and Error Correction
Organizations: Sun Yat-sen University, Guangzhou, China
Abstract
This paper introduces an LLM agent that automates power grid static analysis by converting natural language into MATPOWER scripts. The framework utilizes DeepSeek-OCR to build an enhanced vector database from MATPOWER manuals. To ensure reliability, it devises a three-tier error-correction system: a static pre-check, a dynamic feedback loop, and a semantic validator. Operating via the Model Context Protocol, the tool enables asynchronous execution and automatically debugging in MATLAB. Experimental results demonstrate that the system achieves a 82.38% accuracy regarding the code fidelity, effectively eliminating hallucinations even in complex analysis tasks.
Figures & tables
| Ablation Configuration | RAG Mode 3 | |||
| RAG+ | Feedback | Planner | Validator | |
| Full Model | ✓ | ✓ | ✓ | ✓ |
| Single Pass | ✓ | ✓ | ✓ | |
| Simple Search | ✓ | ✓ | ✓ | |
| Execution Only | ✓ | ✓ | ✓ | |
| Task Type | Task ID | Natural Language Request |
| Easy | Task 1 | Load case14. Increase the active load at bus 2 by 15%. Run a DC power flow and display the results. |
| Task 2 | Load case57. Run a standard AC power flow and a DC power flow. Compare the resulting voltage magnitudes (Vm) and active power branch flows (Pf) between the two methods to evaluate the accuracy of the DC linearization. | |
| Task 3 | Load case39. Compare the convergence performance of the standard Newton-Raphson algorithm and the Fast Decoupled (XB) algorithm. Set the maximum number of iterations to 10 for both and report whether each algorithm successfully converges. | |
| Hard | Task 1 | Load case39. Calculate the Total Transfer Capability (TTC) from Area 1 to Area 2. Write a MATLAB loop that iteratively increases Area 2 loads and Area 1 generation by 5% increments. In each step, run a DC OPF and check if any branch flow exceeds its RATE_A limit. Stop the iteration at the last feasible point before a violation and print the maximum successfully transferred power in MW. |
| Task 2 | Load case9. Add a new generator at Bus 4 (a load bus). You must change Bus 4 type to PV in mpc.bus, add the generator row to mpc.gen, and add a cost row to mpc.gencost. Ensure the BUS_TYPE and generator location are perfectly synchronized before running OPF. | |
| Task 3 | Run AC OPF on case30. Implement a user-defined linear constraint Pg1 + Pg2 30 MW using mpc.A, mpc.l, and mpc.u. Manually calculate the column indices for Pg1 and Pg2 based on the variable ordering without using helper functions. |
Agentic Code Generation
Agentic code generation uses AI agents to plan, write, test, and revise executable programs with tools and feedback.
See all 125 papers