Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Algorithms Homework 4

Course policies and AI category

Read the Artificial Intelligence Policy and Collaboration Policy and Honor Code before starting. Assignment-specific directions control. The categories are No AI, AI permitted after independent work, and AI required.

Unless a problem says otherwise, its category is AI permitted after independent work. Spend about 30 minutes on each top-level problem without AI, solution pages, or another person’s help, stopping early if complete. You may consult lecture notes, textbooks, and nonsolution pages of the course website; bias toward those course sources. Afterward, AI and genuine collaboration, including coding together, are permitted. Everyone must contribute intellectually, understand the work, and verify it.

At the end of each top-level problem, add a concise AI disclosure: approximately how long the independent attempt took, how far you got, where you became stuck, any AI or collaborative help used afterward, and how you verified it. If you used no AI, say so. Do not submit prompts or transcripts. Time estimates help the instructor improve the assignment and are not a speed test.

Overview

This assignment covers nonlinear constrained optimization, specifically:

  • Lecture 18: Introduction to Constrained Optimization

  • Lecture 19: KKT Conditions, Multipliers, and Sensitivity

  • Lecture 20: Constraint Qualifications

You may do the derivations on paper; upload a photo or scan of that work with your notebook. The Python cells are short.

Sign convention. Use the convention from Lectures 18 and 19 throughout:

min⁡xf(x)s.t.g(x)≤0,h(x)=0,L(x,u,v)=f(x)+uTg(x)+vTh(x),\min_{\mathbf{x}} f(\mathbf{x}) \quad \text{s.t.} \quad \mathbf{g}(\mathbf{x}) \le \mathbf{0}, \quad \mathbf{h}(\mathbf{x}) = \mathbf{0}, \qquad L(\mathbf{x}, \mathbf{u}, \mathbf{v}) = f(\mathbf{x}) + \mathbf{u}^T \mathbf{g}(\mathbf{x}) + \mathbf{v}^T \mathbf{h}(\mathbf{x}),

with u≥0\mathbf{u} \ge \mathbf{0}. Problem 2 asks how Pyomo’s sign convention for duals relates to this one.

### Load Python libraries

import sys

if "google.colab" in sys.modules:
    !wget "https://raw.githubusercontent.com/ndcbe/optimization/main/notebooks/helper.py"
    import helper

    helper.easy_install()
else:
    sys.path.insert(0, "../")
    import helper
helper.set_plotting_style()

import numpy as np
import sympy as sym
import pyomo.environ as pyo

x1, x2 = sym.symbols("x1 x2", real=True)

Problem 1: Convexity, local minimizers and second-order conditions

Recall that a nonlinear program is convex when:

  • the objective is convex

  • every inequality gi(x)≤0g_i(\mathbf{x}) \le 0 has a convex gig_i, and

  • every equality is affine.

Then every local minimizer is a global minimizer. The test is sufficient, not necessary: a problem can fail it and still be convex.

1a. Is it convex?

For each problem below, decide whether the test certifies convexity. If it does not, decide whether the problem is nonetheless convex. To show a feasible set is not convex, give two feasible points whose midpoint is infeasible.

1a(i). min⁡(x1−2)2+(x2−2)2\min (x_1 - 2)^2 + (x_2 - 2)^2 s.t. x12+x22≤4x_1^2 + x_2^2 \le 4, x1+x2=1x_1 + x_2 = 1.

Answer:

1a(ii). min⁡x1+x2\min x_1 + x_2 s.t. x12+x22=1x_1^2 + x_2^2 = 1.

Answer:

1a(iii). min⁡x1+x2\min x_1 + x_2 s.t. 1−x1x2≤01 - x_1 x_2 \le 0, x1>0x_1 > 0, x2>0x_2 > 0. Hint: is 1−x1x21 - x_1 x_2 convex? Can the same feasible set be written with a convex constraint function?

# Optional: You can use finite difference or Sympy to compute the Hessian of the function f(x1, x2) = 1 - x1*x2.
# Add your solution here

Answer:

1b. Classify the KKT points of the circle problem

