Introduction to Optimization#

Overview#

Power system operators face countless decisions every day. Which generators should run? How should power flow through the network? Where should we build new transmission lines? These questions share a common structure: we want to find the best solution among many possibilities while respecting physical and operational constraints.

Optimization provides the mathematical framework to answer these questions systematically. This lesson introduces fundamental optimization concepts through concrete power system examples. By the end, you’ll be able to recognize optimization problems in power systems and formulate them mathematically - the crucial first step toward finding solutions.

Learning Objectives#

By completing this lesson, you will be able to:

  1. Recognize optimization problems in power system contexts

  2. Identify decision variables, objectives, and constraints

  3. Classify optimization problems by type (linear, nonlinear, integer)

  4. Formulate optimization problems in standard mathematical form

  5. Solve simple problems graphically to build intuition

  6. Understand why problem structure affects solvability

# Import required libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.patches import Polygon
from matplotlib import cm
import warnings
warnings.filterwarnings('ignore')

# Set up plotting style
plt.style.use('seaborn-v0_8-whitegrid')
plt.rcParams['figure.figsize'] = (10, 6)
plt.rcParams['font.size'] = 12

print("Libraries loaded successfully")
Libraries loaded successfully

1. What is Optimization?#

Optimization is about making the best decision given constraints. In power systems, “best” usually means minimizing cost or maximizing reliability, while constraints represent physical laws and operational limits.

Consider three fundamental power system optimization problems:

Economic Dispatch: Given online generators, how much should each produce to minimize total cost while meeting demand?

Transmission Planning: Where should we build new lines to minimize investment while ensuring reliable power delivery?

Voltage Control: How should we adjust transformer taps and reactive power to maintain voltages within limits using minimal control actions?

Each problem involves:

  • Decisions to be made (generator outputs, line locations, tap settings)

  • An objective to optimize (minimize cost, maximize reliability)

  • Constraints that must be satisfied (power balance, line limits, voltage bounds)

Let’s explore a simple economic dispatch example to see these components in action.

Generator Characteristics:
  Generator  Min_MW  Max_MW  Cost_$/MWh
0        G1      20     100          25
1        G2      30      80          30

System demand: 150 MW

Optimization Components:
Decisions: How much power each generator produces (P1, P2)
Objective: Minimize total cost = 25*P1 + 30*P2
Constraints:
  - Power balance: P1 + P2 = 150
  - G1 limits: 20 ≤ P1 ≤ 100
  - G2 limits: 30 ≤ P2 ≤ 80

Without optimization, we might dispatch generators equally (75 MW each) or proportionally to their capacity. But optimization tells us to use the cheaper generator (G1) as much as possible:

  • Optimal solution: P1 = 100 MW (at maximum), P2 = 50 MW

  • Total cost: \(25 × 100 + \)30 × 50 = $4,000/hour

This saves $125/hour compared to equal dispatch. Over a year, such optimization saves millions of dollars.

2. Components of Optimization Problems#

Every optimization problem has three essential components. Learning to identify these in problem descriptions is the key to successful formulation.

Decision Variables#

These are the quantities we control. In power systems:

  • Generator outputs (MW)

  • Transformer tap positions

  • Line flows (in some formulations)

  • Binary variables (on/off decisions)

Objective Function#

This quantifies what we want to optimize:

  • Minimize: generation cost, power losses, control actions

  • Maximize: social welfare, reliability, renewable integration

Constraints#

These are the requirements that must be satisfied:

  • Equality constraints: power balance (generation = demand + losses)

  • Inequality constraints: line flow limits, voltage bounds, generator limits

  • Some constraints come from physics (Kirchhoff’s laws), others from equipment ratings or operational policies

Exercise 1: Identify Optimization Components

A system operator must decide which generators to start up for tomorrow. The system has:

  • 5 generators with different startup costs and minimum run times

  • Hourly demand that varies from 300 MW (night) to 500 MW (day)

  • A requirement to maintain 50 MW of spinning reserve at all times

  • Each generator has minimum and maximum output limits when running

