Optimal Power Flow (OPF)

In this example, we will see how the problem of Optimal Power Flow (OPF) in a power system can be implemented and solved in AIMMS. In the following, we will first review the OPF problem, and then we will see its implementation in AIMMS.


For more information about the OPF problem modelling and data format used here, see authors’ publication 4 and/or MATPOWER’s documentation 5 as well as a basic introduction if needed 6.

OPF problem

OPF is one of the most important problems to be solved in power systems. Essentially, the goal in OPF is to minimise an objective function (normally the total generation cost) while considering constraints of the power system. In the following, first the OPF formulation is explained, and then its implementation in AIMMS is described.

OPF Formulation

OPF is an optimisation problem. The objective function, as mentioned above, is normally the total cost of generation, and can be defined as below:

(1)\[f(x)= \displaystyle\sum_{i=1}^{ng} f_c (P^{\{ i\}}_g)\]

where \(ng\) is the number of generators, \(f_c\) is the cost for each generator and \(x\) is the vector of the optimisation variables, which are bus powers and voltages:

(2)\[x={[P_g, Q_g, V_a, V_m]}^T\]

The following constraints are defined for the standard AC OPF problem formulation:



(5)\[({P_f}^{\{ i\}}(x))^2+{\left({Q_f}^{\lbrace i\rbrace}(x)\right)}^2 \leq {\left({S}^{\lbrace i\rbrace}_{{L}}\right)}^2\]
(6)\[{\left({P_t}^{\lbrace i\rbrace}(x)\right)}^2+{\left({Q_t}^{\lbrace i\rbrace}(x)\right)}^2 \leq {\left({S}^{\lbrace i\rbrace}_{{L}}\right)}^2\]
  • Equation (3) corresponds to the nodal Active and Reactive power balances for each node in the system.

  • Equation (4) is the definition of the nodal complex power from which nodal Active and Reactive power injections at each node can be calculated.

  • Equations (5) and (6) are the allowable total power carrying capacity limit of transmission lines.

The optimisation problem can therefore be written as below:

\[\begin{split}\min f(x) \\\end{split}\]

Which is subject to satisfying constraints (3), (5), and (6).

Also, note that we have using Matpower’s Branch Model 5 for modelling the network branches. Figure below shows the branch model, where the branch parameters are marked on the diagram. \(rs\) and \(xs\) are the series resistance and reactance, respectively. The total charging susceptance \(bc\) together with these series elements forms the standard \(\pi\) transmission line model. The transformer tap ratio has a magnitude \(m_a\) and phase shift angle theta_sh.


Fig. 77 The network branch model

OPF Implementation in AIMMS

OPF implementation for the IEEE 14-bus system can be downloaded from here.


We request that works that use this project cite the paper below:

Alvarez-Bustos, A., Kazemtabrizi, B., Shahbazi, M. and Acha-Daza, E., 2021. Universal branch model for the solution of optimal power flows in hybrid AC/DC grids. International Journal of Electrical Power & Energy Systems, 126, p.106543.

doi: 10.1016/j.ijepes.2020.106543

The list of identifiers used in this project are shown below. These will be explained in the following.

\[\begin{split}\begin{align} & \textbf{Indices:} \\ &&& \text{$b$} & & \text{Buses} \\ &&& \text{$g$} & & \text{Generators}\\ &&& \text{$l$} & & \text{Lines (branches)} \\ &&& \text{$cc$} & & \text{Cost Coefficients} \\[0.5pc] & \textbf{Parameters:} \\ &&& \text{$VmMax, VmMin$} & & \text{Maximum and minimum voltage for bus $b$} \\ &&& \text{$PL, QL$} & & \text{Load active and reactive power for bus $b$} \\ &&& \text{$GShunt, BShunt$} & & \text{Conductance and susceptance at bus $b$} \\ &&& \text{$Pmin, Pmax, Qmin, Qmax$} & & \text{Maximum and minimum active and reactive powers for generator $g$} \\ &&& \text{$PCostCoeff, QCostCoeff$} & & \text{Cost coefficients for generator $g$} \\ &&& \text{$GenLoc$} & & \text{Location of generator $g$ in the set of buses} \\ &&& \text{$rs , xs$} & & \text{Branch series resistance and reactance for branch $l$} \\ &&& \text{$bc$} & & \text{Branch shunt susceptance for branch $l$} \\ &&& \text{$RateA$} & & \text{Maximum power (or current) for branch $l$} \\ &&& \text{$ma , ThetaSh$} & & \text{Branch model transformer turn ratio parameters for branch $l$} \\ &&& \text{$fb, tb$} & & \text{From and To buses for each branch $l$, from the set of buses}\\[0.5pc] & \textbf{Variables:} \\ &&& \text{$Vm , Va$} & & \text{Voltage magnitude and phase angle for bus $b$} \\ &&& \text{$Pg , Qg$} & & \text{Active and Reactive powers for generator $g$} \\[0.5pc] \end{align}\end{split}\]

