1 //------------------------------------------------------------------------
2 // Copyright (C) 1993, 1994:
3 // J.C. Meza
4 // Sandia National Laboratories
5 // meza@california.sandia.gov
6 //------------------------------------------------------------------------
7 
8 #ifdef HAVE_CONFIG_H
9 #include "OPT++_config.h"
10 #endif
11 
12 #ifdef HAVE_STD
13 #include <cstdio>
14 #include <cstdlib>
15 #include <cmath>
16 #else
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <math.h>
20 #endif
21 
22 #if (defined(__sgi) || defined(__xlc__) || defined (__xlC__))
23 #define WANT_MATH
24 #else
25 #define WANT_STREAM
26 #define WANT_MATH
27 #endif
28 
29 #include "newmatap.h"
30 
31 #include "NLP.h"
32 #include "NLF.h"
33 
34 using std::cerr;
35 
36 using NEWMAT::ColumnVector;
37 using NEWMAT::SymmetricMatrix;
38 
39 using namespace OPTPP;
40 
square(double x)41 static double square(double x) { return x*x; }
42 
init_rosen(int ndim,ColumnVector & x)43 void init_rosen (int ndim, ColumnVector& x)
44 {
45   if (ndim != 2)
46   {
47     exit (1);
48   }
49   x(1) = -1.2;
50   x(2) =  1.0;
51 }
rosen0(int n,const ColumnVector & x,double & fx,int & result)52 void rosen0(int n, const ColumnVector& x, double& fx, int& result)
53 { // Rosenbrock's function
54   double f1, f2, x1, x2;
55 
56   if (n != 2) return;
57 
58   x1 = x(1);
59   x2 = x(2);
60   f1 = (x2 - x1 * x1);
61   f2 = 1. - x1;
62 
63   fx  = 100.* f1*f1 + f2*f2;
64   result = NLPFunction;
65 }
66 
rosen(int mode,int n,const ColumnVector & x,double & fx,ColumnVector & g,int & result)67 void rosen(int mode, int n, const ColumnVector& x, double& fx, ColumnVector& g, int& result)
68 { // Rosenbrock's function
69   double f1, f2, x1, x2;
70 
71   if (n != 2) return;
72 
73   x1 = x(1);
74   x2 = x(2);
75   f1 = (x2 - x1 * x1);
76   f2 = 1. - x1;
77 
78   if (mode & NLPFunction) {
79     fx  = 100.* f1*f1 + f2*f2;
80     result = NLPFunction;
81   }
82   if (mode & NLPGradient) {
83     g(1) = -400.*f1*x1 - 2.*f2;
84     g(2) = 200.*f1;
85     result = NLPGradient;
86   }
87 }
88 
rosen2(int mode,int n,const ColumnVector & x,double & fx,ColumnVector & g,SymmetricMatrix & H,int & result)89 void rosen2(int mode, int n, const ColumnVector& x, double& fx,
90 	ColumnVector& g, SymmetricMatrix& H, int &result)
91 // Rosenbrock's function, n = 2 with first and second order derivatives
92 
93 {
94   //fcn_count++;
95 
96   double f1, f2, x1, x2;
97 
98 
99   // cout << "\nrosen2: mode = " << mode
100   //      << " count = " << fcn_count << "\n";
101   // for(int i=1; i<=n; i++)
102   //     cout << "x(" << i << ") = " << x(i) << "\n";
103 
104   if (n != 2) return;
105 
106   x1 = x(1);
107   x2 = x(2);
108   f1 = (x2 - x1 * x1);
109   f2 = 1. - x1;
110 
111   if (mode & NLPFunction) {
112     fx  = 100.* f1*f1 + f2*f2;
113     result = NLPFunction;
114   }
115   if (mode & NLPGradient) {
116     g(1) = -400.*f1*x1 - 2.*f2;
117     g(2) = 200.*f1;
118     result = NLPGradient;
119   }
120 
121   if (mode & NLPHessian) {
122 
123     f1 = (x2 - 3.0*x1*x1);
124     H(1,1) = -400.0*f1 + 2.0;
125     H(2,1) = -400.0*x1;
126     H(2,2) = 200.0;
127     result = NLPHessian;
128   }
129 }
130 
erosen1(int mode,int ndim,const ColumnVector & x,double & fx,ColumnVector & g,int & result)131 void erosen1(int mode, int ndim, const ColumnVector& x, double& fx,
132 	ColumnVector& g, int &result)
133 // Extended Rosenbrock's function, with analytic  derivatives
134 
135 {
136   static int i;
137   static double f1, f2, x1, x2;
138 
139   fx = 0.;
140 
141   for (i = 1; i <= ndim/2; ++i) {
142     x1 = x(2*i-1);
143     x2 = x(2*i);
144 
145     f1 = (x2 - x1 * x1);
146     f2 = 1. - x1;
147 
148     fx += 100.*square(f1) + square(f2);
149 
150     if (mode & NLPGradient) {
151       g(2*i-1) = -400.*f1*x1 - 2.*f2;
152       g(2*i) = 200.*f1;
153     }
154   }
155   result = NLPFunction;
156   if (mode & NLPGradient) result = NLPGradient;
157 
158 }
init_epowell(int ndim,ColumnVector & x)159 void init_epowell (int ndim, ColumnVector& x)
160 {
161   if (ndim % 4 != 0)
162   {
163     printf ("init_epowell: ndim must be multiple of 4, ndim = %d\n", ndim);
164     exit (1);
165   }
166 
167   for (int i=1; i<=ndim/4; i++)
168   {
169     x(4*i - 3) =  3;
170     x(4*i - 2) = -1;
171     x(4*i - 1) =  0;
172     x(4*i    ) =  1;
173   }
174 }
epowell(int ndim,const ColumnVector & x,double & fx,int & result)175 void epowell(int ndim, const ColumnVector& x, double& fx, int& result)
176 
177 {
178   // Extended Powell Singular Function, n variable, multiple of 4
179   // Standard initial guess  x0   = (psi1, psi2, psi3, psi4, ...)
180   //                         psi1 =  3
181   //                         psi2 = -1
182   //                         psi3 =  0
183   //                         psi4 =  1
184   // Solution                fmin = 0 at origin
185 
186   double f1, f2, f3, f4;
187 
188   if (ndim % 4 != 0) {
189     cerr << "epowell: ndim must be a multiple of 4, ndim = \n" << ndim;
190     return;
191   }
192 
193   fx = 0.0;
194   for (int i=1; i<=ndim/4; i++) {
195     f1 = x(4*i-3) + 10*x(4*i-2);
196     f2 = sqrt(5.0) * ( x(4*i-1) - x(4*i));
197     f3 = square(x(4*i-2) - 2*x(4*i-1));
198     f4 = sqrt(10.0) * (x(4*i-3) - x(4*i));
199     fx += square(f1) + square(f2) + square(f3) + square(f4);
200   }
201   result = NLPFunction;
202 }
203 
init_trig(int ndim,ColumnVector & x)204 void init_trig (int ndim, ColumnVector& x)
205 {
206   for (int i=1; i<=ndim; i++)
207   {
208     x(i) =  1/ double(ndim);
209   }
210 }
211 
trig(int ndim,const ColumnVector & x,double & fx,int & result)212 void trig(int ndim, const ColumnVector& x, double& fx, int& result)
213 
214 {
215   // Trigonometric Function, ndim variable, m = ndim
216   // Standard initial guess  x0   = (1/ndim, ..., 1/ndim)
217   // Solution                fmin = 0
218 
219   double fi;
220 
221   fx = 0.0;
222 
223   for (int i=1; i<=ndim; ++i) {
224     double sum = 0.0;
225     for (int j=1; j<=ndim; ++j) {
226       sum += cos(x(j)) + double(i)*(1.0-cos(x(i)) - sin(x(i)));
227     }
228     fi = ndim - sum;
229     fx += fi*fi;
230   }
231 
232   result = NLPFunction;
233 }
init_erosen(int ndim,ColumnVector & x)234 void init_erosen (int ndim, ColumnVector& x)
235 {
236 
237   if (ndim % 2 != 0)
238   {
239     printf ("init_erosen: ndim must be even, ndim = %d\n", ndim);
240     exit (1);
241   }
242 
243   for (int i = 1; i <= ndim / 2; ++i)
244   {
245     x(2*i - 1) = -1.2;
246     x(2*i)     =  1.0;
247   }
248 
249 }
erosen(int ndim,const ColumnVector & x,double & fx,int & result)250 void erosen(int ndim, const ColumnVector& x, double& fx, int& result)
251 {
252 //  erosen
253 //  Extended rosenbrock function for any even value of n.
254 //                          2
255 //     f    (x) = 10(x   - x    )
256 //      2i-1          2i    2i-1
257 //
258 //     f  (x) = 1 - x
259 //      2i           2i-1
260 //                                           2         2
261 //     f(x) = sum from i = 1 to n/2 (f    (x)  + f  (x) ).
262 //                                    2i-1        2i
263 //
264 //     x  = (epsilon ) where epsilon    = -1.2, epsilon  = 1
265 //      0           i               2i-1               2i
266 //
267 //     f(x ) = 0 at x  = (1,...,1)
268 //        *          *
269 //
270 //     parameters
271 //
272 //     input
273 //        n             dimension of the problem to be solved must be even
274 //        x             point at which the function is to be evaluated
275 //     output
276 //        f             function value at x
277 
278 
279   static int i;
280   static double f1, f2, x1, x2;
281 
282   fx = 0.;
283 
284   for (i = 1; i <= ndim/2; ++i) {
285     x1 = x(2*i-1);
286     x2 = x(2*i);
287 
288     f1 = (x2 - x1 * x1);
289     f2 = 1. - x1;
290 
291     fx += 100.*square(f1) + square(f2);
292   }
293   result = NLPFunction;
294 }
295 
init_penalty1(int ndim,ColumnVector & x)296 void init_penalty1 (int ndim, ColumnVector& x)
297 {
298 
299   for (int i = 1; i <= ndim; i++) x(i) = double(i);
300 
301 }
penalty1(int ndim,const ColumnVector & x,double & fx,int & result)302 void penalty1(int ndim, const ColumnVector& x, double& fx, int& result)
303 {
304 //  Penalty 1
305 //     parameters
306 //
307 //     input
308 //        ndim          dimension of the problem to be solved must be even
309 //        x             point at which the function is to be evaluated
310 //     output
311 //        f             function value at x
312 
313 
314   int i;
315   double fi;
316   double k1  = 1.e-5;
317   double sum = 0.0;
318 
319   fx = 0.;
320 
321   for (i=1; i <= ndim; i++) sum += square(x(i));
322   fx = (sum - .25)*(sum - .25);
323 
324 
325   for (i = 1; i <= ndim; i++) {
326     fi = (x(i) - 1.0);
327     fx += k1*square(fi);
328   }
329   result = NLPFunction;
330 }
init_penalty2(int ndim,ColumnVector & x)331 void init_penalty2 (int ndim, ColumnVector& x)
332 {
333 
334   for (int i = 1; i <= ndim; i++) x(i) = 0.5;
335 
336 }
penalty2(int ndim,const ColumnVector & x,double & fx,int & result)337 void penalty2(int ndim, const ColumnVector& x, double& fx, int& result)
338 {
339 //  Penalty 2
340 //     parameters
341 //
342 //     input
343 //        ndim          dimension of the problem to be solved must be even
344 //        x             point at which the function is to be evaluated
345 //     output
346 //        f             function value at x
347 
348 
349   int i;
350   double f1, fi, yi;
351 
352   double k1  = 1.e-5;
353   double sum = 0.0;
354 
355   fx = 0.0;
356 
357   // Take care of the two end cases first
358   //
359 
360   f1  = x(1) - 0.2;
361   fx  = square(f1);
362   for (i=1; i <= ndim; i++) sum += double(ndim - i + 1)*square(x(i));
363   sum = sum - 1.0;
364   fx += square(sum);
365 
366   //
367   // Now take care of all of the cases in between
368   //
369   for (i = 2; i <= ndim; i++) {
370     yi = exp(double(i)/10.0) + exp(double(i-1)/10.0);
371     fi = exp(x(i)/10.0) + exp(x(i-1)/10.0) - yi;
372     fx += k1*square(fi);
373   }
374   for (i = ndim+1; i < 2*ndim; i++) {
375     fi = exp(x(i-ndim+1)/10.0) - exp(double(-1)/10.0);
376     fx += k1*square(fi);
377   }
378   result = NLPFunction;
379 }
init_vardim(int ndim,ColumnVector & x)380 void init_vardim (int ndim, ColumnVector& x)
381 {
382 
383   for (int i = 1; i <= ndim; i++) x(i) = 1.0 - (double(i)/double(ndim));
384 
385 }
vardim(int ndim,const ColumnVector & x,double & fx,int & result)386 void vardim(int ndim, const ColumnVector& x, double& fx, int& result)
387 {
388 //  Variable dimensioned
389 //     parameters
390 //
391 //     input
392 //        ndim          dimension of the problem to be solved must be even
393 //        x             point at which the function is to be evaluated
394 //     output
395 //        f             function value at x
396 
397 
398   int i;
399   double fn_plus_1, fn_plus_2, fi;
400 
401   double sum = 0.0;
402 
403   fx = 0.;
404 
405   // Take care of the two end cases first
406   //
407   for (i=1; i <= ndim; i++) sum += double(i)*(x(i) - 1.0);
408 
409   fn_plus_1  = sum;
410   fn_plus_2  = square(sum);
411   fx        += square(fn_plus_1) + square(fn_plus_2);
412 
413   //
414   // Now take care of all of the cases in between
415   //
416   for (i = 1; i <= ndim; i++) {
417     fi = x(i) - 1.0;
418     fx += square(fi);
419   }
420   result = NLPFunction;
421 }
init_broyden_tridiag(int ndim,ColumnVector & x)422 void init_broyden_tridiag (int ndim, ColumnVector& x)
423 {
424 
425   for (int i = 1; i <= ndim; i++) x(i)   = -1.0;
426 
427 }
broyden_tridiag(int ndim,const ColumnVector & x,double & fx,int & result)428 void broyden_tridiag(int ndim, const ColumnVector& x, double& fx, int& result)
429 {
430 //  Broyden Tridiagonal function
431 //     parameters
432 //
433 //     input
434 //        ndim          dimension of the problem to be solved must be even
435 //        x             point at which the function is to be evaluated
436 //     output
437 //        f             function value at x
438 
439 
440   int i;
441   double f1, fi, fn;
442   double x0 = 0.0, xn_plus_1 = 0.0;
443 
444   fx = 0.;
445 
446   f1 = (3.0 - 2.0*x(1))*x(1) - x0 - 2.0*x(2) + 1.0;
447   fn = (3.0 - 2.0*x(ndim))*x(ndim) - x(ndim-1) - 2.0*xn_plus_1 + 1.0;
448 
449   fx += square(f1) + square(fn);
450   //
451   // Now take care of all of the cases in between
452   //
453   for (i = 2; i < ndim; i++) {
454     fi  = (3.0 - 2.0*x(i))*x(i) - x(i-1) - 2.0*x(i+1) + 1.0;
455     fx += square(fi);
456   }
457   result = NLPFunction;
458 }
459 //-----------------------------------------------------------------------------
460 // Problems from Conn, Gould, Toint
461 //
462 //-----------------------------------------------------------------------------
init_gen_brown(int ndim,ColumnVector & x)463 void init_gen_brown (int ndim, ColumnVector& x)
464 {
465 
466   for (int i = 1; i < ndim; i=i+2){
467     x(i)   = -1.0;
468     x(i+1) =  1.0;
469   }
470 
471 }
gen_brown(int ndim,const ColumnVector & x,double & fx,int & result)472 void gen_brown(int ndim, const ColumnVector& x, double& fx, int& result)
473 {
474 //  Generalization of a function due to A. Brown
475 //  Conn, Gould, Toint, Test function 21, pp. 427
476 //     parameters
477 //
478 //     input
479 //        ndim          dimension of the problem to be solved must be even
480 //        x             point at which the function is to be evaluated
481 //     output
482 //        f             function value at x
483 
484 
485   int i;
486   double x0, x1, y1, y2;
487 
488   fx = 0.;
489 
490   //
491   for (i = 1; i < ndim; i++) {
492     x0 = x(i);
493     x1 = x(i+1);
494     y1 = square(x1) + 1;
495     y2 = square(x0) + 1;
496     fx += pow(square(x0),y1) + pow(square(x1),y2);
497   }
498   result = NLPFunction;
499 }
init_chain_singular(int ndim,ColumnVector & x)500 void init_chain_singular (int ndim, ColumnVector& x)
501 {
502 
503   if (ndim % 4 != 0)
504   {
505     printf ("init_chain_singular: ndim must be multiple of 4, ndim = %d\n", ndim);
506     exit (1);
507   }
508 
509   for (int i=1; i<=ndim/4; i++)
510   {
511     x(4*i - 3) =  3;
512     x(4*i - 2) = -1;
513     x(4*i - 1) =  0;
514     x(4*i    ) =  1;
515   }
516 
517 }
chain_singular(int ndim,const ColumnVector & x,double & fx,int & result)518 void chain_singular(int ndim, const ColumnVector& x, double& fx, int& result)
519 {
520 //  Chained Singular Function
521 //  Conn, Gould, Toint, Test function 21, pp. 422
522 //     parameters
523 //
524 //     input
525 //        ndim          dimension of the problem to be solved must be even
526 //        x             point at which the function is to be evaluated
527 //     output
528 //        f             function value at x
529 
530 
531   int i;
532   double x0, x1, x2, x3;
533   double t1, t2, t3, t4;
534 
535   fx = 0.;
536 
537   if (ndim % 4 != 0)
538   {
539     printf ("chain_singular: ndim must be multiple of 4, ndim = %d\n", ndim);
540     exit (1);
541   }
542 
543   //
544   for (i = 1; i <= ndim-3; i=i+2) {
545     x0 = x(i);
546     x1 = x(i+1);
547     x2 = x(i+2);
548     x3 = x(i+3);
549     t1 = square(x0 + 10.0*x1);
550     t2 = 5.0*square(x2 - x3);
551     t3 = square(x1 - 2.0*x2)*square(x1 - 2.0*x2);
552     t4 = 10.0*square(x0 - 10.0*x3)*square(x0 - 10.0*x3);
553     fx += t1 + t2 + t3 + t4;
554   }
555   result = NLPFunction;
556 }
init_gen_wood(int ndim,ColumnVector & x)557 void init_gen_wood (int ndim, ColumnVector& x)
558 {
559 
560   if (ndim % 4 != 0)
561   {
562     printf ("init_gen_wood: ndim must be multiple of 4, ndim = %d\n", ndim);
563     exit (1);
564   }
565 
566   x(1) =  -3;
567   x(2) =  -1;
568   x(3) =  -3;
569   x(4) =  -1;
570 
571   for (int i=5; i<=ndim; i=i+2)
572   {
573     x(i)   = -2;
574     x(i+1) =  0;
575   }
576   //  for (int i=1; i<=ndim; i++) x(i) = 1.1;
577 }
gen_wood(int ndim,const ColumnVector & x,double & fx,int & result)578 void gen_wood(int ndim, const ColumnVector& x, double& fx, int& result)
579 {
580 //  Chained Singular Function
581 //  Conn, Gould, Toint, Test function 21, pp. 422
582 //     parameters
583 //
584 //     input
585 //        ndim          dimension of the problem to be solved must be even
586 //        x             point at which the function is to be evaluated
587 //     output
588 //        f             function value at x
589 
590 
591   int i;
592   double x0, x1, x2, x3;
593   double t1, t2, t3, t4, t5, t6;
594 
595   fx = 0.0;
596   if (ndim % 4 != 0)
597   {
598     printf ("gen_wood: ndim must be multiple of 4, ndim = %d\n", ndim);
599     exit (1);
600   }
601 
602   //
603   fx = 1.0;
604   for (i = 1; i <= ndim-3; i=i+4) {
605     x0 = x(i);
606     x1 = x(i+1);
607     x2 = x(i+2);
608     x3 = x(i+3);
609 
610     t1 = 100.0*square(x1 - square(x0));
611     t2 = square(1.0 - x0);
612     t3 = 90.0*square(x3 - square(x2));
613     t4 = square(1.0 - x2);
614     t5 = 10.0*square(x1 + x3 - 2.0);
615     t6 =  0.1*square(x1 - x3);
616     fx += t1 + t2 + t3 + t4 + t5 + t6;
617   }
618   result = NLPFunction;
619 }
init_chain_wood(int ndim,ColumnVector & x)620 void init_chain_wood (int ndim, ColumnVector& x)
621 {
622 
623   if (ndim % 4 != 0)
624   {
625     printf ("init_chain_wood: ndim must be multiple of 4, ndim = %d\n", ndim);
626     exit (1);
627   }
628 
629   x(1) =  -3;
630   x(2) =  -1;
631   x(3) =  -3;
632   x(4) =  -1;
633 
634   for (int i=5; i<=ndim; i=i+2)
635   {
636     x(i)   = -2;
637     x(i+1) =  0;
638   }
639 
640 }
chain_wood(int ndim,const ColumnVector & x,double & fx,int & result)641 void chain_wood(int ndim, const ColumnVector& x, double& fx, int& result)
642 {
643 //  Chained Singular Function
644 //  Conn, Gould, Toint, Test function 21, pp. 422
645 //     parameters
646 //
647 //     input
648 //        ndim          dimension of the problem to be solved must be even
649 //        x             point at which the function is to be evaluated
650 //     output
651 //        f             function value at x
652 
653 
654   int i;
655   double x0, x1, x2, x3;
656   double t1, t2, t3, t4, t5, t6;
657 
658   fx = 0.0;
659   if (ndim % 4 != 0)
660   {
661     printf ("chain_wood: ndim must be multiple of 4, ndim = %d\n", ndim);
662     exit (1);
663   }
664 
665   //
666   fx = 1.0;
667   for (i = 1; i <= ndim-3; i=i+2) {
668     x0 = x(i);
669     x1 = x(i+1);
670     x2 = x(i+2);
671     x3 = x(i+3);
672 
673     t1 = 100.0*square(x1 - square(x0));
674     t2 = square(1.0 - x0);
675     t3 = 90.0*square(x3 - square(x2));
676     t4 = square(1.0 - x2);
677     t5 = 10.0*square(x1 + x3 - 2.0);
678     t6 =  0.1*square(x1 - x3);
679     fx += t1 + t2 + t3 + t4 + t5 + t6;
680   }
681   result = NLPFunction;
682 }
init_broyden(int ndim,ColumnVector & x)683 void init_broyden (int ndim, ColumnVector& x)
684 {
685 
686   for (int i = 1; i <= ndim; i++) x(i)   = -1.0;
687 
688 }
broyden1a(int ndim,const ColumnVector & x,double & fx,int & result)689 void broyden1a(int ndim, const ColumnVector& x, double& fx, int& result)
690 {
691 //  Broyden Tridiagonal function
692 //     parameters
693 //
694 //     input
695 //        ndim          dimension of the problem to be solved must be even
696 //        x             point at which the function is to be evaluated
697 //     output
698 //        f             function value at x
699 
700 
701   int i;
702   double f1, fi, fn;
703   double x0 = 0.0, xn_plus_1 = 0.0;
704   double p  = double(7)/double(3);
705 
706   fx = 1.0;
707 
708   // First the end cases
709   //
710   f1 = fabs((3.0 - 2.0*x(1))*x(1) - x0 - 2.0*x(2) + 1.0);
711   fn = fabs((3.0 - 2.0*x(ndim))*x(ndim) - x(ndim-1) - 2.0*xn_plus_1 + 1.0);
712 
713   fx += pow(f1,p) + pow(fn,p);
714   //
715   // Now take care of all of the cases in between
716   //
717   for (i = 2; i < ndim; i++) {
718     fi  = fabs((3.0 - 2.0*x(i))*x(i) - x(i-1) - 2.0*x(i+1) + 1.0);
719     fx += pow(fi,p);
720   }
721   result = NLPFunction;
722 }
broyden1b(int ndim,const ColumnVector & x,double & fx,int & result)723 void broyden1b(int ndim, const ColumnVector& x, double& fx, int& result)
724 {
725 //  Broyden Tridiagonal function
726 //     parameters
727 //
728 //     input
729 //        ndim          dimension of the problem to be solved must be even
730 //        x             point at which the function is to be evaluated
731 //     output
732 //        f             function value at x
733 
734 
735   int i;
736   double f1, fi, fn;
737   double x0  = 0.0, xn_plus_1 = 0.0;
738   double p   = 2.0;
739 
740   fx = 1.0;
741 
742   // First the end cases
743   //
744   f1 = fabs((3.0 - 2.0*x(1))*x(1) - x0 - 2.0*x(2) + 1.0);
745   fn = fabs((3.0 - 2.0*x(ndim))*x(ndim) - x(ndim-1) - 2.0*xn_plus_1 + 1.0);
746 
747   fx += pow(f1,p) + pow(fn,p);
748   //
749   // Now take care of all of the cases in between
750   //
751   for (i = 2; i < ndim; i++) {
752     fi  = fabs((3.0 - 2.0*x(i))*x(i) - x(i-1) - 2.0*x(i+1) + 1.0);
753     fx += pow(fi,p);
754   }
755   result = NLPFunction;
756 }
broyden2a(int ndim,const ColumnVector & x,double & fx,int & result)757 void broyden2a(int ndim, const ColumnVector& x, double& fx, int& result)
758 {
759 //  Generalization of Broyden Banded function, p = 7/3
760 //     parameters
761 //
762 //     input
763 //        ndim          dimension of the problem to be solved
764 //        x             point at which the function is to be evaluated
765 //     output
766 //        f             function value at x
767 
768 
769   int i, j;
770   double fi;
771   double sum;
772   double p  = double(7)/double(3);
773   int lower_bound, upper_bound;
774 
775   fx = 1.0;
776 
777   int ml = 5;
778   int mu = 1;
779 
780   for (i = 1; i < ndim; i++) {
781 
782     lower_bound = max(1, i-ml);
783     upper_bound = min(ndim, i+mu);
784 
785     sum = 0.0;
786     for (j = 1; j < ndim; j++) {
787       if ( (j >= lower_bound) & (j <= upper_bound))
788 	sum += x(j)*(1+x(j));
789     }
790 
791     fi  = fabs((2.0 + 5.0*square(x(i)))*x(i) + 1.0 - sum);
792     fx += pow(fi,p);
793   }
794   result = NLPFunction;
795 }
broyden2b(int ndim,const ColumnVector & x,double & fx,int & result)796 void broyden2b(int ndim, const ColumnVector& x, double& fx, int& result)
797 {
798 //  Generalization of Broyden Banded function, p = 2
799 //     parameters
800 //
801 //     input
802 //        ndim          dimension of the problem to be solved must be even
803 //        x             point at which the function is to be evaluated
804 //     output
805 //        f             function value at x
806 
807 
808   int i, j;
809   double fi;
810   double sum;
811   double p  = 2.0;
812   int lower_bound, upper_bound;
813 
814   fx = 1.0;
815 
816   int ml = 5;
817   int mu = 1;
818 
819   for (i = 1; i < ndim; i++) {
820 
821     lower_bound = max(1, i-ml);
822     upper_bound = min(ndim, i+mu);
823 
824     sum = 0.0;
825     for (j = 1; j < ndim; j++) {
826       if ( (j >= lower_bound) & (j <= upper_bound))
827 	sum += x(j)*(1+x(j));
828     }
829 
830     fi  = fabs((2.0 + 5.0*square(x(i)))*x(i) + 1.0 - sum);
831     fx += pow(fi,p);
832   }
833   result = NLPFunction;
834 }
tointbroy(int ndim,const ColumnVector & x,double & fx,int & result)835 void tointbroy(int ndim, const ColumnVector& x, double& fx, int& result)
836 {
837 //  Broyden Tridiagonal function
838 //     parameters
839 //
840 //     input
841 //        ndim          dimension of the problem to be solved must be even
842 //        x             point at which the function is to be evaluated
843 //     output
844 //        f             function value at x
845 
846 
847   int i;
848   double f1, fi, fn;
849   double x0 = 0.0, xn_plus_1 = 0.0;
850   double p  = double(7)/double(3);
851 
852   if (ndim % 2 != 0)
853   {
854     printf ("tointbroy: ndim must be multiple of 2, ndim = %d\n", ndim);
855     exit (1);
856   }
857 
858   fx = 1.0;
859 
860   // First the end cases
861   //
862   f1 = fabs((3.0 - 2.0*x(1))*x(1) - x0 - x(2) + 1.0);
863   fn = fabs((3.0 - 2.0*x(ndim))*x(ndim) - x(ndim-1) - xn_plus_1 + 1.0);
864 
865   fx += pow(f1,p) + pow(fn,p);
866   //
867   // Now take care of all of the cases in between
868   //
869   for (i = 2; i < ndim; i++) {
870     fi  = fabs((3.0 - 2.0*x(i))*x(i) - x(i-1) - x(i+1) + 1.0);
871     fx += pow(fi,p);
872   }
873   //
874   // Add extra term due to Toint
875   //
876   int mid = ndim/2;
877   for (i = 1; i <= ndim/2; i++) {
878     fi  = fabs(x(i) + x(i+mid));
879     fx += pow(fi,p);
880   }
881   result = NLPFunction;
882 }
init_gen_cragg_levy(int ndim,ColumnVector & x)883 void init_gen_cragg_levy (int ndim, ColumnVector& x)
884 {
885 
886   if (ndim % 4 != 0)
887   {
888     printf ("init_gen_wood: ndim must be multiple of 4, ndim = %d\n", ndim);
889     exit (1);
890   }
891 
892   x(1) =  1;
893   for (int i=2; i<=ndim; i++) x(i)   = 2;
894 }
gen_cragg_levy(int ndim,const ColumnVector & x,double & fx,int & result)895 void gen_cragg_levy(int ndim, const ColumnVector& x, double& fx, int& result)
896 {
897 //  Generalized Cragg and Levy
898 //  Conn, Gould, Toint, Test function 17, pp. 422
899 //     parameters
900 //
901 //     input
902 //        ndim          dimension of the problem to be solved must be even
903 //        x             point at which the function is to be evaluated
904 //     output
905 //        f             function value at x
906 
907 
908   int i;
909   double x0, x1, x2, x3;
910   double t1, t2, t3, t4, t5;
911 
912   fx = 0.0;
913   if (ndim % 4 != 0)
914   {
915     printf ("gen_cragg_levy: ndim must be multiple of 4, ndim = %d\n", ndim);
916     exit (1);
917   }
918 
919   //
920   for (i = 1; i <= ndim-3; i=i+4) {
921     x0 = x(i);
922     x1 = x(i+1);
923     x2 = x(i+2);
924     x3 = x(i+3);
925 
926     t1 = pow((exp(x0) - x1),4.0);
927     t2 = 100.0*pow((x1 - x2),6.0);
928     t3 = pow((tan(x2 - x3)),4.0);
929     t4 = pow(x0,8.0);
930     t5 = square(x3 - 1.0);
931     fx += t1 + t2 + t3 + t4 + t5;
932   }
933   result = NLPFunction;
934 }
init_toint_trig(int ndim,ColumnVector & x)935 void init_toint_trig (int ndim, ColumnVector& x)
936 {
937 
938   for (int i = 1; i <= ndim; i++) x(i)   = 1.0;
939 
940 }void toint_trig(int ndim, const ColumnVector& x, double& fx, int& result)
941 {
942 //  Toint Trig
943 //     parameters
944 //
945 //     input
946 //        ndim          dimension of the problem to be solved
947 //        x             point at which the function is to be evaluated
948 //     output
949 //        f             function value at x
950 
951 
952   int i, j;
953   ColumnVector beta(ndim);
954   double alpha_ij, c_ij, t;
955   fx = 0.0;
956 
957   for (i = 1; i <= ndim; i++) {
958     beta(i) = 1.0 + double(i)/10.0;
959   }
960 
961   for (i = 1; i <= ndim; i++) {
962 
963     for (j = 1; j <= ndim; j++) {
964 
965       if ((abs(i-j))%4 == 0) {
966 	alpha_ij = 5.0*(1.0 + (i%5) + (j%5));
967 	c_ij     = double(i+j)/10.0;
968 	t        = beta(i)*x(i) + beta(j)*x(j) + c_ij;
969 	fx      += alpha_ij*sin(t);
970       }
971     }
972   }
973   result = NLPFunction;
974 }
init_chebyquad(int ndim,ColumnVector & x)975 void init_chebyquad (int ndim, ColumnVector& x)
976 {
977 
978   for (int i = 1; i <= ndim; i++) x(i)   = double(i)/double(ndim+1);
979 
980 }void chebyquad(int ndim, const ColumnVector& x, double& fx, int& result)
981 {
982 //  Chebyquad
983 //     parameters
984 //
985 //     input
986 //        ndim          dimension of the problem to be solved
987 //        x             point at which the function is to be evaluated
988 //     output
989 //        f             function value at x
990 
991 
992   int i, i2, j;
993   int m2   = ndim/2;
994   int mdim = ndim;
995 
996   ColumnVector fi(ndim);
997   double coeff = 1.0/double(ndim);
998   double t, t0, t1, tj;
999 
1000   fx = 0.0;
1001   fi = 0.0;
1002 
1003   for (i = 1; i <= m2; i++) {
1004     i2 = 2*i;
1005     fi(i2) = 1.0 / double((i2*i2) - 1.0);
1006   }
1007 
1008   for (j = 1; j <= ndim; j++) {
1009     t0 = 1.0;
1010     t1 = 2.0 * x(j) - 1.0;
1011     tj = t1;
1012     fi(1) = fi(1) + coeff*t1;
1013 
1014     for (i = 2; i <= mdim; i++) {
1015       t = 2.0 * tj * t1 - t0;
1016       fi(i) = fi(i) + coeff*t;
1017       t0 = t1;
1018       t1 = t;
1019     }
1020   }
1021   for (i = 1; i <= ndim; i++) {
1022     fx += fi(i)*fi(i);
1023   }
1024 
1025   result = NLPFunction;
1026 }
init_nelder(int ndim,ColumnVector & x)1027 void init_nelder (int ndim, ColumnVector& x)
1028 {
1029 
1030   for (int i = 1; i <= ndim; i++) x(i)   = double(i)/double(ndim+1);
1031 
1032 }
nelder(int ndim,const ColumnVector & x,double & fx,int & result)1033 void nelder(int ndim, const ColumnVector& x, double& fx, int& result)
1034 {
1035 //  Nelder
1036 //     parameters
1037 //
1038 //     input
1039 //        ndim          dimension of the problem to be solved
1040 //        x             point at which the function is to be evaluated
1041 //     output
1042 //        f             function value at x
1043 
1044   double f1, f2, f3;
1045 
1046   if (ndim != 2)
1047   {
1048     exit (1);
1049   }
1050 
1051   f1 = x(4) - x(1)*x(2)*x(3);
1052   f2 = x(3) - x(1)*x(2);
1053   f3 = x(2) - x(1);
1054 
1055   fx = f1*f1 + f2*f2 + f3*f3 + x(1)*x(1);
1056 
1057   result = NLPFunction;
1058 }
1059