Identify:

  1. What are the decision variables?

  2. What is the objective?

  3. List at least 5 types of constraints

# Exercise 1: Your answer here
# Identify the optimization components for the unit commitment problem

# Your solution:

Hide code cell outputs

Unit Commitment Problem Components:

1. Decision Variables:
   - Binary variables: u[g,t] = 1 if generator g is on in hour t, 0 otherwise
   - Continuous variables: p[g,t] = power output of generator g in hour t (MW)
   - Binary variables: v[g,t] = 1 if generator g starts up at hour t
   - Binary variables: w[g,t] = 1 if generator g shuts down at hour t

2. Objective Function:
   Minimize total cost = generation cost + startup cost
   = sum over all g,t of (fuel_cost[g] * p[g,t] + startup_cost[g] * v[g,t])

3. Constraints:
   a) Power balance: sum of p[g,t] = demand[t] for all hours t
   b) Reserve requirement: sum of (p_max[g] * u[g,t] - p[g,t]) >= 50 MW
   c) Generator limits: p_min[g] * u[g,t] <= p[g,t] <= p_max[g] * u[g,t]
   d) Minimum run time: if generator starts, must stay on for min_run_time[g] hours
   e) Minimum down time: if generator stops, must stay off for min_down_time[g] hours
   f) Startup/shutdown logic: v[g,t] >= u[g,t] - u[g,t-1]
   g) Ramping constraints: -ramp_down[g] <= p[g,t] - p[g,t-1] <= ramp_up[g]

This is a mixed-integer linear program (MILP) due to binary variables.

3. Types of Optimization Problems#

The mathematical structure of an optimization problem determines how difficult it is to solve and what solution methods apply. Understanding these classifications helps you choose appropriate solution techniques.

Linear vs. Nonlinear#

Linear programs (LP) have linear objectives and constraints:

  • DC optimal power flow

  • Economic dispatch with linear costs

  • Basic transmission planning

Nonlinear programs (NLP) include nonlinear relationships:

  • AC optimal power flow (power flow equations are nonlinear)

  • Loss minimization (losses are quadratic in current)

  • Voltage optimization

Continuous vs. Discrete#

Continuous variables can take any value in a range:

  • Generator output (e.g., 57.3 MW)

  • Voltage magnitude (e.g., 1.03 per unit)

Discrete variables have specific allowed values:

  • Binary: generator on/off, line built/not built

  • Integer: number of capacitor banks, transformer tap position

Convex vs. Non-convex#

Convex problems have a unique global optimum:

  • Linear programs are always convex

  • Some specially structured nonlinear problems

Non-convex problems may have multiple local optima:

  • AC optimal power flow

  • Most problems with discrete variables

../../_images/bad9ea1d3358860b67a4a88eace5ceec0b3c62790b262508a6239c9646efa304.png
* Linear in continuous variables, but has integer variables

4. Graphical Interpretation#

For problems with two variables, we can visualize the optimization process. This builds intuition for how constraints define feasible regions and how optimal solutions often lie at constraint boundaries.

Let’s solve our two-generator economic dispatch problem graphically.

../../_images/9366246c39bfa132b73f6ea54f479e64e414262fd191b0cae37acd0e6f5e6cec.png
Solution Analysis:
The optimal solution occurs where the cost contour is tangent to the feasible region.
Since G1 is cheaper ($25/MWh vs $30/MWh), we use it at maximum capacity.

Optimal dispatch: P1 = 100 MW, P2 = 50 MW
Total cost: $4000/hour

Note: The solution is at the intersection of two constraints:
  - Power balance: P1 + P2 = 150
  - G1 upper limit: P1 = 100

Exercise 2: Graphical Optimization

The system demand increases to 170 MW. Using the same two generators:

  • G1: 20-100 MW, $25/MWh

  • G2: 30-80 MW, $30/MWh

  1. Is the problem still feasible?

  2. What is the new optimal solution?

  3. What is the marginal cost of this extra 20 MW?

# Exercise 2: Your solution here
# Analyze the system with 170 MW demand

