凸的约束优化

Python 优化入门

Jasmin Ludolf

Content Developer

凸的约束优化

凸函数。

  • 约束:对变量的限制
  • 无差异曲线:表示变量的组合
  • 在无差异曲线上找到使目标函数最小或最大的位置
Python 优化入门

无差异曲线

一位黑发年轻女性在使用笔记本电脑

  • 自由职业软件工程师
  • 重视工作($w$)与闲暇($l$)

 

效用函数:

  • $U(w, l)=w^{0.4}l^{0.6}$
Python 优化入门

绘制无差异曲线

import numpy as np
import matplotlib.pyplot as plt


w = np.linspace(1, 30, 100) l = np.linspace(1, 30, 100)
W, L = np.meshgrid(w, l)
F = W**0.4 * L**0.6
plt.figure(figsize=(8, 6))
contours = plt.contour(W, L, F, levels=[5, 10, 15, 20])
plt.clabel(contours)
plt.title('Indifference Curves for the function: w**0.4 * l**0.6') plt.xlabel('w') plt.ylabel('l') plt.grid(True) plt.show()
Python 优化入门

无差异曲线

可视化无差异曲线

  • 对任意 $w$ 与 $l$ 的具体组合,Alexia 都无差异
Python 优化入门

时间约束

  • 每天 24 小时
  • 线性等式约束

$$ \max_{w,l} w^{0.4}l^{0.6}$$ $$s.t.\ \ \ w+l = 24$$

  • 将约束加到图中:
    • l = 24 - w
    • plt.plot(w, l, color='red')

无差异曲线与线性约束。最优无差异曲线与约束相切。切点处梯度与约束和切线均垂直。

Python 优化入门

用 SciPy 求解

def utility_function(vars):
    w, l = vars
    return -(w**0.4 * l**0.6)


def constraint(vars): return 24 - np.sum(vars)
initial_guess = [12, 12] constraint_definition = {'type': 'eq', 'fun': constraint} result = minimize(utility_function, initial_guess, constraints=constraint_definition) print(result.x)
[ 9.60001122 14.39998878]
Python 优化入门

让我们来练习!

Python 优化入门

Preparing Video For Download...