1 /*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File: sfunc_michalewitz.cpp
4 Revision: $Id: sfunc_michalewitz.cpp 171 2010-10-04 13:57:19Z kulshres $
5 Contents: function module containing Michalewitz' function
6
7 Each << function module >> contains:
8
9 (1) const char* const controlFileName
10 (2) int indepDim;
11 (3) void initProblemParameters( void )
12 (4) void initIndependents( double* indeps )
13 (5) double originalScalarFunction( double* indeps )
14 (6) double tapingScalarFunction( int tag, double* indeps )
15
16 Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
17 Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
18
19 This file is part of ADOL-C. This software is provided as open source.
20 Any use, reproduction, or distribution of the software constitutes
21 recipient's acceptance of the terms of the accompanying license file.
22
23 ---------------------------------------------------------------------------*/
24 #define _SFUNC_MICHALEWITZ_C_
25
26
27 /****************************************************************************/
28 /* INCLUDES */
29 #include <adolc/adolc.h>
30
31 #include <cmath>
32
33
34 /****************************************************************************/
35 /* GLOBAL VARIABLES */
36
37 /*--------------------------------------------------------------------------*/
38 /* Control file name */
39 const char* controlFileName = "michalewitzexam.ctrl";
40
41 /*--------------------------------------------------------------------------*/
42 /* Dimensions */
43 int indepDim;
44
45 /*--------------------------------------------------------------------------*/
46 /* Other problem dependent parameters */
47 #define Pi 3.141592654
48 const double M = 10.0;
49
50
51 /****************************************************************************/
52 /* INIT PROBLEM PARAMETERS */
initProblemParameters(void)53 void initProblemParameters( void ) {
54 fprintf(stdout,"MICHALEWITZ' FUNCTION (ADOL-C Example)\n\n");
55 if (indepDim <= 0) {
56 fprintf(stdout," number of independent variables = ? ");
57 fscanf(stdin,"%d",&indepDim);
58 fprintf(stdout,"\n");
59 }
60 }
61
62
63 /****************************************************************************/
64 /* INITIALIZE INDEPs */
initIndependents(double * indeps)65 void initIndependents( double* indeps ) {
66 int i;
67 for (i=0; i<indepDim; i++)
68 indeps[i] = Pi*(i+1.0)/(2.0+i);
69 }
70
71
72 /****************************************************************************/
73 /* ORIGINAL SCALAR FUNCTION */
74
75 /*--------------------------------------------------------------------------*/
76 /* Michalewitz' function */
micha(int dim,double * indeps)77 double micha( int dim, double* indeps ) {
78 int i;
79 double u = 0;
80 for (i=0; i<dim; i++)
81 u += sin(indeps[i]) * pow(sin((i+1)*indeps[i]*indeps[i]/Pi),2.0*M);
82 return -u;
83 }
84
85 /*--------------------------------------------------------------------------*/
86 /* The interface function */
originalScalarFunction(double * indeps)87 double originalScalarFunction( double* indeps ) {
88 return micha(indepDim, indeps);
89 }
90
91
92 /****************************************************************************/
93 /* TAPING SCALAR FUNCTION */
94
95 /*--------------------------------------------------------------------------*/
96 /* active Michalewitz' function */
activeMicha(int dim,adouble * indeps)97 adouble activeMicha( int dim, adouble* indeps ) {
98 int i;
99 adouble u = 0;
100 for (i=0; i<dim; i++)
101 u += sin(indeps[i]) * pow(sin((i+1)*indeps[i]*indeps[i]/Pi),2.0*M);
102 return -u;
103 }
104
105 /*--------------------------------------------------------------------------*/
106 /* The interface function */
tapingScalarFunction(int tag,double * indeps)107 double tapingScalarFunction( int tag, double* indeps ) {
108 int i;
109 trace_on(tag);
110 adouble* activeIndeps = new adouble[indepDim];
111 adouble* aIP = activeIndeps;
112 double* iP = indeps;
113 for (i=0; i<indepDim; i++)
114 *aIP++ <<= *iP++;
115 adouble ares = activeMicha(indepDim, activeIndeps);
116 double res = 0;
117 ares >>= res;
118 trace_off();
119 return res;
120 }
121
122 #undef _SFUNC_MICHALEWITZ_C_
123
124
125
126
127
128