For min⁡x1+x2\min x_1 + x_2 s.t. h(x)=x12+x22−1=0h(\mathbf{x}) = x_1^2 + x_2^2 - 1 = 0:

  1. Find every KKT point and its multiplier vv, with L=f+v hL = f + v\,h.

  2. At each one, find the critical directions (here, directions d\mathbf{d} with ∇h(x∗)Td=0\nabla h(\mathbf{x}^*)^T \mathbf{d} = 0) and evaluate dT∇xx2L d\mathbf{d}^T \nabla^2_{\mathbf{xx}} L \, \mathbf{d} on them.

  3. Use the second-order conditions to classify each point. Which is the global minimizer, and how do you know a global minimizer exists?

# Add your solution here

Answer:

KKT point x\mathbf{x}vvCritical directions d\mathbf{d}dT∇xx2L d\mathbf{d}^T \nabla^2_{\mathbf{xx}} L \, \mathbf{d}Classification

1c. Replace the equality by an inequality

Now solve min⁡x1+x2\min x_1 + x_2 s.t. x12+x22−1≤0x_1^2 + x_2^2 - 1 \le 0. Is this problem convex? Which of the two points from 1b is still a KKT point, and which condition does the other one fail?

Answer:

1d. What does a local solver return?

The code below solves the equality-constrained circle problem with Ipopt from two starting points. Run it. Which point does each run return, and what termination condition does Ipopt report? Explain the run from (1,1)(1, 1) using 1b. Hint: what happens to x1−x2x_1 - x_2 along the iterates?

for start in [(1.0, 1.0), (0.5, 0.9)]:
    m = pyo.ConcreteModel()
    m.x1 = pyo.Var(initialize=start[0])
    m.x2 = pyo.Var(initialize=start[1])
    m.obj = pyo.Objective(expr=m.x1 + m.x2)
    m.h = pyo.Constraint(expr=m.x1**2 + m.x2**2 == 1)
    res = pyo.SolverFactory("ipopt").solve(m)
    print(f"start {start}: {res.solver.termination_condition},  "
          f"x = ({pyo.value(m.x1):.4f}, {pyo.value(m.x2):.4f}),  f = {pyo.value(m.obj):.4f}")

Answer:

StartTermination conditionx\mathbf{x} returnedffMinimizer or maximizer?
(1,1)(1, 1)
(0.5,0.9)(0.5, 0.9)

AI disclosure

Approximately how long was your independent attempt?

How far did you get? where did you get stuck?

What AI or collaborative help did you use afterward, and how did you verify it?

If you used no AI, answer “not applicable”.

Problem 2: KKT conditions for a small nonlinear program

Consider

min⁡xf(x)=(x1−2)2+(x2−1)2s.t.g1(x)=x12−x2≤0,g2(x)=x1+x2−2≤0.\begin{aligned} \min_{\mathbf{x}} \quad & f(\mathbf{x}) = (x_1 - 2)^2 + (x_2 - 1)^2 \\ \mathrm{s.t.} \quad & g_1(\mathbf{x}) = x_1^2 - x_2 \le 0, \\ & g_2(\mathbf{x}) = x_1 + x_2 - 2 \le 0. \end{aligned}

Sketch the feasible region and a few contours of ff before you start. It will help with every part.

2a. Write the KKT conditions

Write the Lagrangian and all four groups of KKT conditions for this problem: stationarity, primal feasibility, dual feasibility and complementarity. Write them component by component, not only in vector form.

# Add your solution here

Answer:

2b. Check the candidate x=(1,1)T\mathbf{x} = (1, 1)^T

Which constraints are active at x=(1,1)T\mathbf{x} = (1, 1)^T? Solve the stationarity equations for the multipliers. Is (1,1)T(1, 1)^T a KKT point? Justify each of the four groups of conditions.

# Add your solution here

Answer:

g1g_1g2g_2
value at (1,1)(1, 1)
active?
multiplier uiu_i

2c. Check the candidate x=(0,0)T\mathbf{x} = (0, 0)^T

Is x=(0,0)T\mathbf{x} = (0, 0)^T feasible? Which constraints are active? Is it a KKT point? If not, which condition fails, and what does −∇f(0)-\nabla f(\mathbf{0}) tell you about a better direction to move?