# Your solution:

Hide code cell outputs

Feasibility Analysis:
Demand: 170 MW
Minimum possible generation: 50 MW
Maximum possible generation: 180 MW

Is feasible? True

Optimal solution:
P1 = 100 MW (at maximum)
P2 = 70 MW

Total cost: $4600/hour
Previous cost (150 MW): $4000/hour

Marginal cost of extra 20 MW: $600/hour
Marginal cost per MW: $30.0/MWh

Insight: The marginal cost equals G2's cost ($30/MWh) because
the additional power comes entirely from the more expensive generator.
../../_images/d8d7129fa3f2f373a316675c7125d875e5ff8cc2721bafad16acd3a281e11d51.png

5. Mathematical Formulation#

To solve optimization problems computationally, we need to express them in standard mathematical form. This systematic approach ensures we capture all aspects of the problem and prepares it for solution algorithms.

Standard Form for Minimization#

Minimize: \(f(x)\) (objective function)

Subject to:

  • \(g_i(x) \leq 0\) for \(i = 1, ..., m\) (inequality constraints)

  • \(h_j(x) = 0\) for \(j = 1, ..., p\) (equality constraints)

  • \(x_l \leq x \leq x_u\) (variable bounds)

Where \(x\) is the vector of decision variables.

Converting to Standard Form#

  1. Maximization to minimization: Maximize \(f(x)\) → Minimize \(-f(x)\)

  2. Greater-than inequalities: \(g(x) \geq b\)\(-g(x) \leq -b\)

  3. Absolute values: \(|x| \leq b\)\(x \leq b\) and \(-x \leq b\)

  4. Ranges: \(a \leq x \leq b\)\(x \geq a\) and \(x \leq b\)

Economic Dispatch - Mathematical Formulation
==================================================

Given data:
3 generators with quadratic cost functions
System demand: 400 MW
Reserve requirement: 50 MW

Generator parameters:
  Generator  Pmin (MW)  Pmax (MW)  a ($/h)  b ($/MWh)  c ($/MW²h)
0        G1         50        200      500         20       0.020
1        G2         40        150      300         25       0.030
2        G3         60        180      400         22       0.025

Cost function: C_i(P_i) = a_i + b_i*P_i + c_i*P_i²

==================================================
MATHEMATICAL FORMULATION
==================================================

Decision Variables:
P₁, P₂, P₃ = Power output of generators 1, 2, 3 (MW)

Objective Function:
Minimize: C_total = C₁(P₁) + C₂(P₂) + C₃(P₃)
        = (500 + 20P₁ + 0.02P₁²) + (300 + 25P₂ + 0.03P₂²) + (400 + 22P₃ + 0.025P₃²)
        = 1200 + 20P₁ + 25P₂ + 22P₃ + 0.02P₁² + 0.03P₂² + 0.025P₃²

Constraints:

1. Power Balance (equality):
   P₁ + P₂ + P₃ = 400

2. Reserve Requirement (inequality):
   (200 - P₁) + (150 - P₂) + (180 - P₃) ≥ 50
   Simplifying: P₁ + P₂ + P₃ ≤ 480

3. Generator Limits (bounds):
   50 ≤ P₁ ≤ 200
   40 ≤ P₂ ≤ 150
   60 ≤ P₃ ≤ 180

==================================================
PROBLEM CLASSIFICATION
==================================================
Type: Quadratic Programming (QP)
- Quadratic objective function
- Linear constraints
- Continuous variables
- Convex (positive semi-definite Hessian)

Solution approach: Can be solved efficiently to global optimum

Exercise 3: Problem Formulation

A utility must decide where to install capacitor banks to improve voltage profile. The system has:

  • 5 candidate buses for capacitor installation

  • Each location can have 0, 1, or 2 capacitor banks

  • Each bank costs $10,000 and provides 5 MVAR

  • Goal: Minimize voltage deviations from 1.0 pu while limiting cost to $50,000

Formulate this as an optimization problem. Identify:

  1. Decision variables and their type

  2. Objective function

  3. All constraints

  4. Problem classification

