-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcutestmodel.m
185 lines (161 loc) · 4.76 KB
/
cutestmodel.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
173
174
175
176
177
178
179
180
181
182
183
184
185
classdef cutestmodel < model.nlpmodel
%CUTESTMODEL nlpmodel interface for https://github.com/optimizers/cutest-mirror
properties
sparse % flag indicates is dense of sparse model
end
methods
function self = cutestmodel(dirname, sparse)
%CUTESTMODEL Constructor
%
% Inputs:
% dirname directory of cutest problem
% sparse use sparse matrices?
% Construct handle to either sparse or dense interface.
if nargin < 2 || isempty(sparse)
sparse = true;
end
cd(dirname);
% Terminate previous cutest session
try
p = cutest_setup();
catch ME
terminate_msg = 'cutest_setup: cutest_terminate must be called first';
msg_len = length(terminate_msg);
if strcmp(ME.message(1:msg_len), terminate_msg)
cutest_terminate
p = cutest_setup();
else
rethrow(ME);
end
end
x0 = p.x;
p.bl(p.bl < -1e10) = -Inf;
p.cl(p.cl < -1e10) = -Inf;
p.bu(p.bu > 1e10) = Inf;
p.cu(p.cu > 1e10) = Inf;
% Instantiate the base class.
self = [email protected](p.name, x0, p.cl, p.cu, p.bl, p.bu);
% Record sparsity flag
self.sparse = sparse;
% Evaluate constraint and Jacobian at x0.
if self.m > 0
if sparse
[c, J] = cutest_scons(x0);
else
[c, J] = cutest_cons(x0);
end
else
c = [];
J = [];
end
% Jacobian sparsity pattern.
if issparse(J)
self.Jpattern = spones(J);
else
self.Jpattern = ones(size(J));
end
% Hessian sparsity pattern.
y = ones(size(c));
if sparse
H = cutest_sphess(x0, y);
self.Hpattern = spones(H);
else
H = cutest_hess(x0, y);
self.Hpattern = ones(size(H));
end
% Categorize nonlinear constraints. Ampl orders nonlinear
% constraints first.
self.linear = p.linear;
end
function f = fobj_local(~, x)
f = cutest_obj(x);
end
function g = gobj_local(~, x)
g = cutest_grad(x);
end
function H = hobj_local(self, x)
if self.sparse
H = cutest_isphess(x, 0);
else
H = cutest_ihess(x, 0);
end
end
function c = fcon_local(self, x)
if self.m > 0
c = cutest_cons(x);
else
c = [];
end
end
function J = gcon_local(self, x)
if self.m > 0
if self.sparse
[~,J] = cutest_scons(x);
else
[~,J] = cutest_cons(x);
end
else
J = zeros(0, self.n);
end
end
function [Jprod, Jtprod] = gconprod_local(self, x)
if self.m > 0
Jprod = @(v) cutest_jprod(x,v);
Jtprod = @(v) cutest_jtprod(x,v);
else
Jprod = @(v) zeros(0,1);
Jtprod = @(v) zeros(self.n,1);
end
end
function HC = hcon_local(self, x, y)
if self.m > 0
if self.sparse
HC = cutest_sphess(x, y);
HC = HC - self.hobj(x);
else
HC = cutest_hess(x, y);
HC = HC - self.hobj(x);
end
else
HC = sparse(self.n, self.n);
end
end
function Hv = hconprod_local(self, x, y, v)
if self.m > 0
Hv = cutest_hprod(x, y, v);
Hv = Hv - cutest_hprod(x, zeros(self.m,1), v);
else
Hv = zeros(self.n,1);
end
end
function Hv = hlagprod_local(self, x, y, v)
if self.m > 0
Hv = cutest_hprod(x, -y, v);
else
Hv = cutest_hprod(x, v);
end
end
function HL = hlag_local(self, x, y)
if self.m > 0
if self.sparse
HL = cutest_sphess(x, -y);
else
HL = cutest_hess(x, -y);
end
else
if self.sparse
HL = cutest_sphess(x);
else
HL = cutest_hess(x);
end
end
end
function gHiv = ghivprod_local(self, x, g, v)
% Warning: super slow!
gHiv = zeros(self.m,1);
for i=1:self.m
gHiv(i) = g'*(cutest_isphess(x,i)*v);
end
end
end
end % classdef