# Add your solution here

Answer:

g1g_1g2g_2
value at (0,0)(0, 0)
active?

2d. Check with Pyomo and Ipopt

Complete the model below, solve it with Ipopt, and compare the solution to 2b. Pyomo stores the constraint duals in the dual suffix. Compare the reported duals to your u1,u2u_1, u_2: what is the relationship between Pyomo’s sign convention and the one in Lecture 19?

def build_problem2(epsilon=0.0):
    '''Problem 2, with the second constraint perturbed to g2(x) + epsilon <= 0 (used in Problem 3).'''
    m = pyo.ConcreteModel()
    m.x1 = pyo.Var(initialize=0.0)
    m.x2 = pyo.Var(initialize=0.0)
    m.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)

    # Add your solution here
    return m


m1 = build_problem2()
results = pyo.SolverFactory("ipopt").solve(m1)
print("termination condition:", results.solver.termination_condition)
print("x* =", pyo.value(m1.x1), pyo.value(m1.x2), "  f* =", pyo.value(m1.obj))
print("Pyomo duals: g1 ->", m1.dual[m1.g1], "  g2 ->", m1.dual[m1.g2])

Answer:

u1u_1u2u_2
Pyomo dual
Lecture 19 convention

AI disclosure

Approximately how long was your independent attempt?

How far did you get? where did you get stuck?

What AI or collaborative help did you use afterward, and how did you verify it?

If you used no AI, answer “not applicable”.

Problem 3: Multipliers as sensitivities

Perturb the second constraint of Problem 2 as in Lecture 19:

g2(x)+ϵ=x1+x2−2+ϵ≤0.g_2(\mathbf{x}) + \epsilon = x_1 + x_2 - 2 + \epsilon \le 0.

Positive ϵ\epsilon tightens the constraint; negative ϵ\epsilon relaxes it.

3a. Predict the optimal objective

Using only your multipliers from Problem 2, predict the optimal objective f∗(ϵ)f^*(\epsilon) for ϵ=−0.1\epsilon = -0.1, 0.1 and 0.5. State the first-order formula you used.

Answer:

ϵ\epsilon-0.10.10.5
predicted f∗f^*

3b. Re-solve and compare

Re-solve with build_problem2(epsilon) for the three values of ϵ\epsilon. Tabulate the predicted value, the re-solved value and the difference.

# Add your solution here

Answer:

ϵ\epsilonpredicted f∗f^*re-solved f∗f^*re-solved −- predicted
-0.1
0.1
0.5

3c. Interpret

  1. Why does the error in the prediction grow as ∣ϵ∣|\epsilon| grows?

  2. Is the linear prediction above or below the re-solved value for both signs of ϵ\epsilon? What does that tell you about the shape of f∗(ϵ)f^*(\epsilon) near ϵ=0\epsilon = 0?

# Add your solution here

Answer:

AI disclosure

Approximately how long was your independent attempt?

How far did you get? where did you get stuck?

What AI or collaborative help did you use afterward, and how did you verify it?

If you used no AI, answer “not applicable”.

Problem 4: Constraint qualifications at three points

Consider three problems, each with a known minimizer x∗\mathbf{x}^*.

Case A. Problem 2 at x∗=(1,1)T\mathbf{x}^* = (1, 1)^T.

Case B. A disk with a tangent line, at x∗=(1,2)T\mathbf{x}^* = (1, 2)^T:

min⁡x  −x1−2x2s.t.g1=x12+x22−5≤0,g2=x1+2x2−5≤0.\min_{\mathbf{x}} \; -x_1 - 2x_2 \quad \mathrm{s.t.} \quad g_1 = x_1^2 + x_2^2 - 5 \le 0, \quad g_2 = x_1 + 2x_2 - 5 \le 0.

Case C. Two disks that touch at one point, at x∗=(0,0)T\mathbf{x}^* = (0, 0)^T:

