forked from peterkty/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathlcp.m
173 lines (139 loc) · 4.06 KB
/
pathlcp.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
function [z,mu] = pathlcp(M,q,l,u,z,A,b,t,mu)
% pathlcp(M,q,l,u,z,A,b,t,mu)
%
% Solve the standard linear complementarity problem using PATH:
% z >= 0, Mz + q >= 0, z'*(Mz + q) = 0
%
% Required input:
% M(n,n) - matrix
% q(n) - vector
%
% Output:
% z(n) - solution
% mu(m) - multipliers (if polyhedral constraints are present)
%
% Optional input:
% l(n) - lower bounds default: zero
% u(n) - upper bounds default: infinity
% z(n) - starting point default: zero
% A(m,n) - polyhedral constraint matrix default: empty
% b(m) - polyhedral right-hand side default: empty
% t(m) - type of polyhedral constraint default: 1
% < 0: less than or equal
% 0: equation
% > 0: greater than or equal
% mu(m) - starting value for multipliers default: zero
%
% The optional lower and upper bounds are used to define a linear mixed
% complementarity problem (box constrained variational inequality).
% l <= z <= u
% where l_i < z_i < u_i => (Mz + q)_i = 0
% l_i = z => (Mz + q)_i >= 0
% u_i = z => (Mz + q)_i <= 0
%
% The optional constraints are used to define a polyhedrally constrained
% variational inequality. These are transformed internally to a standard
% mixed complementarity problem. The polyhedral constraints are of the
% form
% Ax ? b
% where ? can be <=, =, or >= depending on the type specified for each
% constraint.
Big = 1e20;
if (nargin < 2)
error('PathLCP:BadInputs','two input arguments required for lcp(M, q)');
end
if (~issparse(M))
M = sparse(M); % Make sure M is sparse
end
q = full(q(:)); % Make sure q is a column vector
[mm,mn] = size(M); % Get the size of the inputs
n = length(q);
if (mm ~= mn | mm ~= n)
error('PathLCP:BadInputs','dimensions of M and q must match');
end
if (n == 0)
error('PathLCP:BadInputs','empty model');
end
if (nargin < 3 | isempty(l))
l = zeros(n,1);
end
if (nargin < 4 | isempty(u))
u = Big*ones(n,1);
end
if (nargin < 5 | isempty(z))
z = zeros(n,1);
end
z = full(z(:)); l = full(l(:)); u = full(u(:));
if (length(z) ~= n | length(l) ~= n | length(u) ~= n)
error('PathLCP:BadInputs','Input arguments are of incompatible sizes');
end
l = max(l,-Big*ones(n,1));
u = min(u,Big*ones(n,1));
z = min(max(z,l),u);
m = 0;
if (nargin > 5)
if (nargin < 7)
error('PathLCP:BadConstraints','Polyhedral constraints require A and b');
end
if (~issparse(A))
A = sparse(A);
end
b = full(b(:));
m = length(b);
if (m > 0)
[am, an] = size(A);
if (am ~= m | an ~= n)
error('PathLCP:BadConstraints','Polyhedral constraints of incompatible sizes');
end
if (nargin < 8 | isempty(t))
t = ones(m,1);
end
if (nargin < 9 | isempty(mu))
mu = zeros(m,1);
end
t = full(t(:)); mu = full(mu(:));
if (length(t) ~= m | length(mu) ~= m)
error('PathLCP:BadConstraints','Polyhedral input arguments are of incompatible sizes');
end
l_p = -Big*ones(m,1);
u_p = Big*ones(m,1);
idx = find(t > 0);
if (length(idx) > 0)
l_p(idx) = zeros(length(idx),1);
end
idx = find(t < 0);
if (length(idx) > 0)
u_p(idx) = zeros(length(idx),1);
end
mu = min(max(mu,l_p),u_p);
M = [M -A'; A sparse(m,m)];
q = [q; -b];
z = [z; mu];
l = [l; l_p];
u = [u; u_p];
else
if (nargin >= 9 & ~isempty(mu))
error('PathLCP:BadConstraints','No polyhedral constraints -- multipliers set.');
end
if (nargin >= 8 & ~isempty(t))
error('PathLCP:BadConstraints','No polyhedral constraints -- equation types set.');
end
end
end
idx = find(l > u);
if length(idx) > 0
error('PathLCP:BadConstraints','Bounds infeasible.');
end
nnzJ = nnz(M);
[status, ttime] = lcppath(n+m, nnzJ, z, l, u, M, q);
if (status ~= 1)
status,
type logfile.tmp
error('PathLCP:FailedToSolve','Path fails to solve problem');
end
mu = [];
if (m > 0)
mu = z(n+1:n+m);
z = z(1:n);
end
return;