## 3 Framework
### Bi-Level Optimization Model  
Having established the necessary mathematical concepts in Sections 3.1 and 3.2, an optimization framework for estimating measurement model parameters (i.e., \(g_{km}\), \(b_{km}\) and \(b_{km}^{sh}\)) for any line connecting bus \(k\) and bus \(m\) can be formulated. The framework hypothesis that a free of attack SE output sample exists. Let us label this sample with \(t^{-}\). Hence, at time \(t^{-}\), system states are estimated (i.e., \(E_{k}^{t^{-}}\) and \(E_{m}^{t^{-}}\)). If \(\{P_{km}\) or \(P_{mk}\}\) and \(\{Q_{km}\) or \(Q_{mk}\}\) are part of the measurement set, then estimated measurements \(h_{km}^{p}\) and \(h_{km}^{Q}\) are already available. If not, an estimated measurement out of \(\{P_{km},P_{mk}\}\) and an estimated measurement out of \(\{Q_{km},Q_{mk}\}\) are generated after SE is converged. This step can be augmented to the existing SE routine without a major modification. Therefore, the bi-level model can be derived as:  
\[\min_{x_{u}} \sum_{i=1}^{m}\Biggl{(}1+\frac{1}{II_{i}(x_{u},x_{l})^{2}}\Biggr{)} W_{ii}r_{i}^{2}(x_{u},x_{l}).\] (22) s.t. \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\] \[g_{km}=g_{km}^{pert}-\Delta g_{km},\quad\forall km\in\mathcal{L}\] (24) \[b_{km}=b_{km}^{pert}-\Delta b_{km},\quad\forall km\in\mathcal{L}\] (25) \[b_{km}^{sh}=b_{km}^{sh,pert}-\Delta b_{km}^{sh},\quad\forall km \in\mathcal{L}\] (26) \[x_{l}\in\Psi(x_{u}) \tag{27}\]  
where \(\mathbf{x_{u}}\) is the decision variable vector for the upper-level optimization problem, i.e., voltage magnitude \(V\) and voltage angle \(\theta\) for all buses, and \(\mathbf{x_{l}}\) is the decision variable vector for the lower-level optimization problems, i.e., deviations in system database \(\Delta g_{km},\Delta b_{km}\), and \(\Delta b_{km}^{sh}\) for all lines. The variable \(\mathcal{L}\) is the set of lines in the system under study, and \(\Psi(x_{u})\) is a parameterized range constraint for the lower-level decision vector \(x_{l}\). Such constraint is obtained through the lower-level (follower) optimization problem defined as follows:  
\[\min_{x_{l}} \Delta g_{km}+\Delta b_{km}+\Delta b_{km}^{sh}\] (28) s.t. \[\Delta g_{km}=g_{km}^{pert}-g_{km} \tag{29}\] \[\Delta g_{km}=g_{km}^{pert}-g_{km}\] (30) \[\Delta b_{km}=b_{km}^{pert}-b_{km}\] (31) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]0\[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]1 \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]2 (34) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]3 (35) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]4 (36) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]5 (37) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]6 (38) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]7 (39) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]8 (40) \[r_{i}=z_{i}-h_{i}(x_{u},x_{l}),\quad\forall i=1,2,\ldots m \tag{23}\]9 (41) \[g_{km}=g_{km}^{pert}-\Delta g_{km},\quad\forall km\in\mathcal{L}\]0 (42) \[g_{km}=g_{km}^{pert}-\Delta g_{km},\quad\forall km\in\mathcal{L}\]1  
In the upper-level model, the weighted norm of the error at time \(t^{-}\) is minimized [28]. After \(\Delta t\) seconds, the inner-level model, the parameters delta \(g^{pert}_{km}\), \(b^{pert}_{km}\), and \(b^{sh,pert}_{km}\), which are the current status of the database at time \(t=\Delta t+t^{-}\), which the system operator would like to check, are optimized. The variables \(g_{km}\), \(b_{km}\), and \(b^{sh}_{km}\) are the unknown true states of the database that we seek to obtain. The \((X/R)^{(ratio)}\) is the known ratio of line inductance to line resistance. The function \(f^{param}_{mass_{km}}\) is a function evaluation of the coefficient associated with the given parameter _param_ from bus \(k\) to bus \(m\) for the specified measurement type _meas_ as (9) and (10).