min⁡x  x1+x2s.t.g1=(x1−1)2+x22−1≤0,g2=(x1+1)2+x22−1≤0.\min_{\mathbf{x}} \; x_1 + x_2 \quad \mathrm{s.t.} \quad g_1 = (x_1 - 1)^2 + x_2^2 - 1 \le 0, \quad g_2 = (x_1 + 1)^2 + x_2^2 - 1 \le 0.

Sketch Cases B and C before you start.

4a. LICQ

For each case, list the active constraints, write the matrix of active constraint gradients at x∗\mathbf{x}^*, and decide whether LICQ holds (Lecture 20).

# Add your solution here

Answer:

CaseActive constraintsActive-gradient matrixRankLICQ?
A
B
C

4b. MFCQ

For each case, either give a direction d\mathbf{d} with ∇gi(x∗)Td<0\nabla g_i(\mathbf{x}^*)^T \mathbf{d} < 0 for every active inequality, or explain why no such direction exists.

Answer:

CaseDirection d\mathbf{d}, or why none existsMFCQ?
A
B
C

4c. KKT multipliers

For each case, find all KKT multipliers at x∗\mathbf{x}^*. Are they unique, a set (describe it), or nonexistent? Connect your answer to the theorems in Lecture 20.

Answer:

CaseKKT multipliers u\mathbf{u}Unique, a set, or none?
A
B
C

4d. What does a solver see?

The code below solves Case B from three starting points and Case C from one, reporting the termination condition, the solution, and the multipliers in the Lecture 19 sign convention. Run it. Solver versions differ, so report the values your run produces.

def solve_case(case, x_init):
    m = pyo.ConcreteModel()
    m.x1 = pyo.Var(initialize=x_init[0])
    m.x2 = pyo.Var(initialize=x_init[1])
    m.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)
    if case == "B":
        m.obj = pyo.Objective(expr=-m.x1 - 2 * m.x2)
        m.g1 = pyo.Constraint(expr=m.x1**2 + m.x2**2 - 5 <= 0)
        m.g2 = pyo.Constraint(expr=m.x1 + 2 * m.x2 - 5 <= 0)
    else:
        m.obj = pyo.Objective(expr=m.x1 + m.x2)
        m.g1 = pyo.Constraint(expr=(m.x1 - 1) ** 2 + m.x2**2 - 1 <= 0)
        m.g2 = pyo.Constraint(expr=(m.x1 + 1) ** 2 + m.x2**2 - 1 <= 0)
    results = pyo.SolverFactory("ipopt").solve(m)
    u = (-m.dual[m.g1], -m.dual[m.g2])  # Lecture 19 sign convention
    print(f"Case {case}, start {x_init}: {results.solver.termination_condition}")
    print(f"   x = ({pyo.value(m.x1):.6g}, {pyo.value(m.x2):.6g}),  f = {pyo.value(m.obj):.6g}")
    print(f"   u = ({u[0]:.6g}, {u[1]:.6g})")
    if case == "B":
        print(f"   2*u1 + u2 = {2 * u[0] + u[1]:.6g}")
    print(f"   g = ({pyo.value(m.g1.body):.3g}, {pyo.value(m.g2.body):.3g})")


for start in [(0, 0), (-1, 0), (3, 3)]:
    solve_case("B", start)
solve_case("C", (0.5, 0.5))

4e. Interpret the solver output

  1. Case B. Do the multipliers change with the starting point? Do they satisfy your answer to 4c? Why can a solver return different multipliers for the same x∗\mathbf{x}^*?

  2. Case C. What termination condition does Ipopt report? Which numbers in the output tell you that something is wrong anyway? Explain them using 4a--4c.

  3. Lecture 20 describes degenerate constraints. Which of Cases B and C is degenerate, and is the degeneracy local or global?

Answer:

Case B

Startu1u_1u2u_22u1+u22u_1 + u_2
(0,0)(0, 0)
(−1,0)(-1, 0)
(3,3)(3, 3)

Case C

Termination conditionx\mathbf{x}ffu\mathbf{u}constraint values g\mathbf{g}

AI disclosure

Approximately how long was your independent attempt?

How far did you get? where did you get stuck?

What AI or collaborative help did you use afterward, and how did you verify it?

If you used no AI, answer “not applicable”.