The project consists of different parts, which are explained below:

1- Sets

We have buses, generators and branches in our power system. These are defined and shown using sets. For example, the set of all buses in the IEEE 14-bus system is defined as shown in the figure below. Buses here are numbered from B1 to B14. An index b is defined to represent this set. Similarly, an index l refers to all branches (lines) in our system.


Similarly, the set of all generator buses (G1-G5) and branches (L01-L20) are defined as sets. You can see that the generation costs are also defined as a set of three values (CC1-CC3), which represent the three coefficients in the quadratic equation for generation cost:

\[f_c (P^{\{ i\}}_g)= CC1_i {(P^{\{ i\}}_g)}^2 + CC2_i{(P^{\{ i\}}_g)} +CC3_i\]

2- Case data

The information about the system can be added here. For example, for adding \(r_s\) values of branch data, a new parameter rs is added and the index domain is defined as l which is previously defined as the index for branches (in Sets). Figure below shows has this can be done:


By checking the data of the rs parameter (CTRL + D or RIGHT CLICKData…), the \(r_s\) values can be easily added as shown below. Note that because we have defined the domain for \(r_s\) as l, therefore AIMMS automatically asks for rs values for all branches in our system.


3- Variables

The next step is to define the variables, and their limits. The main variables in our optimisation problem are \(x ={[P_g, Q_g, V_a, V_m]}^T\). These are defined as shown in the figure below. Note that AIMMS makes it very easy to define the variables over the right domain. For example, as we have one \(V_a\) and one \(V_m\) for each bus, we have set the index domain for these variables as b, which is the index for the Set Bus. Similarly, \(P_g\) and \(Q_g\) are defined over the domain Gen.


We have also defined other auxiliary variables that we will need later on in our project. Examples are variables that we will need to be checked in our constraints. First, the branch variables (real and imaginary parts of the \([2\times2]\) model’s admittance matrix for each branch) are calculated, and these are then used to determine the power injection variables (active and reactive powers at the two ends of each branch). Note that it is of course possible to write the complex equations for the power injection variables directly, however, breaking the problem down and linking the auxiliary variables makes implementation and debugging much easier.

4- Constraints

Defining the constraints is very straightforward. As an example, the figure below shows how the inequality constraints of (4) can be implemented. Again it is worth noting that by setting the index domain correctly, we have applied the inequality power constraint to all the branches in our system.


5- The objective function

the objective function is defined in the mathematical program identifier named as OPF. The objective is to minimise the variable GenCost which is defined to calculate the total generation cost of the system.


6- Initialisation and Main Execution

The problem is initialised in the PostMainInitialization pre-build procedure and then solved in the MainExecution.

7- Pages

AIMMS also makes it easy to design a data page for interacting with the optimisation problem. For details of how this can be done, refer to this WebUI Tutorial. Here, as shown in figure below, the OPF Solution page is designed to show the solution variables.




Alvarez-Bustos, A., Kazemtabrizi, B., Shahbazi, M. and Acha-Daza, E., 2021. Universal branch model for the solution of optimal power flows in hybrid AC/DC grids. International Journal of Electrical Power & Energy Systems, 126, p.106543. doi: 10.1016/j.ijepes.2020.106543

    1. Zimmerman, C. E. Murillo-Sanchez. MATPOWER User’s Manual, Version 7.1. 2020. [Online]. Available: https://matpower.org/docs/MATPOWER-manual-7.1.pdf doi: 10.5281/zenodo.4074122


Von Meier, A., Electric Power Systems - A Conceptual Introduction (2006) - Wiley, DOI: 10.1002/0470036427 - Chapter 7 - Power Flow Analysis