# Exercise 3: Your solution here
# Formulate the capacitor placement problem

# Your solution:

Hide code cell outputs

Capacitor Placement Problem Formulation
==================================================

1. DECISION VARIABLES:
   n_i = Number of capacitor banks at bus i (i = 1, 2, 3, 4, 5)
   Type: Integer variables with n_i ∈ {0, 1, 2}

   Note: These will determine reactive power injection:
   Q_i = 5 * n_i MVAR at each bus

2. OBJECTIVE FUNCTION:
   Minimize: sum over all buses k of |V_k - 1.0|²

   Where V_k depends on capacitor placement through power flow equations:
   V_k = f(n_1, n_2, n_3, n_4, n_5)

   This is a nonlinear objective because voltage magnitudes depend
   nonlinearly on reactive power injections.

3. CONSTRAINTS:

   a) Budget constraint:
      10000 * (n_1 + n_2 + n_3 + n_4 + n_5) ≤ 50000
      Simplifying: n_1 + n_2 + n_3 + n_4 + n_5 ≤ 5

   b) Installation limits:
      0 ≤ n_i ≤ 2 for all i
      n_i integer for all i

   c) Power flow constraints (implicit):
      Power flow equations must be satisfied
      These relate voltages to power injections

   d) Voltage limits (optional):
      0.95 ≤ V_k ≤ 1.05 for all buses k

4. PROBLEM CLASSIFICATION:
   Type: Mixed-Integer Nonlinear Program (MINLP)
   - Integer decision variables (n_i)
   - Nonlinear objective (voltage deviations)
   - Mix of linear and nonlinear constraints
   - Non-convex due to power flow equations

5. SOLUTION APPROACH:
   This is a challenging problem class. Approaches include:
   - Enumerate all feasible integer combinations (5 buses × 3 options = 243 cases)
   - Use MINLP solvers with branch-and-bound
   - Approximate with linear power flow models
   - Use metaheuristics (genetic algorithms, particle swarm)
../../_images/1ab44edfbfc6d6e358f795fe811520536cf0aa610ae83127421b0be6c12ad92d.png

6. Solution Concepts#

Understanding key solution concepts helps interpret optimization results and diagnose problems.

Feasibility vs. Optimality#

Feasible solution: Satisfies all constraints but may not be optimal Optimal solution: Feasible and achieves the best objective value Infeasible problem: No solution satisfies all constraints

Local vs. Global Optima#

Local optimum: Best solution in a neighborhood Global optimum: Best solution overall

  • Convex problems have only global optima

  • Non-convex problems may have multiple local optima

Sensitivity Analysis#

How does the solution change if we perturb:

  • Objective coefficients (e.g., fuel prices)

  • Constraint bounds (e.g., transmission limits)

  • Problem data (e.g., demand forecasts)

Shadow prices (dual variables) tell us the marginal value of relaxing constraints.

../../_images/25801bc5130a0124b1b763d00cf0afc405fa2b27d35c5c3a595459a1749f85c7.png
Key Insights:
1. Multiple feasible solutions exist, but only one (or a set) is optimal
2. Non-convex problems require global optimization techniques
3. Shadow prices indicate the value of relaxing constraints
4. Problem feasibility depends on the relationship between constraints

Exercise 4: Transmission Planning Formulation

A utility is planning transmission expansion. They have:

  • 3 existing buses with generators

  • 2 new load centers to connect

  • 4 possible transmission corridors

  • Each line costs $1M per mile

  • Must ensure all loads can be served even with any single line outage (N-1)

Formulate this as an optimization problem:

  1. What makes this problem particularly challenging?

  2. What type of optimization problem is this?

  3. Write the objective and key constraints

# Exercise 4: Your solution here
# Formulate the transmission planning problem

# Your solution:

Hide code cell outputs

Transmission Planning Problem Formulation
============================================================
Possible transmission corridors:
  Corridor  From     To  Distance (miles)  Capacity (MW)
