目录

凸优化与线性规划

misaraty 更新 | 2023-01-24

前言
  • 凸优化Convex optimization与线性规划Linear programming方案汇总。

  • 凸优化涉及在一组凸约束条件下最小化凸目标函数(或最大化凹目标函数)。线性规划是凸优化的一个特例,其中目标函数是线性的,约束由线性等式和不等式组成。

1

Julia

凸优化

线性规划

          安装,

1
2
import Pkg; Pkg.update("JuMP")
import Pkg; Pkg.add("GLPK")

          运行,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using JuMP
using GLPK

model = Model()
set_optimizer(model, GLPK.Optimizer)

#Define variables
@variable(model, x1>=0)
@variable(model, x2>=0)
@variable(model, x3>=0)
@variable(model, x4>=0)
@variable(model, x5>=0)

#Define Constraints
@constraint(model, x1+x2+x3+x4+x5<=12)
@constraint(model, 0.4x1+0.4x2+0.4x3-0.6x4-0.6x5<=0)
@constraint(model, 0.5x1+0.5x2-0.5x3<=0)
@constraint(model, 0.06x1+0.03x2-0.01x3+0.01x4-0.02x5<=0)

#Define Objective
@objective(model, Max, 0.026x1+0.0509x2+0.0864x3+0.06875x4+0.078x5)

#Run the opimization
optimize!(model)

println(model, "------\n", "objcetive value:", objective_value(model), "\n", "x1 = ", value.(x1), "\n", "x2 = ",value.(x2), "\n", "x3 = ",value.(x3), "\n", "x4 = ", value.(x4), "\n", "x5 = ", value.(x5))

2

          结果,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
Max 0.026 x1 + 0.0509 x2 + 0.0864 x3 + 0.06875 x4 + 0.078 x5
Subject to
 x1 + x2 + x3 + x4 + x5 <= 12.0
 0.4 x1 + 0.4 x2 + 0.4 x3 - 0.6 x4 - 0.6 x5 <= 0.0
 0.5 x1 + 0.5 x2 - 0.5 x3 <= 0.0
 0.06 x1 + 0.03 x2 - 0.01 x3 + 0.01 x4 - 0.02 x5 <= 0.0
 x1 >= 0.0
 x2 >= 0.0
 x3 >= 0.0
 x4 >= 0.0
 x5 >= 0.0
------
objcetive value:0.99648
x1 = 0.0
x2 = 0.0
x3 = 7.199999999999999
x4 = 0.0
x5 = 4.8

Matlab

凸优化

线性规划

Python

凸优化

          安装,

1
pip install cvxpy

          运行,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import cvxpy as cp

x = cp.Variable(1)
y = cp.Variable(1)

objective = cp.Minimize(x**2 + y**2)
# objective = cp.Maximize(x**2 + y**2)
# constraints = []
# constraints.append(x >= 0)
# constraints.append(y >= 0)
# constraints.append(x + y == 1)
constraints = [x >= 0,
               y >= 0,
               x + y == 1]

problem = cp.Problem(objective, constraints)

opt = problem.solve()

print("Optimal solution: %f (x = %f, y = %f)" % (opt, x.value, y.value))

34

          结果,

1
Optimal solution: 0.500000 (x = 0.500000, y = 0.500000)

          安装,

1
pip install picos

          运行,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import picos as pc

x = pc.RealVariable("x", 1)
y = pc.RealVariable("y", 1)

problem = pc.Problem()

problem.minimize = x**2 + y**2
problem += x >= 0
problem += y >= 0
problem += x + y == 1

problem.solve(solver="cvxopt")

opt = problem.value

print("Optimal solution: %f (x = %f, y = %f)" % (opt, x, y))

3

          结果,

1
Optimal solution: 0.500000 (x = 0.500000, y = 0.500000)

          Modeling interfaces to the CVXOPT solvers are available in CVXPY and PICOS.

线性规划

警告
Window11环境下,SolverFactory('ipopt')SolverFactory('glpk')报错,且无法通过pipconda正确安装ipoptglpk

R

凸优化

chemical potentials

          cplapyPyPolyhedron


  1. Does convex optimization belong to linear or nonlinear programming? ↩︎

  2. Linear programming in Julia with GLPK and JuMP ↩︎

  3. Convex Optimization in Python: 3 Essential Packages ↩︎

  4. Convex Optimization: A Practical Guide ↩︎