Numerical Algorithms For SQP
Numerical Algorithms For SQP
Quadratic Optimization
The algorithms has been implemented in Matlab and tested on strictly convex
QP’s of sizes up to 1800 variables and 7200 constraints. The testcase is the
quadruple tank process, described in appendix A.
Whenever the Hessian matrix of the QP is positive definite the dual active
set method is prefereble. The calculation of a starting point is implicit in the
method and furthermore convergence is guaranteed.
When the Hessian matrix is positive semi definite, the primal active set can be
used. For this matter an LP solver should be implemented, which computes a
starting point and an active set that makes the reduced Hessian matrix positive
definite. This LP solver has not been implemented, as it is out of the range of
this thesis.
Dansk Resumé
SQP løser det ikke-lineært begrænsede program ved at løse en sekvens af tilhørende
kvadratiske programmer (QP’er). Et QP er et begrænset optimeringsproblem,
hvor objektfunktionen er kvadratisk og begrænsningerne er lineære. Et QP løses
ved at bruge primal aktiv set metoden eller dual aktiv set metoden. Primal aktiv
set metoden løser et konvekst QP, hvor Hessian matricen er positiv semi definit.
Dual aktiv set metoden kræver et strengt konvekst QP, dvs. at Hessian matri-
cen skal være positiv definit. Aktiv set metoderne løser et ulighedsbegrænset
QP ved at løse en sekvens af tilhørende lighedsbegænsede QP’er.
Ved brug af Givens rotationer ungås fuld faktorisering for hver iteration i aktiv
set metoderne. Begrænsningerne deles op i begrænsede variable og egentlige
begrænsninger beskrevet ved funktionsudtryk. Begrænsede variable betyder,
at en andel af variablene er fikserede, mens resten er frie pr. iteration. Dette
udnyttes til yderligere optimering af faktoriseringerne mellem hver iteration.
Hovedresultater
Når antallet af aktive begrænsninger når en vis mængde i forhold til antallet
af variable, bør null space proceduren benyttes. Range space proceduren bør
kun benyttes, når antallet af aktive begrænsninger er lille i forhold til antallet
af variable.
Hvis Hessian matricen af et QP er positiv definit, bør dual aktiv set meto-
den benyttes. Her foregår beregningerne af startpunkt implicit i metoden, og
desuden er konvergens garanteret.
Hvis Hessian matricen er positiv semi definit, kan primal aktiv set metoden
benyttes. Men her skal der benyttes en LP-løser til at beregne et startpunkt og
et tilhørende aktivt set, som medfører at den reducerede Hessian matrix bliver
positiv definit. Denne LP-løser er ikke blevet implementeret, da den ligger
udenfor området af dette projekt.
v
vi Contents
Contents
Abstract i
1 Introduction 1
2.3 Computational Cost of the Range and the Null Space Procedures 15
4.1.1 Survey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
4.3.1 Survey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.3.5 In summary . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4.3.6 Termination . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5.1 Computational Cost of the Range and the Null Space Procedures
with Update . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
6 Nonlinear Programming 95
7 Conclusion 103
Bibliography 107
C Implementation 119
D Matlab-code 123
Introduction
Usually the solution of a dynamic process must be kept within certain lim-
its. In order to generate a feasible solution to the process, these limits have to
be taken into account. If a process like this can be modeled as a constrained
optimization problem, model predictive control can be used in finding a feasible
solution, if it exists.
Model predictive control with nonlinear models can be performed using sequen-
tial quadratic programming (SQP). Model predictive control with linear models
may be conducted using quadratic programming (QP). A variety of different
numerical methods exist for both SQP and QP. Some of these methods com-
prise the subject of this project.
2 Introduction
The main challenge in SQP is to solve the QP, and therefore methods for solv-
ing QP’s constitute a major part of this work. In itself, QP has a variety of
applications, e.g. Portfolio Optimization by Markowitz, found in Nocedal and
Wright [14], solving constraint least squares problems and in Huber regression
Li and Swetits [1]. A QP consists of a quadratic objective function, which we
want to minimize subject to a set of linear constraints. A QP is stated as
1 T
min x Gx + g T x
x∈Rn 2
s.t. l≤x≤u
bl ≤ AT x ≤ bu ,
The methods we describe are the primal active set method and the dual active
set method. Within these methods the Karush-Kuhn-Tucker (KKT) system1
G −Ā x g
T =−
−Ā 0 λ b̄
is solved using the range space procedure or the null space procedure. These
methods in themselves fulfill the demand of reliability, while the demand of ef-
ficiency is obtained by refinement of these methods.
We will investigate the primal and dual active set methods for solving QP’s.
Thus we will discuss the range and the null space procedures together with
different refinements for gaining efficiency and reliability. The methods and
1 This is the KKT system of the primal program, the KKT system of the dual program is
procedures for solving QP’s will be implemented and tested in order to determine
the best suited combination in terms of efficiency for solving different types of
problems. The problems can be divided into two categories, those with a low
number of active constraints in relation to the number of variables, and problems
where the number of active constraints is high in relation to the number of
variables. Finally we will discuss and implement the SQP method to find out
how our QP solver performs in this setting.
The thesis is divided into five main areas: Equality constrained quadratic pro-
gramming, updating of matrix factorizations, active set methods, test and re-
finements and nonlinear programming.
In this chapter we present two methods for solving equality constrained QP’s,
namely the range space procedure and the null space procedure. The methods
are implemented and tested, and their relative benefits, and drawbacks are
investigated.
Both the null space and the range space procedure use matrix factorizations in
solving the equality constrained QP. Whenever the constraint matrix is changed
by either appending or removing a constraint, the matrix factorizations can be
updated using Givens rotations. By avoiding complete re-factorization, compu-
tational savings are achieved. This is the subject of this chapter and methods
for updating the QR and the Cholesky factorizations are presented.
Inequality constrained QP’s can be solved using active set methods. These
methods find a solution by solving a sequence of equality constrained QP’s,
where the difference between two consecutive iterations is a single appended or
4 Introduction
removed constraint. In this chapter we present the primal active set method
and the dual active set method.
In this chapter we test how the presented methods perform in practice, when
combined in different ways. We also implement some refinements, and their
impact on computational speed and stability are likewise tested.
Nonlinear Programming
In this section we present various algorithms for solving convex1 equality con-
strained QP’s. The problem to be solved is
1 T
min f (x) = x Gx + g T x (2.1a)
x∈Rn 2
s.t. AT x = b, (2.1b)
where G ∈ Rn×n is the Hessian matrix of the objective function f . The Hes-
sian matrix must be symmetric and positive semi definite2 . A ∈ Rn×m is the
constraint matrix (coefficient matrix of the constraints), where n is the number
of variables and m is the number of constraints. A has full column rank, that is
the constraints are linearly independent. The right hand side of the constraints
is b ∈ Rm and g ∈ Rn denotes the coefficients of the linear term of the objective
function.
1 The range space procedure presented in section 2.1 requires a strictly convex QP.
2 The range space procedure presented in section 2.1 requires G to be positive definite.
6 Equality Constrained Quadratic Programming
1 T
L(x, λ) = x Gx + g T x − λT (AT x − b), (2.2)
2
∇x L(x, λ) = Gx + g − Aλ (2.3a)
T
∇λ L(x, λ) = −A x + b, (2.3b)
G −A x g
=− . (2.4)
−AT 0 λ b
The KKT system is basically a set of linear equations, and therefore general
solvers for linear systems could be used, e.g. Gaussian elimination. In order to
solve a KKT system as fast and reliable as possible, we want to use Cholesky
and QR factorizations. But according to Gould in Nocedal and Wright [14] the
KKT matrix is indefinite, and therefore it is not possible to solve it by use of
either of the two factorizations. In this chapter, we present two procedures for
solving the KKT system by dividing it into subproblems, on which it is possible
to use these factorizations. Namely the range space procedure and the null space
procedure. We also investigate their individual benefits and drawbacks.
2.1 Range Space Procedure 7
The range space procedure based on Nocedal and Wright [14] and Gill et al. [2]
solves the KKT system (2.4), corresponding to the convex equality constrained
QP (2.1). The Hessian matrix G ∈ Rn×n must be symmetric and positive
definite, because the procedure uses the inverted Hessian matrix G−1 . The
KKT system
G −A x g
=− (2.5)
−AT 0 λ b
Gx − Aλ = −g (2.6a)
T
A x = b. (2.6b)
and substituting (2.7) into (2.6b) gives us one equation with one unknown λ
which is equivalent to
From the Cholesky factorization of G we get G = LLT and G−1 = (LT )−1 L−1 =
(L−1 )T L−1 . This is inserted in (2.9)
so
K T Kλ = K T w + b. (2.12)
Hλ = z. (2.13)
The matrix G is positive definite and the matrix A has full column rank, so H is
also positive definite. This makes it possible to Cholesky factorize H = M M T ,
and by backward and forward substitution λ is found from
M M T λ = z. (2.14)
M q = z, (2.15)
M T λ = q. (2.16)
Gx = Aλ − g (2.17)
2.1 Range Space Procedure 9
gives us
LLT x = Aλ − g, (2.18)
and
which is equivalent to
LT x = Kλ − w. (2.20)
LT x = r. (2.21)
Algorithm 2.1.1 summarizes how the calculations in the range space procedure
are carried out.
10 Equality Constrained Quadratic Programming
The null space procedure based on Nocedal and Wright [14] and Gill et al. [3]
solves the KKT system (2.4) using the null space of A ∈ Rn×m . This procedure
does not need G ∈ Rn×n to be positive definite but only positive semi definite.
This means, that it is not restricted to strictly convex quadratic programs. The
KKT system to be solved is
G −A x g
=− , (2.22)
−AT 0 λ b
where A has full column rank. We compute the null space using the QR fac-
torization of A
R R
A=Q = (Y Z) , (2.23)
0 0
where Z ∈ Rn×(n−m) is the null space and Y ∈ Rn×m is the range space.
(Y Z) ∈ Rn×n is orthogonal and R ∈ Rm×m is upper triangular.
By defining x = Qp we write
py
x = Qp = (Y Z)p = (Y Z) = Y py + Zpz . (2.24)
pz
py
x Y Z 0
= pz , (2.25)
λ 0 0 I
λ
T py
Y Z 0 x
= pz . (2.26)
0 0 I λ λ
12 Equality Constrained Quadratic Programming
Now we will use (2.25) and (2.26) to express the KKT system in a more de-
tailed form, in which it becomes clear what part corresponds to the null space.
Inserting (2.25) and (2.26) in (2.22) gives
T py
Y Z 0 G −A Y Z 0 pz =
0 0 I −AT 0 0 0 I
λ
T
Y Z 0 g
− , (2.27)
0 0 I b
which is equivalent to
T
Y T GY Y T GZ −(AT Y )T py Y g
Z T GY Z T GZ −(AT Z)T pz = − Z T g . (2.28)
−AT Y −AT Z 0 λ b
T
Y T GY Y T GZ −(AT Y )T py Y g
Z T GY Z T GZ 0 pz = − Z T g . (2.29)
−AT Y 0 0 λ b
This system can be solved using backward substitution, but to do this, we need
the following statement based on (2.23)
A=YR (2.30a)
T T
A = (Y R) (2.30b)
T T
A Y = (Y R) Y (2.30c)
T T T
A Y =R Y Y (2.30d)
T T
A Y =R . (2.30e)
−AT Y py = −b (2.31)
2.2 Null Space Procedure 13
is equivalent to
RT py = b. (2.32)
Z T GY py + Z T GZpz = −Z T g, (2.33)
which is equivalent to
Rλ = Y T (Gx + g) (2.37)
and because R is upper triangular, this equation also has a unique solution,
which is found by backward substitution.
This is the most efficient procedure, when the degree of freedom n − m is small,
i.e. when the number of constraints is large compared to the number of variables.
The reduced Hessian matrix Z T GZ ∈ R(n−m)×(n−m) grows smaller, when m
14 Equality Constrained Quadratic Programming
Algorithm 2.2.1 summarizes how the calculations in the null space procedure
are carried out.
Compute py by solving RT py = b
Compute g z = −Z T (GY py + g)
Compute r by solving Lr = g z
Compute pz by solving LT pz = r
Compute x = Y py + Zpz
Compute λ by solving Rλ = Y T (Gx + g)
2.3 Computational Cost of the Range and the Null Space Procedures 15
In this section we want to find out, how the range space and the null space
procedures perform individually. We also consider whether it is worthwhile to
shift between the two procedures dynamically.
In the range space procedure there are three dominating computations in rela-
tion to time consumption. The computation of K ∈ Rn×m , computation of and
Cholesky factorization of H ∈ Rm×m .
Thus, we can estimate the total computational cost of the range space pro-
cedure as
1 3
3m + 2nm2 + n2 m (2.38)
0 ≤ 31 m3 + 2nm2 + n2 m ≤ 10 3
3 n . (2.39)
16 Equality Constrained Quadratic Programming
Here we also see, that the range space procedure gets slower, as the number of
constraints compared to the number of variables increases.
Figure 2.1 shows the theoretical computational speed of the range space proce-
dure. As stated above, it is obvious that the method gets slower as the number
of constraints increases in comparison to the number of variables.
x 10
9 RSP, theoretical computational cost, n = 1000
4
L−1A
3.5 KTK
chol(H)
3 total
RSP
2.5
cost [flops]
1.5
0.5
0
0 100 200 300 400 500 600 700 800 900 1000
m
The time consumption of the null space procedure is dominated by two compu-
tations. The QR factorization of the constraint matrix A and computation of
the reduced Hessian matrix Z T GZ ∈ R(n−m)×(n−m) .
R R
A=Q = Y Z , (2.40)
0 0
We now want to find the amount of work involved in computing the reduced
Hessian matrix Z T GZ. We define Z T = Z T ∈ R(n−m)×n . The computational
workload of making the inner product of two vectors in Rn is 2n. Since Z T
2.3 Computational Cost of the Range and the Null Space Procedures 17
4 3
3n ≤ 2m2 (n − 13 m) + 2n(n − m)(2n − m) ≤ 4n3 . (2.43)
x 10
9 NSP, theoretical computational cost, n = 1000
4
qr(A)
ZTGZ
3.5
total
NSP
3
2.5
cost [flops]
1.5
0.5
0
0 100 200 300 400 500 600 700 800 900 1000
m
Figure 2.2: Theoretical computational costs for the null space procedure.
18 Equality Constrained Quadratic Programming
1 3
3m + 2nm2 + n2 m − 2m2 (n − 13 m) − 2n(n − m)(2n − m) =
m3 − 2nm2 + 7n2 m − 4n3 = 0, (2.44)
In figure 2.3 we have n = 1000 and 0 ≤ m ≤ n, so the ratio, i.e. the point
at which one should shift from using the range space to using the null space
procedure is of course to be found at m ≃ 650.
x 10
9 RSP vs NSP, theoretical computational costs, n = 1000
4
totalRSP
3.5 totalNSP
2.5
cost [flops]
1.5
0.5
0
0 100 200 300 400 500 600 700 800 900 1000
m
Figure 2.3: Total estimated theoretical computational costs for the range space
and the null space procedures.
We made some testruns of the the two procedures by setting up a KKT system
consisting of identity matrices. The theoretical computational costs are based
on full matrices, and we know, that Matlab treats identity matrices like full
matrices. So by means of this simple KKT system we are able to compare the
theoretical behavior of the respective procedures with the real behavior of our
implementation. The test setup consists of n = 1000 variables and 0 ≤ m ≤ n
2.3 Computational Cost of the Range and the Null Space Procedures 19
constraints, as illustrated in figures 2.4 and 2.5. The black curves represent
all computations carried out by the two procedures. It is clear, that they run
parallel to the magenta representing the dominating computations. This verifies
our comparison between the theoretical computational workloads with our test
setup.
In figure 2.4 we test the range space procedure, and the behavior is just as
expected, when compared to the theory represented in figure 2.1.
6 −1
L A
KTK
5 chol(H)
totalRSP
RSP
4
cost [s]
0
0 100 200 300 400 500 600 700 800 900 1000
m
Figure 2.4: Real computational cost for the range space procedure.
In figure 2.5 the null space procedure is tested. The computation of the re-
duced Hessian matrix Z T GZ behaves as expected. The behavior of the QR
factorization of A however is not as expected, compared to figure 2.2. When
0 ≤ m . 100, the computational time is too small to be properly measured. The
QR factorization still behaves some what unexpectedly, when 100 . m ≤ 1000.
Hence the advantage of shifting from the range space to the null space proce-
dure decreases. In other words, the ratio between the number of constraints
related to the number of variables, where the null space procedure gets faster
than the range space procedure, is larger than expected. Therefore using the
null space procedure might actually prove to be a disadvantage. This is clearly
in figure 2.6, where the dominating computations for the range space and the
null space procedures are compared to each other. From this plot it is clear, that
the ratio indicating when to shift procedure is much bigger in practice than in
theory. The cause of this could be an inappropriate implementation of the QR
factorization in Matlab, architecture of the processing unit, memory access
etc.. Perhaps implementation of the QR factorization in a low level language
could prove different.
It must be mentioned at this point, that the difference in unit on the abscissa
20 Equality Constrained Quadratic Programming
in all figures in this section does not influence the shape of the curves between
theory and testruns, because the only difference between them is the constant
ratio time/flop.
6 qr(A)
ZTGZ
total
5 NSP
NSP
4
cost [s]
0
0 100 200 300 400 500 600 700 800 900 1000
m
Figure 2.5: Real computational cost for the null space procedure.
6 totalRSP
totalNSP
4
cost [s]
0
0 100 200 300 400 500 600 700 800 900 1000
m
Figure 2.6: Real computational costs for the range space and the null space
procedures.
Chapter 3
Because of this property, the factorization of the matrices can be done more
efficiently than complete refactorization. This is done by an updating procedure
where the current factorization of the matrices in the sequence, is partly used to
factorize the matrices for the next iteration. The computational complexity of
this updating procedure is O(n2 ) and therefore a factor n faster than a complete
factorization. This is important in particular for large-scale problems. The
updating procedure discussed in the following is based on Givens rotations and
Givens reflections.
22 Updating Procedures for Matrix Factorization
Givens rotations and reflections are methods for introducing zeros in a vector.
This is achieved by rotating or reflecting the coordinate system according to an
angle or a line. This angle or line is defined so that one of the coordinates of
a given point p becomes zero. As both methods serve the same purpose, we
have chosen to use only Givens rotations which we will explain in the following.
This theory is based on the work of Golub and Van Loan [4] and Wilkinson
[5]. A Givens rotation is graphically illustrated in figure 3.1. As illustrated we
can rotate the coordinate system so the coordinates of p in the rotated system
actually become (x′ , 0)T .
y p
y′ x′ θ
x
Figure 3.1: A Givens rotation rotates the coordinate system according to an
angle θ.
c s x y
Q̂ = , c= p = cos(θ), s= p = sin(θ), (3.1)
−s c x2 + y 2 x2 + y 2
y
√ x √
x2 +y 2 x2 +y 2 x
Q̂p =
− √ 2y 2 √ 2x 2 y
x +y x +y
2
√x+y 2
x2 +y 2
= −xy+yx
√
x2 +y 2
p
x2 + y 2
= . (3.2)
0
1 0 0 0 0x x
.. .. ..
0
. 0 0 0 .
.
0 0 1 0 0 x
= x . (3.3)
0 0 0 c s x m
0 0 0 −s c x 0
Any Givens operation introduces only one zero at a time, but if we want to
introduce more zeros, a sequence of Givens operations Q̃ ∈ Rn×n can be con-
structed
which yields
x γ
x
0
Q̃v = Q̃ .. = .. , γ = ±||v||2 . (3.5)
. .
x 0
24 Updating Procedures for Matrix Factorization
x x x m
x x m 0
Q̂3,4 Q̂ Q̂ , (3.6)
x − −→ m −−2,3
→ 0 −−1,2
→ 0
x 0 0 0
When a constraint is appended to, or removed from, the active set, updating
the factorization is done using Givens rotations. This section is based on the
work of Dennis and Schnabel [6], Gill et al. [7] and Golub and Van Loan [4] and
describes how the updating procedure is carried out.
Appending a Constraint
Before the new column is appended, we take a closer look at the constraint
matrix A ∈ Rn×m and its QR-factorization Q ∈ Rn×n and R ∈ Rm×m . The
constraint matrix A has full column rank, and can be written as
A = a1 . . . am , (3.7)
R
A=Q . (3.8)
0
R
QT A = . (3.9)
0
QT A = QT a1 . . . am . (3.10)
x(1,1) ... x(1,m)
R ... ...
QT A = = . (3.11)
0 x(m,m)
0
26 Updating Procedures for Matrix Factorization
Ā = (a1 . . . am ) ā . (3.12)
QT Ā = QT (a1 . . . am ) ā , (3.13)
which is equivalent to
QT Ā = QT (a1 . . . am ) QT ā . (3.14)
T R v v
Q Ā = , = QT ā, (3.15)
0 w w
x(1,1) ... x(1,m)
... ... v
QT Ā = . (3.16)
x(m,m)
0 w
Unless only the first element is different from zero in vector w, the triangular
structure is violated by appending ā. By using Givens rotations, zeros are intro-
duced in the vector (v, w)T with a view to making the matrix upper triangular
again. As a Givens operation only introduces one zero at a time, a sequence
Q̃ ∈ Rn×n of n − m + 1 Givens rotations is used
where Q̂(i+2,i+3) defines the Givens rotation matrix that introduces one zero
at index i + 3 and modifies the element at index i + 2. It is clear from (3.16)
that the smallest amount of Givens rotations are needed, when ā is appended
at index m + 1. This sequence is constructed so that
v
γ
v 0
Q̃ = . (3.18)
w ..
.
0
Now that the sequence Q̃ has been constructed, when we multiply it with (3.16)
we get
T R v
Q̃Q Ā = Q̃
0 w
R v
= 0 γ
0 0
x(1,1) . . . x(1,m)
... ... v
= x(m,m)
0 γ
0 0
R̄
= . (3.19)
0
This indicates, that the triangular shape is regained and the Givens operations
only affects the elements in w.
R̄
Ā = Q̄ . (3.20)
0
Now that R̄ has been found, we only need to find Q̄ to complete the updating
28 Updating Procedures for Matrix Factorization
T R̄
Q̃Q Ā = , (3.21)
0
T
Ā = QQ̃ R̄. (3.22)
T
Q̄ = QQ̃ . (3.23)
The updating procedure, when appending one constraint to the constraint ma-
trix is summarized in algorithm 3.2.1.
Removing a Constraint
To begin with we take a close look at the situation before the column is removed.
A ∈ Rn×m and its QR-factorization Q ∈ Rn×n and R ∈ Rm×m have the
3.2 Updating the QR Factorization 29
following relationship
A = (a1 . . . ai−1 ) ai (ai+1 . . . am ) , (3.24)
where (a1 . . . ai−1 ) are the first i−1 columns, ai is the ith column and (ai+1 . . . am )
are the last m − i columns. The QR-factorization of A is
R
A=Q , (3.25)
0
R
QT A = . (3.26)
0
QT A = QT (a1 . . . ai−1 ) ai (ai+1 . . . am ) , (3.27)
which is equivalent to
QT A = QT (a1 . . . ai−1 ) QT ai QT (ai+1 . . . am ) . (3.28)
R11 R12 R13
0 R22 R23
QT A = (3.29)
0 0 R33
0 0 0
x(1,1) . . . x(1,i−1) x(1,i) x(1,i+1) ... x(1,m)
. . . ... ... ... ... ...
x(i−1,i−1) ... ... ... ...
0 x(i,i) ... ... ...
= .
x(i+1,i+1) ... ...
0 0 ...
x(m,m)
0 0 0
Ā = (a1 . . . ai−1 ) (ai+1 . . . am ) , (3.30)
QT Ā = QT (a1 . . . ai−1 ) (ai+1 . . . am ) , (3.31)
which is equivalent to
QT Ā = QT (a1 . . . ai−1 ) QT (ai+1 . . . am ) . (3.32)
x ... x x ... x
... ... ... ... ...
R11 R13
x x ... x
T
0 R23 0 x ... x
Q Ā = = . (3.33)
0 R33
x ... x
0 0
0 ... ...
x
0 0
3.2 Updating the QR Factorization 31
The triangular structure is obviously violated and in order to regain it, Givens
rotations are used. It is only the upper Hessenberg matrix (R23 , R33 )T , that
needs to be made triangular. This is done using a sequence of m − i Givens
rotation matrices Q̃ ∈ Rn×n
x ... ... x
x ... x ... ... ... ...
... ... ...
x ... ... x
R13 x ... x
m ... ... m
R23
= Q̃ x ... x
=
.
Q̃ m ... m (3.34)
R33 x ... x
... ...
0 ... ...
m
x
0
0
0
This means, that the triangular matrix R̄ is found from the product of Q̃ and
QT Ā, so that we get
x ... x x ... ... x
... ... ... ... ... ...
x x ... ... x
0 m ... ... m
R̄ T
= Q̃Q Ā = m ... m . (3.35)
0
0 ... ...
m
0 0
Now that we have found the upper triangular matrix R̄ of the new factorization
Ā = Q̄R̄, we only need to find the orthogonal matrix Q̄. As Q̃ and QT are
orthogonal (3.35) can be reformulated as
T
Ā = QQ̃ R̄, (3.36)
T
Q̄ = QQ̃ . (3.37)
32 Updating Procedures for Matrix Factorization
The updating procedure, when removing a constraint from the constraint matrix
is summarized in algorithm 3.2.2.
Appending a Constraint
H q
H̄ = , (3.38)
qT r
T L̃ 0
H̄ = L̄L̄ , L̄ = , (3.39)
sT t
H q
H̄ =
qT r
!
T
L̃ 0 L̃ s
=
sT t 0 t
T
!
L̃L̃ L̃s
= T . (3.40)
s L̃T
s s + t2
T
34 Updating Procedures for Matrix Factorization
From this expression L̄ can be found via L̃, s and t. Furthermore from (3.40)
and the fact, that H = LLT , we have
T
H = L̃L̃ = LLT , (3.41)
L̃ = L. (3.42)
From (3.40) and (3.42) we know that s can be found from the expression
r = sT s + t2 . (3.44)
p
t= r − sT s. (3.45)
Now L̃, s and t have been isolated, and the new Cholesky factorization has
been shown to be easily found from (3.39). Algorithm 3.3.1 summarizes how the
updating procedure of the Cholesky factorization is carried out, when appending
3.3 Updating the Cholesky factorization 35
Removing a Constraint
Before removing a constraint, i.e. the ith column, from the constraint matrix
A ∈ Rn×m , the matrix H ∈ Rm×m can be formulated as
H 11 a H 12
H = aT c bT , (3.46)
H T12 b H 22
L11
H = LLT , L = dT e , (3.47)
L12 f L22
where L ∈ Rm×m , L11 ∈ R(i−1)×(i−1) and L22 ∈ R(m−i)×(m−i) are lower tri-
angular and non-singular matrices with positive diagonal-entries. Also having
L12 ∈ R(m−i)×(i−1) . The vectors d and f have dimensions R(i−1) and R(m−i)
respectively and e ∈ R. The column (aT c bT )T and the row (aT c bT ) in (3.46)
are removed, when the constraint at column i is removed from A. This gives us
36 Updating Procedures for Matrix Factorization
H 11 H 12
H̄ = . (3.48)
H T12 H 22
T L̄11
H̄ = L̄L̄ , L̄ = , (3.49)
L̄12 L̄22
which is equivalent to
!
T T
L̄11 L̄11 L̄12
H̄ = T
L̄12 L̄22 L̄22
!
T T
L̄11 L̄11 L̄11 L̄12
= T T T , (3.50)
L̄12 L̄11 L̄12 L̄12 + L̄22 L̄22
H 11 a H 12
aT T
c b =H
H T12 b H 22
= LLT
L11 LT11 d LT12
= dT e e fT , (3.51)
L12 f L22 LT22
which gives
3.3 Updating the Cholesky factorization 37
H 11 a H 12
aT c bT =
H T12 b H 22
L11 LT11 L11 d L11 LT12
dT LT11 dT d + e2 dT LT12 + ef T . (3.52)
T
L12 LT11 T T
L12 d + f e L12 L12 + f f + L22 L22
!
T T
H 11 H 12 L̄11 L̄11 L̄11 L̄12
= . (3.53)
H T12 H 22 T
L̄12 L̄11
T T
L̄12 L̄12 + L̄22 L̄22
T
H 11 = L11 LT11 = L̄11 L̄11 , (3.54)
and
T
H 12 = L11 LT12 = L̄11 L̄12 , (3.55)
T T
H 22 = L12 LT12 + f f T + L22 LT22 = L̄12 L̄12 + L̄22 L̄22 , (3.57)
T
H 22 = L12 LT12 + f f T + L22 LT22 = L12 LT12 + L̄22 L̄22 , (3.58)
38 Updating Procedures for Matrix Factorization
which is equivalent to
T
H 22 = f f T + L22 LT22 = L̄22 L̄22 . (3.59)
T
L̄22 L̄22 = (f L22 )(f L22 )T . (3.60)
From (3.47) we know that (f L22 ) is not triangular. Therefore we now construct
a sequence of Givens rotations Q̃ ∈ R(m−i+1)×(m−i+1) so that
x x
.. ..
. . x
(f L22 )Q̃ = .. .. ..
Q̃
..
. . . .
x x x ... x
x
..
. x
= .. ..
..
. . .
x x ... x 0
= (L̃ 0), (3.61)
T T
L̄22 L̄22 = (f L22 )Q̃Q̃ (f L22 )T , (3.62)
which is equivalent to
T
L̄22 L̄22 = ((f L22 )Q̃)((f L22 )Q̃)T , (3.63)
T T
L̄22 L̄22 = (L̃ 0)(L̃ 0)T = L̃L̃ . (3.64)
3.3 Updating the Cholesky factorization 39
Hence we now have everything for constructing the new Cholesky factorization:
T L̄11
H̄ = L̄L̄ , L̄ = . (3.67)
L̄12 L̄22
1 T
min f (x) = x Gx + g T x (4.1a)
x∈Rn 2
s.t. ci (x) = ai x − bi ≥ 0, i ∈ I. (4.1b)
The solution of this problem x∗ is also the same as to the equality constrained
convex QP
1 T
min f (x) = x Gx + g T x (4.2a)
x∈Rn 2
s.t. ci (x) = ai x − bi = 0, i ∈ A(x∗ ). (4.2b)
In other words, this means that in order to find the optimal point we need to
find the active set A(x∗ ) of (4.1). As we shall see in the following, this is done
by solving a sequence of equality constrained convex QP’s. We will investigate
two methods for solving (4.1), namely the primal active set method (section 4.1)
and the dual active set method (section 4.3).
42 Active Set Methods
The primal active set method discussed in this section is based on the work of
Gill and Murray [8] and Gill et al. [9]. The algorithm solves a convex QP with
inequality constraints (4.1).
4.1.1 Survey
1 T
min x Gx + g T x (4.3a)
x∈Rn 2
s.t. aTi x ≥ bi i∈I (4.3b)
1 T
f (x) = x Gx + g T x (4.4)
2
Ω = {x ∈ Rn : aTi x ≥ bi , i ∈ I} (4.5)
The idea of the primal active set method is to compute a feasible sequence
{xk ∈ Ω}, where k = N0 , with decreasing value of the objective function,
f (xk+1 ) < f (xk ). For each step in the sequence we solve an equality constraint
QP
1 T
min x Gxk + g T xk (4.6a)
xk ∈Rn 2 k
s.t. aTi xk = bi i ∈ A(xk ) (4.6b)
4.1 Primal Active Set Method 43
X
Gx∗ + g − ai µ∗i = 0 (4.7a)
i∈I
aTi x∗ = bi i ∈ Wk (4.7b)
aTi x∗ ≥ bi i ∈ I\Wk (4.7c)
µ∗i ≥ 0 i ∈ Wk (4.7d)
µ∗i = 0 i ∈ I\Wk (4.7e)
are satisfied.
If no constraints are active, aTi x0 > bi for i ∈ I, then the corresponding working
set is empty, W0 = ∅. The objective function (4.4) is convex, and therefore if
minx∈R f (x) ∈ / Ω, then one or more constraints will be violated, when xk seeks
the minimum of the objective function. This explains why the working set is
never empty, once a constraint has become active, i.e. aTi xk = bi , i ∈ Wk 6= ∅,
k ∈ N.
Improving Direction
The feasible sequence {xk ∈ Ω} with decreasing value, f (xk+1 ) < f (xk ), is
generated by following the improving direction p ∈ Rn such that xk+1 = xk + p.
This leads us to
44 Active Set Methods
f (xk+1 ) = f (xk + p)
1
= (xk + p)T G(xk + p) + g T (xk + p)
2
1
= (xTk G + pT G)(xk + p) + g T xk + g T p
2
1
= (xTk Gxk + xTk Gp + pT Gxk + pT Gp) + g T xk + g T p
2
1 1
= xTk Gxk + g T xk + (xTk G + g T )p + pT Gp
2 2
T 1 T
= f (xk ) + (Gxk + g) p + p Gp
2
= f (xk ) + φ(p). (4.8)
and in order to satisfy f (xk+1 ) < f (xk ), the improving direction p must be
computed so that φ(p) < 0 and x̄ = (xk + p) ∈ Ω. Instead of computing
the new optimal point x̄ directly, computational savings are achieved by only
computing p. When x̄ = xk + p, the constraint aTi x̄ = bi becomes
so
aTi p = 0 (4.10)
x̄∗ = xk + p∗ (4.13)
f (x̄∗ ) = f (xk ) + φ(p∗ ). (4.14)
For these relations x̄∗ and f (x̄∗ ) respectively are the optimal solution and the
optimal value of
1 T
min f (x̄) = x̄ Gx̄ + g T x̄ (4.15a)
x̄∈Rn 2
s.t. aTi x̄ = bi i ∈ Wk (4.15b)
and p∗ and φ(p∗ ) respectively are the optimal solution and the optimal value of
1 T
min φ(p) = p Gp + (Gxk + g)T p (4.16a)
p∈Rn 2
s.t. aTi p = 0 i ∈ Wk . (4.16b)
The right hand side of the constraints in (4.16) is zero,which is why it is easier
to find the improving direction p than to solve (4.15). This means that the
improving direction is found by solving (4.16).
Step Length
φ(p∗ ) = 0, p∗ = 0 (4.17)
φ(p∗ ) < 0, p∗ ∈ {p ∈ Rn : aTi p = 0, i ∈ Wk }\{0}. (4.18)
46 Active Set Methods
For p∗ 6= 0 we have
and because of the convexity of the objective function f , this is a fact for all
α ∈]0; 1].
We now know, that if an α ∈]0; 1] exists, then f (xk + αp∗ ) < f (xk ). On this
basis we want to find a point on the line segment p∗ = xk+1 − xk , whereby
the largest possible reduction of the objective function is achieved, and at the
same time the constraints not in the current working set, i.e. i ∈ I\Wk , remain
satisfied. In other words, looking from point xk in the improving direction p∗ ,
we would like to find an α, so that the point xk + αp∗ remains feasible. In this
way the greatest reduction of the objective function is obtained by choosing the
largest possible α without leaving the feasible region.
4.1 Primal Active Set Method 47
Since xk ∈ Ω, we have
bi − aTi xk
ᾱi = ≥ 0, aTi p∗ < 0, i ∈ I\Wk . (4.23)
aTi p∗
From (4.19) and (4.25) we define the line search function h(α) as
dh
(α) = p∗T Gp∗ α − p∗T Gp∗ (4.27a)
dα
= (α − 1)p∗T Gp∗ , (4.27b)
dh
which tells us, that the line search function has its minimum at dα (1) = 0.
Therefore the largest possible reduction in the line search function (4.26) is
achieved by selecting α ∈ [0; 1] as large as possible. So the optimal solution of
1 ∗T
min h(α) = p Gp∗ α2 − p∗T Gp∗ α + f (xk ) (4.28a)
α∈R 2
s.t. aTi (xk + αp∗ ) ≥ bi i∈I (4.28b)
is
∗
α = min 1, min ᾱi
i∈I\Wk :aT
i p <0
∗
bi − aTi xk
= min 1, min ≥ 0. (4.29)
i∈I\Wk :aT
i p <0
∗ aTi p∗
The largest possible reduction in the objective function along the improving
direction p∗ is obtained by the new point xk+1 = xk + α∗ p∗ .
4.1 Primal Active Set Method 49
The largest possible reduction of the objective function in the affine space Mk =
{x ∈ Rn : aTi x = bi , i ∈ Wk } is obtained at point xk+1 = xk + p∗ , i.e. by
selecting α∗ = 1 and Wk+1 = Wk . This point satisfies f (xk+1 ) < f (xk ), and
since Wk+1 = Wk , this point will also be the optimal solution in the affine space
Mk+1 = Mk , thus a new iterate will give p∗ = 0. So, in order to minimize the
objective function further, we must update the working set for each iteration.
This is done either by appending or removing a constraint from the current
working set Wk .
Appending a Constraint
bi − aTi xk
J = arg min (4.30)
i∈I\Wk :aT
i p <0
∗ aTi p∗
aTi p∗ = 0, i ∈ Wk . (4.31)
X
∃γi ∈ R : aj = γi ai , (4.32)
i∈Wk
X
aTj p∗ = γi (aTi p∗ ) = 0, j ∈ I\Wk . (4.33)
| {z }
i∈Wk
=0
Removing a Constraint
X
Gx∗ + g − ai µ∗i = 0 (4.34a)
i∈I
it is seen that xk is the global minimizer x∗ if and only if the pair xk , µ satisfies
X X X
Gx + g − ai µi = Gx + g − ai µi − ai µi = 0 (4.35a)
|{z}
i∈I i∈Wk i∈I\Wk =0
| {z }
=0
aTi xk ≥ bi i∈I (4.35b)
µi ≥ 0 i ∈ I. (4.35c)
µi (aTi xk − bi ) = 0, i ∈ Wk (4.36a)
| {z }
=0
µi (aTi xk − bi ) = 0, i ∈ I\Wk (4.36b)
|{z}
=0
µi (aTi xk − bi ) = 0, i ∈ I. (4.37)
x∗ = xk (4.38)
(
∗ µi , i ∈ Wk
µi = (4.39)
0, i ∈ I\Wk .
But if there exists an index j ∈ Wk such that µj < 0, then the point xk cannot
be the global minimizer of (4.3). So we have to relax, in other words leave, the
constraint aTj xk = bj and move in a direction p such that aTj (xk + αp) > bj .
From sensitivity theory in Nocedal and Wright [14] we know, that a decrease in
function value f is obtained by choosing any constraint for which the Lagrange
52 Active Set Methods
1 T
min φ(p) = p Gp + (Gxk + g)T p (4.40a)
p∈Rn 2
s.t. aTi p = 0 i ∈ Wk \{j}. (4.40b)
X
Gp∗ + Gxk + g − ai µ∗i = 0, (4.41)
i∈Wk \{j}
X
Gxk + g − ai µ̂i = 0 (4.42a)
i∈Wk
aTi xk = bi i ∈ Wk , (4.42b)
X
Gp∗ − ai (µ∗i − µ̂i ) + aj µ̂j = 0, (4.43)
i∈Wk \{j}
which is equivalent to
X µ∗i − µ̂i
aj 6= ai (4.45)
µ̂j
i∈Wk \{j}
implying that p∗ 6= 0. Now we will shortly summarize, what have been stated in
this section so far. If the optimal solution has not been found at iteration k some
negative Lagrange multipliers exist. The constraint j ∈ Wk , which correspond
to the most negative Lagrange multiplier µj is removed from the working set.
The new improving direction is computed by solving (4.40) and it is guaranteed
to be non-zero p∗ 6= 0. This statement is important in the following derivations.
By taking a new step after removing constraint j, we must now guarantee, that
the relaxed constraint is not violated again, in other words that the remaining
constraints in the current working set are still satisfied and that we actually get
a decrease in function value f .
and
As expected, we see that the relaxed constraint j and the constraints in the new
active set are still satisfied.
As (4.40) is strictly convex, we know, that φ(p∗ ) < 0 for the improving direction
p∗ , and the feasible non-optimal solution p = 0 has the value φ(0) = 0. If we
also keep in mind that
From this relation, the convexity of f and because α ∈]0; 1], we know that
So in fact we actually get a decrease in function value f , by taking the new step
having relaxed constraint j.
In this section we have found that if µj < 0, then the current point xk cannot
be a global minimizer. So to proceed we have to remove constraint j from the
current working set, Wk+1 = Wk \{j}, and update the current point by taking
a zero step, xk+1 = xk .
4.1 Primal Active Set Method 55
A constraint removed from the working set cannot be appended to the working
set in the iteration immediately after taking the zero step. This is because a
blocking constraint is characterized by aTj p∗ < 0, while from (4.48) we know,
that aTj p∗ > 0. Still, the possibility of cycling can be a problem for the primal
active set method, for example in cases like
−j −l +j +l −j
. . . −→ {i, j, l} −−→ {i, l} −→ {i} −−→ {i, j} −→ {i, j, l} −−→ {i, l} −→ . . .
(4.54)
This and similar cases are not considered, so if any cycling occurs, it is stopped
by setting a maximum number of iterations for the method.
The procedure of the primal active set method is stated in algorithm 4.1.1.
56 Active Set Methods
Algorithm 4.1.1: Primal Active Set Algorithm for Convex Inequality Con-
strained QP’s.
Input: Feasible point x0 , W = A0 = {i : aTi x0 = bi }.
while NOT STOP do /* find improving direction p∗ */
∗
Find the improving direction p by solving the equality constrained QP:
1
min φ(p) = pT Gp + (Gx + g)T p
p∈Rn 2
s.t. aTi p = 0, i∈W
µi ← 0, i ∈ I\W
if µi ≥ 0 ∀i ∈ W then
STOP, the optimal solution x∗ has been found!
else /* remove constraint j */
x←x
W ← W\{j}, j ∈ W : µj < 0
else /* compute step length α */
bi − aTi x
α = min 1, min
i∈I\W:aT
i p <0
∗ aTi p∗
bi − aTi x
J = arg min
i∈I\W:aT
i p <0
∗ aTi p∗
We will now demonstrate how the primal active set method finds the optimum
in the following example:
1 1 0 0
min f (x) = xT Gx + g T x, G = , g=
x∈Rn 2 0 1 0
s.t. c1 = −x1 + x2 − 1 ≥ 0
1
c2 = − x1 − x2 + 2 ≥ 0
2
c3 = −x2 + 2.5 ≥ 0
c4 = −3x1 + x2 + 3 ≥ 0.
At every iteration k the path (x1 . . . xk ) is plotted together with the constraints,
where active constraints are indicated in red. The 4 constraints and their column
index in A are labeled on the constraint in the plot. The feasible area is in the
top right corner where the plot is lightest. The start position is chosen to be
x = [4.0, 4.0]T which is feasible and the active set is empty W = ∅. For every
iteration we have plotted the situation when we enter the while-loop at x, see
algorithm 4.1.1 .
Iteration 1
The situation is illustrated in figure 4.1. On entering the while-loop the work-
ing set is empty and therefore the improving direction is found to be p =
[−4.0, −4.0]T . As figure 4.1 suggests, the first constraint to be violated taking
this step is c3 which is at step length α = 0.375. The step x̄ = x + αp is taken
and the constraint c3 is appended to the working set.
58 Active Set Methods
Iteration 2
The situation is illustrated in figure 4.2. Now the working set is W = [3] which
means that the new improving direction p is found by minimizing f (x) subject
to c3 (x + p) = 0. The improving direction is found to be p = [−2.5, 0.0]T and
µ = [2.5]. The first constraint to be violated in this direction is c4 which is at
step length α = 0.267. The step x̄ = x + αp is taken and the constraint c4 is
appended to the working set.
Iteration 3
The situation is illustrated in figure 4.3. Here the working set is W = [3, 4]
and the new improving direction is found to be p = [0, 0]T and µ = [3.1, 0.6]T .
Because p = 0 and no negative Lagrange Multipliers exist, position x is optimal.
Therefore the method terminates with x∗ = [1.8, 2.5]T .
In the foregoing, we have described the primal active set method which solves
an inequality constrained convex QP, by keeping track of a working set W. In
this section we will examine the dual active set method, which requires the QP
to be strictly convex. The dual active set method uses the dual set of W, which
we will call WD . The method benefits from always having an easily calculated
feasible starting point and the method does not have the possibility of cycling.
The theory is based on earlier works by Goldfarb and Idnani [10], Schmid and
Biegler [11] and Schittkowski [12].
4.3.1 Survey
1
min f (x) = xT Gx + g T x (4.55a)
x∈Rn 2
s.t. ci (x) = aTi x − bi ≥ 0, i ∈ I. (4.55b)
1 T X
L(x, µ) = x Gx + g T x − µi (aTi x − bi ). (4.56)
2
i∈I
1 X
max L(x, µ) = xT Gx + g T x − µi (aTi x − bi ) (4.57a)
x∈R ,µ∈R
n m 2
i∈I
X
s.t. Gx + g − ai µi = 0 (4.57b)
i∈I
µi ≥ 0 i ∈ I. (4.57c)
The necessary and sufficient conditions for optimality of the dual program is
4.3 Dual active set method 61
X
Gx∗ + g − ai µ∗i = 0 (4.58a)
i∈I
ci (x∗ ) = T ∗
ai x − bi = 0 i ∈ A(x∗ ) (4.58b)
∗
ci (x ) = aTi x∗ − bi > 0 i ∈ I\A(x ) ∗
(4.58c)
∗
µi ≥ 0 i ∈ A(x ) (4.58d)
µi = 0 i ∈ I\A(x∗ ). (4.58e)
These conditions are exactly the same as the optimality conditions of the primal
program (4.55), and this corresponds to the fact that the optimal value L(x∗ , µ∗ )
of the dual program is equivalent to the optimal value f (x∗ ) of the primal
program. This is why the solution of the primal program can be found by
solving the dual program (4.57).
X
Gxk + g − ai µki = 0 (4.59a)
i∈W
ci (xk ) = T k
ai x − bi =0 i∈W (4.59b)
µki ≥0 i ∈ W. (4.59c)
The constraints in the complementary set WD = I\W, i.e. the active set of the
dual program, satisfy
X
Gxk + g − ai µki = 0 (4.60a)
i∈WD
µki = 0 i ∈ WD , (4.60b)
and from (4.58), (4.59) and (4.60) it is clear that an optimum has been found
xk = x∗ if
If this is not the case some violated constraint r ∈ WD exists, i.e. cr (xk ) < 0.
The following relationship explains why {xk , µk } cannot be an optimum in this
case
∂L k k
(x , µ ) = −cr (xk ) > 0. (4.62)
∂µr
This means that (4.57a) can be increased by increasing the Lagrangian multiplier
µr . In fact, this explains the key idea of the dual active set method. The idea is
to choose a constraint cr from the dual active working set WD which is violated
cr (xk ) < 0 and make it satisfied cr (xk ) ≥ 0 by increasing the Lagrangian
multiplier µr . This procedure continues iteratively until no constraints from
WD are violated. At this point the optimum has been found and the method
terminates.
Improving Direction
x̄ = x + s (4.63a)
µ̄i = µi + ui i∈W (4.63b)
µ̄r = µr + t (4.63c)
µ̄i = µi = 0 i ∈ WD \r. (4.63d)
X
Gx̄ + g − ai µ̄i = 0 (4.64a)
i∈I
G −A x̄ g ar
+ − µ̄r = 0, (4.65)
−AT 0 µ̄ b 0
where A = [ai ]i∈W has full column rank, G is symmetric and positive definite,
µ̄ = [µ̄i ]Ti∈W , ar is the constraint from WD we are looking at and µr is the
corresponding Lagrangian multiplier. Using (4.63) this can be formulated as
G −A x g ar
+ − µr +
−AT 0 µ b 0
G −A s ar
− t = 0. (4.66)
−AT 0 u 0
G −A x g ar
+ − µr = 0 (4.67)
−AT 0 µ b 0
G −A s ar
− t = 0, (4.68)
−AT 0 u 0
which is equivalent to
G −A p ar s p
= , = t. (4.69)
−AT 0 v 0 u v
The new improving direction (p, v)T is found by solving (4.69), using a solver
for equality constrained QP’s, e.g. the range space or the null space procedure.
64 Active Set Methods
Step Length
Having the improving direction we now would like to find the step length t
(4.69). This step length should be chosen in a way, that makes (4.57c) satisfied.
From (4.63), (4.66) and (4.69) we have the following statements about the new
step
x̄ = x + s = x + tp (4.70a)
µ̄i = µi + ui = µi + tvi i∈W (4.70b)
µ̄r = µr + t (4.70c)
µ̄i = µi = 0 i ∈ WD \r. (4.70d)
−µi
t ∈ [0, tmax ], tmax = min(∞, min ) ≥ 0. (4.71)
i:vi <0 vi
Now we know what values of t we can choose, in order to retain dual feasibility
when taking the new step. To find out what exact value of t in the interval
(4.71) we should choose to make the step optimal we need to examine what
happens to the primal objective function (4.55a), the dual objective function
(4.57a), and the constraint cr as we take the step.
Now we shall examine how cr (x̄) is related to cr (x). For this reason, we need
to state the following properties. From (4.69) we have
ar = Gp − Av (4.72a)
T
A p = 0. (4.72b)
aTr p = pT Gp ≥ 0 (4.74a)
aTr p T
= p Gp = 0 ⇔ p=0 (4.74b)
aTr p T
= p Gp > 0 ⇔ p 6= 0. (4.74c)
As x̄ = x + tp we get
This means that the constraint cr is increasing (if t > 0) as we move from x to
x̄ and this is exactly what we want as it is negative and violated at x.
In addition to the foregoing we will now investigate what happens to the primal
objective function (4.55a) as we move from x to x̄. Inserting x̄ = x + tp in
(4.55a) gives
1
f (x̄) = f (x + tp) = (x + tp)T G(x + tp) + g T (x + tp), (4.78)
2
1 T 1
f (x̄) = x Gx + g T x + t2 pT Gp + t(Gx + g)T p, (4.79)
2 2
66 Active Set Methods
1
f (x̄) = f (x) + t2 pT Gp + t(Gx + g)T p. (4.80)
2
Gx − Aµ + g − ar µr = 0, (4.81)
which is equivalent to
Gx + g = Aµ + ar µr (4.82)
1
f (x̄) = f (x) + t2 pT Gp + tµr aTr p. (4.85)
2
1 1
f (x̄) = f (x) + t2 aTr p + tµr aTr p = f (x) + t(µr + t)aTr p. (4.86)
2 2
X
L(x̄, µ̄) = f (x̄) − µi ci (x̄), (4.87)
i∈I
By replacing f (x̄) with (4.86), µ̄r with µr + t and cr (x̄) with (4.76), we then
have
1
L(x̄, µ̄) = f (x) + t(µr + t)aTr p − (µr + t)(cr (x) + taTr p), (4.89)
2
which we reformulate as
1
L(x̄, µ̄) = f (x)+µr taTr p+ t2 aTr p−µr cr (x)−µr taTr p−tcr (x)−t2 aTr p (4.90)
2
1
L(x̄, µ̄) = f (x) − µr cr (x) − t2 aTr p − tcr (x). (4.91)
2
X
L(x, µ) = f (x) − µi ci (x) (4.92)
i∈I
1
L(x̄, µ̄) = L(x, µ) − t2 aTr p − tcr (x). (4.94)
2
Now we want to know what values of t make L(x̄, µ̄) ≥ L(x, µ), i.e what values
of t that satisfy
1
L(x̄, µ̄) − L(x, µ) = − t2 aTr p − tcr (x) ≥ 0. (4.95)
2
−cr (x)
t ∈ [0, 2 ]. (4.96)
aTr p
dL
= −taTr p − cr (x). (4.97)
dt
−cr (x)
−t∗ aTr p − cr (x) = 0 ⇔ t∗ = . (4.98)
aTr p
At this point we would like to stop up and present a short summary of what
has been revealed throughout the latest sections. If optimality has not been
4.3 Dual active set method 69
found at iteration k, some violated constraint cr (xk ) ≤ 0 must exist. The new
improving direction is found by solving the equality constrained QP
G −A p ar
= (4.99)
−AT 0 v 0
where A = [ai ]i∈W has full column rank, G is symmetric and positive definite
and ar is the violated constraint. The optimal step length t, which ensures
feasibility is found from statements (4.71) and (4.98)
Both the dual objective function (4.57) and the violated constraint increase as
we take the step.
The KKT system (4.69) can only be solved if G is positive definite, and A
has full column rank. If the constraints in A = [ai ]i∈W and ar are linearly
dependent, it is not possible to add constraint r to the working set W, as
A = [ai ]i∈W∪r in this case would not have full column rank. This problem is
solved by removing constraint j from W, which makes the constraints in the
new working set W̄ = W\{j} and ar linearly independent. This particular case
will be investigated in the following. The linear dependency of A = [ai ]i∈W and
ar can be written as
m
X
ar = γi ai = Aγ. (4.101)
i=1
aTr p = γ T AT p, (4.102)
70 Active Set Methods
When ar and the constraints in the working set are linearly dependent and
(4.69) is solved
G −A p ar
= (4.105)
−AT 0 v 0
we know from (4.74b) and (4.103) that p = 0. If v contains any negative values,
t can be calculated using (4.71)
−µj −µj
t = min ≥ 0, j = arg min . (4.106)
j:vj <0 vj j:vj <0 vj
and this is why we need to remove constraint cj from W and we call the new set
W̄ = W\{j}. Now we will see that ar is linearly independent of the vectors ai
for i ∈ W̄. This proof is done by contradiction. Lets assume that ar is linearly
dependent of the vectors ai for i ∈ W̄, i.e. ar ∈ spanA = [ai ]i∈W̄ .
X
ar = Gp − Av = −Av = A(−v) = ai (−vi ). (4.108)
i∈W
4.3 Dual active set method 71
X
ar = ai (−vi ) + aj (−vj ) (4.109)
i∈W̄
1 1 X
aj = ar + ai vi . (4.110)
−vj −vj
i∈W̄
X
ar = ai γi (4.111)
i∈W̄
1 X 1 X
aj = ai γi + ai vi , (4.112)
−vj −vj
i∈W̄ i∈W̄
which is equivalent to
X γi + vi
aj = ai . (4.113)
−vj
i∈W̄
As we have
X γi + vi
aj = βi ai , βi = (4.114)
−vj
i∈W̄
ar ∈ spanA = [ai ]i∈W̄ cannot be true, and therefore we must conclude that
ar ∈
/ spanA = [ai ]i∈W̄ .
1
L(x̄, µ̄) = L(x, µ) − t2 aTr p − tcr (x) (4.115)
2
and as tcr (x) ≤ 0 we know that L(x̄, µ̄) ≥ L(x, µ) when ar ∈ spanA = [ai ]i∈W .
Now we shall see what happens when no negative elements exist in v from
(4.105). From (4.71) we know that t can be chosen as any non negative value,
and therefore (4.116) becomes
In this case the dual program is unbounded which means that the primal pro-
gram (4.55) is infeasible. Proof of this is to be found in Jørgensen [13]. This
means that no solution exists.
In short, what has been stated in this section, is that when (4.69) is solved and
aTr p = 0 we know that A = [ai ]i∈W and ar are linearly dependent and p = 0.
If there are no negative elements in v the problem is infeasible and no solution
exist. If some negative Lagrangian multipliers exist we should find constraint
cj from W where
−µj −µj
j = arg min , t = min ≥0 (4.118)
j:vj <0 vj j:vj <0 vj
One of the forces of the dual active set method is that a feasible starting point
is easily calculated. Starting out with all constraints in the dual active set WD
and therefore W being empty
µi = 0, i ∈ WD = I, W =∅ (4.120)
and if we start in
x = −G−1 g (4.121)
X
Gx + g − ai µi = Gx + g = 0 (4.122a)
i∈I
µi ≥ 0 i ∈ I. (4.122b)
1 T X
L(x, µ) = x Gx + g T x − µi (aTi x − bi )
2
i∈I
1
= xT Gx + g T x
2
= f (x), (4.123)
which means that the starting point is at the minimum of the objective function
of the primal program (4.55a) without taking notice of the constrains (4.55b).
74 Active Set Methods
Because the inverse Hessian matrix is used we must require the QP to be strictly
convex.
4.3.5 In summary
Now we will summarize what has been discussed in this section and show how
the dual active set method works. At iteration k we have (x, µ, r, W, WD ) where
cr (x) < 0. Using the null space or the range space procedure the new improving
direction is calculated by solving
G −A p ar
= , A = [ai ]i∈W . (4.124)
−AT 0 v 0
If A = [ai ]i∈W and ar are linearly dependent and no elements from v are
negative the problem is infeasible and the method is terminated. Using (4.103)
and (4.117) this is the case when
aTr p = 0 ∧ vi ≥ 0, i ∈ W. (4.125)
If on the other hand A = [ai ]i∈W and ar are linearly dependent and some
elements from v are negative, cj is removed from W, step length t is calculated
according to (4.106) and a new step is taken
−µj −µj
t = min ≥ 0, j = arg min (4.126a)
j:vj <0 vj j:vj <0 vj
µ̄i = µi + tvi i∈W (4.126b)
µ̄r = µr + t (4.126c)
W = W\{j}. (4.126d)
If A = [ai ]i∈W and ar are linearly independent and some elements from v are
negative, we calculate two step lengths t1 and t2 according to (4.71) and (4.98)
−µj −µj
t1 = min(∞, min ), j = arg min (4.127a)
j:vj <0 vj j:vj <0 vj
−cr (x)
t2 = , (4.127b)
aTr p
where t1 can be regarded as the step length in dual space because it assures
that (4.57c) is satisfied whenever 0 ≤ t ≤ t1 . Constraint (4.55b) is satisfied for
4.3 Dual active set method 75
If tD < tP then tD is used as the step length and (4.57c) remain satisfied when
we take the step. After taking the step, cj is removed from W because µ̄j = 0
x̄ = x + tD p (4.128a)
µ̄i = µi + tD vi , i ∈ W (4.128b)
µ̄r = µr + tD (4.128c)
W = W\{j}. (4.128d)
If tP ≤ tD then tP is used as the step length and (4.55b) get satisfied for cr .
After taking the step we have that cr (x̄) = 0 and therefore r is appended to W
x̄ = x + tP p (4.129a)
µ̄i = µi + tP vi , i ∈ W (4.129b)
µ̄r = µr + tP (4.129c)
W = W ∪ {r}. (4.129d)
If A = [ai ]i∈W and ar are linearly independent and no elements from v are
negative we have found the optimum and the program is terminated.
The procedure of the dual active set method is stated in algorithm 4.3.1.
76 Active Set Methods
Algorithm 4.3.1: Dual Active Set Algorithm for Strictly Convex Inequality
Constrained QP’s. Note: WD = I\W.
Compute x0 = −G−1 g, set µi = 0, i ∈ WD and W = ∅.
while NOT STOP do
if ci (x) ≥ 0 ∀i ∈ WD then
STOP, the optimal solution x∗ has been found!
Select r ∈ WD : cr (x) < 0.
while cr (x) < 0 do /* find improving direction p */
Find the improving direction p by solving the equality constrained
QP:
G −A p ar
= , A = [ai ]i∈W
−AT 0 v 0
if aTr p = 0 then
if vi ≥ 0 ∀i ∈ W then
STOP, the problem is infeasible!
else /* compute step length t, remove constraint j */
−µi −µi
t = min , J = arg min
i∈W:vi <0 vi i∈W:vi <0 vi
x←x
µi ← µi + tvi , i ∈ W
µr ← µr + t
W ← W\{j}, j ∈ J
else /* compute step length tD and tP */
−µi −µi
tD = min ∞, min , J = arg min
i∈W:vi <0 vi i∈W:vi <0 vi
−cr (x)
tP =
aTr p
4.3.6 Termination
The dual active set method does not have the ability to cycle as it terminates
in a finite number of steps. This is one of the main forces of the method, and
therefore we will now investigate this property.
As the algorithm (4.3.1) suggests, the method mainly consists of two while-loops
which we call outer-loop and inner-loop. In the outer-loop we test if optimality
has been found. If this is not the case we choose some violated constraint r,
cr (x) < 0 and move to the inner-loop.
A step in dual space is taken whenever tD < tP and in this case we have
cr (x̄) = cr (x) + tD aTr p < cr (x) + tP aTr p = 0. This means that we will never
leave the inner-loop after a step in dual space as cr (x̄) < 0. A constraint cj
is removed from the working set W when we take a step in dual space, which
means that |W| is the maximum number of steps in dual space that can be
taken in succession. After a sequence of 0 ≤ s ≤ |W| steps in dual space, a step
in primal space will cause us to leave the inner-loop. This step in primal space
guarantees that L is strictly larger when we leave the inner-loop than when we
entered it.
As the constraints in the working set W are linearly independent at any time,
the corresponding solution (x, µ) is unique. Also as L(xq+1 , µq+1 ) > L(xq , µq )
(where q defines the q ′ th iteration of the outer loop) we know that the combina-
tion of constraints in W is unique for any iteration q. And because the number
of different ways the working set can be chosen from I is finite and bounded by
2|I| , we know that the method will terminate in a finite number of iterations.
78 Active Set Methods
In the following example we will demonstrate how the dual active set method
finds the optimum
1 1 0 0
min f (x) = xT Gx + g T x, G = , g=
x∈Rn 2 0 1 0
s.t. c1 = −x1 + x2 − 1 ≥ 0
1
c2 = − x1 − x2 + 2 ≥ 0
2
c3 = −x2 + 2.5 ≥ 0
c4 = −3x1 + x2 + 3 ≥ 0.
At every iteration k we plot the path (x1 . . . xk ) together with the constraints,
where active constraints are indicated with red. The 4 constraints and their
column-index in A are labeled on the constraint in the plots. The primal fea-
sible area is in the top right corner where the plot is lightest. We use the least
negative value of c(xk ) every time cr is chosen, even though any negative con-
straint could be used. For every iteration we have plotted the situation when
we enter the while loop.
Iteration 1
This situation is illustrated in figure 4.4. The starting point is at x = G−1 g =
(0, 0)T , µ = 0 and W = ∅. On entering the while loop we have c(x) =
[1.0, −2.0, −2.5, −3.0]T and therefore r = 2 is chosen because the second element
is least negative. The working set is empty and therefore the new improving
direction is found to p = [0.5, 1.0]T and u = []. As the step length in primal
space is tP = 1.6 and the step length in dual space is tD = ∞, tP is used, and
therefore r is appended to the working set. The step is taken as seen in figure
4.5.
4.4 Dual active set method by example 79
Iteration 2
This situation is illustrated in figure 4.5. On entering the while loop we have
W = [2], and because cr (x) = 0 we should choose a new r. As c(x) =
[0.2, 0, −0.9, −2.2]T , r = 3 is chosen. The new improving direction is found
to be p = [−0.4, 0.2]T and u = [−0.8]. As tD = 2.0 and tP = 4.5 a step in dual
space is taken and c2 is removed from W.
Iteration 3
This situation is illustrated in figure 4.6. Because cr (x) 6= 0 we keep r = 3.
The working set is empty W = ∅. The improving direction is found to be
p = [0.0, 1.0]T and u = []. A step in primal space is taken as tD = ∞ and
80 Active Set Methods
Iteration 4
This situation is illustrated in figure 4.7. Now cr (x) = 0 and therefore a new r
should be chosen. As c(x) = [−1.5, 0.5, 0, −5.5]T , we choose r = 1. The working
set is W = [3], and the new improving direction is p = [1.0, 0.0]T and u = [1].
We use tP as tD = ∞ and tP = 1.5 and r is appended to W after taking the
step.
Iteration 5
This situation is illustrated in figure 4.8. As cr (x) = 0 we must choose a new
r and as c(x) = [0, 1.25, 0, −1.0]T , r = 4 is chosen. At this point W = [3, 1].
4.4 Dual active set method by example 81
The new improving direction is p = [0.0, 0.0]T and u = [−2, −3] and therefore
aTr p = 0. This means that ar is linearly dependent of the constraints in W and
therefore 1 is removed from W.
Iteration 6
This situation is illustrated in figure 4.9. On entering the while loop we have
c3 in the working set W = [3]. And r remains 4 because cr (x) 6= 0. The new
improving direction is p = [3.0, 0.0]T and u = [1]. The step lengths are tD = ∞
and tP = 0.11 and therefore a step in primal space is taken and r is appended
to W.
Iteration 7
This situation is illustrated in figure 4.10. Now the working set is W = [3, 4]
and as cr (x) = 0, a new r must be chosen. But c(x) = [0.33, 1.42, 0, 0]T (no
negative elements) and therefore the global optimal solution has been found
and the algorithm is terminated. The optimal solution is x∗ = [1.83, 2.50]T and
µ∗ = [0, 0, 3.11, 0.61]T .
Figure 4.10: Iteration 7, W = [3, 4], x∗ = [1.8, 2.5]T , µ∗ = [0, 0, 3.11, 0.61]T .
When solving an inequality constrained convex QP, we use either the primal
active set method or the dual active set method. In both methods we solve a
sequence of KKT systems, where each KKT system correspond to an equality
constrained QP. To solve the KKT system we use one of four methods: The
range space procedure, the null space procedure, or one of the two with fac-
torization update instead of complete factorizations. We will test these four
methods for computational speed to find out how they perform compared to
each other.
Usually the constraints in an inequality constrained QP are divided into bounded
variables and general constraints. This division can be used to further optimiza-
tion of the factorization updates, as we will discuss later in this chapter. As a
test case we will use the quadruple tank problem which is described in appendix
A.
The active set methods solve a sequence of KKT systems by use of the range
space procedure, the null space procedure or one of the two with factorization
84 Test and Refinements
update. Now we will compare the performance of these methods by solving the
quadruple tank problem. By discretizing with N = 300 we define an inequality
constrained strictly convex QP with n = 1800 variables and |I| = 7200 con-
straints. We have chosen to use the dual active set method because it does
not need a precalculated starting point. Different parts of the process are il-
lustrated in figure 5.1. Figure 5.1(a) shows the computational time for solving
the KKT system for each iteration and figure 5.1(c) shows the number of active
constraints |W | for each iteration. The size of the active set grows rapidly in
the first third of the iterations after which this upward movement fades out a
little. This explains why the computational time for the null space procedure
decreases fast to begin with and then fades out, as it is proportional to the
size of the null space (n − m). Likewise, the computational time for the range
space procedure grows proportional to the dimension of the range space (m).
The null space procedure with factorization update is much faster than the null
space procedure with complete factorization even though some disturbance is
observed in the beginning. This disturbance is probably due to the fact that the
testruns are carried out on shared servers. The range space procedure improves
slightly whenever factorization update is used. When solving this particular
problem, it is clear from figures 5.1(a) and 5.1(c), that range space procedure
with factorization update should be used until approximately 800 constraints
800
are active, corresponding to 1800 n =≃ 0.45n, after which the null space proce-
dure with factorization update should be used. In theory the total number of
iterations should be exactly the same for all four methods, however they differ
a little due to numerical instability as seen in figure 5.1(c), where the curves
are not completely aligned. The number of active constraints at the optimal
solution is |W | = 1658.
5.1 Computational Cost of the Range and the Null Space Procedures with
Update 85
40 40
time [s]
time [s]
30 30
20 20
10 10
0 0
0 1000 2000 3000 4000 5000 6000 7000 8000 9000 0 200 400 600 800 1000 1200 1400 1600 1800
iteration |W| (appending)
(a) Computational time for solving the (b) Computational time for solving the
KKT system plotted for each iteration. KKT system each time a constraint is ap-
pended to the active set W .
Number of elements in W at each iteration , max(|W|) =1658 Test of Speed: N =300, n =1800
1800 70
null space
null space update
1600
60 range space
range space update
1400 null space
null space update 50
1200 range space
range space update
40
time [s]
1000
|W|
800 30
600
20
400
10
200
0 0
0 1000 2000 3000 4000 5000 6000 7000 8000 9000 0 200 400 600 800 1000 1200 1400 1600 1800
iteration |W| (removing)
(c) Number of active constraints plotted at (d) Computational time for solving the
each iteration. KKT system each time a constraint is re-
moved from the active set W .
Figure 5.1: The process of solving the quadruple tank problem with N=300,
(1800 variables and 7200 constraints). Around 8800 iterations are needed (de-
pending on the method) and the number of active constraints at the solution is
|W | = 1658.
86 Test and Refinements
So far, we have only considered constraints defined like aTi x ≥ bi . Since some
of the constraints are likely to be bounds on variables xi ≥ bi , we divide all
constraints into bounds and general constraints. The structure of our QP solver
is then defined as follows
1 T
min x Gx + g T x (5.1a)
x∈Rn 2
s.t. li ≤ xi ≤ ui i ∈ Ib = 1, 2, ..., n (5.1b)
(bl )i ≤ aTi x ≤ (bu )i i ∈ Igc = 1, 2, ..., mgc (5.1c)
where Ib is the set of bounds and Igc is the set of general constraints. This
means that we have upper and lower limits on every bound and on every general
constraint, so that the total number of constraints is |I| = 2n + 2mgc . We call
the active constraint matrix C ∈ Rn×m and it contains both active bounds
and active general constraints. Whenever a bound is active we say that the
corresponding variable xi is fixed. By use of a permutation matrix P ∈ Rn×n
we organize x and C in the following manner
x̃ C̃
= P x, = PC (5.2)
x̂ Ĉ
0 Ã
P C = P (B A) = (5.3)
I Â
where B ∈ Rn×n̂ contains the bounds and A ∈ Rn×(m−n̂) contains the general
constraints. So we have à ∈ Rñ×(m−n̂) and  ∈ Rn̂×(m−n̂) and I ∈ Rn̂×n̂ as
the identity matrix.
defined as
Q̃ 0 0 T̃
Q= , T = (5.4)
0 I I Â
The part of the QT factorization which corresponds to the free variables consists
of Q̃ and T̃ . From (5.4) it is clear that this is the only part that needs to be
updated whenever a constraint is appended to or removed from the active set.
The details of how these updates are carried out can be found in Gill et al.
[9] but the basic idea is similar to the one described in chapter 3. The QT
structure is obtained using givens rotations on specific parts of the modified QT
factorization after appending or removing a constraint.
number of constraints
3.5 800
time [s]
3
600
2.5
2 400
1.5
200 total number of active constraints
number of active bounds
1
number of active general constraints
0.5 0
0 500 1000 1500 2000 2500 3000 3500 0 500 1000 1500 2000 2500 3000 3500
iteration iteration
(a) Computational time for solving the (b) The number of active bounds and active
KKT system plotted for each iteration. Null general constraints and the sum of the two
space update 2 is the new update based on plotted at each iteration.
fixed and free variables.
Figure 5.2: Computational time and the corresponding number of active bounds
and active general constraints plotted for each iteration when solving the
quadruple tank problem with N=200, (1200 variables and 4800 constraints).
The problem is solved using both the null space update and the null space
update based on fixed and free variables.
and the corresponding pairs, indexed p, are then organized in P in the following
manner
5.3 Corresponding Constraints 89
1, 2, ..., n, (5.10)
| {z }
x≥l
Unbounded variables and unbounded general constraints, where the upper and/or
lower limits are ±∞ respectively, are never violated. So they are not considered,
when I and P are initialized. E.g. if l2 = −∞, then i = 2 will not exist in I,
and p2 = n + 2 will not exist in P.
In practice, the primal and the dual active set methods are implemented using
two sets, the active set given as the working set Wk and the inactive set I\Wk .
When a constraint j ∈ I\Wk becomes active, it is appended to the active set
and appended to the inactive set together with its corresponding pair
Besides the gain of computational speed, the stability of the dual active set
method is also increased. Equality constraints are given as two inequalities
with the same value as upper and lower limits. So because of numerical in-
stabilities, the method tends to append corresponding constraints to the active
set, when it is close to the solution. If this is the case, the constraint matrix A
becomes linearly depended, and the dual active set method terminates because
of infeasibility. But by removing the corresponding pairs from the inactive set,
this problem will never occur. The primal active set method will always find the
solution before the possibility of two corresponding constraints becomes active
simultaneously, so for this method we gain computational speed only.
The quadruple tank problem is now solved, see figure 5.3, without removing the
corresponding pairs from the inactive set - so only the active constraints are
removed.
In figure 5.3(a) and 5.3(b) we see the indices of the constraints of the active
set Wk and the inactive set I\Wk respectively. Not surprisingly it is seen,
that the constraints currently in the active set are missing in the inactive set.
Also in figure 5.3(c) and 5.3(d) we see, that the relation between the number of
constraints in the active set and the number of constraints in the inactive set
as expected satisfy
220 220
200 200
180 180
160 160
140 140
120 120
i
i
100 100
80 80
60 60
40 40
20 20
(a) Indices of active bounds (blue) and ac- (b) Indices of inactive bounds (blue) and
tive general constraints (red) per iteration. inactive general constraints (red) per iter-
ation.
I \ W: max no bounds = 9, max no gen constr = 58, max total = 67 I\W: min no bounds = 111, min no gen constr = 62, min total = 173
240 240
no bounds no bounds
220 220
no gen constr no gen constr
200 total 200 total
180 180
160 160
140 140
no
no
120 120
100 100
80 80
60 60
40 40
20 20
(c) Number of active bounds and general (d) Number of inactive bounds and general
constraints per iteration. constraints per iteration.
Figure 5.3: The process of solving the quadruple tank problem using the primal
active set method with N = 10, so n = 60 and |I| = 240, without removing the
corresponding pairs from the inactive set I\Wk .
92 Test and Refinements
The quadruple tank problem is now solved again, see figure 5.4, but this time
we remove the corresponding pairs from the inactive set as well.
220 220
200 200
180 180
160 160
140 140
120 120
i
i
100 100
80 80
60 60
40 40
20 20
(a) Indices of active bounds (blue) and ac- (b) Indices of inactive bounds (blue) and
tive general constraints (red) per iteration. inactive general constraints (red) per iter-
ation.
I \ W: max no bounds = 9, max no gen constr = 58, max total = 67 I\W: min no bounds = 102, min no gen constr = 4, min total = 106
240 240
no bounds no bounds
220 220
no gen constr no gen constr
200 total 200 total
180 180
160 160
140 140
no
no
120 120
100 100
80 80
60 60
40 40
20 20
(c) Number of active bounds and general (d) Number of inactive bounds and general
constraints per iteration. constraints per iteration.
Figure 5.4: The process of solving the quadruple tank problem using the primal
active set method with N = 10, so n = 60 and |I| = 240, showing the effect of
removing the corresponding pairs from the inactive set I\Wk .
In figure 5.4(a) the indices of the constraints in the active set Wk are the same
as before the removal of the corresponding pairs. And in figure 5.4(b) we now
see, that all active constraints and their corresponding pairs are removed from
the inactive set I\Wk , and the set is seen to be much more sparse. The new
relation between the number of constraints in the active set and the number of
constraints in the inactive set is seen in figure 5.4(c) and 5.4(d). And the indices
of the constraints in the inactive set, when we also remove the corresponding
pairs, are found to be {I\Wk }\{pi }, i ∈ Wk . So now we have the new relation
5.4 Distinguishing Between Bounds and General Constraints 93
described as follows
which leads to
So we see, that the inactive set overall is reduced twice the size of the active set
by also removing all corresponding constraints pi , i ∈ Wk , from the inactive set.
This is also seen by comparing figure 5.4(c) and 5.3(c).
In both the primal and the dual active set methods some computations involving
the constraints are made, e.g. checking the feasibility of the constraints. All
constraints in I are divided into bounds and general constraints, and via the
indices i ∈ I it is easy to distinguish, if a constraint is a bound or a general
constraint. This can be exploited to gain some computational speed, since
computations regarding a bound only involve the fixed variable, and therefore
it is very cheap to carry out.
94 Test and Refinements
Chapter 6
Nonlinear Programming
In this chapter we will investigate how nonlinear convex programs with nonlinear
constraints can be solved by solving a sequence of QP’s. The nonlinear program
is solved using Newton’s method and the calculation of a Newton step can
be formulated as a QP and found using a QP solver. As Newton’s method
solves a nonlinear program by a sequence of Newton steps, this method is called
sequential quadratic programming (SQP).
Each step of Newton’s method is found by solving a QP. The theory is based
on the work of Nocedal and Wright [14] and Jørgensen [15]. To begin with, we
will focus on solving the equality constrained nonlinear program
function
F1 (x, y)
F (x, y) =
F2 (x, y)
∇x L(x, y)
=
∇y L(x, y)
∇f (x) − ∇h(x)y
= = 0. (6.4)
−h(x)
F1 (x, y)
∇F (x, y) = ∇
F2 (x, y)
∂F1 ∂F2
∂ x1 ∂ x1
= ∂F1 ∂F2
∂y2 ∂y2
2
∇xx L(x, y) −∇h(x)
= , (6.6)
−∇h(x)T 0
6.1 Sequential Quadratic Programming 97
m
X
∇2xx L(x, y) = ∇2 f (x) − yi ∇2 hi (x). (6.7)
i=1
Because ∇F (x, y) is symmetric we know that J(x, y) = ∇F (x, y)T = ∇F (x, y),
and therefore Newton’s method (6.5) gives
∇2xx L(x, y) −∇h(x) ∆x ∇f (x) − ∇h(x)y
=− . (6.8)
−∇h(x)T 0 ∆y −h(x)
1
min ∆xT (∇2xx L(x, y))∆x + (∇x L(x, y))T ∆x (6.9a)
∆x∈Rn 2
s.t. ∇h(x)T ∆x = −h(x). (6.9b)
This is clearly a QP and the optimum (∆xT , ∆y T ) from (6.8) is found by using
a QP-solver, e.g. the one implemented in this thesis, see appendix B.
The system (6.8) can be expressed in a simpler form, by replacing ∆y with µ−y
∇2xx L(x, y) −∇h(x) ∆x ∇f (x) − ∇h(x)y
=− (6.10)
−∇h(x)T 0 µ−y −h(x)
which is equivalent to
∇2xx L(x, y) −∇h(x) ∆x ∇h(x)y
+ =
−∇h(x)T 0 µ 0
∇f (x) ∇h(x)y
− + . (6.11)
−h(x) 0
98 Nonlinear Programming
∇2xx L(x, y) −∇h(x) ∆x ∇f (x)
=− , (6.12)
−∇h(x)T 0 µ −h(x)
1
min ∆xT ∇2xx L(x, y)∆x + ∇f (x)T ∆x (6.13a)
∆x 2
s.t. ∇h(x)T ∆x = −h(x). (6.13b)
then the program, that defines the Newton step is an inequality constrained QP
of the form
1
min ∆xT ∇2xx L(x, y)∆x + ∇f (x)T ∆x (6.15a)
∆x 2
s.t. ∇h(x)T ∆x ≥ −h(x). (6.15b)
When ∇2xx L(x, y) is positive definite and ∇h(x)T has full column rank the
solution to this program can be found using either the primal active set method
or the dual active set method.
6.2 SQP by example 99
In this section our SQP implementation will be tested and each Newton step
will be illustrated graphically. The nonlinear program that we want to solve is
where y are the Lagrangian multipliers and h(x) are the function values of the
constraints. This is done by using Newton’s method to find the solution of
F1 (x, y)
F (x, y) =
F2 (x, y)
∇x L(x, y)
=
∇y L(x, y)
∇f (x) − ∇h(x)y
= = 0. (6.18)
−h(x)
∇2xx L(x, y) −∇h(x) ∆x ∇f (x) − ∇h(x)y
=− . (6.19)
−∇h(x)T 0 ∆y −h(x)
In this example we have calculated the analytical Hessian matrix ∇2xx L(x, y) of
(6.17) to make the relation (6.19) exact, even though a BFGS update by Powell
[16] has been implemented. This is done for illustrational purpose alone, as we
want to plot each Newton step in F11 (x, y) and F12 (x, y) from (6.18) in relation
100 Nonlinear Programming
to the improving direction. The analytical Hessian matrix can be very expensive
to evaluate, and therefore the BFGS approximation is usually preferred. When
the improving direction [∆x, ∆y]T has been found by solving (6.19), the step
size α is calculated in a line search function implemented according to the one
suggested by Powell [16].
Iteration 1
We start at position (x0 , y 0 ) = ([−4, −4]T , [0, 0, 0]T ) with the corresponding
Lagrangian function value L(x0 , y 0 ) = 512. The first Newton step leads us
to (x1 , y 1 ) = ([−0.6253, −2.4966]T , [0, 32.6621, 0]T ) with L(x1 , y 1 ) = 410.9783,
and the path is illustrated in figure 6.1(a). In figures 6.1(b) and 6.1(c) F11
and F12 are plotted in relation to the step size α, where the red line illustrates
the step taken. We have plotted F11 and F12 for α ∈ [−1, 3] even though the
line search function returns α ∈ [0, 1]. In figures 6.1(b) and 6.1(c), α = 0 is the
position before the step is taken and α ∈ [0, 1] where the red line ends illustrates
the position after taking the step. It is clear from the figures, that a full step
(α = 1) is taken and that F11 and F12 increase from −256 to −172.4725, and
−256 to −94.9038, respectively.
1500
−200
1000
F2
F1
500 −400
0
−600
−500
−1000 −800
−1 0 1 2 3 −1 0 1 2 3
α α
(a) The path. (b) Newton step in F11 . (c) Newton step in F12 .
Figure 6.1: The Newton step at iteration 1. L(x0 , y 0 ) = 512 and L(x1 , y 1 ) =
410.9783.
Iteration 2
Having taken the second step the position is (x2 , y 2 ) = ([1.3197, −1.3201]T , [0, 25.7498, 0]T )
as seen in figure 6.2(a). The Lagrangian function value is L(x2 , y 2 ) = 103.4791.
The step size is α = 1, F11 increases from −172.4725 to −25.8427 and F12
increases from −94.9038 to −34.9515 as seen in figures 6.2(b) and 6.2(c).
6.2 SQP by example 101
−50
500
−100
F2
F1
0
−150
−500
−200
−1000 −250
−1 0 1 2 3 −1 0 1 2 3
α α
(a) The path. (b) Newton step in F11 . (c) Newton step in F12 .
Iteration 3
After the third step, the position is (x3 , y 3 ) = ([1.6667, 1.9907]T , [15.7893, 44.2432, 0]T ),
see figure 6.3(a). The Lagrangian function value is L(x3 , y 3 ) = 30.6487. Again
the step size is α = 1, F11 increases from −25.8427 to 25.8647 and F12 increases
from −34.9515 to −28.4761 as seen in figure 6.3(b) and 6.3(c).
2000
200
1500
F2
F1
100 1000
500
0
0
−100 −500
−1 0 1 2 3 −1 0 1 2 3
α α
(a) The path. (b) Newton step in F11 . (c) Newton step in F12 .
Iteration 4
The fourth step takes us to (x4 , y 4 ) = ([1.6667, 2.1111]T , [2.1120, 35.1698, 0]T ),
see figure 6.4(a). The Lagrangian function value is L(x4 , y 4 ) = 27.5790. The
step size is α = 1, F11 decreases from 25.8647 to −3.55271e−15 and F12 increases
from −28.4761 to 0.3533 as seen in figures 6.4(b) and 6.4(c). Even though
refinements can be made by taking more steps we stop the algorithm at the
optimal position (x∗ , y ∗ ) = (x4 , y 4 ) = ([1.6667, 2.1111]T , [2.1120, 35.1698, 0]T )
where the optimal value is f (x∗ ) = 27.5790.
102 Nonlinear Programming
40
50
20
F2
F1
0 0
−20
−50
−40
−60 −100
−1 0 1 2 3 −1 0 1 2 3
α α
(a) The path. (b) Newton step in F11 . (c) Newton step in F12 .
Conclusion
In this thesis we have investigated the active set methods, together with the
range and null space procedures which are used in solving QP’s. We have also
focused on refining the methods and procedures in order to gain efficiency and
reliability. Now we will summarize the most important observations found in
the thesis.
The primal active set method is the most intuitive method. However, it has
two major disadvantages. Firstly, it requires a feasible starting point, which is
not trivial to find. Secondly and most crucially, is the possibility of cycling.
The dual active set method does not suffer from these drawbacks. The method
easily computes the starting point itself, and furthermore convergence is guar-
anteed. On the other hand, the primal active set method has the advantage of
only requiring the Hessian matrix to be positive semi definite.
The range space and the null space procedures are equally good. But, where
the range space procedure is fast, the null space procedure is slow and vice
versa. Thus, in practice the choice of method is problem specific. For problems
consisting of a small number of active constraints in relation to the number of
variables, the range space procedure is preferable. And for problems with a large
number active constraints compared to the number of variables, the null space
procedure is to be preferred. If the nature of the problem potentially allows a
large number of constraints in comparison to the number of variables, then, to
104 Conclusion
By using Givens rotations, the factorizations used to solve the KKT system can
be updated instead of completely recomputed. And as the active set methods
solve a sequence of KKT systems, the total computational savings are signifi-
cant. The null space procedure in particular has become more efficient. These
updates have been further refined by distinguishing bounds, i.e. fixed variables,
from general constraints. The greater fraction of active bounds compared to
active general constraints, the smaller the KKT system gets and vice versa.
Therefore, this particular update is of the utmost importance, when the QP
contains potentially many active bounds.
Dynamic Shift Implementation of dynamic shift between the range space and
null space procedures would be interesting, because computational speed
could be gained this way.
Low Level Language Our QP solver has been implemented in Matlab, and
standard Matlab functions such as chol and qr have been used. In
future works, implementation in Fortran or C++ would be preferable.
This would make the performance tests of the different methods more
reliable. Implementation in any low level programming language may
be expected to improve general performance significantly. Furthermore,
any theoretically computed performances may also be expected to hold in
practice.
Precomputed Active Set The dual active set method requires the Hessian
matrix G of the objective function to be positive definite, as it computes
7.1 Future Work 105
[2] Gill, P E., Gould, N. I. M., Murray, W., Saunders, M. A., Wright, M. H.
A Weighted Gram-Schmidt Method for Convex Quadratic Pro- gramming.
Mathematical Programming, 30, (1984).
[5] Wilkinson, J. H.
The Algebraic Eigenvalue Problem, (1965).
[8] Gill, P. E. and Murray, W. Numerically Stable Methods for Quadratic Pro-
gramming. Mathematical Programming, 14.,(1978).
108 BIBLIOGRAPHY
The quadruple tank process Jørgensen [17] is a system of four tanks, which are
connected through pipes as illustrated in figure A.1. Water from a main-tank
is transported around the system and the flow is controlled by the pumps F1
and F2 . The optimization problem is to stabilize the water level in tank 1 and
2 at some level, called set points illustrated as a red line. Values γ1 and γ2 of
the two valves control how much water is pumped directly into tank 1 and 2
respectively. The valves are constant, and essential for the ease with which the
process is controlled.
The dynamics of the quadruple tank process are described in the following
differential equations
dh1 γ1 a3 p a1 p
= F1 + 2gh3 − 2gh1 (A.1a)
dt A1 A1 A1
dh2 γ2 a4 p a2 p
= F2 + 2gh4 − 2gh2 (A.1b)
dt A2 A2 A2
dh3 1 − γ2 a3 p
= F2 − 2gh3 (A.1c)
dt A3 A3
dh4 1 − γ1 a4 p
= F1 − 2gh4 (A.1d)
dt A4 A4
110 Quadruple Tank Process
where Ai is the cross sectional area, ai is the area of outlet pipe, hi is the water
level of tank no. i, γ1 and γ2 are flow distribution constants of the two valves,
g is acceleration of gravity and F1 and F2 are the two rate of flows. As the QP
solver requires the constraints to be linear we need to linearize the equations in
(A.1). Of course this linearization causes the model to be a much more coarse
approximation, but as the purpose is to build a convex QP for testing, this is
of no importance. The linearizations are
111
dh1 γ1 a3 a1
= F1 + 2gh3 − 2gh1 (A.2a)
dt A1 A1 A1
dh2 γ2 a4 a2
= F2 + 2gh4 − 2gh2 (A.2b)
dt A2 A2 A2
dh3 1 − γ2 a3
= F2 − 2gh3 (A.2c)
dt A3 A3
dh4 1 − γ1 a4
= F1 − 2gh4 . (A.2d)
dt A4 A4
d
x(t) = f (x(t), u(t)), x = [h1 h2 h3 h4 ]T , u = [F1 F2 ]T (A.3)
dt
The function F (xk , uk , xk+1 ) = 0 defines four equality constraints, one for
each height in x. As the time period is discretized into N time steps, this
gives 4N equality constraints. Because each equality constraint is defined as
two inequality constraints of identical value, as lower and upper bound, (A.4)
defines 8N inequality constraints, called general constraints.
which gives 2N (|u| + |x|) = 12N inequality constraints, called bounds. We have
also defined restrictions on how much the rate of flows can change between two
time steps
112 Quadruple Tank Process
Z
1
min ((h1 (t) − r1 )2 + (h2 (t) − r2 )2 )dt (A.7)
2
where r1 and r2 are the set points. The exact details of how the system is set
up as a QP can be found in either Jørgensen [17] or our Matlab implemen-
tation quad tank demo.m. The quadruple tank process defines an inequality
constrained convex QP of (|u| + |x|)N = 6N variables and (8 + 12 + 4)N = 24N
inequality constraints consisting of 12N bounds and 12N general constraints.
t = [0, 360]
N = 100
umin = [0, 0]T
umax = [500, 500]T
∆umin = [−50, −50]T
∆umax = [50, 50]T
u0 = [0, 0]T
γ1 = 0.45
γ2 = 0.40
r1 = 30
r2 = 30
xmin = [0, 0, 0, 0]T
xmax = [40, 40, 40, 40]T
x0 = [0, 0, 0, 0]T .
113
40 40
30 30
h [cm]
h [cm]
20 20
2
1
10 10
0 0
0 50 100 150 200 250 300 350 400 0 50 100 150 200 250 300 350 400
t [min] t [min]
40 40
30 30
h3 [cm]
h [cm]
20 20
4
10 10
0 0
0 50 100 150 200 250 300 350 400 0 50 100 150 200 250 300 350 400
t [min] t [min]
600 600
400 400
F1 [cm /s]
F [cm3/s]
3
200 200
2
0 0
−200 −200
−50 0 50 100 150 200 250 300 350 400 −50 0 50 100 150 200 250 300 350 400
t [min] t [min]
100 100
50 50
∆ F1 [cm /s ]
∆ F [cm3/s2]
3 2
0 0
2
−50 −50
−100 −100
−50 0 50 100 150 200 250 300 350 400 −50 0 50 100 150 200 250 300 350 400
t [min] t [min]
Figure A.2: The solution of the quadruple tank problem found by using our QP
solver. It is seen that the water levels in tank 1 and 2 are stabilized around the
setpoints. The water levels in tank 3 and 4, the two flows F1 and F2 and the
difference in flow between time steps ∆F1 and ∆F2 are also plotted.
(a) k = 1 (b) k = 3
(a) k = 6 (b) k = 10
(a) k = 15 (b) k = 20
(a) k = 25 (b) k = 30
(a) k = 40 (b) k = 60
QP Solver Interface
1
minn f (x) = xT Gx + g T x (B.1a)
x∈R 2
s.t. li ≤ xi ≤ ui i = 1, 2, ..., n (B.1b)
(bl )i ≤ aTi x ≤ (bu )i i = 1, 2, ..., m (B.1c)
where f is the objective function. The number of bounds is 2n and the number
of general constraints is 2m. This means, that we have upper and lower limits
on every bound and on every general constraint. The Matlab interface of the
QP solver is constructed as follows
It is possible to define equalities, by means the lower and upper limits are equal,
as li = ui and (bl )i = (bu )i respectively. If any of the limits are unbounded,
they must be defined as −∞ for lower limits and ∞ for upper limits. If the QP
solver is called with a starting point x, then the primal active set method is
called within the QP solver. The feasibility of the starting point is checked by
the QP solver before the primal active set method is called.
It must be mentioned, that both the QP and LP solver has some additional
input and output parameters. These parameters are e.g. used for tuning and
performance analysis of the solvers. For a further description of these parameters
we refer to the respective Matlab help files.
Appendix C
Implementation
The algorithms discussed in the thesis have been implemented in Matlab ver-
sion 7.3.0.298 (R2006b), August 03, 2006. In the following, we have listed
the implemented functions in sections.
The null space procedure solves an equality constrained QP by using the null
space of the constraint matrix, and is implemented in
null space.m .
The range space procedure solves the same problem by using the range space of
the constraint matrix, and is implemented in
range space.m .
120 Implementation
The active set methods have been integrated in a QP solver that sets up the
QP with a set of bounds and a set of general constraints. An interface has been
provided that offers different options to the user.
QP solver.m .
If the user want to use the primal active set method in the QP solver, a feasible
starting point must be calculated. This can be done by using the LP solver
LP solver.m .
SQP solver.m .
By updating the matrix factorizations, the efficiency of the null space procedure
and the range space procedure can be increased significantly. The following
C.4 Updating the Matrix Factorizations 121
implementations are used for updating the matrix factorizations used in the
null space procedure and the range space procedure. All the updates are based
on Givens rotations which are computed in
And for the null space procedure the matrix updates are implemented in
C.5 Demos
QP demo.m
Among other options the user can choose between the primal active set method
and the dual active set method.
Besides having the possibility of adjusting the valves, pumps and the set points
individually, the user can vary the size of the QP by the input N .
SQP demo.m .
add2mat.m .
line search algorithm.m .
Appendix D
Matlab-code
null space.m
1 function [ x , u ] = n u l l s p a c e (G, A, g , b )
2
3 % NULL SPACE s o l v e s t h e e q u a l i t y c o n s t r a i n e d c o n v e x QP:
4 % min 1/2 x ’ Gx+g ’ x (G i s r e q u i r e d t o be p o s t i v e s e m i
definite )
5 % s.t. A’ x = b (A i s r e q u i r e d t o hav e f u l l col um n
rank )
6 % w here t h e number o f v a r i a b l e s i s n and t h e number o f c o n s t r a i n t s i s m.
7 % The n u l l s p a c e o f t h e OP i s u s e d t o f i n d t h e s o l u t i o n .
8 %
9 % Call
10 % [ x , u ] = n u l l s p a c e (G, A, g , b )
11 %
12 % Input parameters
13 % G : i s t h e H e s s i a n m a t r i x ( nxn ) o f t h e QP.
14 % A : i s t h e c o n s t r a i n t m a t r i x ( nxm) : e v e r y col um n c o n t a i n s
a from t h e
15 % equality : a ’ x = b .
16 % g : i s t h e g r a d i e n t ( nx1 ) o f t h e QP .
17 % b : i s t h e r i g h t hand s i d e o f t h e c o n s t r a i n t s .
18 %
19 % Output p a r a m e t e r s
20 % x : the s o l u t i o n
21 % mu : the l ag r ang i an m u l t i p l i e r s
22 %
23 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
24 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
25 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
26 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
27 % D ate : 08. february 2007.
28
29 [ n ,m] = s i z e (A) ;
30 i f ( m˜=0 ) % f o r situations w here A i s empty
124 Matlab-code
31 [ Q, R ] = q r (A) ;
32 Q1 = Q ( : , 1 :m) ;
33 Q2 = Q ( : ,m+ 1:n ) ;
34 R = R ( 1 : m, : ) ;
35 py = R’ \ b ;
36 Q2t = Q2 ’ ;
37 g z = Q2t ∗ (G∗ (Q1∗py ) + g ) ;
38 Gz = Q2t ∗G∗Q2 ;
39 L = c h o l ( Gz ) ’ ;
40 pz = L\−g z ;
41 pz = L ’ \ pz ;
42 x = Q1∗py + Q2∗ pz ;
43 u = R\ (Q1 ’ ∗ (G∗x + g ) ) ;
44 else
45 x = −G\ g ;
46 u = [];
47 end
range space.m
1 f u n c t i o n [ x mu ] = r a n g e s p a c e ( L , A, g , b )
2 % RANGE SPACE s o l v e s t h e e q u a l i t y c o n s t r a i n e d c o n v e x QP:
3 % min 1/2 x ’ Gx+g ’ x (G i s r e q u i r e d t o be p o s t i v e
definite )
4 % s.t. A’ x = b (A i s r e q u i r e d t o hav e f u l l col um n
rank )
5 % w here t h e number o f v a r i a b l e s i s n and t h e number o f c o n s t r a i n t s i s m.
6 % The r a n g e s p a c e o f t h e OP i s u s e d t o f i n d t h e s o l u t i o n .
7 %
8 % Call
9 % [ x , u ] = r a n g e s p a c e ( L , A, g , b )
10 % Input parameters
11 % L : i s th e c h o l e s k y f a c t o r i z a t i o n o f th e H es s i an matrix (
nxn ) o f t h e QP.
12 % A : i s t h e c o n s t r a i n t m a t r i x ( nxm) : e v e r y col um n c o n t a i n s
a from t h e
13 % equality : a ’ x = b .
14 % g : i s t h e g r a d i e n t ( nx1 ) o f t h e QP .
15 % b : i s t h e r i g h t hand s i d e o f t h e c o n s t r a i n t s .
16 % Output p a r a m e t e r s
17 % x : the s o l u t i o n
18 % mu : the l a gr an gi an m u l t i p l i e r s
19
20 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
21 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
22 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
23 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
24 % D ate : 08. february 2007.
25 % Reference : −−−−−−−−−−−−−−−−−−−−−
26
27 Lt = L ’ ;
28 K = Lt \A ;
29 H = K’ ∗K ;
30 w = Lt \ g ;
31 z = b+K’ ∗w ;
32 M = c h o l (H) ;
33 mu = M’ \ z ;
34 mu = M\mu ;
35 y = K∗mu−w ;
36 x = L\y ;
D.2 Inequality Constrained QP’s 125
77
78
79 % i n i t i a l i z e options . . .
80 t o l = opts ( 1) ;
81 it max = opts (2 ) ;
82 method = o p t s ( 3 ) ;
83 % i n i t i a l i z e containers . . .
84 %t r a c e = ( n a r g o u t > 3 ) ;
85 perf = [ ] ;
86 i f trace
87 X = repm at ( z e r o s ( n , 1 ) , 1 , i t m a x ) ;
88 F = repm at ( 0 , 1 , i t m a x ) ;
89 Mu = repm at ( z , 1 , i t m a x ) ;
90 C = repm at ( z , 1 , i t m a x ) ;
91 W act = repm at ( z , 1 , i t m a x ) ;
92 W non = repm at ( z , 1 , i t m a x ) ;
93 end
94
95 % i n i t i a l i z e counters . . .
96 i t = 0;
97 Q = [ ] ; T = [ ] ; L = [ ] ; rem = []; % both for null space update and for
null space update FXFR
98
99 if method == 3 % n u l l space w i t h FXFR−u p d a t e
100 nb1 = n ∗ 2 ; % number o f bounds
101 else
102 nb1 = 0 ;
103 end
104
105 nab = 0 ; % number of active bouns
106 P = eye ( n ) ;
107
108 w act = [];
109 Q old = []; T old = []; L old = []; rem = [];
110
111 % iterate . . .
112 stop = 0 ;
113 w hi l e ˜ stop
114 it = i t + 1;
115 i f i t >= i t m a x
116 s t o p = 2 ; % maximum no iterations exceeded
117 end
118
119 % c a l l range / n u l l space procedure . . .
120 mu = z ;
121
122 if method == 1
123 [ p , mu ] = n u l l s p a c e (G, A ( : , w a c t ) ,G∗x+g , z e r o s ( l e n g t h ( w a c t ) , 1 ) ) ;
124 end
125
126 if method == 2
127 [ p , mu , Q, T , L ] = n u l l s p a c e u p d a t e (G, A ( : , w a c t ) ,G∗x+g , z e r o s ( l e n g t h ( w a c t
) , 1 ) ,Q, T , L , rem ) ;
128 end
129
130 if method == 3
131 Cr = G∗x+g ;
132 A = A ( : , w a c t ( nab + 1: end ) ) ;
133 % a j u s t C ( : , r ) t o make i t c o r r e s p o n d t o t h e f a c t o r i z a t i o n s o f
the
134 % F i x e d v a r i a b l e s ( w h e n e v e r −1 a p p e a r s a t v a r i a b l e i C ( : , r ) i
s houl d change s i g n )
135 i f nab % some bounds a r e i n t h e a c t i v e s e t
136 u i d x = f i n d ( w a c t > nb1 /2 & w a c t< nb1 +1) ;
137 v a r = n−nab+u i d x ;
138 Cr ( v a r ) = −Cr ( v a r ) ;
139 A ( v ar , : ) = −A ( v ar , : ) ;
140 end
141 [ p , mu , Q, T , L ] = n u l l s p a c e u p d a t e F R F X (Q, T , L , G, A , Cr , z e r o s ( l e n g t h ( w a c t )
, 1 ) , nab , rem−nab ) ;
142 end
143
144 mu( w a c t ) = mu ;
145
146 if norm ( p ) < t o l
147 i f mu > − t o l
148 s top = 1 ; % s o l u t i o n found
149 else
150 % com pute i n d e x j o f bound / c o n s t r a i n t t o be rem ov ed . . .
151 [ dummy , rem ] = min ( mu ) ;
152 [ w a c t w non A P x nab G g ] = r e m o v e c o n s t r a i n t ( rem , A , w a c t , w non , x
, P , nb1 , nab , n , G, g , pbc , b ) ;
153 end
154 else
155 % com pute s t e p l e n g t h and i n d e x j o f bound / c o n s t r a i n t t o be appended . . .
156 [ a l p h a , j ] = s t e p l e n g t h (A, b , x , p , w non , nb , n , t o l ) ;
157 i f alpha < 1
D.2 Inequality Constrained QP’s 127
158 % make c o n s t r a i n e d s t e p . . .
159 x = x + a l p h a ∗p ;
160 [ w a c t w non A P x nab G g Q] = a p p e n d c o n s t r a i n t ( j , A , w a c t , w non , x
, P , nb1 , nab , n , G, g , Q, pbc , b ) ; % r i s i n d e x o f A
161 else
162 % make f u l l step . . .
163 x = x + p;
164 end
165 end
166 % c o l l e c t i n g output i n c o n t a i n e r s . . .
167 i f trace
168 i f nb1 % method 3 i s u s e d
169 X ( : , i t ) = P’ ∗ x ;
170 else
171 X( : , i t ) = x ;
172 end
173 F ( i t ) = o b j e c t i v e (G, g , x ) ;
174 Mu ( : , i t ) = mu ;
175 C ( : , i t ) = c o n s t r a i n t s (A, b , x ) ;
176 W act ( w a c t , i t ) = w a c t ;
177 W non ( w non , i t ) = w non ;
178 end
179 end
180
181 if nb1 % method 3 is used
182 x = P’ ∗ x ;
183 end
184
185 % building info . . .
186 i n f o = [ o b j e c t i v e (G, g , x ) it stop ] ;
187
188 % building perf . . .
189 i f trace
190 X = X ( : , 1 : i t ) ; X = [ x0 X ] ;
191 F = F(1: i t ) ; F = [ f0 F ] ;
192 Mu = Mu ( : , 1 : i t ) ; Mu = [ mu0 Mu ] ;
193 C = C ( : , 1 : i t ) ; C = [ c0 C ] ;
194 W act = W act ( : , 1 : i t ) ; W act = [ w a c t 0 W act ] ;
195 W non = W non ( : , 1 : i t ) ; W non = [ w non0 W non ] ;
196 p e r f = s t r u c t ( ’ x ’ , {X} , ’ f ’ , { F} , ’mu ’ , {Mu} , ’ c ’ , {C} , ’Wa ’ , { W act } , ’ Wi ’ , { W non } ) ;
197 end
198
199 f u n c t i o n [ a l p h a , j ] = s t e p l e n g t h (A , b , x , p , w non , nb , n , t o l )
200 alpha = 1 ; j = [ ] ;
201 f o r app = w non
202 i f app > nb
203 fv = 1 : 1 : n ; % general constraint
204 else
205 f v = mod ( app −1 ,n ) + 1; % i n d e x o f f i x e d v a r i a b e l
206 end
207 ap = A( f v , app ) ’ ∗ p ( f v ) ;
208 i f ap < − t o l
209 temp = ( b ( app ) − A( f v , app ) ’ ∗ x ( f v ) ) / ap ;
210 i f − t o l < temp & temp < a l p h a
211 a l p h a = temp ; % s m a l l e s t s t e p l e n g t h
212 j = app ; % i n d e x j o f bound t o be appended
213 end
214 end
215 end
216
217 % f u n c t i o n [ w a c t , w non ] = a p p e n d c o n s t r a i n t ( b , w a c t , w non , j , pbc )
218 % w a c t = [ w a c t j ] ; % append c o n s t r a i n t j t o a c t i v e s e t
219 % w non = w non ( f i n d ( w non ˜= j ) ) ; % rem ov e c o n s t r a i n t j from n o n a c t i v e s e t
220 % i f ˜ i s i n f ( b ( pbc ( j ) ) )
221 % w non = w non ( f i n d ( w non ˜= pbc ( j ) ) ) ; % rem ov e c o n s t r a i n t pbc ( j ) from
n o n a c t i v e s e t , i f n o t unbounded
222 % end
223
224 % f u n c t i o n [ w a c t , w non ] = r e m o v e c o n s t r a i n t ( b , w a c t , w non , j , pbc )
225 % w a c t = w a c t ( f i n d ( w a c t ˜= j ) ) ; % rem ov e c o n s t r a i n t j from a c t i v e s e t
226 % w non = [ w non j ] ; % append c o n s t r a i n t j t o n o n a c t i v e s e t
227 % i f ˜ i s i n f ( b ( pbc ( j ) ) )
228 % w non = [ w non pbc ( j ) ] ; % append c o n s t r a i n t pbc ( j ) t o n o n a c t i v e s e t , if
n o t unbounded
229 % end
230
231 f u n c t i o n [ w a c t w non C P x nab G g ] = r e m o v e c o n s t r a i n t ( wi , C , w a c t , w non , x , P ,
nb , nab , n , G, g , pbc , b ) % w i i s i n d e x o f w a c t
232 j = w a c t ( wi ) ;
233
234 if j < nb+1 % j is a bound and we hav e to
r e o r g a n i z e the v a r i a b l e s
235 v a r 1 = n−nab + 1;
236 v a r 2 = n−nab+w i ;
237
238 temp = C( v ar1 , : ) ;
239 C( v ar1 , : ) = C( v ar2 , : ) ;
128 Matlab-code
76 perf = [ ] ;
77 i f trace
78 X = repm at ( z e r o s ( n , 1 ) , 1 , i t m a x ) ;
79 F = repm at ( 0 , 1 , i t m a x ) ;
80 Mu = repm at ( z , 1 , i t m a x ) ;
81 Con = repm at ( z , 1 , i t m a x ) ;
82 W act = repm at ( z , 1 , i t m a x ) ;
83 W non = repm at ( z , 1 , i t m a x ) ;
84 end
85
86 if method == 3 % n u l l s p a c e w i t h FXFR−u p d a t e
87 nb = n ∗ 2 ; % number o f bounds
88 else
89 nb = 0 ;
90 end
91 nab = 0 ; % number of active bouns
92 P = eye ( n ) ;
93
94 x = −G\ g ;
95 mu = z e r o s (m, 1 ) ;
96 w act = [ ] ;
97
98 x0 = x ;
99 f 0 = o b j e c t i v e (G, C , g , b , x , mu) ;
100 mu0 = mu ;
101 c o n 0 = c o n s t r a i n t s (G, C ( : , w non ) , g , b ( w non ) , x , mu) ;
102 w act0 = z ;
103 w non0 = ( 1 : 1 :m) ’ ;
104
105 Q = [ ] ; T = [ ] ; L = [ ] ; R = [ ] ; rem = [ ] ; % both f o r r a n g e− and
n u l l s p a c e u p d a t e and f o r n u l l s p a c e u p d a t e F X F R
106 if method == 4 | | method == 5 % r a n g e s p a c e o r r a n g e s p a c e update
107 c h o l G = c h o l (G) ;
108 end
109 i t t o t = 0;
110 i t = 0;
111 max itr = it max ;
112 stop = 0 ;
113
114 w hi l e ˜ stop
115
116 c = c o n s t r a i n t s (G, C ( : , w non ) , g , b ( w non ) , x , mu) ;
117 i f c >= − t o l ;%−1e−12%−s q r t ( e p s ) % a l l e l e m e n t s must be >= 0
118 % d i s p ( [ ’ / / / / / / i t r : ’ , i n t 2 s t r ( i t +1) , ’ / / / / / / / / / / / / / / ’ ] )
119 % d i s p ( ’ STOP : a l l i n a c t i v e c o n s t r a i n t s >= 0 ’ )
120 stop = 1 ;
121 else
122 % we f i n d t h e most n e g a t i v e v a l u e o f c
123 [ c r , r ] = min ( c ) ;
124 r = w non ( r ) ;
125 end
126
127 it = i t + 1;
128 i f i t >= m a x i t r % no c o n v e r g e n c e
129 d i s p ( [ ’ / / / / / / i t r : ’ , i n t 2 s t r ( i t +1) , ’ / / / / / / / / / / / / / / ’ ] )
130 d i s p ( ’STOP : i t >= m a x i t r ( o u t e r w h i l e l o o p ) ’ )
131 stop = 2;
132 end
133
134 it2 = 0;
135 s t o p 2 = max ( 0 , s t o p ) ;
136 w h i l e ˜ s t o p 2 %c r < −s q r t ( e p s )
137 it2 = it2 + 1;
138
139 i f method == 1
140 [ p , v ] = n u l l s p a c e (G, C ( : , w a c t ) ,−C ( : , r ) ,− z e r o s ( l e n g t h ( w a c t ) , 1 ) ) ;
141 end
142 i f method == 2
143 [ p , v , Q, T , L ] = n u l l s p a c e u p d a t e (G, C ( : , w a c t ) ,−C ( : , r ) , z e r o s ( l e n g t h (
w a c t ) , 1 ) ,Q, T , L , rem ) ;
144 end
145 i f method == 3
146 Cr = C ( : , r ) ;
147 A = C ( : , w a c t ( nab + 1: end ) ) ;
148 % a j u s t C ( : , r ) t o make i t c o r r e s p o n d t o t h e f a c t o r i z a t i o n s
of the
149 % F i x e d v a r i a b l e s ( w h e n e v e r −1 a p p e a r s a t v a r i a b l e i C ( : , r ) i
s houl d change s i g n )
150 i f nab % some bounds a r e i n t h e a c t i v e s e t
151 u i d x = f i n d ( w a c t > nb /2 & w a c t< nb+1) ;
152 v a r = n−nab+u i d x ;
153 Cr ( v a r ) = −Cr ( v a r ) ;
154 A ( v ar , : ) = −A ( v ar , : ) ;
155 end
156 [ p , v , Q, T , L ] = n u l l s p a c e u p d a t e F R F X (Q, T , L , G, A ,−Cr , z e r o s ( l e n g t h (
w a c t ) , 1 ) , nab , rem−nab ) ;
157 end
D.2 Inequality Constrained QP’s 131
158 if method == 4
159 [ p , v ] = r a n g e s p a c e ( c h o l G , C ( : , w a c t ) ,−C ( : , r ) ,− z e r o s ( l e n g t h ( w a c t )
,1) ) ;
160 end
161 i f method == 5
162 [ p , v , Q, R ] = r a n g e s p a c e u p d a t e ( c h o l G , C ( : , w a c t ) ,−C ( : , r ) , z e r o s (
l e n g t h ( w a c t ) , 1 ) ,Q, R, rem ) ;
163 end
164
165 if isempty ( v )
166 v = [];
167 end
168
169 arp = C ( : , r ) ’∗p ;
170 i f a b s ( a r p ) <= t o l % l i n e a r d e p e n d e n c y
171 i f v >= 0 % s o l u t i o n d o e s n o t e x i s t
172 d i s p ( [ ’ / / / / / / i t r : ’ , i n t 2 s t r ( i t +1) , ’ / / / / / / / / / / / / / / ’ ] )
173 d i s p ( ’STOP : v >= 0 , PROBLEM I S INFEASIBLE ! ! ! ’ )
174 stop = 3 ;
175 stop2 = stop ;
176 else
177 t = inf ;
178 f or k = 1: length (v)
179 i f v(k) < 0
180 temp = −mu( w a c t ( k ) ) / v ( k ) ;
181 i f temp < t
182 t = temp ;
183 rem = k ;
184 end
185 end
186 end
187 mu( w a c t ) = mu( w a c t ) + t ∗v ;
188 mu( r ) = mu( r ) + t ;
189 % rem ov e l i n e a r d e p e n d e n t c o n s t r a i n t from A
190 [ w a c t w non C P x nab G g ] = r e m o v e c o n s t r a i n t ( rem , C , w act ,
w non , x , P , nb , nab , n , G, g , pbc , b ) ; % rem i s i n d e x o f w act
191 end
192 else
193 % s t e p s i z e in dual space
194 t1 = i n f ;
195 for k = 1: length (v)
196 i f v(k) < 0
197 temp = −mu( w a c t ( k ) ) / v ( k ) ;
198 i f temp < t 1
199 t 1 = temp ;
200 rem = k ;
201 end
202 end
203 end
204 % s t e p s i z e i n primal space
205 t 2 = −c o n s t r a i n t s (G, C ( : , r ) , g , b ( r ) , x , mu) / a r p ;
206 i f t 2 <= t 1
207 x = x + t 2 ∗p ;
208 mu( w a c t ) = mu( w a c t ) + t 2 ∗v ;
209 mu( r ) = mu( r ) + t 2 ;
210 % append c o n s t r a i n t t o a c t i v e s e t
211 [ w a c t w non C P x nab G g Q ] = a p p e n d c o n s t r a i n t ( r , C , w act ,
w non , x , P , nb , nab , n , G, g , Q, pbc , b ) ; % r i s i n d e x o f C
212 else
213 x = x + t 1 ∗p ;
214 mu( w a c t ) = mu( w a c t ) + t 1 ∗v ;
215 mu( r ) = mu( r ) + t 1 ;
216 % rem ov e c o n s t r a i n t from a c t i v e s e t
217 [ w a c t w non C P x nab G g ] = r e m o v e c o n s t r a i n t ( rem , C , w act ,
w non , x , P , nb , nab , n , G, g , pbc , b ) ; % rem i s i n d e x o f w act
218 end
219 end
220 c r = c o n s t r a i n t s (G, C ( : , r ) , g , b ( r ) , x , mu) ;
221 i f c r > −t o l
222 s t o p 2 = 1 ; % l e a v e t h e i n n e r w h i l e−l o o p but d o e s n t s t o p t h e
algorithm
223 end
224
225 if i t 2 >= m a x i t r % no c o n v e r g e n c e ( t e r m i n a t e t h e a l g o r i t h m )
226 d i s p ( [ ’ / / / / / / i t r : ’ , i n t 2 s t r ( i t +1) , ’ / / / / / / / / / / / / / / ’ ] )
227 d i s p ( ’STOP : i t >= m a x i t r ( i n n e r w h i l e l o o p ) ’ )
228 stop = 2 ;
229 stop2 = stop ;
230 end
231
232 % c o l l e c t i n g output i n c o n t a i n e r s . . .
233 i f trace
234 i f nb % method 3 i s u s e d
235 X( : , i t ) = P’ ∗ x ;
236 else
237 X( : , i t ) = x ;
238 end
132 Matlab-code
QP solver.m
1 function [ x , i n f o , p e r f ] = Q P s o l v e r (H , g , l , u , A , b l , bu , x , o p t s )
2
3 % QP SOLVER S o l v i n g an i n e q u a l i t y c o n s t r a i n e d QP o f t h e form :
4 % min f ( x ) = 0 . 5 ∗ x ’ ∗H∗x + g ∗x
5 % s.t. l <= x <= u
6 % b l <= A∗x <= bu ,
7 % u s i n g t h e p r i m a l a c t i v e s e t method o r t h e d u a l a c t i v e s e t method . The
8 % a c t i v e s e t m ethods u s e s t h e r a n g e s p a c e p r o c e d u r e o r t h e n u l l s p a c e
9 % p r o c e d u r e t o s o l v e t h e KKT s y s t e m . Both t h e r a n g e s p a c e and t h e n u l l
10 % s p ace p r o c e d u r e s has been p rov i d ed with f a c t o r i z a t i o n u p dates . E q u a l i t y
11 % c o n s t r a i n t s a r e d e f i n e d a s l = u and b l = bu r e s p e c t i v e l y .
12 %
13 % Call
14 % x = Q P s o l v e r (H, g , l , u , A, bl , bu )
15 % x = Q P s o l v e r (H, g , l , u , A, bl , bu , x , opts )
16 % [ x , info , p e r f ] = QP solver ( ... )
17 %
18 % Input parameters
19 % H : The H e s s i a n m a t r i x o f t h e o b j e c t i v e f u n c t i o n .
20 % g : The l i n e a r term o f t h e o b j e c t i v e f u n c t i o n .
21 % l : Lower l i m i t s o f bounds . S e t a s I n f , i f unbounded .
22 % u : Upper l i m i t s o f bounds . S e t a s −I n f , i f unbounded .
23 % A : The c o n s t r a i n t m a t r i x h o l d i n g t h e g e n e r a l c o n s t r a i n t s a s row s .
24 % bl : Lower l i m i t s o f g e n e r a l c o n s t r a i n t s . S e t a s I n f , i f unbounded .
25 % bu : Upper l i m i t s o f g e n e r a l c o n s t r a i n t s . S e t a s −I n f , i f unbounded .
26 % x : S t a r t i n g p o i n t . I f x i s n o t g i v e n o r empty , t h e n t h e d u a l a c t i v e
27 % s e t method i s us ed , o t h e r w i s e t h e p r i m a l a c t i v e s e t method i s
28 % used .
29 % op ts : V ector with 3 el em en ts :
30 % o p t s ( 1 ) = T o l e r a n c e u s e d t o s t a b i l i z e t h e m ethods n u m e r i c a l l y .
31 % I f | v a l u e | <= o p t s ( 1 ) , t h e n v a l u e i s r e g a r d e d a s z e r o .
32 % o p t s ( 2 ) = maximum no . o f i t e r a t i o n s t e p s .
33 % P r i m a l a c t i v e s e t method :
34 % opts ( 3 ) = 1 : Using n u l l space procedure .
35 % 2 : Using n u l l s p ace p roced u re with f a c t o r i z a t i o n
36 % update .
37 % 3 : Using n u l l s p ace p roced u re with f a c t o r i z a t i o n
134 Matlab-code
38 % u p d a t e b a s e d on f i x e d and f r e e v a r i a b l e s .
39 % I f op ts ( 3 ) > 3 , then op ts ( 3 ) i s s e t to 3 a u t o m a t i c a l l y .
40 % D ual a c t i v e s e t method :
41 % opts ( 3 ) = 1 : Using n u l l space procedure .
42 % 2 : Using n u l l s p ace p roced u re with f a c t o r i z a t i o n
43 % update .
44 % 3 : Using n u l l s p ace p roced u re with f a c t o r i z a t i o n
45 % u p d a t e b a s e d on f i x e d and f r e e v a r i a b l e s .
46 % 4 : Using range space procedure .
47 % 5 : Using ran ge s p ace p roced u re with f a c t o r i z a t i o n
48 % update .
49 % I f o p t s i s n o t g i v e n o r empty , t h e d e f a u l t o p t s = [ 1 e−8 1 0 0 0 3 ] .
50 %
51 % Output p a r a m e t e r s
52 % x : The o p t i m a l s o l u t i o n .
53 % i n f o : Performace i n form ati on , v e c t o r with 3 el em en ts :
54 % inf o (1) = f i n a l v al ue s o f the o b j e c t i v e f un ct i on .
55 % inf o (2) = no . o f i t e r a t i o n s t e p s .
56 % P r i m a l a c t i v e s e t method :
57 % inf o (3) = 1 : F e a s i b l e s o l u t i o n found .
58 % 2 : No . o f i t e r a t i o n s t e p s e x c e e d e d .
59 % D ual a c t i v e s e t method :
60 % inf o (3) = 1 : F e a s i b l e s o l u t i o n found .
61 % 2 : No . o f i t e r a t i o n s t e p s e x c e e d e d .
62 % 3 : Probl em i s i n f e a s i b l e .
63 % p e r f : Performace , s t r u c t h o l d i n g :
64 % perf . x : V a l u e s o f x , s i z e i s nx ( i t +1) .
65 % perf . f : V a l u e s o f t h e o b j e c t i v e f u n c t i o n , s i z e i s 1x ( i t +1) .
66 % p e r f . mu : V a l u e s o f mu, s i z e i s nx ( i t +1) .
67 % perf . c : V a l u e s o f c ( x ) , s i z e i s ( n+n+m+m) x ( i t +1) .
68 % p e r f .Wa : A c t i v e s e t , s i z e i s ( n+n+m+m) x ( i t +1) .
69 % p e r f . Wi : I n a c t i v e s e t , s i z e i s ( n+n+m+m) x ( i t +1) .
70 % S i z e ( n+n+m+m) x ( i t +1) i s r e f e r i n g t o i n d i c e s i l = 1 : n , i u = ( n+1) : 2
71 % n , i b l = ( 2 n+1) : ( 2 n+m) and i b u = ( 2 n+m+1) : ( 2 n+2m) .
72 %
73 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 .
74 % Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
75 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n .
76 % M. Sc . , IMM, DTU, DK−2800 Lyngby .
77 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r .
78 % Per Grove Thomsen , P r o f e s s o r .
79 % D ate : 0 7 . J une 2 0 0 7 .
80
81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % Tune i n p u t and g a t h e r i n f o r m a t i o n %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % Tune . . .
85 l = l (:) ; u = u(:) ;
86 b l = b l ( : ) ; bu = bu ( : ) ;
87 g = g(:) ;
88 % Gather . . .
89 [ m, n ] = s i z e (A) ;
90
91 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % Set o ption s %
93 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 i f n argi n < 9 | isempty ( op ts )
95 t o l = 1 e −8;
96 it max = 1000;
97 method = 3 ;
98 o p t s = [ t o l i t m a x method ] ;
99 else
100 opts = opts ( : ) ’ ;
101 end
102 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103 % Check n a r g i n / n a r g o u t %
104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105 e r r o r ( nargchk ( 7 , 9 , n argi n ) )
106 e r r o r ( nargoutchk ( 1 , 3 , nargout ) )
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % Check i n p u t / o u t p u t %
109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % Check H . . .
111 s i z e H = s i z e (H) ;
112 i f s i z e H ( 1 ) ˜= n | s i z e H ( 2 ) ˜= n
113 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o H must be o f s i z e ’ ,
i n t 2 s t r (n) , ’x ’ , in t 2 s t r (n) , ’ . ’ ] )
114 end
115 Hdiff = H − H’ ;
116 i f norm ( H d i f f ( : ) , i n f ) > e p s ∗norm (H ( : ) , i n f ) % r e l a t i v e c h e c k o f b i g g e s t a b s o l u t e
value in Hd if f
117 e r r o r ( ’H must be s y m m e t r i c . ’ )
118 end
119 [ dummy , p ] = c h o l (H) ;
120 if p
121 e r r o r ( ’H must be p o s i t i v e d e f i n i t e . ’ )
122 end
D.2 Inequality Constrained QP’s 135
123 % Check g . . .
124 sizeg = size (g) ;
125 i f s i z e g ( 1 ) ˜= n | s i z e g ( 2 ) ˜= 1
126 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o g must be a v e c t o r
o f ’ , i n t 2 s t r (n ) , ’ elements . ’ ] )
127 end
128 % Check l and u . . .
129 %l ( 4 0 , 1 ) = i n f ; % ? ? ?
130 sizel = size ( l ) ;
131 i f s i z e l ( 1 ) ˜= n | s i z e l ( 2 ) ˜= 1
132 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o l must be a v e c t o r
o f ’ , i n t 2 s t r (n ) , ’ elements . ’ ] )
133 end
134 siz eu = s i z e (u) ;
135 i f s i z e u ( 1 ) ˜= n | s i z e u ( 2 ) ˜= 1
136 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o u must be a v e c t o r
o f ’ , i n t 2 s t r (n ) , ’ elements . ’ ] )
137 end
138 for i = 1:n
139 i f l ( i ,1) > u( i ,1)
140 e r r o r ( [ ’ l ( ’ , i n t 2 s t r ( i ) , ’ ) must be s m a l l e r t h a n o r e q u a l t o u ( ’ , i n t 2 s t r (
i ) , ’ ) . ’ ])
141 end
142 end
143 % Check b l and bu . . .
144 s i z e bl = s iz e ( bl ) ;
145 i f s i z e b l ( 1 ) ˜= n | s i z e b l ( 2 ) ˜= 1
146 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o b l must be a v e c t o r
o f ’ , i n t 2 s t r (m) , ’ e l e m e n t s . ’ ] )
147 end
148 s i z e b u = s i z e ( bu ) ;
149 i f s i z e b u ( 1 ) ˜= n | s i z e b u ( 2 ) ˜= 1
150 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o bu must be a v e c t o r
o f ’ , i n t 2 s t r (m) , ’ e l e m e n t s . ’ ] )
151 end
152 f o r i = 1 :m
153 i f b l ( i ) > bu ( i )
154 e r r o r ( [ ’ b l ( ’ , i n t 2 s t r ( i ) , ’ ) must be s m a l l e r t h a n o r e q u a l t o bu ( ’ ,
int2str ( i ) , ’ ) . ’ ])
155 end
156 end
157 % Check x . . .
158 i f n argi n > 7 & ˜ isempty ( x )
159 %o p t s ( 1 ) = 1 e −20; % ? ? ?
160 feasible = 1;
161 sizex = size (x) ;
162 i f s i z e x ( 1 ) ˜= n | s i z e x ( 2 ) ˜= 1
163 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o x must be a
v ec to r o f ’ , i n t 2 s t r (n ) , ’ elements . ’ ] )
164 end
165 i l = f i n d ( x − l < −o p t s ( 1 ) ) ; i u = f i n d ( x − u > o p t s ( 1 ) ) ;
166 i b l = f i n d (A∗x − b l < −o p t s ( 1 ) ) ; i b u = f i n d (A∗x − bu > o p t s ( 1 ) ) ;
167 i f ˜ isempty ( i l )
168 d i s p ( [ ’ F o l l o w i n g bound ( s ) v i o l a t e d , b e c a u s e x − l < ’ , num 2s tr (− o p t s ( 1 ) )
, ’ : ’ ])
169 f p r i n t f ( [ ’ \b ’ , i n t 2 s t r ( i l ’ ) , ’ . \ n ’ ] )
170 feas ible = 0;
171 end
172 i f ˜ isempty ( i u )
173 d i s p ( [ ’ F o l l o w i n g bound ( s ) v i o l a t e d , b e c a u s e x − u > ’ , num 2s tr ( o p t s ( 1 ) ) ,
’ : ’ ])
174 f p r i n t f ( [ ’ \b ’ , i n t 2 s t r ( i u ’ ) , ’ . \ n ’ ] )
175 feas ible = 0;
176 end
177 i f ˜ isempty ( i b l )
178 d i s p ( [ ’ F o l l o w i n g g e n e r a l c o n s t r a i n t ( s ) v i o l a t e d , b e c a u s e A∗x − b l < ’ ,
num 2s tr (− o p t s ( 1 ) ) , ’ : ’ ] )
179 f p r i n t f ( [ ’ \b ’ , i n t 2 s t r ( i b l ’ ) , ’ . \ n ’ ] )
180 feas ible = 0;
181 end
182 i f ˜ isempty ( i b u )
183 d i s p ( [ ’ F o l l o w i n g g e n e r a l c o n s t r a i n t ( s ) v i o l a t e d , b e c a u s e A∗x − bu > ’ ,
num 2s tr ( o p t s ( 1 ) ) , ’ : ’ ] )
184 f p r i n t f ( [ ’ \b ’ , i n t 2 s t r ( i b u ’ ) , ’ . \ n ’ ] )
185 feas ible = 0;
186 end
187 if ˜feasible
188 e r r o r ( ’ S t a r t i n g p o i n t f o r p r i m a l a c t i v e s e t method i s n o t f e a s i b l e . ’ )
189 end
190 end
191 % Check o p t s . . .
192 i f l e n g t h ( o p t s ) ˜= 3
193 e r r o r ( ’ O p t i o n s must be a v e c t o r o f 3 e l e m e n t s . ’ )
194 end
195 i = 1;
196 i f ˜ i s r e a l ( opts ( i ) ) | i s i n f ( opts ( i ) ) | i s nan ( opts ( i ) ) | opts ( i ) < 0
197 e r r o r ( ’ o p t s ( 1 ) must be p o s i t i v e . ’ )
136 Matlab-code
198 end
199 i = 2;
200 i f ˜ i s r e a l ( opts ( i ) ) | i s i n f ( opts ( i ) ) | i s nan ( opts ( i ) ) | opts ( i ) < 0 | mod ( o p t s (
i ) ,1)
201 e r r o r ( ’ o p t s ( 2 ) must be a positive integer . ’ )
202 end
203 i = 3;
204 i f ˜ i s r e a l ( opts ( i ) ) | i s i n f ( opts ( i ) ) | i s nan ( opts ( i ) ) | opts ( i ) < 1 | 5 < opts (
i ) | mod ( o p t s ( i ) , 1 )
205 e r r o r ( ’ o p t s ( 3 ) must be an integer in range 1 <= v a l u e <= 5 . ’ )
206 end
207
208 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 % Initialize %
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
211 I = eye ( n ) ;
212 At = A ’ ;
213
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 % O r g a n i z e bounds and c o n s t r a i n t s %
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 % C o n v e r t i n p u t s t r u c t u r e l <= I ∗x <= u and b l <= A∗x <= bu t o C∗x >= b , w here
C = [ I −I A −A ] = [ B A ] (A = [ A −A ] ) and b = [ l −u b l −bu ] . . .
218 B = [ I −I ] ; % l <= I ∗x <= u −−> I ∗x >= l & −I ∗x >= −u
219 A = [ At −At ] ; % b l <= A∗x <= bu −−> A∗x >= b l & −A∗x >= −bu
220 C = [B A] ;
221 b = [ l ; −u ; b l ; −bu ] ;
222
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % B u i l d i n a c t i v e s e t and c o r r e s p o n d i n g c o n s t r a i n t s %
225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 % I n i t i a l i z e inactive set . . .
227 w non = 1 : 1 : 2 ∗ ( n+m) ;
228 % Remove unbounded c o n s t r a i n t s from i n a c t i v e s e t . . .
229 w non = w non ( f i n d ( ˜ i s i n f ( b ) ) ) ;
230 % Ind ic es of corresponding c o nst ra int s . . .
231 c c = [ ( n+1) : 1 : ( 2 ∗ n ) 1 : 1 : n ( 2 ∗ n+m+1) : 1 : 2 ∗ ( n+m) 2∗ n + 1 : 1 : 2 ∗ n+m ] ; % w non = [ i l
i u i b l i b u ] −> c c = [ i u i l i b u i b l ]
232
233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
234 % Startup i n f o %
235 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % Disregarded c o n s t r a i n t s . . .
237 %l ( 4 0 , 1 ) = − i n f ; % ? ? ?
238 i l = find ( i s i n f ( l ) ) ; i u = find ( i s i n f (u) ) ;
239 i b l = f i n d ( i s i n f ( b l ) ) ; i b u = f i n d ( i s i n f ( bu ) ) ;
240 i f ˜ isempty ( i l )
241 d i s p ( ’ F o l l o w i n g c o n s t r a i n t ( s ) d i s r e g a r d e d , b e c a u s e l i s unbounded : ’ )
242 disp ( [ ’ i = [ ’ , int 2s tr ( i l ’ ) , ’ ] ’ ] )
243 end
244 i f ˜ isempty ( i u )
245 d i s p ( ’ F o l l o w i n g c o n s t r a i n t ( s ) d i s r e g a r d e d , b e c a u s e u i s unbounded : ’ )
246 disp ( [ ’ i = [ ’ , int 2s tr ( i u ’ ) , ’ ] ’ ] )
247 end
248 i f ˜ isempty ( i b l )
249 d i s p ( ’ F o l l o w i n g c o n s t r a i n t ( s ) d i s r e g a r d e d , b e c a u s e b l i s unbounded : ’ )
250 disp ( [ ’ i = [ ’ , int 2s tr ( i bl ’ ) , ’ ] ’ ] )
251 end
252 i f ˜ isempty ( i b u )
253 d i s p ( ’ F o l l o w i n g c o n s t r a i n t ( s ) d i s r e g a r d e d , b e c a u s e bu i s unbounded : ’ )
254 disp ( [ ’ i = [ ’ , i n t 2 s t r ( i bu ’ ) , ’ ] ’ ] )
255 end
256
257 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % C a l l p r i m a l a c t i v e s e t o r d u a l a c t i v e s e t method %
259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 t r ac e = ( nargout > 2) ; % b ui l d i n g p e r f
261 i f n argi n < 8 | isempty ( x )
262 d i s p ( ’ C a l l i n g d u a l a c t i v e s e t method . ’ )
263 [ x , mu, i n f o , p e r f ] = d u a l a c t i v e s e t m e t h o d (H , g , C , b , w non , cc , o p t s , t r a c e ) ;
264 else
265 disp ( ’ Starting point i s f e a s i b l e . ’ )
266 d i s p ( ’ C a l l i n g p r i m a l a c t i v e s e t method . ’ )
267 i f opts (3 ) > 3
268 opts (3 ) = 3;
269 end
270 [ x , mu, i n f o , p e r f ] = p r i m a l a c t i v e s e t m e t h o d (H , g , C , b , x , w non , cc , o p t s , t r a c e ) ;
271 end
272 % Display i n f o . . .
273 i f i n f o ( 1 ) == 2
274 d i s p ( ’ No s o l u t i o n found , maximum number o f i t e r a t i o n s t e p s e x c e e d e d . ’ )
275 end
276 i f i n f o ( 1 ) == 3
277 d i s p ( ’ No s o l u t i o n found , p r o b l e m i s u n f e a s i b l e . ’ )
278 end
279 d i s p ( ’ QPsolver term i nated . ’ )
D.2 Inequality Constrained QP’s 137
LP solver.m
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % Check i n p u t %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
87 % Check l and u . . .
88 %l ( 4 0 , 1 ) = i n f ; % ? ? ?
89 si zel = size ( l ) ;
90 i f s i z e l ( 1 ) ˜= n | s i z e l ( 2 ) ˜= 1
91 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o l must be a v e c t o r
of ’ , i n t 2 s t r (n ) , ’ elements . ’ ] )
92 end
93 siz eu = s i z e (u) ;
94 i f s i z e u ( 1 ) ˜= n | s i z e u ( 2 ) ˜= 1
95 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o u must be a v e c t o r
of ’ , i n t 2 s t r (n ) , ’ elements . ’ ] )
96 end
97 for i = 1: n
98 i f l ( i ,1) > u( i ,1)
99 e r r o r ( [ ’ l ( ’ , i n t 2 s t r ( i ) , ’ ) must be s m a l l e r t h a n o r e q u a l t o u ( ’ , i n t 2 s t r (
i ) , ’ ) . ’ ])
100 end
101 end
102 % Check b l and bu . . .
103 s i z e bl = s i ze ( bl ) ;
104 i f s i z e b l ( 1 ) ˜= n | s i z e b l ( 2 ) ˜= 1
105 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o b l must be a v e c t o r
o f ’ , i n t 2 s t r (m) , ’ e l e m e n t s . ’ ] )
106 end
107 s i z e b u = s i z e ( bu ) ;
108 i f s i z e b u ( 1 ) ˜= n | s i z e b u ( 2 ) ˜= 1
109 e r r o r ( [ ’ S i z e o f A i s ’ , i n t 2 s t r (m) , ’ x ’ , i n t 2 s t r ( n ) , ’ , s o bu must be a v e c t o r
o f ’ , i n t 2 s t r (m) , ’ e l e m e n t s . ’ ] )
110 end
111 f o r i = 1 :m
112 i f b l ( i ) > bu ( i )
113 e r r o r ( [ ’ b l ( ’ , i n t 2 s t r ( i ) , ’ ) must be s m a l l e r t h a n o r e q u a l t o bu ( ’ ,
int2str ( i ) , ’ ) . ’ ])
114 end
115 end
116
117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 % I n i t i a l i z e input f o r l in p r o g %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % R e p l a c e +/− i n f w i t h p s e u d o i n f ( l i n p r o g r e q u i r e m e n t ) . . .
121 i f p s e u d o i n f ˜= i n f
122 l ( f i n d ( l == − i n f ) ) = −p s e u d o i n f ;
123 u ( f i n d ( u == i n f ) ) = p s e u d o i n f ;
124 b l ( f i n d ( b l == − i n f ) ) = −p s e u d o i n f ;
125 bu ( f i n d ( bu == i n f ) ) = p s e u d o i n f ;
126 end
127 % O b j e c t i v e f u n c t i o n f d e f i n e d a s a v e c t o r −> l i n p r o g i s u s i n g i n f −norm . . .
128 f = ones (n , 1 ) ;
129 % I n i t i a l i z e constraints . . .
130 if 1
131 % Fi nd i n d i c e s o f e q u a l i t y and i n e q u a l i t y c o n s t r a i n t s . . .
132 i n = 1 : 1 :m; % i n d i c e s o f a l l c o n s t r a i n t s
133 eq = f i n d ( a b s ( bu − b l ) <= e q u a l i t y t o l ) ’ ; % i n d i c e s o f e q u a l i t y c o n s t r a i n t s
134 f o r i = eq
135 i n = i n ( f i n d ( i n ˜= i ) ) ; % rem ov e i n d i c e s o f e q u a l i t y c o n s t r a i n t s
136 end
137 % S p l i t c o n s t r a i n t s i n t o e q u a l i t y and i n e q u a l i t y c o n s t r a i n t s . . .
138 A eq = A ( : , eq ) ; A i n = A ( : , i n ) ;
139 b l e q = b l ( eq ) ; b l i n = b l ( i n ) ;
140 b u e q = bu ( eq ) ; b u i n = bu ( i n ) ;
141 A = [− A i n ’ ; A i n ’ ] ; % c o n s t r a i n t m a t r i x o f i n e q u a l i t y c o n s t r a i n t s
142 b = [− b l i n ; b u i n ] ; % i n e q u a l i t y c o n s t r a i n t s
143 Aeq = A eq ’ ; % c o n s t r a i n t m a t r i x o f e q u a l i t y c o n s t r a i n t s
144 beq = ( b l e q + b u e q ) / 2 ; % e q u a l i t y c o n s t r a i n t s
145 else
146 % a l l c o n s t r a i n t s i n i t i a l i z e d as i n e q u a l i t i e s
147 A = [−A ’ ; A ’ ] ; % o n s t r a i n t m a t r i x o f i n e q u a l i t y c o n s t r a i n t s
148 b = [− b l ; bu ] ; % i n e q u a l i t y c o n s t r a i n t s
149 Aeq = [ ] ; % no e q u a l i t y c o n s t r a i n t s
150 beq = [ ] ; % no e q u a l i t y c o n s t r a i n t s
151 end
152
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % Fi nd f e a s i b l e p o i n t and d i s p l a y u s e r i n f o %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % Fi nd f e a s i b l e p o i n t u s i n g l i n p r o g w i t h d e f a u l t s e t t i n g s . . .
157 disp ( ’ Calling linprog . ’ )
158 [ x , dummy , e x i t f l a g ] = l i n p r o g ( f , A, b , Aeq , beq , l , u , [ ] , o p t i m s e t ( ’ D i s p l a y ’ , ’ o f f ’ ) ) ;
159 % R e p l a c e p s e u d o l i m i t w i t h +/− i n f . . .
160 i f p s e u d o i n f ˜= i n f
161 b ( f i n d ( b == −p s e u d o i n f ) ) = − i n f ;
162 b ( f i n d ( b == p s e u d o i n f ) ) = i n f ;
163 l ( f i n d ( l == −p s e u d o i n f ) ) = − i n f ;
164 u ( f i n d ( u == p s e u d o i n f ) ) = i n f ;
D.2 Inequality Constrained QP’s 139
165 end
166 % Display i n f o . . .
167 i f e x i t f l a g ˜= 1
168 d i s p ( [ ’ No f e a s i b l e p o i n t found , e x i t f l a g = ’ , i n t 2 s t r ( e x i t f l a g ) , ’ , s e e ” h e l p
linprog ”. ’ ])
169 end
170 fo r i = 1: length (x)
171 i f x ( i ) > pseudoinf
172 di s p ( [ ’ Fe a s i b l e poi nt regarded as i n f i n i t e , x ( ’ , i n t 2 s t r ( i ) , ’ ) > ’ ,
num 2s tr ( p s e u d o i n f ) , ’ . ’ ] )
173 end
174 i f x ( i ) < −p s e u d o i n f
175 di s p ( [ ’ Fe a s i b l e poi nt regarded as i n f i n i t e , x ( ’ , i n t 2 s t r ( i ) , ’ ) < ’ ,
num 2s tr (− p s e u d o i n f ) , ’ . ’ ] )
176 end
177 end
178 di s p ( ’ LPsolver terminated . ’ )
140 Matlab-code
SQP solver.m
1 f u n c t i o n [ x , i n f o , p e r f ] = S Q P s o l v e r ( modfun , modsens , c o s t f u n , c o s t s e n s , x0 ,
pi0 , opts , v a r a r g i n )
2 % SQP SOLVER S o l v e s a n o n l i n e a r program o f t h e form
3 %
4 % min f (x)
5 % s . t . h ( x ) >= 0
6 %
7 % Where f : Rˆn −> R , and h : Rˆn −>Rˆm, m eani ng t h a t n i s t h e number o f
v a r i a b l e s and m
8 % i s t h e number o f c o n s t r a i n t s . SQP s o l v e s t h e program by u s e o f t h e L a g r a n g i a n
function
9 % L ( x , y ) = f ( x )−y ’ h ( x ) w hi ch means t h a t i t i s t h e f o l l o w i n g s y s t e m t h a t i s
10 % solved
11 %
12 % n a b l a x ( L ( x , y ) ) = n a b l a ( f ( x ) )−n a b l a ( h ( x ) ) y = 0
13 % n a b l a y ( L ( x , y ) ) = −h ( x ) = 0 .
14 %
15 % Newtons method i s u s e d t o a p p r o x i m a t e t h e s o l u t i o n . Each Newton s t e p i s
c a l c u l a t e d by
16 % s o l v i n g a QP d e f i n e d a s
17 %
18 % min 0 . 5 ∗ d e l t a x ’ [ n a b l a ˆ2 x x ( L ( x , y ) ) ] d e l t a x + [ n a b l a ( f ( x ) ) ] ’ d e l t a x
19 % s . t . n a b l a ( h ( x ) ) ’ d e l t a x >= −h ( x )
20 %
21 % T h i s means t h a t t h e s o l u t i o n can o n l y be f o u n d i f n a b l a ˆ2 x x ( L ( x , y ) ) i s
22 % p o s i t i v e d e f i n i t e . An BFGS−u p d a t e h a s b e e n p r o v i d e d w hi ch a p p r o x i m a t e s n a b l a
ˆ2 x x ( L ( x , y ) ) .
23 % The s o l u t i o n i s f o u n d by s o l v i n g a s e q u e n c e o f t h e s e QPs . The
24 % d u a l a c t i v e s e t method i s u s e d f o r s o l v i n g t h e QP’ s .
25 %
26 % Call
27 % [ x , i n f o , p e r f ] = S Q P s o l v e r ( @modfun , @modsens , @ c o s t f u n , @ c o s t s e n s , x0 ,
pi0 , opts )
28 %
29 % Input parameters
30 % @modfun : f u n c t i o n s that d e f i n e s : h (x ) : Rˆn −>Rˆm
31 % @modsens : f u n c t i o n s that d e f i n e s : nabla (h ( x ) ) : Rˆn −>Rˆ ( nxm )
32 % @costfun : f u n c t i o n s that d e f i n e s : f (x ) : Rˆn −> R
33 % @costsens : f u n c t i o n s that d e f i n e s : nabla ( f ( x ) ) : Rˆn −> Rˆn
34 % x0 : starting quess
35 % pi0 : l agra nge m u l t i p l i e r s f o r the c o n s t r a i n t s .
36 % ( c o u l d be a z e r o −v e c t o r o f l e n g t h m) .
37 % opts :
38 % op ts : V ector with 3 el em en t :
39 % o p t s ( 1 ) = T o l e r a n c e u s e d t o s t a b i l i z e t h e m ethods n u m e r i c a l l y .
40 % I f | v al u e | < op ts ( 1 ) , then v al u e i s regard ed as z e r o .
41 % o p t s ( 2 ) = maximum no . o f i t e r a t i o n s t e p s .
42 % opts ( 3 ) = 1 : Using n u l l space procedure .
43 % = 2 : Using n u l l s p ace p roced u re with f a c t o r i z a t i o n
44 % update .
45 % I f op ts ( 3 ) > 2 , then op ts ( 3 ) i s s e t to 2 a u t o m a t i c a l l y .
46 % I f o p t s i s n o t g i v e n o r empty , t h e d e f a u l t o p t s = [ 1 e−8 1 0 0 0 2 ] .
47 %
48 % Output p a r a m e t e r s
49 % x : The o p t i m a l s o l u t i o n .
50 % i n f o : Performace i n form ati on , v e c t o r with 3 el em en ts :
51 % inf o (1) = f i n a l values of f .
52 % inf o (2) = no . o f i t e r a t i o n s t e p s .
53 % inf o (3) = 1 : F e a s i b l e s o l u t i o n found .
54 % 2 : No . o f i t e r a t i o n s t e p s e x c e e d e d .
55 % p e r f : Performace , s t r u c t h o l d i n g :
56 % perf . x : V a l u e s o f x from e a c h i t e r a t i o n o f SQP . S i z e i s n x i t .
57 % perf . f : V a l u e s o f f ( x ) from e a c h i t e r a t i o n o f SQP . S i z e i s 1
xit .
58 % p e r f . itQP : Number o f i t e r a t i o n s from t h e d u a l a c t i v e s e t method
59 % e a c h t i m e a QP i s s o l v e d . S i z e i s 1 x i t .
60 % p e r f . stopQP : r e a s o n why t h e d u a l a c t i v e s e t method h a s
61 % t e r m i n a t e d e a c h t i m e a QP i s s o l v e d . S i z e
62 % is 1xit .
63 % p e r f . stopQP ( i ) = 1 : s o l u t i o n o f QP h a s b e e n f o u n d s u c c e s s f u l l y .
64 % p e r f . stopQP ( i ) = 2 : s o l u t i o n o f QP h a s n o t b e e n f o u n d s u c c e s s f u l l y a s
65 % i t e r a t i o n number h a s e x c e e d e d m a x i t e r a t i o n
number .
66 % p e r f . stopQP ( i ) = 3 : s o l u t i o n o f QP h a s n o t b e e n f o u n d a s t h e QP i s
infeasible .
67 %
68 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 .
69 % Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
D.3 Nonlinear Programming 141
70 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n .
71 % M. Sc . , IMM, DTU, DK−2800 Lyngby .
72 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r .
73 % Per Grove Thomsen , P r o f e s s o r .
74 % D ate : 0 7 . J une 2 0 0 7 .
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76
77 if n argi n < 7 | isempty ( op ts )
78 t o l = 1 e −8;
79 it max = 1000;
80 method = 2 ;
81 o p t s = [ t o l i t m a x method ] ;
82 else
83 i f opts (3 ) > 2
84 opts (3 ) = 2 ;
85 end
86 opts = opts ( : ) ’ ;
87 end
88
89 f 0 = f e v a l ( c o s t f u n , x0 , v a r a r g i n { : } ) ;
90 g0 = f e v a l ( modfun , x0 , v a r a r g i n { : } ) ;
91 c = f e v a l ( c o s t s e n s , x0 , v a r a r g i n { : } ) ;
92 A = f e v a l ( modsens , x0 , v a r a r g i n { : } ) ;
93 W = e y e ( l e n g t h ( x0 ) ) ;
94 w non = ( 1 : 1 : l e n g t h ( g0 ) ) ;
95
96 stop = 0;
97 t o l = opts (1 ) ;
98 it max = opts ( 2) ;
99 it r = 0;
100 n = l e n g t h ( x0 ) ;
101 x i n i t = x0 ;
102 f i n i t = f0 ;
103
104 % i n i t i a l i z e containers . . .
105 t r a c e = ( nargout > 2) ;
106 i f trace
107 X = repm at ( z e r o s ( n , 1 ) , 1 , i t m a x ) ; % x o f SQP
108 F = repm at ( 0 , 1 , i t m a x ) ; % f u n c t i o n v a l u e o f SQP
109 I t = repm at ( 0 , 1 , i t m a x ) ; % no . i t e r a t i o n s o f QP
110 S t o p = repm at ( 0 , 1 , i t m a x ) ; % s t o p o f QP
111 end
112 max itr = it max ;
113
114 w hi l e ˜ stop
115 X ( : , i t r +1) = x0 ;
116 i t r = i t r + 1;
117 i f ( i t r > max itr )
118 stop = 2 ;
119 end
120
121 [ d e l t a x , mu, i n f o ] = d u a l a c t i v e s e t m e t h o d (W, c , A,−g0 , w non , [ ] , o p t s ) ;
122
123 if( a b s ( c ’ ∗ d e l t a x ) + a b s (mu’ ∗ g0 ) ) < t o l
124 d i s p ( ’ s o l u t i o n has been found ’ )
125 stop = 1 ;
126 else
127
128 if i t r == 1
129 s i g m a = a b s (mu) ;
130 else
131 for i = 1: l e n g t h (mu)
132 s i g m a ( i ) = max ( a b s (mu( i ) ) , 0 . 5 ∗ ( s i g m a ( i )+a b s (mu( i ) ) ) ) ;
133 end
134 end
135
136 [ a l p h a , x , f , g ] = l i n e s e a r c h a l g o r i t h m ( modfun , c o s t f u n , f 0 , g0 , c , x0 , d e l t a x
, s i gm a , 1 e −4) ;
137
138 p i i = p i 0 + a l p h a ∗ (mu−p i 0 ) ;
139
140 n a b l a L 0 = c−A∗ p i i ;
141 c = f e v a l ( costsens , x , varargin {:}) ;
142 A = f e v a l ( modsens , x , v a r a r g i n { : } ) ;
143 n a b l a L = c−A∗ p i i ;
144 s = x − x0 ;
145 y = nabla L − nabla L0 ;
146 sy = s ’ ∗ y ;
147 sWs = s ’ ∗W∗ s ;
148 i f ( s y >= 0 . 2 ∗ sWs )
149 theta = 1;
150 else
151 t h e t a = ( 0 . 8 ∗ sWs ) / ( sWs−s y ) ;
152 end
153 Ws = W∗ s ;
154 sW = s ’ ∗W;
155 r = t h e t a ∗y+(1− t h e t a ) ∗Ws ;
142 Matlab-code
1 f u n c t i o n [ x , u , Q, R ] = r a n g e s p a c e u p d a t e ( L , A, g , b , Q, R, c o l r e m )
2 % RANGE SPACE UPDATE u s e s t h e r a n g e−s p a c e p r o c e d u r e f o r s o l v i n g a QP p r o b l e m :
min f ( x ) = 0. 5∗x ’ Gx+g ’ x s t : A ’ x=b ,
3 % w here A c o n t a i n s m c o n s t r a i n t s and t h e s y s t e m h a s n v a r i a b l e s .
RANGE SPACE UPDATE c o n t a i n s m ethods f o r
4 % updati ng the f a c t o r i z a t i o n s u s i n g Givens r o t a t i o n s .
5
6 % ∗∗∗ when s o l v i n g an i n e q u a l i t y c o n s t r a i n e d QP, a s e q e n c e o f e q u a l i t y
7 % c o n s t r a i n e d QPs a r e s o l v e d . The d i f f e r e n c e b e t w e e n two o f t h e s e
8 % f o l l o w i n g e q u a l i t y c o n s t r a i n e d QP i s one appended c o n s t r a i n t a t t h e
9 % l a s t i n d e x o f A , o r a c o n s t r a i n t rem ov ed a t i n d e x c o l r e m o f A .
10 %
11 % Call
12 % [ x , u , Q, R ] = r a n g e s p a c e u p d a t e ( L , A, g , b , Q, R, c o l r e m )
13 %
14
15 % Input parameters
16 % L : is the Cholesky factorization of the Hessian matrix G
o f f ( x ) . L i s nxn
17 % A : is the constraint matrix . The constraints are columns
i n A . A i s nxm
18 % g : c on ta i ns n elements
19 % b : c on ta i ns m elements
144 Matlab-code
20 % Q and R : i s t h e QR−f a c t o r i z a t i o n o f t h e QP w hi ch h a s j u s t b e e n s o l v e d
21 % ( i f not the f i r s t i t e r a t i o n ) i n the s eq uence d e s c i b e d i n ∗∗∗.
22 % col rem : i s t h e i n d e x a t w hi ch a c o n s t r a i n t h a s b e e n rem ov ed
from A .
23 %
24 % Q, R and c o l r e m can be empty [ ] w hi ch means that The QP
25 % i s t h e f i r s t one i n the sequence ( s e e ∗∗∗) .
26
27 % Output p a r a m e t e r s
28 % x : i s the optimized poi nt
29 % u : i s the corres pondi ng Lagrangian Multipliers
30 % Q and R : i s t h e QR−f a c t o r i z a t i o n o f A
31
32 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
33 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
34 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
35 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
36 % D ate : 11. february 2007.
37 % Reference : −−−−−−−−−−−−−−−−−−−−−
38
39 [ nA ,mA] = s i z e (A) ;
40 [ nR ,mR] = s i z e (R) ;
41 K = L\A ;
42 w = L\ g ;
43 z = b+(w’ ∗K) ’ ;
44 i f i s e m p t y (Q) && i s e m p t y (R)
45 % di s p ( ’ complete f a c t o r i z a t i o n ’ ) ;
46 [ Q, R ] = q r (K) ;
47 e l s e i f mR < mA % new col um n h a s b e e n appended t o A
48 % d i s p ( ’ append update ’ ) ;
49 [ Q, R ] = q r f a c t u p d a t e a p p c o l (Q, R, K ( : , end ) ) ;
50 e l s e i f mR > mA % col um n h a s b e e n rem ov ed from A a t i n d e x col rem
51 % d i s p ( ’ rem ov e u p d a t e ’ ) ;
52 [ Q, R ] = q r f a c t u p d a t e r e m c o l (Q, R, c o l r e m ) ;
53 end
54 u = R( 1 : l en gth ( z ) , : ) ’\ z ;
55 u = R ( 1 : l e n g t h ( z ) , : ) \u ;
56 y = K∗u−w ;
57 x = L’\y ;
1 f u n c t i o n [ Q, R ] = q r f a c t u p d a t e a p p c o l (Q, R, c o l n e w )
2 % QR FACT UPDATE APP COL u p d a t e s t h e q r−f a c t o r i z a t i o n when a s i n g l e col um n i s
3 % appended a t i n d e x m+ 1. And t h e f a c t o r i z a t i o n from b e f o r e a d d i n g t h e col um n
i s known
4 % : ( Q o l d and R o l d )
5 %
6 % Call
7 % [ q r ] = q r f a c t u p d a t e a p p c o l ( Q ol d , R o l d , c o l n e w )
8 %
9 % Input parameters
10 % Q o l d i s t h e Q p a r t o f t h e QR−f a c t o r i z a t i o n from t h e f o r m e r
11 % m a t r i x A ( t h e m a t r i x we want t o append one col um n a t i n d e x m+1) .
12 % R o l d i s t h e R p a r t from t h e QR−f a c t o r i z a t i o n from t h e f o r m e r
13 % m a t r i x A ( t h e m a t r i x we want t o append one col um n a t i n d e x m+1) .
14 % c o l n e w i s t h e col um n we want t o append
15 %
16 % Output p a r a m e t e r s
17 % Q i s t h e u p d a t e d Q−m a t r i x
18 % R i s t h e u p d a t e d R−m a t r i x ( e v e r y t h i n g but t h e u p p e r mxm m a t r i x i s z e r o s
)
19 %
20 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
21 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
22 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
23 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
24 % D ate : 31. october 2006.
25 % Reference : −−−−−−−−−−−−−−−−−−−−−
26
27 [ n m] = s i z e (R) ;
28 sw = ( c o l n e w ’ ∗Q) ’ ;
29 for j = n : − 1 :m+2
30 i = j −1;
31 [ c s ] = g i v e n s r o t a t i o n m a t r i x ( sw ( i ) , sw ( j ) ) ;
32 e 1 = sw ( i ) ∗ c − sw ( j ) ∗ s ;
33 sw ( j ) = sw ( i ) ∗ s + sw ( j ) ∗ c ;
34 sw ( i ) = e 1 ;
D.4 Updating the Matrix Factorizations 145
35 v1 = Q ( : , i ) ∗ c − Q ( : , j ) ∗ s ;
36 Q( : , j ) = Q( : , i ) ∗ s + Q( : , j ) ∗c ;
37 Q ( : , i ) = v1 ;
38 end
39 R = [ R sw ] ;
1 f u n c t i o n [ Q, R ] = q r f a c t u p d a t e r e m c o l (Q, R, c o l i n d e x )
2 % QR FACT UPDATE REM COL u p d a t e s t h e q r−f a c t o r i z a t i o n when a s i n g l e col um n i s
3 % rem ov ed .
4 %
5 % Call
6 % [ q r ] = q r f a c t u p d a t e r e m c o l ( Q ol d , R o l d , c o l n e w )
7 %
8 % Input parameters
9 % Q o l d i s t h e Q p a r t from t h e QR−f a c t o r i z a t i o n from t h e f o r m e r
10 % m a t r i x A ( t h e m a t r i x we want t o rem ov e one col um n ) .
11 % R o l d i s t h e R p a r t from t h e QR−f a c t o r i z a t i o n from t h e f o r m e r
12 % m a t r i x A ( t h e m a t r i x we want t o rem ov e one col um n ) .
13 % c o l i n d e x i s t h e i n d e x o f t h e col um n we want t o rem ov e
14 %
15 % Output p a r a m e t e r s
16 % Q i s t h e u p d a t e d Q−m a t r i x
17 % R i s t h e u p d a t e d R−m a t r i x ( e v e r y t h i n g but t h e u p p e r mxm m a t r i x i s z e r o s
)
18 %
19 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
20 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
21 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
22 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
23 % D ate : 31. october 2006.
24 % Reference : −−−−−−−−−−−−−−−−−−−−−
25
26 [ n m] = s i z e (R) ;
27 t = m − col index ;
28 for i = 1:1: t
29 j = i + 1;
30 [c s ] = g i v e n s r o t a t i o n m a t r i x (R( c o l i n d e x +i −1 , c o l i n d e x +i ) , R( c o l i n d e x +j
−1 , c o l i n d e x + i ) ) ;
31 v1 = R( c o l i n d e x +i −1 , c o l i n d e x + 1: end ) ∗ c − R( c o l i n d e x +j −1 , c o l i n d e x + 1: end ) ∗
s;
32 R( c o l i n d e x +j −1 , c o l i n d e x + 1: end ) = R( c o l i n d e x +i −1 , c o l i n d e x + 1: end ) ∗ s + R(
c o l i n d e x+j −1 , c o l i n d e x + 1: end ) ∗ c ;
33 R( c o l i n d e x +i −1 , c o l i n d e x + 1: end ) = v1 ;
34 q1 = Q ( : , c o l i n d e x+i −1) ∗ c − Q ( : , c o l i n d e x +j −1) ∗ s ;
35 Q ( : , c o l i n d e x +j −1) = Q ( : , c o l i n d e x +i −1) ∗ s + Q ( : , c o l i n d e x +j −1) ∗ c ;
36 Q ( : , c o l i n d e x +i −1) = q1 ;
37 end
38 R = [ R ( : , 1 : c o l i n d e x −1) R ( : , c o l i n d e x + 1: end ) ] ;
16 % Input parameters
17 % G : i s t h e H e s s i a n m a t r i x o f f ( x ) . G i s nxn
18 % A : i s t h e c o n s t r a i n t m a t r i x . The c o n s t r a i n t s a r e c o l u m n s
i n A . A i s nxm
19 % g : c o nt ai ns n elements
20 % b : c o nt ai ns m elements
21 % Q o l d and T o l d : i s t h e QT−f a c t o r i z a t i o n o f t h e QP w hi ch h a s j u s t b e e n
solved
22 % ( i f n o t t h e f i r s t i t e r a t i o n ) i n t h e s e q u e n c e d e s c i b e d i n ∗ ∗ ∗ . The
23 % T p a r t o f t h e QT−f a c t o r i z a t i o n i s l o w e r t r i a n g u l a r
24 % L old : i s the Cholesky f a c t o r i z a t i o n o f the reduced H es s i an
25 % m a t r i x o f t h e QP j u s t s o l v e d ( s e e ∗ ∗ ∗ ) .
26 % col rem : i s t h e i n d e x a t w hi ch a c o n s t r a i n t h a s b e e n rem ov ed
from A .
27 %
28 % Q ol d , T old , L ol d and c o l r e m can be empty [] w hi ch means t h a t The QP
29 % i s the f i r s t one i n the s eq u en ce ( s e e ∗∗∗) .
30
31 % Output p a r a m e t e r s
32 % x : is the optimized poi nt
33 % u : is the corres pondi ng Lagrangian M u l t i p l i e r s
34 % Q new and T new : is t h e QT−f a c t o r i z a t i o n o f A
35 % L new : is the Cholesky f a c t o r i z a t i o n o f the reduced Hessian
matrix .
36
37 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
38 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
39 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
40 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
41 % D ate : 08. february 2007.
42
43 [ nA ,mA] = s i z e (A) ;
44 [ nT ,mT] = s i z e ( T o l d ) ;
45
46 di m N ul Space = nA−mA;
47
48 Q new = Q o l d ;
49 T new = T o l d ;
50 L new = L o l d ;
51 i f i s e m p t y ( Q o l d ) && i s e m p t y ( T o l d ) && i s e m p t y ( L o l d )
52 %d i s p ( ’ c o m p l e t e f a c t o r i z a t i o n ’ ) ;
53 [ Q, R ] = q r (A) ;
54 I t i l d e = f l i p u d ( e y e (nA ) ) ;
55 T new = I t i l d e ∗R ;
56 Q new = Q∗ I t i l d e ;
57 Q1 = Q new ( : , 1 : di m N ul Space ) ;
58 Gz = Q1 ’ ∗G∗Q1 ;
59 L new = c h o l ( Gz ) ’ ;
60 e l s e i f mT < mA % new col um n h a s b e e n appended t o A
61 % d i s p ( ’ append update ’ ) ;
62 [ Q new , T new , L new ] = n u l l s p a c e u p d a t e f a c t a p p c o l ( Q ol d , T old , L old ,
A ( : , end ) ) ;
63 e l s e i f mT > mA% col um n h a s b e e n rem ov ed from A a t i n d e x c o l r e m
64 % d i s p ( ’ rem ov e u p d a t e ’ ) ;
65 [ Q new , T new , L new ] = n u l l s p a c e u p d a t e f a c t r e m c o l ( Q ol d , T old , L old ,
G, c o l r e m ) ;
66 end
67
68 Q1 = Q new ( : , 1 : di m N ul Space ) ;
69 Q2 = Q new ( : , di m N ul Space + 1:nA ) ;
70 T newMark = T new ( di m N ul Space + 1: end , : ) ;
71 py = T newMark ’ \ b ;
72 g z = −((G∗ (Q2∗py ) + g ) ’ ∗Q1 ) ’ ;
73 z = L new \ g z ;
74 pz = L new ’ \ z ;
75 x = Q2∗py + Q1∗ pz ;
76 u = ( (G∗x + g ) ’ ∗Q2 ) ’ ;
77 u = T newMark \u ;
1 f u n c t i o n [ Q, T , L ] = n u l l s p a c e u p d a t e f a c t a p p c o l (Q, T , L , c o l n e w )
2 % NULL SPACE UPDATE FACT APP COL u p d a t e s t h e QT−f a c t o r i z a t i o n o f A when a
3 % s i n g l e col um n c o l n e w i s appended t o A a s t h e l a s t col um n . The r e s u l t i n g
4 % c o n s t r a i n t m a t r i x i s Abar = [ A c o l n e w ] . The c o r r e s p o n d i n g QP p r o b l e m h a s a
reduced Hessian
5 % m a t r i x redH and t h e c h o l e s k y f a c t o r i z a t i o n o f redH i s L o l d .
6
7 % Call
D.4 Updating the Matrix Factorizations 147
1 f u n c t i o n [ Q, T , L ] = n u l l s p a c e u p d a t e f a c t r e m c o l (Q, T , L , G, c o l r e m )
2 % NULL SPACE UPDATE FACT REM COL u p d a t e s t h e QT−f a c t o r i z a t i o n o f A when a
3 % col um n i s rem ov ed from A a t column−i n d e x c o l r e m . The new C o n s t r a i n t m a t r i x
i s c a l l e d Abar .
4 % The c o r r e s p o n d i n g QP p r o b l e m h a s a r e d u c e d H e s s i a n m a t r i x redH and t h e
cholesky f a c to ri z a ti o n
5 % o f redH i s L .
6
7 % Call
8 % [ Q, T , L ] = n u l l s p a c e u p d a t e f a c t r e m c o l (Q, T , L , G, col rem )
9
10 % Input parameters
11 % Q and T : i s t h e QT−f a c t o r i z a t i o n o f A
12 % L : i s the chol es k y f a c t o r i z a t i o n o f the reduced Hessian
m a t r i x o f t h e c o r r e s p o n d i n g QP p r o b l e m .
13 % G : i s t h e H e s s i a n m a t r i x o f t h e QP p r o b l e m .
14 % col rem : i s t h e column−i n d e x a t w hi ch a col um n h a s b e e n
rem ov ed from A
15
16 % Output p a r a m e t e r s
17 % Q and T : i s t h e QT−f a c t o r i z a t i o n o f Abar
18 % L : i s the Cholesky f a c t o r i z a t i o n o f the reduced Hessian
m a t r i x o f t h e new QP p r o b l e m .
19
20 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
21 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
22 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
23 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
24 % D ate : 08. february 2007.
148 Matlab-code
25 % Reference : −−−−−−−−−−−−−−−−−−−−−
26
27 [ n ,m] = s i z e (T) ;
28 di m N ul Space = n−m;
29 j = col rem ;
30 mm = m−j ;
31 nn = mm+ 1;
32
33
34 for i = 1 : 1 :mm
35 i d x 1 = nn−i ;
36 i d x 2 = i d x 1 + 1;
37 [ s , c ] = g i v e n s r o t a t i o n m a t r i x (T( di m N ul Space+i d x 1 , j+ i ) ,T( di m N ul Space+i d x 2 , j
+i ) ) ;
38 temp = T( di m N ul Space+i d x 1 , j + 1: end ) ∗ c + T( di m N ul Space+i d x 2 , j + 1: end ) ∗ s ;
39 T( di m N ul Space+i d x 2 , j + 1: end ) = −T( di m N ul Space+i d x 1 , j + 1: end ) ∗ s + T(
di m N ul Space+i d x 2 , j + 1: end ) ∗ c ;
40 T( di m N ul Space+i d x 1 , j + 1: end ) = temp ;
41 temp = Q ( : , di m N ul Space+i d x 1 ) ∗ c + Q ( : , di m N ul Space+i d x 2 ) ∗ s ;
42 Q ( : , di m N ul Space+i d x 2 ) = −Q ( : , di m N ul Space+i d x 1 ) ∗ s + Q ( : , di m N ul Space+i d x 2 ) ∗ c ;
43 Q ( : , di m N ul Space+i d x 1 ) = temp ;
44 end
45
46 T = [ T ( : , 1 : j −1) T ( : , j + 1: end ) ] ;
47 z = Q ( : , di m N ul Space +1) ;
48 l = L \ ( (G∗ z ) ’ ∗Q ( : , 1 : di m N ul Space ) ) ’ ;
49 d e l t a = s q r t ( z ’ ∗G∗ z−l ’ ∗ l ) ;
50 L = [ L z e r o s ( di m N ul Space , 1 ) ; l ’ d e l t a ] ;
1 f u n c t i o n [ x , u , Q f r , T f r , L f r ] = n u l l s p a c e u p d a t e F R F X ( Q f r , T f r , L f r , G, A, g , b ,
dim fx , c o l r e m )
2
3 % NULL SPACE UPDATE FRFX u s e s t h e same p r o c e d u r e a s NULL SPACE UPDATE f o r
solving f ( x ) = 0. 5∗x ’ Gx+g ’ x s t : A’ x=b ,
4 %( s o p l e a s e t a k e a l o o k a t i t ) . The d i f f e r e n c e i s t h a t NULL SPACE UPDATE FRFX
t a k e s a d v a n t a g e o f t h e f a c t t h a t some o f t h e a c t i v e c o n s t r a i n t s
5 % a r e bounds ( u s u a l l y ) . An a c t i v e bound c o r r e s p o n d t o one f i x e d v a r i a b l e . T h i s
means t h a t x can be d e v i d e d i n t o [ x f r e e , x f i x e d ] ’
6 % w here x f i x e d a r e t h o s e v a r i a b l e s w hi ch a r e f i x e d . The p a r t o f t h e
7 % f a c t o r i z a t i o n s w hi ch c o r r e s p o n d t o t h e f i x e d v a r i a b l e s can n o t be c h a n g e s
8 % ( a s t h e y a r e f i x e d ) and t h i s means t h a t we a r e o n l y r e q u i r e d t o
9 % r e f a c t o r i z e t h e p a r t w hi ch c o r r e s p o n d t o t h e f r e e v a r i a b l e s .
10
11 % ∗∗∗ when s o l v i n g an i n e q u a l i t y c o n s t r a i n e d QP, a s e q e n c e o f e q u a l i t y
12 % c o n s t r a i n e d QPs a r e s o l v e d . The d i f f e r e n c e b e t w e e n two o f t h e s e
13 % f o l l o w i n g e q u a l i t y c o n s t r a i n e d QP i s one appended c o n s t r a i n t o r one
14 % rem ov ed c o n s t a i n t
15 %
16 % Call
17 % [ x , u , Q f r , T f r , L f r ] = n u l l s p a c e u p d a t e F R F X ( Q f r , T f r , L f r , G, A, g , b ,
dim fx , c o l r e m )
18 %
19 % Input parameters
20 % G : i s t h e H e s s i a n m a t r i x o f f ( x ) . G i s nxn
21 % A : i s t h e c o n s t r a i n t m a t r i x w hi ch o n l y c o n t a i n s a c t i v e
g e n e r a l c o n s t r a i n t s ( t h e bound−c o n s t r a i n t s h a s b e e n rem ov ed ) .
22 % The d i m e n s i o n o f A i s n x m f r ( n i s number
23 % o f v a r i a b l e s and m f r i s t h e number o f
24 % active general c o nstra ints )
25 % g : i s t h e g r a d i e n t o f f ( x ) and t h e d i m e n s i o n i s nx1
26 % b : c o n t a i n s t h e max v a l u e s o f t h e c o n s t r a i n t s ( b o t h
27 % g e n e r a l and an bound c o n s t r a i n t s ) and t h e r e f o r e
the dimension i s
28 % ( m f r+mfx ) x1 .
29 % Q f r and T f r a r e t h e f r e e p a r t o f t h e QT−f a c t o r i z a t i o n o f t h e p a r t o f
t h e QP w hi ch h a s j u s t b e e n s o l v e d
30 % ( i f n o t t h e f i r s t i t e r a t i o n ) i n t h e s e q u e n c e d e s c i b e d i n ∗ ∗ ∗ . The
31 % T p a r t o f t h e QT−f a c t o r i z a t i o n i s l o w e r t r i a n g u l a r
32 % L old : i s the Cholesky f a c t o r i z a t i o n o f the reduced H es s i an
33 % m a t r i x o f t h e QP j u s t s o l v e d ( s e e ∗ ∗ ∗ ) .
34 % col rem : i s t h e i n d e x a t w hi ch a c o n s t r a i n t h a s b e e n rem ov ed
from A ( i f a c o n s t r a i n t h a s b e e n appended t h i s
35 % v a r i a b l e i s unus ed .
36 %
37 % Q ol d , T o l d , L o l d and c o l r e m can be empty [] w hi ch means t h a t The QP
38 % i s the f i r s t one i n t h e s e q u e n c e ( s e e ∗ ∗ ∗ ) .
39 % dim fx : number o f f i x e d v a r i a b l e s
40 %
D.4 Updating the Matrix Factorizations 149
41 % Output p a r a m e t e r s
42 % x : is the s o l u t i o n
43 % u : is the corres pondi ng Lagrangian M u l t i p l i e r s
44 % Q f r and T f r : is t h e QT−f a c t o r i z a t i o n o f A c o r r e s p o n d i n g t o t h e
45 % free variables .
46 % L fr : i s the Cholesky f a c t o r i z a t i o n o f the reduced
47 % H es s i an matrix .
48 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
49 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
50 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
51 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
52 % D ate : 08. february 2007.
53 % Reference : −−−−−−−−−−−−−−−−−−−−−
54
55 [ nT mT] = s i z e ( T f r ) ;
56 [ nA mA] = s i z e (A) ;
57 d i m f x o l d = nA−nT ;
58
59 if i s e m p t y (A) % n o t h i n g t o f a c t o r i z e
60 % d i s p ( ’ A i s empty ’ )
61 C = eye ( di m fx ) ;
62 C = [ z e r o s ( l e n g t h ( g )−d i m f x , d i m f x ) ; C ] ;
63 [ x u ] = n u l l s p a c e (G, C , g , b ) ;
64 Q fr = [ ] ; T f r = [ ] ; L f r = [ ] ; A fx = [ ] ;
65
66 e l s e i f i s e m p t y ( T f r ) | | ( (mA == mT) && ( d i m f x == d i m f x o l d ) ) % c o m p l e t e
factorization
67 % di s p ( ’ complete f a c t o r i z a t i o n ’ )
68 A f r = A ( 1 : end−d i m f x , : ) ;
69 [ n m] = s i z e ( A f r ) ;
70 G f r f r = G( 1 : n , 1 : n ) ;
71 dns = n−m;
72 [ Q, R ] = q r ( A f r ) ;
73 I t i l d e = f l i p u d ( eye ( n ) ) ;
74 T f r = I t i l d e ∗R ;
75 Q f r = Q∗ I t i l d e ;
76 Qz = Q f r ( : , 1 : dns ) ;
77 Gz = Qz ’ ∗ G f r f r ∗Qz ;
78 L f r = c h o l ( Gz ) ’ ;
79 A f x = A( end−d i m f x + 1: end , : ) ;
80 [ x u ] = h e l p f u n ( Q f r , T f r , L f r , A fx , G, g , b ) ;
81
82 e l s e i f mA > mT % one g e n e r a l c o n s t r a i n t h a s b e e n appended
83 % d i s p ( ’ append g e n e r a l c o n s t r a i n t ’ )
84 [ Q fr , T fr , L f r ] = n u l l s p a c e u p d a t e f a c t a p p g e n e r a l F R F X ( Q fr , T fr ,
L f r , A ( : , end ) ) ;
85 dim fr = s i z e ( T fr , 1 ) ;
86 A f x = A( d i m f r + 1: end , : ) ;
87 [ x u ] = h e l p f u n ( Q f r , T f r , L f r , A fx , G, g , b ) ;
88 e l s e i f mA < mT % one g e n e r a l c o n s t r a i n t h a s b e e n rem ov ed a t i n d x c o l r e m
89 % d i s p ( ’ rem ov e g e n e r a l c o n s t r a i n t ’ )
90 dim fr = s i z e ( T fr , 1 ) ;
91 G f r f r = G( 1 : d i m f r , 1 : d i m f r ) ;
92 [ Q fr , T fr , L f r ] = n u l l s p a c e u p d a t e f a c t r e m g e n e r a l F R F X ( Q fr , T fr ,
L fr , G frfr , col rem ) ;
93 A f x = A( d i m f r + 1: end , : ) ;
94 [ x u ] = h e l p f u n ( Q f r , T f r , L f r , A fx , G, g , b ) ;
95
96 e l s e i f d i m f x > d i m f x o l d % one bound h a s b e e n appended
97 % d i s p ( ’ append bound ’ )
98 [ Q fr , T fr , L f r ] = null space update fact app bound FRFX ( Q fr , T fr , L fr
);
99 dim fr = s i z e ( T fr , 1 ) ;
100 A f x = A( d i m f r + 1: end , : ) ;
101 [ x u ] = h e l p f u n ( Q f r , T f r , L f r , A fx , G, g , b ) ;
102
103 e l s e i f d i m f x < d i m f x o l d % one bound h a s b e e n rem ov ed
104 % d i s p ( ’ rem ov e bound ’ )
105 [ nT mT] = s i z e ( T f r ) ;
106 dns = nT−mT;
107 T f r = T f r ( dns + 1: end , : ) ;
108 T f r = [ T f r ; A( nT + 1 , : ) ] ;
109 [ Q fr , T fr , L f r ] = null space update fact rem bound FRFX ( Q fr , T fr , L fr
, G) ;
110 A f x = A( nT+ 2: end , : ) ;
111 [ x u ] = h e l p f u n ( Q f r , T f r , L f r , A fx , G, g , b ) ;
112 end
113
114 f u n c t i o n [ x new u new ] = h e l p f u n ( Q f r , T f r , L f r , A fx , G, g , b )
115 % disp ( ’ help fun ’ )
116 [ nT ,mT] = s i z e ( T f r ) ;
117 dns = nT−mT;
118 d i m f r = nT ;
119 d i m f x = l e n g t h ( g )−d i m f r ;
120 Q1 = Q f r ( : , 1 : dns ) ;
121 Q2 = Q f r ( : , dns + 1:nT ) ;
150 Matlab-code
1 f u n c t i o n [ Q fr , T fr , L f r ] = n u l l s p a c e u p d a t e f a c t a p p g e n e r a l F R F X ( Q fr ,
T fr , L fr , col new )
2
3 % NULL SPACE UPDATE FACT APP GENERAL FRFX u p d a t e s t h e QT−f a c t o r i z a t i o n o f A
when a
4 % g e n e r a l c o n s t r a i n t : c o l n e w i s appended t o A a s t h e l a s t col um n . The
resulting
5 % c o n s t r a i n t m a t r i x i s Abar = [ A c o l n e w ] . The c o r r e s p o n d i n g QP p r o b l e m h a s a
reduced Hessian
6 % m a t r i x redH and t h e c h o l e s k y f a c t o r i z a t i o n o f redH i s L f r . I t i s o n l y
7 % t h e p a r t c o r r e s p o n d i n g t o t h e f r e e v a r i a b l e s w hi ch a r e u p d a t e d ( t h e f i x e d
8 % part are not changi ng )
9 % Call
10 % [ Q fr , T fr , L f r ] = n u l l s p a c e u p d a t e f a c t a p p g e n e r a l F R F X ( Q fr , T fr
, L fr , col new )
11 %
12 % Input parameters :
13 % Q f r and T f r : i s t h e QT−f a c t o r i z a t i o n o f A ( t h e p a r t
14 % corres pondi ng to the f r e e v a r i a b l e s )
15 % L fr : i s the chol es k y f a c t o r i z a t i o n o f the reduced Hessian
m a t r i x o f t h e c o r r e s p o n d i n g QP p r o b l e m .
16 % col new : i s t h e g e n e r a l c o n s t r a i n t t h a t i s appended t o A : Abar
= [A col new ]
17
18 % Output p a r a m e t e r s
19 % Q f r and T f r : is
t h e QT−f a c t o r i z a t i o n o f Abar ( t h e p a r t
20 % corres pondi ng to the f r e e v a r i a b l e s )
21 % L fr : i s the Cholesky f a c t o r i z a t i o n o f the reduced Hessian
matrix o f t h e new QP p r o b l e m .
22
23 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
24 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
25 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
26 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
27 % D ate : 08. february 2007.
28 % Reference : −−−−−−−−−−−−−−−−−−−−−
29
30 [ n ,m] = s i z e ( T f r ) ;
31 dns = n−m;
32 Z f r = Q f r ( : , 1 : dns ) ;
33 Y f r = Q f r ( : , dns + 1: end ) ;
34 T f r = T f r ( dns + 1: end , : ) ;
35 a f r = col new ( 1 : n ) ;
36 wv = ( a f r ’ ∗ Q f r ) ’ ;
37 w = wv ( 1 : dns ) ;
38 v = wv ( dns + 1: end ) ;
39 f o r i = 1: l e n g t h (w)−1
40 j = i + 1;
41 [ s , c ] = g i v e n s r o t a t i o n m a t r i x (w( i ) ,w( j ) ) ;
42
43 temp = w( i ) ∗ c + w( j ) ∗ s ;
44 w( j ) = −w( i ) ∗ s + w( j ) ∗ c ;
45 w( i ) = temp ;
D.4 Updating the Matrix Factorizations 151
46
47 temp = Z f r ( : , i ) ∗ c + Z f r ( : , j ) ∗ s ;
48 Z f r ( : , j ) = −Z f r ( : , i ) ∗ s + Z f r ( : , j ) ∗ c ;
49 Z f r ( : , i ) = temp ;
50
51 temp = L f r ( i , : ) ∗ c + L f r ( j , : ) ∗ s ;
52 L f r ( j , : ) = −L f r ( i , : ) ∗ s + L f r ( j , : ) ∗ c ;
53 L f r ( i , : ) = temp ;
54 end
55 gamma = w( end ) ;
56 T f r = [ z e r o s ( 1 , s i z e ( T f r , 2 ) ) gamma ; T f r v ] ;
57 L f r = L f r ( 1 : end − 1 , : ) ;
58 [ nn mm] = s i z e ( L f r ) ;
59 f o r i = 1 : 1 : nn
60 j= i + 1;
61 [ c , s ] = givens rotation matrix ( L fr ( i , i ) , L fr ( i , j ) ) ;
62
63 temp = L f r ( : , i ) ∗ c − L f r ( : , j ) ∗ s ;
64 L fr ( : , j ) = L fr ( : , i )∗s + L fr ( : , j )∗c ;
65 L f r ( : , i ) = temp ;
66 end
67 Q fr = [ Z f r Y fr ] ;
68 T f r = [ z e r o s ( dns −1 , s i z e ( T f r , 2 ) ) ; T f r ] ;
69 L f r = L f r ( : , 1 : dns −1) ;
45 i d x 1 = nn−i ;
46 i d x 2 = i d x 1 + 1;
47 [ s , c ] = g i v e n s r o t a t i o n m a t r i x (N( i d x 1 , i ) ,N( i d x 2 , i ) ) ;
48
49 temp = N( i d x 1 , : ) ∗ c + N( i d x 2 , : ) ∗ s ;
50 N( i d x 2 , : ) = −N( i d x 1 , : ) ∗ s +N( i d x 2 , : ) ∗ c ;
51 N( i d x 1 , : ) = temp ;
52
53 temp = Q21 ( : , i d x 1 ) ∗ c + Q21 ( : , i d x 2 ) ∗ s ;
54 Q21 ( : , i d x 2 ) = −Q21 ( : , i d x 1 ) ∗ s + Q21 ( : , i d x 2 ) ∗ c ;
55 Q21 ( : , i d x 1 ) = temp ;
56 end
57 N = N ( 2 : end , : ) ;
58 T new = [ z e r o s ( nn −1 , j −1) N ; T11 M] ;
59 T new = [ z e r o s ( dns +1 ,m−1) ; T new ] ;
60 Q new = [ Q1 Q21 Q22 ] ;
61 z = Q new ( : , dns +1) ;
62 l = L f r \ ( ( G f r f r ∗ z ) ’ ∗ Q1) ’ ;
63 d e l t a = s q r t ( z ’ ∗ G f r f r ∗ z−l ’ ∗ l ) ;
64 L new = [ L f r z e r o s ( dns , 1 ) ; l ’ d e l t a ] ;
50 end
51
52 Q f r = Q f r ( 1 : end − 1 , 1 : end −1) ;
53 T f r = TL ( 1 : end −1 , dns + 1: end ) ;
54 L new = TL ( 1 : dns − 1 , 1 : dns ) ;
55 [ nn mm] = s i z e ( L new ) ;
56 f o r i = 1 : 1 : nn
57 j= i + 1;
58 [ c , s ] = g i v e n s r o t a t i o n m a t r i x ( L new ( i , i ) , L new ( i , j ) ) ;
59
60 temp = L new ( : , i ) ∗ c − L new ( : , j ) ∗ s ;
61 L new ( : , j ) = L new ( : , i ) ∗ s + L new ( : , j ) ∗ c ;
62 L new ( : , i ) = temp ;
63 end
64 L f r = L new ( : , 1 : end −1) ;
D.5 Demos
QP demo.m
79 % check f e a s i b i l i t y o f x . . .
80 i b = f i n d ( At∗x − b < − t o l ) ;
81 i f ˜ isempty ( i b )
82 d i s p ( [ ’ F o l l o w i n g c o n s t r a i n t ( s ) v i o l a t e d , b e c a u s e A∗x < b : ’ ] )
83 f p r i n t f ( [ ’ \b ’ , i n t 2 s t r ( i b ’ ) , ’ . \ n ’ ] )
84 e r r o r ( ’ S t a r t i n g p o i n t f o r p r i m a l a c t i v e s e t method i s n o t f e a s i b l e , run
demo a g a i n . ’ )
85 end
86 % iterate . . .
87 stop = 0 ;
88 w hi l e ˜ stop
89 it = i t + 1;
90 i f i t >= i t m a x
91 d i s p ( ’ No . o r i t e r a t i o n s s t e p s e x e e d e d . ’ )
92 s t o p = 2 ; % maximum no i t e r a t i o n s e x c e e d e d
93 end
94 % c a l l range / n u l l space procedure . . .
95 mu = z e r o s (m, 1 ) ;
96 [ p , m u act ] = n u l l s p a c e d e m o (G, A ( : , w a c t ) ,G∗x+g , z e r o s ( l e n g t h ( w a c t ) , 1 ) ) ;
97 mu( w a c t ) = m u act ;
98 % plot . . .
99 a c t i v e s e t p l o t (G, At , g , b , X ( : , 1 : i t ) ,mu, w a c t , [ − 4 8; −4 8 ] , [ 2 0 20 50 100 t o l
ftp ]) ;
100 d i s p ( ’ P r e s s any k ey t o c o n t i n u e . . . ’ )
101 pause
102 % check i f s o l u t i o n found . . .
103 i f norm ( p ) <= t o l
104 i f mu >= − t o l
105 s top = 1 ; % s o l u t i o n found
106 d i s p ( ’ S o l u t i o n f o u n d by p r i m a l a c t i v e s e t method , demo t e r m i n a t e d . ’
)
107 else
108 % com pute i n d e x j o f bound / c o n s t r a i n t t o be rem ov ed . . .
109 [ dummy , j ] = min (mu) ;
110 w a c t = w a c t ( f i n d ( w a c t ˜= j ) ) ; % rem ov e c o n s t r a i n t j from a c t i v e
set
111 w non = [ w non j ] ; % append c o n s t r a i n t j t o n o n a c t i v e s e t f u n c t i o n
112 end
113 else
114 % com pute s t e p l e n g t h and i n d e x j o f bound / c o n s t r a i n t t o be appended . . .
115 alpha = 1 ;
116 f o r app = w non
117 ap = At ( app , : ) ∗p ; % At ( app , : ) = A ( : , app ) ’
118 i f ap < − t o l
119 temp = ( b ( app ) − At ( app , : ) ∗ x ) / ap ;
120 i f −t o l < temp & temp < a l p h a
121 a l p h a = temp ; % s m a l l e s t s t e p l e n g t h
122 j = app ; % i n d e x j o f bound t o be appended
123 end
124 end
125 end
126 i f alpha < 1
127 % make c o n s t r a i n e d s t e p . . .
128 x = x + a l p h a ∗p ;
129 w a c t = [ w a c t j ] ; % append c o n s t r a i n t j t o a c t i v e s e t
130 w non = w non ( f i n d ( w non ˜= j ) ) ; % rem ov e c o n s t r a i n t j from
nonactive set
131 else
132 % make f u l l s t e p . . .
133 x = x + p;
134 end
135 end
136 X ( : , i t +1) = x ;
137 % % plot . . .
138 % i f ˜ stop
139 % %d i s p ( ’ P r e s s any k ey t o c o n t i n u e . . . ’ )
140 % %p a u s e
141 % a c t i v e s e t p l o t (G, At , g , b , X ( : , 1 : i t +1) ,mu, w a c t , [ − 4 8; −4 8 ] , [ 2 0 20 50
100 t o l f t p ] ) ;
142 % d i s p ( ’ P r e s s any k ey t o c o n t i n u e . . . ’ )
143 % pause
144 % end
145 end
146
147 f u n c t i o n d u a l a c t i v e s e t d e m o (G, g , A , b , f t p )
148 % initialize ...
149 [ n ,m] = s i z e (A) ;
150 C = A;
151 w non = 1 : 1 :m;
152 w act = [ ] ;
153 x = −G\ g ; x = x ( : ) ;
154 mu = z e r o s (m, 1 ) ;
155 % i n i t i a l i z e options . . .
156 t o l = sq r t ( eps ) ;
157 max itr = 100;
158 % i n i t i a l i z e c o u n t e r s and c o n t a i n e r s . . .
159 i t = 0;
D.5 Demos 157
160 it draw = 1;
161 X = repm at ( z e r o s ( n , 1 ) , 1 , m a x i t r ) ;
162 % plot . . .
163 a c t i v e s e t p l o t (G, C ’ , g , b , x , mu, w a c t , [ − 4 8; −4 8 ] , [ 2 0 20 50 100 t o l f t p ] ) ;
164 d i s p ( ’ P r e s s any k ey t o c o n t i n u e . . . ’ )
165 pause
166 X( : , 1 ) = x ;
167 % iterate . . .
168 stop = 0;
169 w hi l e ˜ stop
170 c = c o n s t r a i n t s (G, C ( : , w non ) , g , b ( w non ) , x , mu) ;
171 i f c >= − t o l ;
172 stop = 1 ;
173 %d i s p ( ’ STOP : a l l i n a c t i v e c o n s t r a i n t s >= 0 ’ )
174 d i s p ( ’ S o l u t i o n f o u n d by d u a l a c t i v e s e t method , demo t e r m i n a t e d . ’ )
175 else
176 % we f i n d t h e l e a s t n e g a t i v e v a l u e o f c
177 c r = max ( c ( f i n d ( c < −s q r t ( e p s ) ) ) ) ;
178 r = f i n d ( c == c r ) ;
179 r = r (1) ;
180 end
181 it = i t + 1;
182 i f i t >= m a x i t r
183 d i s p ( ’ No . o r i t e r a t i o n s s t e p s e x e e d e d ( o u t e r l o o p ) . ’ )
184 s t o p = 3 ; % maximum no i t e r a t i o n s e x c e e d e d
185 end
186 % iterate . . .
187 it2 = 0;
188 s t o p 2 = max ( 0 , s t o p ) ;
189 w hi l e ˜ stop2
190 it2 = it2 + 1;
191 i f i t 2 >= m a x i t r
192 d i s p ( ’ No . o r i t e r a t i o n s s t e p s e x e e d e d ( i n n e r l o o p ) . ’ )
193 stop = 3 ;
194 stop2 = stop ;
195 end
196 % c a l l range / n u l l space procedure . . .
197 [ p , v ] = n u l l s p a c e d e m o (G, C ( : , w a c t ) ,−C ( : , r ) , z e r o s ( l e n g t h ( w a c t ) , 1 ) ) ;
198 i f isempty ( v )
199 v = [];
200 end
201 arp = C ( : , r ) ’∗p ;
202 i f a b s ( a r p ) <= t o l % l i n e a r d e p e n d e n c y
203 i f v >= 0 % s o l u t i o n d o e s n o t e x i s t
204 d i s p ( ’ Probl em i s i n f e a s i b l e , demo t e r m i n a t e d . ’ )
205 stop = 2 ;
206 stop2 = stop ;
207 else
208 t = inf ;
209 f or k = 1: length (v)
210 i f v(k) < 0
211 temp = −mu( w a c t ( k ) ) / v ( k ) ;
212 i f temp < t
213 t = temp ;
214 rem = k ;
215 end
216 end
217 end
218 mu( w a c t ) = mu( w a c t ) + t ∗v ;
219 mu( r ) = mu( r ) + t ;
220 w a c t = w a c t ( f i n d ( w a c t ˜= w a c t ( rem ) ) ) ;
221 end
222 else
223 % s t e p s i z e in dual space . . .
224 t1 = i n f ;
225 for k = 1: length (v)
226 i f v(k) < 0
227 temp = −mu( w a c t ( k ) ) / v ( k ) ;
228 i f temp < t 1
229 t 1 = temp ;
230 rem = k ;
231 end
232 end
233 end
234 % s t e p s i z e i n primal space . . .
235 t 2 = −c o n s t r a i n t s (G, C ( : , r ) , g , b ( r ) , x , mu) / a r p ;
236 i f t 2 <= t 1
237 x = x + t 2 ∗p ;
238 mu( w a c t ) = mu( w a c t ) + t 2 ∗v ;
239 mu( r ) = mu( r ) + t 2 ;
240 w act = [ w act r ] ;
241 else
242 x = x + t 1 ∗p ;
243 mu( w a c t ) = mu( w a c t ) + t 1 ∗v ;
244 mu( r ) = mu( r ) + t 1 ;
245 w a c t = w a c t ( f i n d ( w a c t ˜= w a c t ( rem ) ) ) ;
246 end
158 Matlab-code
247 end
248 c r = c o n s t r a i n t s (G, C ( : , r ) , g , b ( r ) , x , mu) ;
249 i f c r > −t o l
250 stop2 = 1 ; % l e a v e s the inner w h i l e−l o o p but does not stop the
algorithm
251 end
252 it draw = it draw + 1;
253 X( : , it d r aw ) = x ;
254 %p l o t . . .
255 i f ˜ stop
256 a c t i v e s e t p l o t (G, C ’ , g , b , X ( : , 1 : i t d r a w ) , mu, w a c t , [ − 4 8; −4 8] ,[20 20
50 100 t o l f t p ]) ;
257 d i s p ( ’ P r e s s any k ey to conti nue . . . ’ )
258 pause
259 end
260 end % w h i l e
261 end % w h i l e
262
263 f u n c t i o n [ x , mu ] = n u l l s p a c e d e m o (G, A , g , b )
264 % initialize ...
265 [ n m] = s i z e (A) ;
266 % QR f a c t o r i z a t i o n o f A s o t h a t A = [ Y Z ] ∗ [ R 0 ] ’ . . .
267 [ Q, R ] = q r (A) ; % m atl ab ’ s i m p l e m e n t a t i o n
268 Y = Q ( : , 1 : m) ;
269 Z = Q ( : ,m+ 1:n ) ;
270 R = R ( 1 : m, : ) ;
271 Zt = Z ’ ;
272 % S o l v e f o r t h e r a n g e s p a c e com ponent py . . .
273 py = R’ \ b ;
274 % Compute t h e r e d u c e d g r a d i e n t . . .
275 g z = Zt ∗ (G∗ (Y∗py ) + g ) ;
276 % Compute t h e r e d u c e d H e s s i a n and com pute i t s C h o l e s k y factorization . . .
277 Gz = Zt ∗G∗Z ;
278 L = c h o l ( Gz ) ’ ;
279 % S o l v e f o r t h e n u l l s p a c e com ponent pz . . .
280 pz = L\−g z ;
281 pz = L ’ \ pz ;
282 % Compute t h e s o l u t i o n . . .
283 x = Y∗py + Z∗ pz ;
284 % Compute t h e L a g r a n g e m u l t i p l i e r s . . .
285 mu = R\ (Y’ ∗ ( G∗x + g ) ) ;
286
287 f u n c t i o n f = o b j e c t i v e (G, A , g , b , x , mu)
288 f = x ’ ∗G∗x + g ’ ∗ x ;
289
290 f u n c t i o n c = c o n s t r a i n t s (G, A , g , b , x , mu)
291 c = A’ ∗ x − b ;
292
293 f u n c t i o n l = l a g r a n g i a n (G, A , g , b , x , mu)
294 L = o b j e c t i v e (G, A , g , b , x , mu) − mu’ ∗ c o n s t r a i n t s (G, A , g , b , x , mu) ;
112 i f w act
113 c o n s t p l o t ( @ c o n s t r a i n t s , G, A , g , b , mu, D , w a c t , m, o p t s , f s i z e )
114 end
115 t i t l e ( [ ’ x = ( ’ , num 2s tr ( x ( 1 , end ) , 2 ) , ’ , ’ , num 2s tr ( x ( 2 , end ) , 2 ) , ’ ) , f ( x ) =
’ , num 2s tr ( o b j e c t i v e (G, A, g , b , x ( : , end ) ,mu) , 2 ) , ’ , W a = [ ’ , i n t 2 s t r (
w a c t ) , ’ ] , \mu = [ ’ , num 2s tr (mu’ , 2 ) , ’ ] ’ ] , ’ F o n t S i z e ’ , f s i z e )
116 end
117 else
118 %i f isempty ( w act )
119 % w a c t = f i n d ( a b s (A ( 2 , : ) ’ ∗ x ( 2 ) + f e v a l ( @ c o n s t r a i n t s , G, A ( 1 , : ) , g , b , x ( 1 ) ,
mu) ) <= o p t s ( 5 ) ) ’ ; % A( 2 ) ∗ x2 + (A( 1 ) ∗ x1 − b ) <= e p s
120 %end
121 c o n s t p l o t ( @ c o n s t r a i n t s , G, A, g , b , mu, D , w a c t , m, o p t s , fsize )
122 t i t l e ( [ ’ x = ( ’ , num 2s tr ( x ( 1 , end ) , 2 ) , ’ , ’ , num 2s tr ( x ( 2 , end ) , 2 ) , ’ ) , f ( x ) = ’ ,
num 2s tr ( o b j e c t i v e (G, A , g , b , x ( : , end ) ,mu) , 2 ) , ’ , W a = [ ’ , i n t 2 s t r ( w act ) , ’
] , \mu = [ ’ , num 2s tr (mu’ , 2 ) , ’ ] ’ ] , ’ F o n t S i z e ’ , fsize )
123 end
124
125 % p l o t th e path . . .
126 pathplot (x)
127 hold o f f
128
129 f u n c t i o n c o n t p l o t ( fun , G, A , g , b , mu, D, o p t s )
130 [ X1 , X2 ] = m e s h g r i d ( l i n s p a c e (D( 1 , 1 ) ,D ( 1 , 2 ) , o p t s ( 1 ) ) , l i n s p a c e (D( 2 , 1 ) ,D( 2 , 2 ) , o p t s
(2) ) ) ;
131 F = z e r o s ( opts ( 1 : 2 ) ) ;
132 f o r i = 1: opts (1 )
133 f o r j = 1 : opts ( 2)
134 F ( i , j ) = norm ( f e v a l ( fun , G, A , g , b , [ X1 ( i , j ) ; X2 ( i , j ) ] , mu) , 2 ) ;
135 end
136 end
137 c o n t o u r ( X1 , X2 , F , o p t s ( 3 ) )
138
139 f u n c t i o n c o n s t p l o t ( fun , G, A , g , b , mu, D , w a c t , m, o p t s , f s i z e )
140 f c o l o r = [ . 4 . 4 . 4 ] ; f a l p h a = . 4 ; % c o l o r and a l p h a v a l u e s o f f a c e s m ark i ng
unfeasable region
141 b c o l o r = [ . 8 . 8 . 8 ] ; % b a c k g r o u n d c o l o r o f c o n s t r a i n t num beri ng
142 x1 = l i n s p a c e (D ( 1 , 1 ) ,D( 1 , 2 ) , o p t s ( 4 ) ) ;
143 x2 = l i n s p a c e (D ( 2 , 1 ) ,D( 2 , 2 ) , o p t s ( 4 ) ) ;
144 C = z e r o s (m, o p t s ( 4 ) ) ;
145 f o r j = 1: opts (4 )
146 f o r i = 1 :m
147 i f A( 2 , i ) % i f A( 2 ) ˜= 0
148 C( i , j ) = − f e v a l ( fun , G, A( 1 , i ) , g , b ( i ) , x1 ( j ) ,mu) /A( 2 , i ) ; % x2 = −(A( 1 )
∗ x1 − b ) /A( 2 )
149 else
150 C( i , j ) = b ( i ) /A( 1 , i ) ; % A( 2 ) = 0 => x1 = b/A( 1 ) , must be p l o t t e t
r e v e r s e l y a s (C( i , : ) , x2 )
151 end
152 end
153 end
154 f o r i = 1 :m
155 i f any ( i == w a c t )
156 l w i d t h = 1 ; c o l o r = [ 1 0 0 ] ; % l i n e w i d t h and c o l o r o f a c t i v e
constraints
157 else
158 l w i d t h = 1 ; c o l o r = [ 0 0 0 ] ; % l i n e w i d t h and c o l o r o f i n a c t i v e
constraints
159 end
160 i f A( 2 , i ) % i f A( 2 ) ˜= 0
161 i f A( 2 , i ) > 0 % i f A( 2 ) > 0
162 f i l l ( [ D( 1 , 1 ) D ( 1 , 2 ) D( 1 , 2 ) D( 1 , 1 ) ] , [ C( i , 1 ) C( i , end ) min (D( 2 , 1 ) ,C( i ,
end ) ) min (D( 2 , 1 ) ,C( i , 1 ) ) ] , f c o l o r , ’ FaceA l pha ’ , f a l p h a )
163 else
164 f i l l ( [ D( 1 , 1 ) D ( 1 , 2 ) D( 1 , 2 ) D( 1 , 1 ) ] , [ C( i , 1 ) C( i , end ) max (D( 2 , 2 ) ,C( i ,
end ) ) max (D( 2 , 2 ) ,C( i , 1 ) ) ] , f c o l o r , ’ FaceA l pha ’ , f a l p h a )
165 end
166 p l o t ( x1 , C( i , : ) , ’− ’ , ’ Li neWi dth ’ , l w i d t h , ’ C o l o r ’ , c o l o r )
167 i f C( i , 1 ) < D( 1 , 1 )% | C( i , 1 ) < D( 2 , 1 )
168 t e x t (− f e v a l ( fun , G, A( 2 , i ) , g , b ( i ) ,D ( 2 , 1 ) ,mu) /A( 1 , i ) ,D( 2 , 1 ) , i n t 2 s t r ( i )
, ’ C ol or ’ , ’ k ’ , ’ EdgeColor ’ , c o l o r , ’ BackgroundColor ’ , b col or , ’
F o n t S i z e ’ , f s i z e ) % x1 = −(A( 2 ) ∗ x2 − b ) /A( 1 )
169 else
170 i f C( i , 1 ) > D ( 2 , 2 )
171 t e x t (− f e v a l ( fun , G, A( 2 , i ) , g , b ( i ) ,D( 2 , 1 ) ,mu) /A( 1 , i ) ,D ( 2 , 1 ) ,
i n t 2 s t r ( i ) , ’ C ol or ’ , ’ k ’ , ’ EdgeColor ’ , c o l o r , ’ BackgroundColor ’
, b c o l o r , ’ F o n t S i z e ’ , f s i z e ) % x1 = −(A( 2 ) ∗ x2 − b ) /A( 1 )
172 else
173 t e x t (D( 1 , 1 ) ,C( i , 1 ) , i n t 2 s t r ( i ) , ’ C o l o r ’ , ’ k ’ , ’ E d g e C o l o r ’ , c o l o r , ’
BackgroundColor ’ , b col or , ’ F on tS i ze ’ , f s i z e )
174 end
175 end
176 else
177 i f A( 1 , i ) > 0 % i f A( 1 ) > 0
178 f i l l ( [ D( 1 , 1 ) C( i , 1 ) C( i , end ) D( 1 , 1 ) ] , [ D( 2 , 1 ) D( 2 , 1 ) D ( 2 , 2 ) D( 2 , 2 ) ] ,
f c o l o r , ’ FaceA l pha ’ , f a l p h a )
179 else
D.5 Demos 161
180 f i l l ( [ C( i , 1 ) D( 1 , 2 ) D ( 1 , 2 ) C( i , end ) ] , [ D( 2 , 1 ) D( 2 , 1 ) D ( 2 , 2 ) D( 2 , 2 ) ] ,
f c o l o r , ’ FaceA l pha ’ , f a l p h a )
181 end
182 p l o t (C( i , : ) , x2 , ’− ’ , ’ Li neWi dth ’ , l w i d t h , ’ C o l o r ’ , c o l o r )
183 t e x t (C( i , 1 ) ,D( 2 , 1 ) , i n t 2 s t r ( i ) , ’ C o l o r ’ , ’ k ’ , ’ E d g e C o l o r ’ , c o l o r , ’
BackgroundColor ’ , b col or , ’ F on tS i ze ’ , f s i z e )
184 end
185 end
186
187 function pathplot (x)
188 lwidth = 2 ; msize = 6 ;
189 p l o t ( x ( 1 , 1 ) , x ( 2 , 1 ) , ’ ob ’ , ’ Li neWi dth ’ , l w i d t h , ’ M a r k e r s i z e ’ , m s i z e ) % s t a r t i n g
position
190 p l o t ( x ( 1 , : ) , x ( 2 , : ) , ’ Li neWi dth ’ , l w i d t h ) % p a t h
191 p l o t ( x ( 1 , end ) , x ( 2 , end ) , ’ og ’ , ’ Li neWi dth ’ , l w i d t h , ’ M a r k e r s i z e ’ , m s i z e ) % c u r r e n t
position
192
193 f u n c t i o n f = o b j e c t i v e (G, A , g , b , x , mu)
194 f = 0 . 5 ∗ x ’ ∗G∗x + g ’ ∗ x ;
195
196 f u n c t i o n c = c o n s t r a i n t s (G, A, g , b , x , mu)
197 c = A’ ∗ x − b ;
198
199 f u n c t i o n l = l a g r a n g i a n (G, A , g , b , x , mu)
200 l = o b j e c t i v e (G, A , g , b , x , mu) − mu’ ∗ c o n s t r a i n t s (G, A, g , b , x , mu) ;
1 function q u a d t a n k d e m o ( t , N , r , F , dF , gam , w , pd )
2
3 % QUAD TANK DEMO D e m o n s t r a t i o n o f t h e q u a d r u p l e t a n k p r o c e s s . The w a t e r
4 % l e v e l s i n t a n k 1 and 2 a r e c o n t r o l l e d a c c o r d i n g t o t h e s e t p o i n t s . The
5 % h e i g h t s o f a l l f o u r t a n k s a r e 50 cm . The w o r k s p a c e i s s a v e d a s
6 % ’ q u a d r u p l e t a n k p r o c e s s . mat ’ i n c u r r e n t d i r e c t o r y , s o i t i s p o s s i b l e t o
7 % run t h e a n i m a t i o n a g a i n by c a l l i n g q u a d t a n k a n i m a t e w i t h o u t r e c o m p u t i n g
8 % the setup .
9 % NOTE: A new c a l l o f q u a d t a n k d e m o w i l l o v e r w r i t e t h e s a v e d w o r k s p a c e
10 % ’ q u a d r u p l e t a n k p r o c e s s . mat ’ . The f i l e must be d e l e t e d m a n u a l l y .
11 %
12 % Call
13 % q u a d t a n k d e m o ( t , N, r , F , dF , gam , pd )
14 %
15 % Input parameters
16 % t : [ min ] S i m u l a t i o n t i m e o f t a n k p r o c e s s . 1 <= t <= 3 0 . D e f a u l t i s
17 % 5 . The t i m e i s p l o t t e t a s s e c o n d s . The l a s t d i s c r e t e p o i n t i s n o t
18 % animated / p l o t t e t .
19 % N : D i s c r e t i z a t i o n o f t . 5 <= N <= 1 0 0 , must be an i n t e g e r . D e f a u l t
20 % i s 1 0 . Number o f v a r i a b l e s i s 6∗N and number o f c o n s t r a i n t s i s
21 % 24∗N .
22 % r : [ cm ] S e t p o i n t s o f t a n k 1 and 2 . 0 <= r ( i ) <= 5 0 . D e f a u l t i s
23 % [30 3 0] .
24 % F : [ l / min ] Max f l o w r a t e s o f pump 1 and 2 . 0 <= F ( i ) <= 1 0 0 0 .
25 % Default i s [500 50 0 ].
26 % dF : [ l / min ˆ 2 ] Min/max c h a n g e i n f l o w r a t e s o f pump 1 and 2 . −100 <=p o i n t
27 % dF ( i ) <= 1 0 0 . D e f a u l t i s [ pump1 pump2 ] = [ −50 50 −50 5 0 ] .
28 % gam : F r a c t i o n o f f l o w from pump 1 and 2 g o i n g d i r e c t l y t o t a n k 1 and
29 % 2 . 0 <= gam ( i ) <= 1 . D e f a u l t i s [ 0 . 4 5 0 . 4 0 ] .
30 % w : S e t t i n g p r i o r i t y o f c o n t r o l l i n g w a t e r l e v e l i n t a n k 1 and 2
31 % r e l a t i v e t o one a n o t h e r . 1 <= w( i ) <= 1 0 0 0 . D e f a u l t i s [ 1 1 ] .
32 % pd : 1 : U s i n g p r i m a l a c t i v e s e t method , 2 : U s i n g d u a l a c t i v e s e t
33 % method . D e f a u l t i s 2 .
34 % I f i n p u t p a r a m e t e r s a r e empty , t h e n d e f a u l t v a l u e s a r e u s e d .
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 % Check n a r g i n / n a r g o u t %
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39 e r r o r ( nargchk ( 7 , 8 , n argi n ) )
40 e r r o r ( nargoutchk ( 0 , 0 , nargout ) )
41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 % Check i n p u t %
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 % Check t . . .
45 i f isempty ( t )
46 t = 5 ∗ 6 0 ; % t = min∗ s e c
47 else
48 t = c h e c k i n p u t ( t , 1 , 3 0 , 1 ) ∗ 6 0 ; % t = min∗ s e c
49 end
50 % Check N . . .
51 i f i s e m p t y (N)
52 N = 10;
162 Matlab-code
53 else
54 i f mod (N, 1 )
55 e r r o r ( ’N must be an i n t e g e r . ’ )
56 end
57 N = c h e c k i n p u t (N, 5 , 1 0 0 , 1 ) ;
58 end
59 % Check r . . .
60 i f isempty ( r )
61 r = [30 30 ];
62 else
63 r = check input ( r ,0 ,50 , 2) ;
64 end
65 % Check F . . .
66 i f isempty (F)
67 F = [500 500] ;
68 else
69 F = c h e c k i n p u t (F , 0 , 1 0 0 0 , 2 ) ;
70 end
71 % Check dF . . .
72 i f i s e m p t y ( dF )
73 dF = [ −50 50 −50 5 0 ] ;
74 else
75 dF = c h e c k i n p u t ( dF , − 1 0 0 , 1 0 0 , 4 ) ;
76 end
77 % Check gam . . .
78 i f i s e m p t y ( gam )
79 gam = [ 0 . 4 5 0 . 4 ] ;
80 else
81 gam = c h e c k i n p u t ( gam , 0 , 1 , 2 ) ;
82 end
83 % Check w . . .
84 i f i s e m p t y (w)
85 w = [1 1 ] ;
86 else
87 w = c h e c k i n p u t (w, 1 , 1 0 0 0 , 2 ) ;
88 end
89 % Check pd . . .
90 i f n a r g i n < 8 | i s e m p t y ( pd )
91 pd = 2 ;
92 else
93 i f mod ( pd , 1 )
94 e r r o r ( ’ pd must be an i n t e g e r . ’ )
95 end
96 pd = c h e c k i n p u t ( pd , 1 , 2 , 1 ) ;
97 end
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99 % Startup i n f o %
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 i f N >= 30
102 c o n t = ’ do ’ ;
103 w h i l e ˜ strcmp ( l ow er ( con t ) , ’ y ’ ) & ˜ strcmp ( l ow er ( con t ) , ’ n ’ )
104 c o n t = i n p u t ( ’N >= 3 0 , s o c o m p u t a t i o n a l t i m e w i l l be s e v e r a l m i n u t e s ,
do you want t o c o n t i n u e ? y /n [ y ] : ’ , ’ s ’ ) ;
105 i f isempty ( con t )
106 cont = ’ y ’ ;
107 f p r i n t f ( ’ \b ’ )
108 disp ( ’y ’ )
109 end
110 %d i s p ( ’ ’ )
111 end
112 i f c o n t == ’ n ’
113 d i s p ( ’ S i m u l a t i o n t e r m i n a t e d by u s e r . ’ )
114 return
115 end
116 end
117 d i s p ( [ ’N = ’ , i n t 2 s t r (N) , ’ , s o number o f v a r i a b l e s i s ’ , i n t 2 s t r ( 6 ∗N) , ’ and
number o f c o n s t r a i n t s i s ’ , i n t 2 s t r ( 2 4 ∗ N) , ’ . ’ ] )
118 d i s p ( [ ’ t = ’ , num 2s tr ( t , 2 ) , ’ , gam = [ ’ , num 2s tr ( gam , 2 ) , ’ ] , w = [ ’ , num 2s tr (w , 2 ) , ’
]. ’ ])
119 d i s p ( [ ’ r = [ ’ , num 2s tr ( r ) , ’ ] , F = [ ’ , num 2s tr (F , 2 ) , ’ ] , dF = [ ’ , num 2s tr ( dF , 2 ) , ’ ] . ’
])
120 d i s p ( ’ Computing s i m u l a t i o n , p l e a s e w a i t . . . ’ )
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % S e t u p demo %
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % UI %
126 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 % physics . . .
128 g = 5 ; % g r a v i t y i s s m a l l due t o l i n e a r i z e d s y s t e m
129 % t i m e s pan and number o f s a m p l i n g p o i n t s . . .
130 t s p a n = [ 0 t ] ; %3 6 0 ] ;
131 % weights m atri ces . . .
132 Q = [ w ( 1 ) 0 ; 0 w ( 2 ) ] ; % w e i g h t m a t r i x , u s e d i n Q−norm , s e t t i n g p r i o r i t y o f h1
and h2 r e l a t i v e t o e a c h o t h e r
133 Hw = 1 e 6 ; % w e i g h i n g h1 and h2 (= Hw) i n r e l a t i o n t o h3 , h4 , u1 and u2 (= 1 )
134 % pump 1 . . .
D.5 Demos 163
302 % ##############################################################
303 % p l o t s o f quad−t a n k p r o c e s s
304 % ##############################################################
305 output = x ;
306 % making t . . .
307 t = tspan ( 1 ) : dt : tspan ( 2 ) ;
308 % making h , F , d f . . .
309 u0 = o u t p u t ( 1 : 2 ) ;
310 x1 = o u t p u t ( 3 : 6 ) ;
311 n u l 0 = B∗u0−I x ∗ x1+Ax0 ;
312 nul0 = nul0 ’ ∗ nul0 ;
313 x k = x1 ;
314 h = x k (1:2) ;
315 h e i g h t s ( : , 1 ) =x0 ;
316 h e i g h t s ( : , 2 ) =x k ;
317 f l o w ( : , 1 ) = u0m i nus 1 ;
318 f l o w ( : , 2 ) = u0 ;
319 f o r k = 1 : N−1
320 k s = 3+(k −1) ∗ ( nx+nu ) ;
321 u k = o u t p u t ( k s + 4: k s +5) ;
322 x k p l u s = o u t p u t ( k s + 6: k s +9) ;
323
324 n u l k = A∗ x k+B∗ u k−I x ∗ x k p l u s ;
325 nul k = nul k ’∗ nul k ;
326
327 x k = x k plus ;
328 % h = x k (1:2)
329 h e i g h t s ( : , k +2)=x k ;
330 f l o w ( : , k+2) = u k ;
331 end
332 dF = [ d i f f ( f l o w ( 1 , : ) ) ; d i f f ( f l o w ( 2 , : ) ) ] ;
333 % plot . . .
334 f s i z e = 10;
335 f i gu re (1) , c l f
336 subplot (4 ,2 ,1)
337 p l o t ( t , h e i g h t s ( 1 , : ) , ’−o ’ )
338 h o l d on
339 p l o t ( t , r ( 1 ) ∗ o n e s ( 1 , N+1) , ’ r ’ )
340 p l o t ( t , hmax1∗ o n e s ( 1 , N+1) , ’ k ’ )
341 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ h 1 [ cm ] ’ , ’ F o n t S i z e ’ , f s i z e )% , l e g e n d
( ’h 1 ’ , ’ r 1 ’)
342 a x i s ( [ tspan (1 ) tspan (2 ) 0 5 0 ] )
343 subplot (4 ,2 ,2)
344 p l o t ( t , h e i g h t s ( 2 , : ) , ’−o ’ )
345 h o l d on
346 p l o t ( t , r ( 2 ) ∗ o n e s ( 1 , N+1) , ’ r ’ )
347 p l o t ( t , hmax2∗ o n e s ( 1 , N+1) , ’ k ’ )
348 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ h 2 [ cm ] ’ , ’ F o n t S i z e ’ , f s i z e )% , l e g e n d
( ’h 2 ’ , ’ r 2 ’)
349 a x i s ( [ tspan (1 ) tspan (2 ) 0 5 0 ] )
350 subplot (4 ,2 ,3)
351 p l o t ( t , h e i g h t s ( 3 , : ) , ’−o ’ )
352 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ h 3 [ cm ] ’ , ’ F o n t S i z e ’ , f s i z e )
353 h o l d on
354 p l o t ( t , hmax3∗ o n e s ( 1 , N+1) , ’ k ’ )
355 a x i s ( [ tspan (1 ) tspan (2 ) 0 5 0 ] )
356 subplot (4 ,2 ,4)
357 p l o t ( t , h e i g h t s ( 4 , : ) , ’−o ’ )
358 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ h 4 [ cm ] ’ , ’ F o n t S i z e ’ , f s i z e )
359 h o l d on
360 p l o t ( t , hmax4∗ o n e s ( 1 , N+1) , ’ k ’ )
361 a x i s ( [ tspan (1 ) tspan (2 ) 0 5 0 ] )
362 subplot (4 ,2 ,5)
363 p l o t ( t ( 1 : end )−dt , f l o w ( 1 , : ) , ’−o ’ )
364 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ F 1 [ cmˆ3/ s ] ’ , ’ F o n t S i z e ’ , f s i z e )
365 a x i s ( [ t ( 1 )−d t t s p a n ( 2 ) 0 Fmax1 ] )
366 subplot (4 ,2 ,6)
367 p l o t ( t ( 1 : end )−dt , f l o w ( 2 , : ) , ’−o ’ )
368 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ F 2 [ cmˆ3/ s ] ’ , ’ F o n t S i z e ’ , f s i z e )
369 %s t a i r s ( f l o w ( 2 , : ) )
370 a x i s ( [ t ( 1 )−d t t s p a n ( 2 ) 0 Fmax2 ] )
371 subplot (4 ,2 ,7)
372 p l o t ( t ( 1 : end −1)−dt , dF ( 1 , : ) , ’−o ’ )
373 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ \ D e l t a F 1 [ cmˆ3/ s ˆ 2 ] ’ , ’ F o n t S i z e ’ ,
fsize )
374 a x i s ( [ t ( 1 )−d t t s p a n ( 2 ) dFmin1 dFmax1 ] )
375 subplot (4 ,2 ,8)
376 p l o t ( t ( 1 : end −1)−dt , dF ( 2 , : ) , ’−o ’ )
377 x l a b e l ( ’ t [ s ] ’ , ’ F o n t S i z e ’ , f s i z e ) , y l a b e l ( ’ \ D e l t a F 2 [ cmˆ3/ s ˆ 2 ] ’ , ’ F o n t S i z e ’ ,
fsize )
378 a x i s ( [ t ( 1 )−d t t s p a n ( 2 ) dFmin2 dFmax2 ] )
379 hold o f f
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
381 % Animate demo %
382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
383 save qu a dr up le t an k pr oc e ss
384 quad tank animate
166 Matlab-code
385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 % Auxilery function ( s ) %
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 function v = check input (v , l , u , n)
389 v = v (:) ’;
390 m = length (v) ;
391 i f m ˜= n
392 e r r o r ( [ num 2s tr ( i nputnam e ( 1 ) ) , ’ must be a v e c t o r o f l e n g t h ’ , i n t 2 s t r ( n ) , ’ . ’
])
393 end
394 for i = 1: n
395 i f isch ar (v( i ) ) | ˜ i s r e a l (v( i ) ) | i s i n f (v( i ) ) | isnan (v( i ) ) | v( i ) < l | u
< v( i )
396 e r r o r ( [ num 2s tr ( i nputnam e ( 1 ) ) , ’ ( ’ , i n t 2 s t r ( i ) , ’ ) must be i n r a n g e ’ ,
num 2s tr ( l ) , ’ <= v a l u e <= ’ , num 2s tr ( u ) , ’ . ’ ] )
397 end
398 end
1 % ##############################################################
2 % a n i m a t i o n o f quad−t a n k p r o c e s s
3 % ##############################################################
4 load ( ’ qua dr up le t a nk pr oc e ss ’ )
5 output = x ;
6 % making t . . .
7 t = tspan ( 1 ) : dt : tspan ( 2 ) ;
8 % making h , F , d f . . .
9 u0 = o u t p u t ( 1 : 2 ) ;
10 x1 = o u t p u t ( 3 : 6 ) ;
11 n u l 0 = B∗u0−I x ∗ x1+Ax0 ;
12 nul0 = nul0 ’ ∗ nul0 ;
13 x k = x1 ;
14 h = x k (1:2) ;
15 h e i g h t s ( : , 1 ) =x0 ;
16 h e i g h t s ( : , 2 ) =x k ;
17 f l o w ( : , 1 ) = u0m i nus 1 ;
18 f l o w ( : , 2 ) = u0 ;
19 f o r k = 1 : N−1
20 k s = 3+(k −1) ∗ ( nx+nu ) ;
21 u k = o u t p u t ( k s + 4: k s +5) ;
22 x k p l u s = o u t p u t ( k s + 6: k s +9) ;
23
24 n u l k = A∗ x k+B∗ u k−I x ∗ x k p l u s ;
25 nul k = nul k ’∗ nul k ;
26
27 x k = x k plus ;
28 % h = x k (1:2)
29 h e i g h t s ( : , k +2)=x k ;
30 f l o w ( : , k +2) = u k ;
31 end
32 dF = [ d i f f ( f l o w ( 1 , : ) ) ; d i f f ( f l o w ( 2 , : ) ) ] ;
33 f o r i = 1 : l e n g t h ( t )−1
34 f i gur e (2)
35 q u a d t a n k p l o t ( t ( i ) , h e i g h t s ( : , i ) , bmax , r , f l o w ( : , i ) , dF ( : , i ) , gam )
36 M( i ) = g e t f r a m e ;
37 end
38
39 % make m ov i e f o r p r e s e n t a t i o n . . .
40 r e p l y = i n p u t ( ’ Do you want t o make 4 t a n k d e m o m o v i e . a v i f i l e ? y /n [y]: ’ , ’s ’)
;
41 i f i s e m p t y ( r e p l y ) | r e p l y == ’ y ’
42 fp s = input ( ’ Sp e ci fy fp s in avi f i l e ? [ 1 5 ] : ’ ) ;
43 i f isempty ( f p s )
44 fps = 15;
45 end
46 d i s p ( ’ Making a v i f i l e , p l e a s e w a i t . . . ’ )
47 m o v i e 2 a v i (M, ’ 4 t a n k d e m o m o v i e . a v i ’ , ’ f p s ’ , f p s )
48 d i s p ( ’ F i n i s h e d making a v i f i l e . . . ’ )
49 end
SQP demo.m
D.5 Demos 167
84
85 p i i = p i 0 + a l p h a ∗ (mu−p i 0 ) ;
86
87 % h e r e t h e new ton s t e p i s p l o t t e d
88 i f plotNewtonStep
89 t s p a n = l i n s p a c e ( −1 ,3 ,100) ;
90 f o r i = 1: l e n g t h ( t s p a n )
91 x h a t = x0+t s p a n ( i ) ∗ d e l t a x ;
92 p i h a t = p i 0+t s p a n ( i ) ∗ (mu−p i 0 ) ;
93 na b la fx = c o s t s e n s ( x hat ) ;
94 n a b l a h x = m ods ens ( x h a t ) ;
95 y va l ( : , i ) = n a bl a f x − nabla hx ∗ pi ha t ;
96 end
97
98 subplot (1 ,3 ,1)
99 hold o f f
100 p l o t s c e n e ( @costfun ) ;
101 X temp = X ;
102 X temp ( : , i t r +1) = x ;%0+ d e l t a x ;
103 x f i n = x ;%x0+a l p h a ∗ d e l t a x ;
104 p a t h p l o t ( X temp )
105 t i t l e ( { [ ’ x { o l d } = ( ’ , num 2s tr ( x0 ( 1 ) ) , ’ , ’ , num 2s tr ( x0 ( 2 ) ) , ’ ) ˆT ’ ] ; [ ’ x
{new } = ( ’ , num 2s tr ( x f i n ( 1 ) ) , ’ , ’ , num 2s tr ( x f i n ( 2 ) ) , ’ ) ˆT ’ ] } , ’
FontSize ’ , f s i z e s m a l l ) ;
106 x l a b e l ( ’ x 1 ’ , ’ FontSize ’ , f s i z e ) , y l a b e l ( ’ x 2 ’ , ’ FontSize ’ , f s i z e )
107
108 subplot (1 ,3 ,2)
109 hold o f f
110 p lot ( t span , y va l ( 1 , : ) ) ;
111
112 nabla fx = c o s t s e n s ( x0 ) ;
113 nabla hx = m ods ens ( x0 ) ;
114 startPos = n ab la f x − nabla hx ∗pi0 ;
115 startPos y = startPos (1) ;
116 startPos x = 0;
117
118 endPos x = 1 ;
119 endPos y = 0 ;
120
121 h o l d on
122 p l o t ( [ s t a r t P o s x e n d P o s x ] , [ s t a r t P o s y e n d P o s y ] , ’ Li neWi dth ’ , 2 ) %
path
123 p l o t ( [ s t a r t P o s x a l p h a ] , [ s t a r t P o s y (1− a l p h a ) ∗ s t a r t P o s y ] , ’
Li neWi dth ’ , 2 , ’ c o l o r ’ , ’ r ’ ) % p a t h
124 p l o t ( [ t s p a n ( 1 ) t s p a n ( end ) ] , [ 0 0 ] , ’−− ’ ) % y=0
125
126 pi fin = p i 0+a l p h a ∗ (mu−p i 0 ) ;
127 nabla fx f in = costsens ( x fin ) ;
128 nabla hx f i n = m ods ens ( x f i n ) ;
129 endvalue = nabla fx f in − nabla hx fin ∗ p i f in ;
130
131 t i t l e ( { [ ’ F ( x 1 ) { o l d } = ’ , num 2s tr ( s t a r t P o s y ) ] ; [ ’ F ( x 1 ) {new } = ’ ,
num 2s tr ( e n d v a l u e ( 1 ) ) ] } , ’ F o n t S i z e ’ , f s i z e s m a l l ) ;
132 x l a b e l ( ’ \ alpha ’ , ’ FontSize ’ , f s i z e ) , y l a b e l ( ’ F 1 ’ , ’ FontSize ’ , f s i z e )
133
134 subplot (1 ,3 ,3)
135 hold o f f
136 p lot ( t span , y va l ( 2 , : ) ) ;
137 startPos y = startPos (2) ;
138 h o l d on
139 p l o t ( [ s t a r t P o s x e n d P o s x ] , [ s t a r t P o s y e n d P o s y ] , ’ Li neWi dth ’ , 2 ) %
path
140 p l o t ( [ s t a r t P o s x a l p h a ] , [ s t a r t P o s y (1− a l p h a ) ∗ s t a r t P o s y ] , ’
Li neWi dth ’ , 2 , ’ c o l o r ’ , ’ r ’ ) % p a t h
141 p l o t ( [ t s p a n ( 1 ) t s p a n ( end ) ] , [ 0 0 ] , ’−− ’ ) % y=0
142 t i t l e ( { [ ’ F ( x 2 ) { o l d } = ’ , num 2s tr ( s t a r t P o s y ) ] ; [ ’ F ( x 2 ) {new } = ’ ,
num 2s tr ( e n d v a l u e ( 2 ) ) ] } , ’ F o n t S i z e ’ , f s i z e s m a l l ) ;
143 x l a b e l ( ’ \ alpha ’ , ’ FontSize ’ , f s i z e ) , y l a b e l ( ’ F 2 ’ , ’ FontSize ’ , f s i z e )
144 end
145 n a b l a L 0 = c−A∗ p i i ;
146 c = costsens (x) ;
147 A = m ods ens ( x ) ;
148 n a b l a L = c−A∗ p i i ;
149 s = x − x0 ;
150 y = nabla L − nabla L0 ;
151 sy = s ’ ∗ y ;
152 sWs = s ’ ∗W∗ s ;
153 i f ( s y >= 0 . 2 ∗ sWs )
154 theta = 1;
155 else
156 t h e t a = ( 0 . 8 ∗ sWs ) / ( sWs−s y ) ;
157 end
158 Ws = W∗ s ;
159 sW = s ’ ∗W;
160 r = t h e t a ∗y+(1− t h e t a ) ∗Ws ;
161 W = W−(Ws∗sW) /sWs+( r ∗ r ’ ) / ( s ’ ∗ r ) ;
162 x0 = x ;
D.5 Demos 169
163 pi0 = p i i ;
164 f0 = f ;
165 g0 = g ;
166 end
167 end
168
169 function pathplot (x)
170 lwidth = 2 ; msize = 6 ; f s i z e = 12;
171 p l o t ( x ( 1 , 1 ) , x ( 2 , 1 ) , ’ ob ’ , ’ Li neWi dth ’ , l w i d t h , ’ M a r k e r s i z e ’ , m s i z e ) % s t a r t i n g
position
172 p l o t ( x ( 1 , : ) , x ( 2 , : ) , ’ Li neWi dth ’ , l w i d t h ) % p a t h
173 p l o t ( x ( 1 , : ) , x ( 2 , : ) , ’ ob ’ , ’ Li neWi dth ’ , l w i d t h , ’ M a r k e r s i z e ’ , m s i z e ) % p a t h
174 p l o t ( x ( 1 , end ) , x ( 2 , end ) , ’ og ’ , ’ Li neWi dth ’ , l w i d t h , ’ M a r k e r s i z e ’ , m s i z e ) % c u r r e n t
position
175 t i t l e ( [ ’ x = ( ’ , num 2s tr ( x ( 1 , end ) ’ ) , ’ , ’ , num 2s tr ( x ( 2 , end ) ) , ’ ) ’ ] , ’ F o n t S i z e ’ , f s i z e
)
176
177
178
179 function fx = costfun (x)
180 % The f u n c t i o n t o be m i n i m i z e d
181 f x = x ( 1 ) ∗ x ( 1 ) ∗x ( 1 ) ∗ x ( 1 )+x ( 2 ) ∗ x ( 2 ) ∗x ( 2 ) ∗ x ( 2 ) ; % : c o s t = ( X1 . ˆ 4 + X2 . ˆ 4 ) ;
182
183 f u n c t i o n dx = c o s t s e n s ( x )
184 % The g r a d i e n t o f t h e c o s t f u n c t i o n
185 dx =[4∗ x ( 1 ) ∗x ( 1 ) ∗x ( 1 ) ; 4∗ x ( 2 ) ∗ x ( 2 ) ∗x ( 2 ) ] ; % : gradient of ( X1 . ˆ 4 + X2 . ˆ 4 ) ;
186
187
188 f u n c t i o n f x = modfun ( x )
189 % The c o n s t r a i n t s
190 c 1 = −x ( 1 ) ˆ2+x ( 1 )+x ( 2 ) −1; % x2 >= x1 . ˆ 2 − x1 + 1
191 c 2 = −x ( 1 ) ˆ2+4∗x ( 1 )+x ( 2 ) −6; % x2 >= x1 ˆ2 − 4 x1 + 6
192 c 3 = −x ( 1 ) . ˆ2+ 3∗ x ( 1 )−x ( 2 ) + 2; % x2 <= −x1 ˆ2 + 3 x1 + 2
193 f x = [ c1 c2 c3 ] ’ ;
194
195 f u n c t i o n d f x = m ods ens ( x )
196 % gr ad i e nt of the c o n s t r a i n t s
197
198 dc1 = [ −2∗x ( 1 ) +1 1 ] ’ ; % x2 >= x1 . ˆ 2 − x1 + 1
199 dc2 = [ −2∗x ( 1 ) +4 1 ] ’ ; % x2 >= x1 ˆ2 − 4 x1 + 6
200 dc3 = [ −2∗x ( 1 ) +3 − 1 ] ’ ; % x2 <= −x1 ˆ2 + 3 x1 + 2
201 dfx = [ dc1 dc2 dc3 ] ;
202
203 f u n c t i o n H = h e s s i a n ( x , mu)
204
205 H e s s C t r 1 = [ −2 0; 0 0]; % x2 >= x1 . ˆ 2 − x1 + 1
206 H e s s C t r 2 = [ −2 0; 0 0]; % x2 >= x1 ˆ2 − 4 x1 + 6
207 H e s s C t r 3 = [ −2 0; 0 0]; % x2 <= −x1 ˆ2 + 3 x1 + 2
208
209 HessCost = [ 12∗ x ( 1 ) ∗x ( 1 ) 0;
210 0 12∗ x ( 2 ) ∗x ( 2 ) ] ; % : c o s t = ( X1 . ˆ 4 + X2 . ˆ 4 ) ;
211
212 H = H e s s C o s t −(mu( 1 ) ∗ H e s s C t r 1 ) −(mu( 2 ) ∗ H e s s C t r 2 ) −(mu( 3 ) ∗ H e s s C t r 3 ) ;
213
214 f un c ti on p l o t s c e n e ( costfun , v ar ar gi n )
215 f c o l o r = [ . 4 . 4 . 4 ] ; f a l p h a = . 4 ; % c o l o r and a l p h a v a l u e s o f f a c e s m ark i ng
unfeasable region
216 plot left = −5;
217 plot right = 5;
218 p l o t b u t t o m = −5;
219 plot top = 5;
220 p l o td et a i l s = 30;
221 l i n s p a c e d e t ai l s = 100;
222 contours = 10;
223 ctr1 = 1;
224 ctr2 = 1;
225 ctr3 = 0;
226 ctr4 = 0;
227 ctr5 = 1;
228 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
229 % c o n s t r a i n t s d e f i n e d f o r z=0
230 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
231 x ctr = linspace ( p lot lef t , plot right , l ins pace details ) ;
232 y c t r 1 = x c t r .ˆ2 − x c t r + 1; ctr1 geq = 1; % x2 >= x1 . ˆ 2 − x1 + 1
233 y c t r 2 = x c t r . ˆ 2 − 4∗ x c t r + 6 ; ctr2 geq = 1; % x2 >= x1 ˆ2 − 4 x1 + 6
234 y ctr3 = sin ( x ctr ) + 3; ctr3 geq = 0; % x2 <= s i n ( x1 ) + 3
235 y c t r 4 = cos ( x c t r ) + 2 ; ctr4 geq = 1; % x2 >= c o s ( x1 ) + 2
236
237 y c t r 5 = − x c t r . ˆ2+ 3∗ x c t r + 2; ctr5 geq = 0; % x2 <= −x1 ˆ2 + 3 x1 + 2
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % pl ot the c os t f u nc t i o n
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 delta = dist ( plo t le ft , p lot r ight )/ p lot de t ails ;
242 [ X1 , X2 ] = m e s h g r i d ( p l o t l e f t : d e l t a : p l o t r i g h t ) ; %c r e a t e a m a t r i x o f (X, Y) from
vector
243 f o r i = 1 : l e n g t h ( X1 )
244 f o r j = 1 : l e n g t h ( X2)
170 Matlab-code
add2mat.m
78 end
79 i f ˜ i s n u m e r i c ( i n i t n ) | | l e n g t h ( i n i t n ) ˜= 1
80 e r r o r ( ’INITATCOLUMN must be an i n t e g e r . ’ )
81 end
82 i f ˜ i s s t r ( addstyle )
83 e r r o r ( ’ADDSTYLE n o t d e f i n e d . ’ )
84 end
85 [ m1, n1 ] = s i z e ( m a t r i x 1 ) ;
86 [ m2, n2 ] = s i z e ( m a t r i x 2 ) ;
87 i f m2 > m1 | | n2 > n1
88 e r r o r ( [ ’MATRIX2 w i t h d i m e n s i o n ( s ) ’ , i n t 2 s t r (m2) , ’ x ’ , i n t 2 s t r ( n2 ) , ’ d o e s n o t
f i t i n s i d e MATRIX1 w i t h d i m e n s i o n ( s ) ’ . . .
89 , i n t 2 s t r (m1) , ’ x ’ , i n t 2 s t r ( n1 ) , ’ . ’ ] )
90 end
91 i f i n i t m > m1 | | i n i t n > n1
92 e r r o r ( [ ’ I n i t i a l poi nt ( ’ , i n t 2 s t r ( initm ) , ’ , ’ , i n t 2 s t r ( i n i t n ) , ’ ) ex ceeds
d i m e n s i o n ( s ) ’ , i n t 2 s t r (m1) , ’ x ’ , i n t 2 s t r ( n1 ) , . . .
93 ’ o f MATRIX1 . ’ ] )
94 end
95 i f i n i t m+m2−1 > m1 | | i n i t n +n2−1 > n1
96 e r r o r ( [ ’ With i n i t i a l p o i n t ( ’ , i n t 2 s t r ( i n i t m ) , ’ , ’ , i n t 2 s t r ( i n i t n ) , ’ ) ,
d i m e n s i o n ( s ) ’ , i n t 2 s t r (m2) , ’ x ’ , i n t 2 s t r ( n2 ) , . . .
97 ’ o f MATRIX2 e x c e e d s d i m e n s i o n ( s ) ’ , i n t 2 s t r (m1) , ’ x ’ , i n t 2 s t r ( n1 ) , ’
o f MATRIX1 . ’ ] )
98 end
99 switch a dds ty l e
100 c a s e ’ add ’
101 m a t r i x 1 ( i n i t m : i n i t m+m2−1 , i n i t n : i n i t n+n2 −1) = m a t r i x 1 ( i n i t m : i n i t m+m2−1 ,
i n i t n : i n i t n +n2 −1)+m a t r i x 2 ;
102 matrix3 = matrix1 ;
103 c a s e ’ s ub ’
104 m a t r i x 1 ( i n i t m : i n i t m+m2−1 , i n i t n : i n i t n+n2 −1) = m a t r i x 1 ( i n i t m : i n i t m+m2−1 ,
i n i t n : i n i t n +n2 −1)−m a t r i x 2 ;
105 matrix3 = matrix1 ;
106 c a s e ’ mul ’
107 m a t r i x 1 ( i n i t m : i n i t m+m2−1 , i n i t n : i n i t n+n2 −1) = m a t r i x 1 ( i n i t m : i n i t m+m2−1 ,
i n i t n : i n i t n +n2 −1) . ∗ m a t r i x 2 ;
108 matrix3 = matrix1 ;
109 case ’ div ’
110 m a t r i x 1 ( i n i t m : i n i t m+m2−1 , i n i t n : i n i t n+n2 −1) = m a t r i x 1 ( i n i t m : i n i t m+m2−1 ,
i n i t n : i n i t n +n2 −1) . / m a t r i x 2 ;
111 matrix3 = matrix1 ;
112 cas e ’ rep ’
113 m a t r i x 1 ( i n i t m : i n i t m+m2−1 , i n i t n : i n i t n+n2 −1) = m a t r i x 2 ;
114 matrix3 = matrix1 ;
115 end
1 f u n c t i o n [ a l p h a , x , f , g ] = l i n e s e a r c h a l g o r i t h m ( modfun , c o s t f u n , f 0 , g0 , c , x0 ,
d e l t a x , s i gm a , c1 , v a r a r g i n )
2
3 % LINE SEARCH ALGORITHM i m p l e m e n t e d a c c o r d i n g t o P o w e l l s l 1 −P e n a l t y
4 % function
5 %
6 % By : C a r s t e n V\ ¨ o l c k e r , s 9 6 1 5 7 2 & Es ben L u n d s a g e r Hansen , s 0 2 2 0 2 2 .
7 % Subject : N u m e r i c a l Methods f o r S e q u e n t i a l Q u a d r a t i c O p t i m i z a t i o n ,
8 % M a s t e r T h e s i s , IMM, DTU, DK−2800 Lyngby .
9 % S u p e r v i s o r : John B a g t e r p J ø r g e n s e n , A s s i s t a n t P r o f e s s o r & Per Grove
Thomsen , P r o f e s s o r .
10 % D ate : 08. february 2007.
11
12 n0 = s i gm a ’ ∗ a b s ( g0 ) ;
13 T0 = f 0+n0 ;
14 dT0 = c ’ ∗ d e l t a x −n0 ;
15 a l p h a 1 = 1 ;%a l p h a v a l ; % 1 ;
16
17 x = x0+a l p h a 1 ∗ d e l t a x ;
18 f = f e v a l ( costfun , x , v ara rg i n { :} ) ;
19 g = f e v a l ( modfun , x , v a r a r g i n { : } ) ;
20 T1 = f+s i gm a ’ ∗ a b s ( g ) ;
21
22 if T1 <= T0+c 1 ∗dT0
23 alpha = alpha1 ;
24 return
25 end
26
27 a l p h a m i n = dT0 / ( 2 ∗ ( T0+dT0−T1 ) ) ;
28 a l p h a 2 = max ( 0 . 1 ∗ a l p h a 1 , a l p h a m i n ) ; % s k a l 0.1 v æ re c1 i stedet f o r ??
29
D.6 Auxiliary Functions 173
30 x = x0+a l p h a 2 ∗ d e l t a x ;
31 f = f e v a l ( costfun , x , va ra rg in {: } ) ;
32 g = f e v a l ( modfun , x , v a r a r g i n { : } ) ;
33 T2 = f+s i gm a ’ ∗ a b s ( g ) ;
34
35 if T2 <= T0+c 1 ∗ a l p h a 2 ∗dT0
36 alpha = alpha2 ;
37 return
38 end
39
40 stop = 0;
41 max itr = 100;
42 it r = 0;
43 w hi l e ˜ stop
44 i t r = i t r + 1;
45 i f i t r > max itr
46 disp ( ’ l i ne s ear ch ( i t r > mat itr ) ’ ) ;
47 stop = 1 ;
48 end
49
50 ab = 1 / ( a l p h a 1 −a l p h a 2 ) ∗ [ 1 / ( a l p h a 1 ∗ a l p h a 1 ) −1/( a l p h a 2 ∗ a l p h a 2 ) ;− a l p h a 2 / (
a l p h a 1 ∗ a l p h a 1 ) a l p h a 1 / ( a l p h a 2 ∗ a l p h a 2 ) ] ∗ [ T1−dT0∗ a l p h a 1−T0 ; T2−dT0∗
a l p h a 2 −T0 ] ;
51 a = ab ( 1 ) ;
52 b = ab ( 2 ) ;
53 i f ( a b s ( a )<e p s )
54 a l p h a m i n = −dT0/b ;
55 else
56 a l p h a m i n = (−b+( s q r t ( b∗b−3∗ a ∗dT0 ) ) ) /3∗ a ;
57 end
58
59 if ( a l p h a m i n <= 0 . 1 ∗ a l p h a 2 )
60 alpha = 0.1∗ alpha2 ;
61 else
62 if ( a l p h a m i n >= 0 . 5 ∗ a l p h a 2 )
63 alpha = 0.5∗ alpha2 ;
64 else
65 alpha = alpha min ;
66 end
67 end
68
69 x = x0+a l p h a ∗ d e l t a x ;
70 f = f e v a l ( costfun , x , v ar ar gi n { : }) ;
71 g = f e v a l ( modfun , x , v a r a r g i n { : } ) ;
72 T a l p h a = f+s i gm a ’ ∗ a b s ( g ) ;
73
74 i f T a l p h a <= T0+c 1 ∗ a l p h a ∗dT0
75 return
76 end
77 alpha1 = alpha2 ;
78 alpha2 = alpha ;
79 T1 = T2 ;
80 T2 = T a l p h a ;
81 end
174 Matlab-code