File: ffnewuoa.cpp

package info (click to toggle)
freefem%2B%2B 3.61.1%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,108 kB
  • sloc: cpp: 141,214; ansic: 28,664; sh: 4,925; makefile: 3,142; fortran: 1,171; perl: 844; awk: 290; php: 199; pascal: 41; f90: 32
file content (178 lines) | stat: -rw-r--r-- 6,201 bytes parent folder | download
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
/****************************************************************************/
/* This file is part of FreeFem++.                                          */
/*                                                                          */
/* FreeFem++ is free software: you can redistribute it and/or modify        */
/* it under the terms of the GNU Lesser General Public License as           */
/* published by the Free Software Foundation, either version 3 of           */
/* the License, or (at your option) any later version.                      */
/*                                                                          */
/* FreeFem++ 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 Lesser General Public License for more details.                      */
/*                                                                          */
/* You should have received a copy of the GNU Lesser General Public License */
/* along with FreeFem++. If not, see <http://www.gnu.org/licenses/>.        */
/****************************************************************************/
// SUMMARY : ...
// LICENSE : LGPLv3
// ORG     : LJLL Universite Pierre et Marie Curie, Paris, FRANCE
// AUTHORS : Frederic Hecht
// E-MAIL  : frederic.hecht@sorbonne-universite.fr

// *INDENT-OFF* //
//ff-c++-LIBRARY-dep:
//ff-c++-cpp-dep: newuoa.f
// *INDENT-ON* //

#include  <iostream>
#include  <cfloat>
using namespace std;
#include "error.hpp"
#include "AFunction.hpp"
#include "rgraph.hpp"
#include "RNM.hpp"
#include "MatriceCreuse_tpl.hpp"
#include "Mesh3dn.hpp"
#include "MeshPoint.hpp"
#include "lgfem.hpp"
#include "lgmesh3.hpp"
#include "lgsolver.hpp"
#include "problem.hpp"

typedef int integer;
typedef int logical;
typedef void (*typecalfunc)(integer *, double *, double *f, void *);

#define F77newuoa newuoa_

extern "C" {
	double F77newuoa (integer *N, integer *NPT, double *x, double *rhob, double *rhog,
	                  integer *iprint, integer *maxfun,
	                  double *w, void *iwf, typecalfunc calfun);
}

void calfun (integer *n, double *x, double *f, void *t);

// template<class R>
extern Block *currentblock;

typedef double R;
void calfun (integer *n, double *x, double *f, void *t);
class OptimNewoa: public OneOperator
{
	public:
		typedef KN<R> Kn;
		typedef KN_<R> Kn_;
		const int cas;

		class ffcalfunc {	// to call the freefem function .. J
			public:
				Stack stack;
				Expression JJ, theparame;

				ffcalfunc (Stack s, Expression JJJ, Expression epar)
					: stack(s), JJ(JJJ), theparame(epar) {}

				double J (Kn_ x) const {
					KN<double> *p = GetAny<KN<double> *>((*theparame)(stack));
					*p = x;
					double ret = GetAny<R>((*JJ)(stack));
					WhereStackOfPtr2Free(stack)->clean();
					return ret;
				}
		};

		class E_newoa: public E_F0mps {
			public:
				const int cas;
				static basicAC_F0::name_and_type name_param [];
				static const int n_name_param = 4;
				Expression nargs[n_name_param];
				Expression X;
				C_F0 inittheparam, theparam, closetheparam;
				Expression JJ;
				long arg (int i, Stack stack, long a) const {return nargs[i] ? GetAny<long>((*nargs[i])(stack)) : a;}

				R arg (int i, Stack stack, R a) const {return nargs[i] ? GetAny<R>((*nargs[i])(stack)) : a;}

				E_newoa (const basicAC_F0 &args, int cc):
					cas(cc) {
					int nbj = args.size() - 1;

					Block::open(currentblock);	// make a new block to
					X = to<Kn *>(args[nbj]);
					C_F0 X_n(args[nbj], "n");
					// the expression to init the theparam of all
					inittheparam = currentblock->NewVar<LocalVariable>("the parameter", atype<KN<R> *>(), X_n);
					theparam = currentblock->Find("the parameter");	// the expression for the parameter
					args.SetNameParam(n_name_param, name_param, nargs);
					const Polymorphic *opJ = 0;
					if (nbj > 0) {
						opJ = dynamic_cast<const Polymorphic *>(args[0].LeftValue());
						assert(opJ);
					}

					JJ = to<R>(C_F0(opJ, "(", theparam));
					// closetheparam=currentblock->close(currentblock);   // the cleanning block expression
					closetheparam = C_F0((Expression)Block::snewclose(currentblock), atype<void>());
				}

				virtual AnyType operator () (Stack stack)  const {
					double cost = 1e100;

					WhereStackOfPtr2Free(stack) = new StackOfPtr2Free(stack);	// FH mars 2005
					Kn &x = *GetAny<Kn *>((*X)(stack));
					long n = x.N();
					double rhobeg = arg(0, stack, 1E-6);// not used ....
					double rhoend = arg(1, stack, 2.);	// not used ....
					long maxfun = arg(2, stack, 1000L);	// bof bof
					long npt = arg(3, stack, n * 2L + 1L);	// bof bof
					long iprint = verbosity;
					ffcalfunc ffJ(stack, JJ, theparam);
					int lw = (npt + 13) * (npt + n) + 3 * n * (n + 3) / 2;
					KN<double> w(lw);
					integer N = n, NPT = npt, IPRINT = iprint, MAXFUN = maxfun;
					cost = F77newuoa(&N, &NPT, (double *)x, &rhobeg, &rhoend, &IPRINT, &MAXFUN, (double *)w, (void *)&ffJ, calfun);
					closetheparam.eval(stack);	// clean memory
					WhereStackOfPtr2Free(stack)->clean();	// FH mars 2005
					return cost;// SetAny<long>(0);  Modif FH  july 2005
				}

				operator aType () const {return atype<double>();}};

		E_F0*code (const basicAC_F0 &args) const {
			return new E_newoa(args, cas);
		}

		OptimNewoa (int c):   OneOperator(atype<double>(),
			                              atype<Polymorphic *>(),
			                              atype<KN<R> *>()), cas(c) {}
};

basicAC_F0::name_and_type OptimNewoa::E_newoa::name_param [] =
{
	{"rhobeg", &typeid(double)},
	{"rhoend", &typeid(double)},
	{"maxfun", &typeid(long)},
	{"npt", &typeid(long)}
};

void calfun (integer *n, double *x, double *f, void *t) {
	OptimNewoa::ffcalfunc *tt = static_cast<OptimNewoa::ffcalfunc *>(t);

	*f = tt->J(KN_<double>(x, *n));
	if (verbosity > 20) {cout << " F= " << *f << endl;}
}

/*  class Init { public:
 * Init();
 * };
 *
 * $1 */

static void Load_Init () {	// le constructeur qui ajoute la fonction "splitmesh3"  a freefem++
	Global.Add("newuoa", "(", new OptimNewoa(1));	// j + dJ
}

LOADFUNC(Load_Init)