0       C1  Gen1  Load1                50            200
1       C2  Gen2  Load1                40            200
2       C3  Gen3  Load2                60            200
3       C4  Gen1  Load2                80            200

1. WHAT MAKES THIS CHALLENGING:
   a) Combinatorial nature: 2^4 = 16 possible network configurations
   b) N-1 security: Must check all contingencies for each configuration
   c) Power flow coupling: Line flows depend on entire network topology
   d) Long-term uncertainty: Load growth, generation changes
   e) Discrete decisions: Can't build 'half a line'

2. PROBLEM TYPE:
   Mixed-Integer Linear Program (MILP) if using DC power flow
   - Binary variables for line construction decisions
   - Linear power flow constraints (DC approximation)
   - Multiple scenarios for contingencies

3. MATHEMATICAL FORMULATION:

Decision Variables:
   x_i = 1 if corridor i is built, 0 otherwise (i = 1,2,3,4)
   f_ij^s = Power flow on line from i to j in scenario s
   g_i^s = Generation at bus i in scenario s

Objective Function:
   Minimize: Investment Cost
   = 1,000,000 × (50×x₁ + 40×x₂ + 60×x₃ + 80×x₄)

Constraints:

   a) Power balance at each bus in each scenario:
      sum of flows in - sum of flows out + generation = load

   b) Flow limits on built lines:
      -200×x_i ≤ f_i^s ≤ 200×x_i for each corridor i and scenario s
      (If line not built, flow must be zero)

   c) Generation limits:
      0 ≤ g_i^s ≤ G_max_i for each generator i

   d) Connectivity requirement:
      Each load must be connected to at least one generator
      For Load1: x₁ + x₂ ≥ 1
      For Load2: x₃ + x₄ ≥ 1

   e) N-1 security (key constraint):
      All demands must be met in scenarios where any one line is out
      This creates |lines| + 1 scenarios to consider

   f) DC power flow physics:
      Flows must satisfy Kirchhoff's laws (linear with DC approximation)
../../_images/6069c05e5550c99177612507d356a847ffe639e7e8454f6b7e071074c1f44fec.png
SOLUTION APPROACH:
1. Enumerate all 2^4 = 16 possible network configurations
2. For each configuration, check N-1 security
3. Select cheapest configuration that meets all constraints

For larger systems, use MILP solvers with branch-and-bound

Exercise 5: Complete Problem Formulation

An independent power producer wants to optimize the operation of their portfolio:

  • 2 gas turbines: 50 MW each, $40/MWh operating cost, 5-minute start time

  • 1 coal plant: 200 MW, $25/MWh, 4-hour start time, minimum 100 MW when on

  • 1 battery: 20 MW / 80 MWh capacity, 90% round-trip efficiency

  • Must provide 150 MW firm capacity during peak hours (3-7 PM)

  • Energy prices vary hourly based on market forecast

Formulate a 24-hour optimization problem to maximize profit.

# Exercise 5: Your solution here
# Formulate the portfolio optimization problem

# Your solution:

Hide code cell outputs

Portfolio Optimization Problem
============================================================
GIVEN DATA:

Generation Resources:
- Gas Turbine 1: 0-50 MW, $40/MWh, fast start
- Gas Turbine 2: 0-50 MW, $40/MWh, fast start
- Coal Plant: 0 or 100-200 MW, $25/MWh, 4-hour start
- Battery: ±20 MW, 80 MWh capacity, 90% efficiency
../../_images/e68b55abedeba5329bfef697f6483017dfed32222091dd21fad7db60e05c86a0.png
============================================================
MATHEMATICAL FORMULATION
============================================================

DECISION VARIABLES:
For each hour t = 0, ..., 23:
- p_g1[t] = Gas turbine 1 output (MW)
- p_g2[t] = Gas turbine 2 output (MW)
- p_c[t] = Coal plant output (MW)
- u_c[t] = Coal plant on/off (binary)
- p_b[t] = Battery discharge (positive) or charge (negative) (MW)
- e_b[t] = Battery energy level (MWh)
- v_c[t] = Coal plant startup indicator (binary)

