source: trunk/Clp/src/ClpHelperFunctions.hpp @ 1402

Last change on this file since 1402 was 1376, checked in by forrest, 10 years ago

chnages for speed and alternate factorization

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1/* $Id: ClpHelperFunctions.hpp 1376 2009-07-02 16:26:49Z forrest $ */
2// Copyright (C) 2003, International Business Machines
3// Corporation and others.  All Rights Reserved.
4#ifndef ClpHelperFunctions_H
5#define ClpHelperFunctions_H
6
7/**
8    Note (JJF) I have added some operations on arrays even though they may
9    duplicate CoinDenseVector.  I think the use of templates was a mistake
10    as I don't think inline generic code can take as much advantage of
11    parallelism or machine architectures or memory hierarchies.
12
13*/
14
15double maximumAbsElement(const double * region, int size);
16void setElements(double * region, int size, double value);
17void multiplyAdd(const double * region1, int size, double multiplier1,
18                 double * region2, double multiplier2);
19double innerProduct(const double * region1, int size, const double * region2);
20void getNorms(const double * region, int size, double & norm1, double & norm2);
21#if COIN_LONG_WORK
22  // For long double versions
23CoinWorkDouble maximumAbsElement(const CoinWorkDouble * region, int size);
24void setElements(CoinWorkDouble * region, int size, CoinWorkDouble value);
25void multiplyAdd(const CoinWorkDouble * region1, int size, CoinWorkDouble multiplier1,
26                 CoinWorkDouble * region2, CoinWorkDouble multiplier2);
27CoinWorkDouble innerProduct(const CoinWorkDouble * region1, int size, const CoinWorkDouble * region2);
28void getNorms(const CoinWorkDouble * region, int size, CoinWorkDouble & norm1, CoinWorkDouble & norm2);
29inline void
30CoinMemcpyN(const double * from, const int size, CoinWorkDouble * to)
31{
32  for (int i=0;i<size;i++)
33    to[i]=from[i];
34}
35inline void
36CoinMemcpyN(const CoinWorkDouble * from, const int size, double * to)
37{
38  for (int i=0;i<size;i++)
39    to[i]=static_cast<double>(from[i]);
40}
41inline CoinWorkDouble
42CoinMax(const CoinWorkDouble x1, const double x2)
43{
44    return (x1 > x2) ? x1 : x2;
45}
46inline CoinWorkDouble
47CoinMax(double x1, const CoinWorkDouble x2)
48{
49    return (x1 > x2) ? x1 : x2;
50}
51inline CoinWorkDouble
52CoinMin(const CoinWorkDouble x1, const double x2)
53{
54    return (x1 < x2) ? x1 : x2;
55}
56inline CoinWorkDouble
57CoinMin(double x1, const CoinWorkDouble x2)
58{
59    return (x1 < x2) ? x1 : x2;
60}
61inline CoinWorkDouble CoinSqrt(CoinWorkDouble x)
62{
63  return sqrtl(x);
64}
65#else
66inline double CoinSqrt(double x)
67{
68  return sqrt(x);
69}
70#endif
71
72/// Following only included if ClpPdco defined
73#ifdef ClpPdco_H
74
75
76inline double pdxxxmerit(int nlow, int nupp, int *low, int *upp, CoinDenseVector <double> &r1,
77                CoinDenseVector <double> &r2, CoinDenseVector <double> &rL,
78                CoinDenseVector <double> &rU, CoinDenseVector <double> &cL,
79                CoinDenseVector <double> &cU ){
80
81// Evaluate the merit function for Newton's method.
82// It is the 2-norm of the three sets of residuals.
83  double sum1, sum2;
84  CoinDenseVector <double> f(6);
85  f[0] = r1.twoNorm();
86  f[1] = r2.twoNorm();
87  sum1 = sum2 = 0.0;
88  for (int k=0; k<nlow; k++){
89    sum1 += rL[low[k]]*rL[low[k]];
90    sum2 += cL[low[k]]*cL[low[k]];
91  }
92  f[2] = sqrt(sum1);
93  f[4] = sqrt(sum2);
94  sum1 = sum2 = 0.0;
95  for (int k=0; k<nupp; k++){
96    sum1 += rL[upp[k]]*rL[upp[k]];
97    sum2 += cL[upp[k]]*cL[upp[k]];
98  }
99  f[3] = sqrt(sum1);
100  f[5] = sqrt(sum2);
101
102  return f.twoNorm();
103}
104
105//-----------------------------------------------------------------------
106// End private function pdxxxmerit
107//-----------------------------------------------------------------------
108
109
110//function [r1,r2,rL,rU,Pinf,Dinf] =    ...
111//      pdxxxresid1( Aname,fix,low,upp, ...
112//                   b,bl,bu,d1,d2,grad,rL,rU,x,x1,x2,y,z1,z2 )
113
114inline void pdxxxresid1(ClpPdco *model, const int nlow, const int nupp, const int nfix,
115                 int *low, int *upp, int *fix,
116                 CoinDenseVector <double> &b, double *bl, double *bu, double d1, double d2,
117                 CoinDenseVector <double> &grad, CoinDenseVector <double> &rL,
118                 CoinDenseVector <double> &rU, CoinDenseVector <double> &x,
119                 CoinDenseVector <double> &x1, CoinDenseVector <double> &x2,
120                 CoinDenseVector <double> &y,  CoinDenseVector <double> &z1,
121                 CoinDenseVector <double> &z2, CoinDenseVector <double> &r1,
122                 CoinDenseVector <double> &r2, double *Pinf, double *Dinf){
123
124// Form residuals for the primal and dual equations.
125// rL, rU are output, but we input them as full vectors
126// initialized (permanently) with any relevant zeros.
127
128// Get some element pointers for efficiency
129  double *x_elts  = x.getElements();
130  double *r2_elts = r2.getElements();
131 
132  for (int k=0; k<nfix; k++)
133    x_elts[fix[k]]  = 0;
134
135  r1.clear();
136  r2.clear();   
137  model->matVecMult( 1, r1, x );
138  model->matVecMult( 2, r2, y );
139  for (int k=0; k<nfix; k++)
140    r2_elts[fix[k]]  = 0;
141
142
143  r1      = b    - r1 - d2*d2*y;
144  r2      = grad - r2 - z1;              // grad includes d1*d1*x
145  if(nupp > 0)       
146    r2    = r2 + z2;   
147
148  for (int k=0; k<nlow; k++)
149    rL[low[k]] = bl[low[k]] - x[low[k]] + x1[low[k]];
150  for (int k=0; k<nupp; k++)
151    rU[upp[k]] = - bu[upp[k]] + x[upp[k]] + x2[upp[k]];
152
153  double normL = 0.0;
154  double normU = 0.0;
155  for (int k=0; k<nlow; k++)
156    if (rL[low[k]] > normL) normL = rL[low[k]];
157  for (int k=0; k<nupp; k++)
158    if (rU[upp[k]] > normU) normU = rU[upp[k]];
159
160  *Pinf    = CoinMax(normL, normU); 
161  *Pinf    = CoinMax( r1.infNorm() , *Pinf );
162  *Dinf    = r2.infNorm();
163  *Pinf    = CoinMax( *Pinf, 1e-99 );
164  *Dinf    = CoinMax( *Dinf, 1e-99 );
165}
166
167//-----------------------------------------------------------------------
168// End private function pdxxxresid1
169//-----------------------------------------------------------------------
170
171
172//function [cL,cU,center,Cinf,Cinf0] = ...
173//      pdxxxresid2( mu,low,upp,cL,cU,x1,x2,z1,z2 )
174
175inline void pdxxxresid2(double mu, int nlow, int nupp, int *low, int *upp,
176                 CoinDenseVector <double> &cL, CoinDenseVector <double> &cU,
177                 CoinDenseVector <double> &x1, CoinDenseVector <double> &x2,
178                 CoinDenseVector <double> &z1, CoinDenseVector <double> &z2,
179                 double *center, double *Cinf, double *Cinf0){
180
181// Form residuals for the complementarity equations.
182// cL, cU are output, but we input them as full vectors
183// initialized (permanently) with any relevant zeros.
184// Cinf  is the complementarity residual for X1 z1 = mu e, etc.
185// Cinf0 is the same for mu=0 (i.e., for the original problem).
186
187  double maxXz = -1e20;
188  double minXz = 1e20;
189
190  double *x1_elts = x1.getElements();
191  double *z1_elts = z1.getElements();
192  double *cL_elts = cL.getElements();
193  for (int k=0; k<nlow; k++){
194    double x1z1    = x1_elts[low[k]] * z1_elts[low[k]];
195    cL_elts[low[k]] = mu - x1z1;
196    if(x1z1 > maxXz) maxXz = x1z1;
197    if(x1z1 < minXz) minXz = x1z1;
198  }
199
200  double *x2_elts = x2.getElements();
201  double *z2_elts = z2.getElements();
202  double *cU_elts = cU.getElements();
203  for (int k=0; k<nupp; k++){
204    double x2z2    = x2_elts[upp[k]] * z2_elts[upp[k]];
205    cU_elts[upp[k]] = mu - x2z2;
206    if(x2z2 > maxXz) maxXz = x2z2;
207    if(x2z2 < minXz) minXz = x2z2;
208  }
209
210  maxXz   = CoinMax( maxXz, 1e-99 );
211  minXz   = CoinMax( minXz, 1e-99 );
212  *center  = maxXz / minXz;
213
214  double normL = 0.0;
215  double normU = 0.0;
216  for (int k=0; k<nlow; k++)
217    if (cL_elts[low[k]] > normL) normL = cL_elts[low[k]];
218  for (int k=0; k<nupp; k++)
219    if (cU_elts[upp[k]] > normU) normU = cU_elts[upp[k]];
220  *Cinf    = CoinMax( normL, normU);
221  *Cinf0   = maxXz;
222}
223//-----------------------------------------------------------------------
224// End private function pdxxxresid2
225//-----------------------------------------------------------------------
226
227inline double  pdxxxstep( CoinDenseVector <double> &x, CoinDenseVector <double> &dx ){
228
229// Assumes x > 0.
230// Finds the maximum step such that x + step*dx >= 0.
231
232  double step     = 1e+20;
233
234  int n = x.size();
235  double *x_elts = x.getElements();
236  double *dx_elts = dx.getElements();
237  for (int k=0; k<n; k++)
238    if (dx_elts[k] < 0)
239      if ((x_elts[k]/(-dx_elts[k])) < step)
240        step = x_elts[k]/(-dx_elts[k]);
241  return step;
242}
243//-----------------------------------------------------------------------
244// End private function pdxxxstep
245//-----------------------------------------------------------------------
246
247inline double  pdxxxstep(int nset, int *set, CoinDenseVector <double> &x, CoinDenseVector <double> &dx ){
248
249// Assumes x > 0.
250// Finds the maximum step such that x + step*dx >= 0.
251
252  double step     = 1e+20;
253
254  int n = x.size();
255  double *x_elts = x.getElements();
256  double *dx_elts = dx.getElements();
257  for (int k=0; k<n; k++)
258    if (dx_elts[k] < 0)
259      if ((x_elts[k]/(-dx_elts[k])) < step)
260        step = x_elts[k]/(-dx_elts[k]);
261  return step;
262}
263//-----------------------------------------------------------------------
264// End private function pdxxxstep
265//-----------------------------------------------------------------------
266#endif
267#endif
Note: See TracBrowser for help on using the repository browser.