File: minimize.c

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (389 lines) | stat: -rw-r--r-- 10,512 bytes parent folder | download | duplicates (4)
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* minimize - minimization routines for XLISP-STAT                    */
/* XLISP-STAT 2.1 Copyright (c) 1990, by Luke Tierney                  */
/* Additions to Xlisp 2.1, Copyright (c) 1989 by David Michael Betz    */
/* You may give out copies of this software; for conditions see the    */
/* file COPYING included with this distribution.                       */

/*
 * Nonlinear optimization modules adapted from Dennis and Schnabel, 
 * "Numerical Methods for Unconstrained Optimization and Nonlinear
 * Equations."
 */

#include "linalg.h"
                            
/* forward declarations */
static VOID cholsolve P4H(int, double *, double *, double *);
static VOID lsolve P4H(int, double *, double *, double *);
static VOID ltsolve P4H(int, double *, double *, double *);
static VOID modelhess P5H(int, double *, double *, double *, double *);
static double basegradsize P5H(int, double *, double *, double *, double);
static double linesearch_step P6H(double, double, double,
				  double, double, double);


/************************************************************************/
/**                                                                    **/
/**                         Utility Functions                          **/
/**                                                                    **/
/************************************************************************/

static double Max P2C(double, a, double, b)
{
  return(a > b ? a : b);
}

static double Min P2C(double, a, double, b)
{
  return(a > b ? b : a);
}

#define Copy(n, x, y) blas_dcopy(n, x, 1, y, 1)
#define Scale(n, a, x) blas_dscal(n, a, x, 1)
#define Axpy(n, a, x, y) blas_daxpy(n, a, x, 1, y, 1)
#define Dot(n, x, y) blas_ddot(n, x, 1, y, 1)
#define Nrm2(n, x) blas_dnm2(n, x, 1)

static double maxrelsize P4C(int, n, double *, x, double *, y, double *, s)
{
  double value, ax, ay, si, tmp;

  value = 0.0;
  while (n-- > 0) {
    ax = *x++; if (ax < 0.0) ax = -ax; /* ax = fabs(*x++) */
    ay = *y++; if (ay < 0.0) ay = -ay; /* ay = fabs(*y++) */
    si = 1.0 / *s++;
    tmp = ax / (ay > si ? ay : si);    /* tmp = ax / Max(ay, si) */
    if (value < tmp) value = tmp;      /* value = Max(value, tmp) */
  }
  return value;
}


/************************************************************************/
/**                                                                    **/
/**                    Cholesky Solving Functions                      **/
/**                                                                    **/
/************************************************************************/

/* solve (L L^T) s = -g for s */
static VOID cholsolve P4C(int, n, double *, g, double *, L, double *, s)
{
  /* solve Ly = g */
  lsolve(n, g, L, s);
  
  /* solve L^Ts = y */
  ltsolve(n, s, L, s);

  Scale(n, -1.0, s);
}

/* solve Ly = b for y */
static VOID lsolve P4C(int, n, double *, b, double *, L, double *, y)
{
  int i, in;
  double Lii;

  if ((Lii = L[0]) != 0) y[0] = b[0] / Lii;
  for (i = 1, in = n; i < n; i++, in += n) {
    y[i] = b[i] - Dot(i, L + in, y);
    if ((Lii = L[in + i]) != 0) y[i] /= Lii;
  }
}

/* solve (L^T)x = y for x */
static VOID ltsolve P4C(int, n, double *, y, double *, L, double *, x)
{
  int i, in;
  double Lii;

  Copy(n, y, x);
  if ((Lii = L[n * n - 1]) != 0.0) x[n - 1] /= Lii;
  for (i = n - 2, in = i * n; i >= 0; i--, in -= n) {
    Axpy(i + 1, -x[i+1], L + in + n, x);
    if ((Lii = L[in + i]) != 0.0) x[i] /= Lii;
  }    
}

static VOID modelhess P5C(int, n,
			  double *, sx,
			  double *, H,
			  double *, L,
			  double *, hessadd)
{
  int i, j, in, ii, ij;
  double sqrteps, maxdiag, mindiag, maxoff, maxoffl, maxposdiag, mu,
    maxadd, maxev, minev, offrow, sdd;

  /* scale H on both sides with sx */
  for (i = 0, in = 0; i < n; i++, in += n)
    for (j = 0, ij = in; j < n; j++, ij++)
      H[ij] /= sx[i] * sx[j];

  /* 
   * find mu to add to diagonal so no diagonal elements are negative
   * and largest diagonal dominates largest off diagonal element in H 
   */
  sqrteps = sqrt(macheps());
  for (mindiag = maxdiag = H[0], i = 1, in = n; i < n; i++, in += n) {
    maxdiag = Max(maxdiag, H[in + i]);
    mindiag = Min(mindiag, H[in + i]);
  }
  maxposdiag = Max(0.0, maxdiag);
  
  if (mindiag <= sqrteps * maxposdiag) {
    mu = 2 * (maxposdiag - mindiag) * sqrteps - mindiag;
    maxdiag += mu;
  }
  else mu = 0.0;
  
  if (n > 1) {
    for (maxoff = fabs(H[1]), i = 0, in = 0; i < n; i++, in += n) 
      for (j = i + 1, ij = in + j; j < n; j++, ij++) 
        maxoff = Max(maxoff, fabs(H[ij]));
  }
  else maxoff = 0.0;

  if (maxoff * (1 + 2 * sqrteps) > maxdiag) {
    mu += (maxoff - maxdiag) + 2 * sqrteps * maxoff;
    maxdiag = maxoff * (1 + 2 * sqrteps);
  }
  
  if (maxdiag == 0.0) {
    mu = 1;
    maxdiag = 1;
  }

  if (mu > 0)
    for (i = 0, in = 0; i < n; i++, in += n)
      H[in + i] += mu;

  maxoffl = sqrt(Max(maxdiag, maxoff / n));

  /*
   * compute the perturbed Cholesky decomposition
   */
  Copy(n * n, H, L);
  choldecomp(L, n, maxoffl, &maxadd);

  /*
   * add something to diagonal, if needed, to make positive definite
   * and recompute factorization
   */
  if (maxadd > 0) {
    maxev = H[0];
    minev = H[0];
    for (i = 0, in = 0; i < n; i++, in += n) {
      for (offrow = 0.0, j = 0, ij = in; j < n; j++, ij++) 
        if (i != j) offrow += fabs(H[ij]);
      ii = in + i;
      maxev = Max(maxev, H[ii] + offrow);
      minev = Min(minev, H[ii] - offrow);
    }
    sdd = (maxev - minev) * sqrteps - minev;
    sdd = Max(sdd, 0.0);
    mu = Min(maxadd, sdd);
    for (i = 0, in = 0; i < n; i++, in += n)
      H[in + i] += mu;
    Copy(n * n, H, L);
    choldecomp(L, n, maxoffl, &maxadd);
    *hessadd = mu;
  }
  else *hessadd = 0.0;

  /* unscale H and L */
  for (i = 0, in = 0; i < n; i++, in += n)
    for (j = 0, ij = in; j < n; j++, ij++) {
      H[ij] *= sx[i] * sx[j];
      L[ij] *= sx[i];
    }
}

/************************************************************************/
/**                                                                    **/
/**                        Stopping Criteria                           **/
/**                                                                    **/
/************************************************************************/

static double basegradsize P5C(int, n,
			       double *, delf,
			       double *, x,
			       double *, sx,
			       double, scale)
{
  int i;
  double term, size;

  size = 0.0;
  if (scale > 0.0) {
    for (i = 0; i < n; i++) {
      term = fabs(delf[i]) * Max(fabs(x[i]), 1.0 / sx[i]) / scale;
      size = Max(size, term);
    }
  }
  return size;
}


/************************************************************************/
/**                                                                    **/
/**                     Backtracking Line Search                       **/
/**                                                                    **/
/************************************************************************/

static double linesearch_step P6C(double, f,
				  double, initslope,
				  double, lambda,
				  double, newf,
				  double, lambdaprev,
				  double, fprev)
{
  double lambdatemp, a, b, disc, f1, f2, a11, a12, a21, a22, del;

  if (lambda == 1.0) { /* first backtrack, quadratic fit */
    lambdatemp = - initslope / (2 * (newf - f - initslope));
  }
  else { /* all subsequent backtracks, cubic fit */
    del = lambda - lambdaprev;
    f1 = newf - f - lambda * initslope;
    f2 = fprev - f - lambdaprev * initslope;
    a11 = 1.0 / (lambda * lambda);
    a12 = -1.0 / (lambdaprev * lambdaprev);
    a21 = -lambdaprev * a11;
    a22 = -lambda * a12;
    a = (a11 * f1 + a12 * f2) / del;
    b = (a21 * f1 + a22 * f2) / del;
    disc = b * b - 3 * a * initslope;
    if (a == 0) { /* cubic is a quadratic */
      lambdatemp = - initslope / (2 * b);
    }
    else { /* legitimate cubic */
      lambdatemp = (-b + sqrt(disc)) / (3 * a);
    }
    lambdatemp = Min(lambdatemp, 0.5 * lambda);
  }
  return lambdatemp;
}

/**** This error checking needs cleaning up */
static LVAL xlgafloatvec(V)
{
  LVAL x = xlgatvec();
  if (gettvecetype(x) != a_flonum) xlbadtype(x);
  return x;
}

static LVAL xlgafloatmat(V)
{
  LVAL x = xlgadarray();
  x = getdarraydata(x);
  if (! tvecp(x) || gettvecetype(x) != a_flonum) xlbadtype(x);
  return x;
}

static VOID checktvecsize P2C(LVAL, x, int, n)
{
  if (gettvecsize(x) != n) xlbadtype(x);
}

LVAL xsminmaxrelsize(V)
{
  LVAL x, y, s;
  double *dx, *dy, *ds;
  int n;

  x = xlgafloatvec();
  y = xlgafloatvec();
  s = xlgafloatvec();
  xllastarg();

  n = gettvecsize(x);
  checktvecsize(y, n);
  checktvecsize(s, n);
  dx = (double *) gettvecdata(x);
  dy = (double *) gettvecdata(y);
  ds = (double *) gettvecdata(s);
  return cvflonum((FLOTYPE) maxrelsize(n, dx, dy, ds));
}

LVAL xsmincholsolve(V)
{
  int n;
  LVAL g, L, s;
  double *dg, *dL, *ds;

  g = xlgafloatvec();
  L = xlgafloatmat();
  s = xlgafloatvec();
  xllastarg();

  n = gettvecsize(g);
  checktvecsize(L, n * n);
  checktvecsize(s, n);
  dg = (double *) gettvecdata(g);
  dL = (double *) gettvecdata(L);
  ds = (double *) gettvecdata(s);

  cholsolve(n, dg, dL, ds);
  return NIL;
}

LVAL xsminmodelhess(V)
{
  LVAL s, H, L;
  int n;
  double *ds, *dH, *dL, value;

  s = xlgafloatvec();
  H = xlgafloatmat();
  L = xlgafloatmat();
  xllastarg();

  n = gettvecsize(s);
  checktvecsize(H, n * n);
  checktvecsize(L, n * n);
  ds = (double *) gettvecdata(s);
  dH = (double *) gettvecdata(H);
  dL = (double *) gettvecdata(L);

  modelhess(n, ds, dH, dL, &value);
  return cvflonum((FLOTYPE) value);
}

LVAL xsmingradsize(V)
{
  LVAL g, x, s;
  int n;
  double *dg, *dx, *ds, scale;

  g = xlgafloatvec();
  x = xlgafloatvec();
  s = xlgafloatvec();
  scale = makefloat(xlgetarg());
  xllastarg();

  n = gettvecsize(g);
  checktvecsize(x, n);
  checktvecsize(s, n);
  dg = (double *) gettvecdata(g);
  dx = (double *) gettvecdata(x);
  ds = (double *) gettvecdata(s);

  return cvflonum((FLOTYPE) basegradsize(n, dg, dx, ds, scale));
}

LVAL xsminlinesearch(V)
{
  double f, initslope, lambda, newf, lambdaprev, fprev;

  f = makefloat(xlgetarg());
  initslope = makefloat(xlgetarg());
  lambda = makefloat(xlgetarg());
  newf = makefloat(xlgetarg());
  lambdaprev = makefloat(xlgetarg());
  fprev = makefloat(xlgetarg());
  xllastarg();

  return cvflonum((FLOTYPE) linesearch_step(f, initslope, lambda,
					    newf, lambdaprev, fprev));
}