OBJECTIVE FUNCTION:
Maximize: Profit = Revenue - Cost
= sum_t [price[t] × (p_g1[t] + p_g2[t] + p_c[t] + p_b[t])
        - 40 × (p_g1[t] + p_g2[t]) - 25 × p_c[t]]

Note: Battery has no fuel cost, only efficiency losses

CONSTRAINTS:

1. Generation Limits:
   0 ≤ p_g1[t] ≤ 50 for all t
   0 ≤ p_g2[t] ≤ 50 for all t
   100×u_c[t] ≤ p_c[t] ≤ 200×u_c[t] for all t
   -20 ≤ p_b[t] ≤ 20 for all t

2. Coal Plant Startup Logic:
   v_c[t] ≥ u_c[t] - u_c[t-1] for t > 0
   v_c[0] ≥ u_c[0] (assuming off initially)

3. Coal Plant Minimum Up Time (4 hours):
   sum(u_c[k] for k in t to t+3) ≥ 4×v_c[t]
   (If started at t, must run for at least 4 hours)

4. Battery Energy Balance:
   e_b[t+1] = e_b[t] - p_b[t] + 0.9×min(0, p_b[t])
   (Charging has 90% efficiency, discharging is 100%)
   0 ≤ e_b[t] ≤ 80 for all t
   e_b[0] = 40 (initial state)
   e_b[24] ≥ 40 (end state requirement)

5. Firm Capacity Requirement (hours 15-19):
   p_g1[t] + p_g2[t] + p_c[t] + max(0, p_b[t]) ≥ 150
   for t in {15, 16, 17, 18}
   (Battery discharge counts, charging doesn't)

6. Battery Power-Energy Relationship:
   Can't discharge more than available energy
   Can't charge beyond capacity

PROBLEM CLASSIFICATION:
- Mixed-Integer Linear Program (MILP)
- Binary variables for coal plant operation
- Continuous variables for power outputs
- Linear constraints throughout

SOLUTION INSIGHTS:
- Gas turbines will run only during high-price hours (> $40/MWh)
- Coal plant will run if average price over 4+ hours exceeds $25/MWh
- Battery will charge during low prices and discharge during peaks
- Firm capacity requirement may force suboptimal generation
../../_images/9850c774bafa6ba4172643f1359ad2d82434cb896b426cadb4962aa5beae87c5.png

Summary#

This lesson introduced the fundamental concepts of optimization through power system examples. You learned to:

  1. Recognize optimization problems in power system contexts, from economic dispatch to transmission planning

  2. Identify the three key components: decision variables (what we control), objective function (what we optimize), and constraints (what we must satisfy)

  3. Classify problems by type: linear vs. nonlinear, continuous vs. discrete, convex vs. non-convex - understanding that problem structure determines solution difficulty

  4. Formulate problems mathematically, converting verbal descriptions into precise mathematical statements ready for computational solution

  5. Visualize simple problems graphically, seeing how constraints create feasible regions and optimal solutions lie at boundaries

  6. Understand solution concepts like feasibility, optimality, and sensitivity that help interpret results

Key Takeaways#

  • Formulation is crucial: A well-formulated problem is halfway to solution. Poor formulation leads to wrong answers or computational intractability.

  • Problem structure matters: Linear problems are reliably solvable even at large scale. Integer variables and nonlinearity add significant complexity.

  • Constraints define feasibility: In power systems, constraints come from physics (power balance), equipment (ratings), and policy (reliability standards).

  • Optimal solutions often lie at boundaries: This explains why generators often run at minimum or maximum, and why transmission lines congest.

  • Multiple objectives require trade-offs: Cost, reliability, and environmental goals often conflict, requiring careful balancing.

Next Steps#

With these fundamentals established, you’re ready to implement optimization problems computationally. In the next lesson, we’ll explore Python tools for solving linear programs, starting with the economic dispatch and market clearing problems we formulated here.

Remember: every optimization problem in power systems started as a real operational challenge. By mastering these techniques, you’ll be able to tackle new challenges as the grid evolves.