All problems
KKT Condition Check
Problem
Check the KKT conditions for minimizing f(x, y) = 0.5*a*x^2 + 0.5*b*y^2 subject to g(x, y) = d - x - y <= 0.
The Lagrangian is f + lambda*g. Return True when all hold within the tolerance: a*x - lambda == 0, b*y - lambda == 0, g <= 0, lambda >= 0, and lambda*g == 0.
Hint
Stationarity, primal feasibility, dual feasibility, and complementary slackness must all hold.
Test cases
case 1
in: [2,2,1,0.5,0.5,1]
expected: true
case 2
in: [2,2,1,0.5,0.5,0]
expected: false
case 3
in: [2,2,1,0.3,0.5,1]
expected: false
case 4
in: [1,3,2,1.5,0.5,1.5]
expected: true
case 5
in: [2,2,0,0,0,0]
expected: true
Starter codePython
def kkt_check(a, b, d, x, y, lam, tol=1e-9):
# Your code here
pass5 test cases · run your code against them in the editor