-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_4_3.py
More file actions
55 lines (42 loc) · 1.61 KB
/
ex_4_3.py
File metadata and controls
55 lines (42 loc) · 1.61 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Copyright 2013 Steven Diamond
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# for decimal division
from __future__ import division
import cvxopt
from cvxpy import Minimize, Problem, Variable, quad_form
# Taken from CVX website http://cvxr.com/cvx/examples/
# Example: CVX Textbook exercise 4.3: Solve a simple QP with inequality constraints
# Ported from cvx matlab to cvxpy by Misrab Faizullah-Khan
# Original comments below
# From Boyd & Vandenberghe, "Convex Optimization"
# Joelle Skaf - 09/26/05
#
# Solves the following QP with inequality constraints:
# minimize 1/2x'*P*x + q'*x + r
# s.t. -1 <= x_i <= 1 for i = 1,2,3
# Also shows that the given x_star is indeed optimal
# Generate data
n = 3
P = cvxopt.matrix([ 13, 12, -2,
12, 17, 6,
-2, 6, 12], (n,n))
q = cvxopt.matrix([-22, -14.5, 13], (n,1))
r = 1
x_star = cvxopt.matrix([1, 1/2, -1], (n,1))
# Frame and solve the problem
x = Variable(n)
objective = Minimize( 0.5 * quad_form(x, P) + q.T * x + r )
constraints = [ x >= -1, x <= 1]
p = Problem(objective, constraints)
# The optimal objective is returned by p.solve().
result = p.solve()