-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathClpHelperFunctions.hpp
305 lines (268 loc) · 9.28 KB
/
ClpHelperFunctions.hpp
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* $Id$ */
// Copyright (C) 2003, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#ifndef ClpHelperFunctions_H
#define ClpHelperFunctions_H
#include "ClpConfig.h"
#ifdef HAVE_CMATH
#include <cmath>
#else
#ifdef HAVE_MATH_H
#include <math.h>
#else
#error "don't have header file for math"
#endif
#endif
/**
Note (JJF) I have added some operations on arrays even though they may
duplicate CoinDenseVector. I think the use of templates was a mistake
as I don't think inline generic code can take as much advantage of
parallelism or machine architectures or memory hierarchies.
*/
double maximumAbsElement(const double *region, int size);
void setElements(double *region, int size, double value);
void multiplyAdd(const double *region1, int size, double multiplier1,
double *region2, double multiplier2);
double innerProduct(const double *region1, int size, const double *region2);
void getNorms(const double *region, int size, double &norm1, double &norm2);
#if COIN_LONG_WORK
// For long double versions
CoinWorkDouble maximumAbsElement(const CoinWorkDouble *region, int size);
void setElements(CoinWorkDouble *region, int size, CoinWorkDouble value);
void multiplyAdd(const CoinWorkDouble *region1, int size, CoinWorkDouble multiplier1,
CoinWorkDouble *region2, CoinWorkDouble multiplier2);
CoinWorkDouble innerProduct(const CoinWorkDouble *region1, int size, const CoinWorkDouble *region2);
void getNorms(const CoinWorkDouble *region, int size, CoinWorkDouble &norm1, CoinWorkDouble &norm2);
inline void
CoinMemcpyN(const double *from, const int size, CoinWorkDouble *to)
{
for (int i = 0; i < size; i++)
to[i] = from[i];
}
inline void
CoinMemcpyN(const CoinWorkDouble *from, const int size, double *to)
{
for (int i = 0; i < size; i++)
to[i] = static_cast< double >(from[i]);
}
inline CoinWorkDouble
CoinMax(const CoinWorkDouble x1, const double x2)
{
return (x1 > x2) ? x1 : x2;
}
inline CoinWorkDouble
CoinMax(double x1, const CoinWorkDouble x2)
{
return (x1 > x2) ? x1 : x2;
}
inline CoinWorkDouble
CoinMin(const CoinWorkDouble x1, const double x2)
{
return (x1 < x2) ? x1 : x2;
}
inline CoinWorkDouble
CoinMin(double x1, const CoinWorkDouble x2)
{
return (x1 < x2) ? x1 : x2;
}
inline CoinWorkDouble CoinSqrt(CoinWorkDouble x)
{
return sqrtl(x);
}
#else
inline double CoinSqrt(double x)
{
return sqrt(x);
}
#endif
/// Trace
#ifdef NDEBUG
#define ClpTraceDebug(expression) \
{ \
}
#else
void ClpTracePrint(std::string fileName, std::string message, int line);
#define ClpTraceDebug(expression) \
{ \
if (!(expression)) { \
ClpTracePrint(__FILE__, __STRING(expression), __LINE__); \
} \
}
#endif
/// Following only included if ClpPdco defined
#ifdef ClpPdco_H
inline double pdxxxmerit(int nlow, int nupp, int *low, int *upp, CoinDenseVector< double > &r1,
CoinDenseVector< double > &r2, CoinDenseVector< double > &rL,
CoinDenseVector< double > &rU, CoinDenseVector< double > &cL,
CoinDenseVector< double > &cU)
{
// Evaluate the merit function for Newton's method.
// It is the 2-norm of the three sets of residuals.
double sum1, sum2;
CoinDenseVector< double > f(6);
f[0] = r1.twoNorm();
f[1] = r2.twoNorm();
sum1 = sum2 = 0.0;
for (int k = 0; k < nlow; k++) {
sum1 += rL[low[k]] * rL[low[k]];
sum2 += cL[low[k]] * cL[low[k]];
}
f[2] = sqrt(sum1);
f[4] = sqrt(sum2);
sum1 = sum2 = 0.0;
for (int k = 0; k < nupp; k++) {
sum1 += rL[upp[k]] * rL[upp[k]];
sum2 += cL[upp[k]] * cL[upp[k]];
}
f[3] = sqrt(sum1);
f[5] = sqrt(sum2);
return f.twoNorm();
}
//-----------------------------------------------------------------------
// End private function pdxxxmerit
//-----------------------------------------------------------------------
//function [r1,r2,rL,rU,Pinf,Dinf] = ...
// pdxxxresid1( Aname,fix,low,upp, ...
// b,bl,bu,d1,d2,grad,rL,rU,x,x1,x2,y,z1,z2 )
inline void pdxxxresid1(ClpPdco *model, const int nlow, const int nupp, const int nfix,
int *low, int *upp, int *fix,
CoinDenseVector< double > &b, double *bl, double *bu, double d1, double d2,
CoinDenseVector< double > &grad, CoinDenseVector< double > &rL,
CoinDenseVector< double > &rU, CoinDenseVector< double > &x,
CoinDenseVector< double > &x1, CoinDenseVector< double > &x2,
CoinDenseVector< double > &y, CoinDenseVector< double > &z1,
CoinDenseVector< double > &z2, CoinDenseVector< double > &r1,
CoinDenseVector< double > &r2, double *Pinf, double *Dinf)
{
// Form residuals for the primal and dual equations.
// rL, rU are output, but we input them as full vectors
// initialized (permanently) with any relevant zeros.
// Get some element pointers for efficiency
double *x_elts = x.getElements();
double *r2_elts = r2.getElements();
for (int k = 0; k < nfix; k++)
x_elts[fix[k]] = 0;
r1.clear();
r2.clear();
model->matVecMult(1, r1, x);
model->matVecMult(2, r2, y);
for (int k = 0; k < nfix; k++)
r2_elts[fix[k]] = 0;
r1 = b - r1 - d2 * d2 * y;
r2 = grad - r2 - z1; // grad includes d1*d1*x
if (nupp > 0)
r2 = r2 + z2;
for (int k = 0; k < nlow; k++)
rL[low[k]] = bl[low[k]] - x[low[k]] + x1[low[k]];
for (int k = 0; k < nupp; k++)
rU[upp[k]] = -bu[upp[k]] + x[upp[k]] + x2[upp[k]];
double normL = 0.0;
double normU = 0.0;
for (int k = 0; k < nlow; k++)
if (rL[low[k]] > normL)
normL = rL[low[k]];
for (int k = 0; k < nupp; k++)
if (rU[upp[k]] > normU)
normU = rU[upp[k]];
*Pinf = CoinMax(normL, normU);
*Pinf = CoinMax(r1.infNorm(), *Pinf);
*Dinf = r2.infNorm();
*Pinf = CoinMax(*Pinf, 1e-99);
*Dinf = CoinMax(*Dinf, 1e-99);
}
//-----------------------------------------------------------------------
// End private function pdxxxresid1
//-----------------------------------------------------------------------
//function [cL,cU,center,Cinf,Cinf0] = ...
// pdxxxresid2( mu,low,upp,cL,cU,x1,x2,z1,z2 )
inline void pdxxxresid2(double mu, int nlow, int nupp, int *low, int *upp,
CoinDenseVector< double > &cL, CoinDenseVector< double > &cU,
CoinDenseVector< double > &x1, CoinDenseVector< double > &x2,
CoinDenseVector< double > &z1, CoinDenseVector< double > &z2,
double *center, double *Cinf, double *Cinf0)
{
// Form residuals for the complementarity equations.
// cL, cU are output, but we input them as full vectors
// initialized (permanently) with any relevant zeros.
// Cinf is the complementarity residual for X1 z1 = mu e, etc.
// Cinf0 is the same for mu=0 (i.e., for the original problem).
double maxXz = -1e20;
double minXz = 1e20;
double *x1_elts = x1.getElements();
double *z1_elts = z1.getElements();
double *cL_elts = cL.getElements();
for (int k = 0; k < nlow; k++) {
double x1z1 = x1_elts[low[k]] * z1_elts[low[k]];
cL_elts[low[k]] = mu - x1z1;
if (x1z1 > maxXz)
maxXz = x1z1;
if (x1z1 < minXz)
minXz = x1z1;
}
double *x2_elts = x2.getElements();
double *z2_elts = z2.getElements();
double *cU_elts = cU.getElements();
for (int k = 0; k < nupp; k++) {
double x2z2 = x2_elts[upp[k]] * z2_elts[upp[k]];
cU_elts[upp[k]] = mu - x2z2;
if (x2z2 > maxXz)
maxXz = x2z2;
if (x2z2 < minXz)
minXz = x2z2;
}
maxXz = CoinMax(maxXz, 1e-99);
minXz = CoinMax(minXz, 1e-99);
*center = maxXz / minXz;
double normL = 0.0;
double normU = 0.0;
for (int k = 0; k < nlow; k++)
if (cL_elts[low[k]] > normL)
normL = cL_elts[low[k]];
for (int k = 0; k < nupp; k++)
if (cU_elts[upp[k]] > normU)
normU = cU_elts[upp[k]];
*Cinf = CoinMax(normL, normU);
*Cinf0 = maxXz;
}
//-----------------------------------------------------------------------
// End private function pdxxxresid2
//-----------------------------------------------------------------------
inline double pdxxxstep(CoinDenseVector< double > &x, CoinDenseVector< double > &dx)
{
// Assumes x > 0.
// Finds the maximum step such that x + step*dx >= 0.
double step = 1e+20;
int n = x.size();
double *x_elts = x.getElements();
double *dx_elts = dx.getElements();
for (int k = 0; k < n; k++)
if (dx_elts[k] < 0)
if ((x_elts[k] / (-dx_elts[k])) < step)
step = x_elts[k] / (-dx_elts[k]);
return step;
}
//-----------------------------------------------------------------------
// End private function pdxxxstep
//-----------------------------------------------------------------------
inline double pdxxxstep(int nset, int *set, CoinDenseVector< double > &x, CoinDenseVector< double > &dx)
{
// Assumes x > 0.
// Finds the maximum step such that x + step*dx >= 0.
double step = 1e+20;
int n = x.size();
double *x_elts = x.getElements();
double *dx_elts = dx.getElements();
for (int k = 0; k < n; k++)
if (dx_elts[k] < 0)
if ((x_elts[k] / (-dx_elts[k])) < step)
step = x_elts[k] / (-dx_elts[k]);
return step;
}
//-----------------------------------------------------------------------
// End private function pdxxxstep
//-----------------------------------------------------------------------
#endif
#endif
/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/