{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using LHS Value to Debug An Infeasible Problem\n", "\n", "This notebook aims to show how to use the left-hand side (LHS) value (`Constraint.e`) to debug an infeasible problem.\n", "\n", "The LHS value is the value of the left-hand side of the equation. It is useful to check which constraints are violated." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ams" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "ams.config_logger(stream_level=20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, let's solve a DCOPF." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Parsing input file \"/Users/jinningwang/work/ams/ams/cases/matpower/case14.m\"...\n", "Input file parsed in 0.0070 seconds.\n", "Zero line rates detacted in rate_a, rate_b, rate_c, adjusted to 999.\n", "System set up in 0.0034 seconds.\n" ] } ], "source": [ "sp = ams.load(ams.get_case('matpower/case14.m'),\n", " no_output=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Building system matrices\n", "Parsing OModel for \n", "Evaluating OModel for \n", "Finalizing OModel for \n", " initialized in 0.0144 seconds.\n", " solved as optimal in 0.0160 seconds, converged in 11 iterations with CLARABEL.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.run(solver='CLARABEL')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2.21, 0.38, 0. , 0. , 0. ])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.pg.v.round(2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, we can lower down the generator maximum output to create an infeasible problem." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.StaticGen.set(src='pmax', attr='v', idx=[1, 2, 3, 4, 5], value=0.5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Remember to update the corresponding parameters in the routine." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.update('pmax')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, we can see that the problem is infeasible." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "DCOPF failed as infeasible in 9 iterations with CLARABEL!\n" ] }, { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.run(solver='CLARABEL')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then, we can turn off some constraints and sovle it again." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Turn off constraints: pglb, pgub\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.disable(['pglb', 'pgub'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that since there are some constraints touched, the OModel will be updated automatically." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Disabled constraints: pglb, pgub\n", "Finalizing OModel for \n", " initialized in 0.0019 seconds.\n", " solved as optimal in 0.0167 seconds, converged in 9 iterations with CLARABEL.\n" ] }, { "data": { "text/plain": [ "True" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.run(solver='CLARABEL')" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 2.31448356, 0.39836206, -0.04094854, -0.04094854, -0.04094854])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.pg.v" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since all the inequality constraints are written in the form of $Ax -b \\leq 0$, we can check the violated constraints by finding the those with positive `Constraint.e`." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 1.81448356, -0.10163794, -0.54094854, -0.54094854, -0.54094854])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.pgub.e" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-2.31448356, -0.39836206, 0.04094854, 0.04094854, 0.04094854])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sp.DCOPF.pglb.e" ] } ], "metadata": { "kernelspec": { "display_name": "ams", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }