File: newton_search.cpp

package info (click to toggle)
libitpp 4.3.1-13
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 9,952 kB
  • sloc: cpp: 73,628; makefile: 661; python: 548; sh: 261
file content (705 lines) | stat: -rw-r--r-- 15,796 bytes parent folder | download | duplicates (7)
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*!
 * \file
 * \brief Newton Search optimization algorithms - source file
 * \author Tony Ottosson
 *
 * -------------------------------------------------------------------------
 *
 * Copyright (C) 1995-2010  (see AUTHORS file for a list of contributors)
 *
 * This file is part of IT++ - a C++ library of mathematical, signal
 * processing, speech processing, and communications classes and functions.
 *
 * IT++ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * IT++ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with IT++.  If not, see <http://www.gnu.org/licenses/>.
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/optim/newton_search.h>
#include <itpp/base/specmat.h>
#include <itpp/stat/misc_stat.h>


namespace itpp
{


Newton_Search::Newton_Search()
{
  method = BFGS;

  initial_stepsize = 1.0;
  stop_epsilon_1 = 1e-4;
  stop_epsilon_2 = 1e-8;
  max_evaluations = 100;

  f = NULL;
  df_dx = NULL;

  no_feval = 0;
  init = false;
  finished = false;
  trace = false;
}

void Newton_Search::set_function(double(*function)(const vec&))
{
  // Add checks to see that function is OK???
  f = function;
}

void Newton_Search::set_gradient(vec(*gradient)(const vec&))
{
  // Add checks to see that function is OK???
  df_dx = gradient;
}

void Newton_Search::set_start_point(const vec &x, const mat &D)
{
  // check that parameters are valid???
  x_start = x;
  n = x.size();
  D_start = D;

  finished = false;
  init = true;
}

void Newton_Search::set_start_point(const vec &x)
{
  // check that parameters are valid???
  x_start = x;
  n = x.size();
  D_start = eye(n);

  finished = false;
  init = true;
}

bool Newton_Search::search()
{
  // Check parameters and function call ???
  // check that x_start is a valid point, not a NaN and that norm(x0) is not inf

  it_assert(f != NULL, "Newton_Search: Function pointer is not set");
  it_assert(df_dx != NULL, "Newton_Search: Gradient function pointer is not set");

  it_assert(init, "Newton_Search: Starting point is not set");


  F = f(x_start); // function initial value
  vec g = df_dx(x_start); // gradient initial value
  vec x = x_start;
  no_feval++;

  finished = false;

  // Initial inverse Hessian, D
  mat D = D_start;


  bool fst = true; // what is this???

  bool stop = false;

  // Finish initialization
  no_iter = 0;
  ng = max(abs(g)); // norm(g,inf)

  double Delta = initial_stepsize;
  nh = 0; // what is this???
  vec h;

  if (trace) { // prepare structures to store trace data
    x_values.set_size(max_evaluations);
    F_values.set_size(max_evaluations);
    ng_values.set_size(max_evaluations);
    Delta_values.set_size(max_evaluations);
  }

  Line_Search ls;
  ls.set_functions(f, df_dx);

  if (ng <= stop_epsilon_1)
    stop = true;
  else {
    h = zeros(n);
    nh = 0;
    ls.set_stop_values(0.05, 0.99);
    ls.set_max_iterations(5);
    ls.set_max_stepsize(2);
  }

  bool more = true; //???

  while (!stop && more) {
    vec h, w, y, v;
    double yh, yv, a;

    // Previous values
    vec xp = x, gp = g;
    // double Fp = F;           ### 2006-02-03 by ediap: Unused variable!
    double nx = norm(x);

    h = D * (-g);
    nh = norm(h);
    bool red = false;

    if (nh <= stop_epsilon_2*(stop_epsilon_2 + nx))  // stop criterion
      stop = true;
    else {
      if (fst || nh > Delta) {  // Scale to ||h|| = Delta
        h = (Delta / nh) * h;
        nh = Delta;
        fst = false;
        red = true;
      }
      //  Line search
      ls.set_start_point(x, F, g, h);
      more = ls.search(x, F, g);
      no_feval = no_feval + ls.get_no_function_evaluations();

      if (more == false) {  // something wrong in linesearch?
        x_end = x;
        return false;
      }
      else {
        if (ls.get_alpha() < 1)   // Reduce Delta
          Delta = .35 * Delta;
        else if (red && (ls.get_slope_ratio() > .7))   // Increase Delta
          Delta = 3 * Delta;

        //  Update ||g||
        ng = max(abs(g)); // norm(g,inf);

        if (trace) { // store trace
          x_values(no_iter) = x;
          F_values(no_iter) = F;
          ng_values(no_iter) = ng;
          Delta_values(no_iter) = Delta;
        }

        no_iter++;
        h = x - xp;
        nh = norm(h);

        //if  (nh == 0)
        //  found = 4;
        //else {
        y = g - gp;
        yh = dot(y, h);
        if (yh > std::sqrt(eps) * nh * norm(y)) {
          //  Update  D
          v = D * y;
          yv = dot(y, v);
          a = (1 + yv / yh) / yh;
          w = (a / 2) * h - v / yh;
          D += outer_product(w, h) + outer_product(h, w); //D = D + w*h' + h*w';
        }  // update D
        //  Check stopping criteria
        double thrx = stop_epsilon_2 * (stop_epsilon_2 + norm(x));
        if (ng <= stop_epsilon_1)
          stop = true; // stop = 1, stop by small gradient
        else if (nh <= thrx)
          stop = true; // stop = 2, stop by small x-step
        else if (no_feval >= max_evaluations)
          stop = true; // stop = 3, number of function evaluations exeeded
        else
          Delta = std::max(Delta, 2 * thrx);
        //} found =4
      }  // Nonzero h
    } // nofail
  }  // iteration

  //  Set return values
  x_end = x;
  finished = true;

  if (trace) { // trim size of trace output
    x_values.set_size(no_iter, true);
    F_values.set_size(no_iter, true);
    ng_values.set_size(no_iter, true);
    Delta_values.set_size(no_iter, true);
  }

  return true;
}

bool Newton_Search::search(vec &xn)
{
  bool state = search();
  xn = get_solution();
  return state;
}

bool Newton_Search::search(const vec &x0, vec &xn)
{
  set_start_point(x0);
  bool state = search();
  xn = get_solution();
  return state;
}

vec Newton_Search::get_solution()
{
  it_assert(finished, "Newton_Search: search is not run yet");
  return x_end;
}

double Newton_Search::get_function_value()
{
  if (finished)
    return F;
  else
    it_warning("Newton_Search::get_function_value, search has not been run");

  return 0.0;
}

double Newton_Search::get_stop_1()
{
  if (finished)
    return ng;
  else
    it_warning("Newton_Search::get_stop_1, search has not been run");

  return 0.0;
}

double Newton_Search::get_stop_2()
{
  if (finished)
    return nh;
  else
    it_warning("Newton_Search::get_stop_2, search has not been run");

  return 0.0;
}

int Newton_Search::get_no_iterations()
{
  if (finished)
    return no_iter;
  else
    it_warning("Newton_Search::get_no_iterations, search has not been run");

  return 0;
}

int Newton_Search::get_no_function_evaluations()
{
  if (finished)
    return no_feval;
  else
    it_warning("Newton_Search::get_no_function_evaluations, search has not been run");

  return 0;
}


void Newton_Search::get_trace(Array<vec> & xvalues, vec &Fvalues, vec &ngvalues, vec &dvalues)
{
  if (finished) {
    if (trace) { // trim size of trace output
      xvalues = x_values;
      Fvalues = F_values;
      ngvalues = ng_values;
      dvalues = Delta_values;
    }
    else
      it_warning("Newton_Search::get_trace, trace is not enabled");
  }
  else
    it_warning("Newton_Search::get_trace, search has not been run");
}

//================================== Line_Search =============================================

Line_Search::Line_Search()
{
  method = Soft;

  if (method == Soft) {
    stop_rho = 1e-3;
    stop_beta = 0.99;
  }

  max_iterations = 10;
  max_stepsize = 10;

  f = NULL;
  df_dx = NULL;
  no_feval = 0;
  init = false;
  finished = false;
  trace = false;
}

void Line_Search::set_function(double(*function)(const vec&))
{
  // Add checks to see that function is OK???
  f = function;
}

void Line_Search::set_gradient(vec(*gradient)(const vec&))
{
  // Add checks to see that function is OK???
  df_dx = gradient;
}


void Line_Search::set_stop_values(double rho, double beta)
{
  // test input values???
  stop_rho = rho;
  stop_beta = beta;
}


void Line_Search::set_start_point(const vec &x, double F, const vec &g, const vec &h)
{
  // check values ???
  x_start = x;
  F_start = F;
  g_start = g;
  h_start = h;
  n = x.size();

  finished = false;
  init = true;
}

void Line_Search::get_solution(vec &xn, double &Fn, vec &gn)
{
  it_assert(finished, "Line_Search: search is not run yet");

  xn = x_end;
  Fn = F_end;
  gn = g_end;
}

bool Line_Search::search()
{
  it_assert(f != NULL, "Line_Search: Function pointer is not set");
  it_assert(df_dx != NULL, "Line_Search: Gradient function pointer is not set");

  it_assert(init, "Line_search: Starting point is not set");

  // Default return values and simple checks
  x_end = x_start;
  F_end = F_start;
  g_end = g_start;

  // add some checks???
  finished = false;

  vec g;

  // return parameters
  no_feval = 0;
  slope_ratio = 1;



  // Check descent condition
  double dF0 = dot(h_start, g_end);

  if (trace) { // prepare structures to store trace data
    alpha_values.set_size(max_iterations);
    F_values.set_size(max_iterations);
    dF_values.set_size(max_iterations);
    alpha_values(0) = 0;
    F_values(0) = F_end;
    dF_values(0) = dF0;
  }


  if (dF0 >= -10*eps*norm(h_start)*norm(g_end)) {  // not significantly downhill
    if (trace) { // store trace
      alpha_values.set_size(1, true);
      F_values.set_size(1, true);
      dF_values.set_size(1, true);
    }
    return false;
  }

  // Finish initialization
  double F0 = F_start, slope0, slopethr;

  if (method == Soft) {
    slope0 = stop_rho * dF0;
    slopethr = stop_beta * dF0;
  }
  else { // exact line search
    slope0 = 0;
    slopethr = stop_rho * std::abs(dF0);
  }

  // Get an initial interval for am
  double a = 0, Fa = F_end, dFa = dF0;
  bool stop = false;
  double b = std::min(1.0, max_stepsize), Fb = 0, dFb = 0;


  while (!stop) {
    Fb = f(x_start + b * h_start);
    g = df_dx(x_start + b * h_start);
    // check if these values are OK if not return false???
    no_feval++;

    dFb = dot(g, h_start);
    if (trace) { // store trace
      alpha_values(no_feval) = b;
      F_values(no_feval) = Fb;
      dF_values(no_feval) = dFb;
    }

    if (Fb < F0 + slope0*b) {  // new lower bound
      alpha = b;
      slope_ratio = dFb / dF0; // info(2);

      if (method == Soft) {
        a = b;
        Fa = Fb;
        dFa = dFb;
      }

      x_end = x_start + b * h_start;
      F_end = Fb;
      g_end = g;

      if ((dFb < std::min(slopethr, 0.0)) && (no_feval < max_iterations) && (b < max_stepsize)) {
        // Augment right hand end
        if (method == Exact) {
          a = b;
          Fa = Fb;
          dFa = dFb;
        }
        if (2.5*b >= max_stepsize)
          b = max_stepsize;
        else
          b = 2 * b;
      }
      else
        stop = true;
    }
    else
      stop = true;
  } // phase 1: expand interval



  if (stop)  // OK so far.  Check stopping criteria
    stop = (no_feval >= max_iterations)
           || (b >= max_stepsize && dFb < slopethr)
           || (a > 0 && dFb >= slopethr);
  // Commented by ediap 2006-07-17: redundant check
  //  || ( (method == Soft) && (a > 0 & dFb >= slopethr) );  // OK


  if (stop && trace) {
    alpha_values.set_size(no_feval, true);
    F_values.set_size(no_feval, true);
    dF_values.set_size(no_feval, true);
  }

  // Refine interval
  while (!stop) {

    double c, Fc, dFc;

    //c = interpolate(xfd,n);
    double C = Fb - Fa - (b - a) * dFa;
    if (C >= 5*n*eps*b) {
      double A = a - 0.5 * dFa * (sqr(b - a) / C);
      c = std::min(std::max(a + 0.1 * (b - a), A), b - 0.1 * (b - a));  // % Ensure significant resuction
    }
    else
      c = (a + b) / 2;

    Fc = f(x_start + c * h_start);
    g = df_dx(x_start + c * h_start);
    dFc = dot(g, h_start);
    // check these values???
    no_feval++;

    if (trace) { // store trace
      alpha_values(no_feval) = c;
      F_values(no_feval) = Fc;
      dF_values(no_feval) = dFc;
    }

    if (method == Soft) {
      // soft line method
      if (Fc < F0 + slope0*c) {  // new lower bound
        alpha = c;
        slope_ratio = dFc / dF0;

        x_end = x_start + c * h_start;
        F_end = Fc;
        g_end = g;
        a = c;
        Fa = Fc;
        dFa = dFc; // xfd(:,1) = xfd(:,3);
        stop = (dFc > slopethr);
      }
      else { // new upper bound
        b = c;
        Fb = Fc;
        dFb = dFc; // xfd(:,2) = xfd(:,3);
      }

    }
    else { // Exact line search
      if (Fc < F_end) {  // better approximant
        alpha = c;
        slope_ratio = dFc / dF0;
        x_end = x_start + c * h_start;
        F_end = Fc;
        g_end = g;
      }
      if (dFc < 0) {  // new lower bound
        a = c;
        Fa = Fc;
        dFa = dFc; // xfd(:,1) = xfd(:,3);
      }
      else { //new upper bound
        b = c;
        Fb = Fc;
        dFb = dFc; // xfd(:,2) = xfd(:,3);
      }
      stop = (std::abs(dFc) <= slopethr) | ((b - a) < stop_beta * b);
    }

    stop = (stop | (no_feval >= max_iterations));
  } // refine

  finished = true;

  if (trace) { // store trace
    alpha_values.set_size(no_feval + 1, true);
    F_values.set_size(no_feval + 1, true);
    dF_values.set_size(no_feval + 1, true);
  }

  return true;
}

bool Line_Search::search(vec &xn, double &Fn, vec &gn)
{
  bool state = search();
  get_solution(xn, Fn, gn);
  return state;
}

bool Line_Search::search(const vec &x, double F, const vec &g, const vec &h,
                         vec &xn, double &Fn, vec &gn)
{
  set_start_point(x, F, g, h);
  bool state = search();
  get_solution(xn, Fn, gn);
  return state;
}


double Line_Search::get_alpha()
{
  if (finished)
    return alpha;
  else
    it_warning("Line_Search::get_alpha, search has not been run");

  return 0.0;
}

double Line_Search::get_slope_ratio()
{
  if (finished)
    return slope_ratio;
  else
    it_warning("Line_Search::get_slope_raio, search has not been run");

  return 0.0;
}

int Line_Search::get_no_function_evaluations()
{
  if (finished)
    return no_feval;
  else
    it_warning("Line_Search::get_no_function_evaluations, search has not been run");

  return 0;
}


void Line_Search::set_max_iterations(int value)
{
  it_assert(value > 0, "Line_Search, max iterations must be > 0");
  max_iterations = value;
}

void Line_Search::set_max_stepsize(double value)
{
  it_assert(value > 0, "Line_Search, max stepsize must be > 0");
  max_stepsize = value;
}

void Line_Search::set_method(const Line_Search_Method &search_method)
{
  method = search_method;

  if (method == Soft) {
    stop_rho = 1e-3;
    stop_beta = 0.99;
  }
  else { // exact line search
    method = Exact;
    stop_rho = 1e-3;
    stop_beta = 1e-3;
  }
}


void Line_Search::get_trace(vec &alphavalues, vec &Fvalues, vec &dFvalues)
{
  if (finished) {
    if (trace) { // trim size of trace output
      alphavalues = alpha_values;
      Fvalues = F_values;
      dFvalues = dF_values;
    }
    else
      it_warning("Line_Search::get_trace, trace is not enabled");
  }
  else
    it_warning("Line_Search::get_trace, search has not been run");
}

// =========================== functions ==============================================

vec fminunc(double(*function)(const vec&), vec(*gradient)(const vec&), const vec &x0)
{
  Newton_Search newton;
  newton.set_functions(function, gradient);

  vec xn;
  newton.search(x0, xn);

  return xn;
}



} // namespace itpp