cisst-saw
Loading...
Searching...
No Matches
nmrFminSolver.h
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3
4/*
5
6 Author(s): Ankur Kapoor
7 Created on: 2004-10-26
8
9 (C) Copyright 2004-2007 Johns Hopkins University (JHU), All Rights
10 Reserved.
11
12--- begin cisst license - do not edit ---
13
14This software is provided "as is" under an open source license, with
15no warranty. The complete license can be found in license.txt and
16http://www.cisst.org/cisst/license.txt.
17
18--- end cisst license ---
19*/
20
21
26
27
28#ifndef _nmrFminSolver_h
29#define _nmrFminSolver_h
30
31
34
35#include <cnetlib.h>
36#include <string.h>
37
42
44 // we have this class so that we reserve memory only one
45 // would help if svd of a same size matrix (or a matrix)
46 // that doesnt change much is desired.
47
48protected:
49 struct O8USERFUNC UserFn;
50 struct O8USERCONFIG UserCfg;
51 CISSTNETLIB_INTEGER N, NLin, NNonLin, NIter, NStep;
52
53public:
59 {
60 Allocate(0, 0, 0, 4000, 20);
61 }
62
63
76 nmrFminSolver(CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nlin, CISSTNETLIB_INTEGER nnonlin, CISSTNETLIB_INTEGER niter, CISSTNETLIB_INTEGER nstep)
77 {
78 Allocate(n, nlin, nnonlin, niter, nstep);
79 }
80
82 {
83 donlp2_memory_free();
84 }
85
91 inline void Allocate(CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nlin, CISSTNETLIB_INTEGER nnonlin, CISSTNETLIB_INTEGER niter, CISSTNETLIB_INTEGER nstep)
92 {
93 memset (&UserFn, 0, sizeof(struct O8USERFUNC));
94 donlp2_init_size(n, nlin, nnonlin, niter, nstep);
95 donlp2_memory_malloc();
96 N = n; NLin = nlin; NNonLin = nnonlin; NIter = niter; NStep = nstep;
97 }
98
99 inline struct O8USERCONFIG & GetOptions(void) {
100 donlp2_getoptions(&UserCfg);
101 return this->UserCfg;
102 }
103
104 inline void SetOptions(struct O8USERCONFIG & userCfg) {
105 memcpy (&UserCfg, &userCfg, sizeof(struct O8USERCONFIG));
106 donlp2_setoptions(&UserCfg);
107 }
108
109 inline void PrintOptions(void) {
110 donlp2_printoptions(stderr);
111 }
112
113 template <int __instanceLine, class __elementType>
115 throw (std::runtime_error) {
116 if (N+1 != static_cast<CISSTNETLIB_INTEGER>(X.size())) {
117 cmnThrow(std::runtime_error("nmrFminSolver Solve: Size used for Allocate was different"));
118 }
119 /* set the user functions */
120 UserFn.user_ef = callBack.FunctionFdonlp2;
121 donlp2_setuserfunctions(&UserFn);
122 donlp2_setuserinit(X.Pointer(), NULL, NULL, NULL);
123 /* call the DONLP2 C function */
124 donlp2();
125 CISSTNETLIB_DOUBLE fx;
126 donlp2_getresult(X.Pointer(), &fx);
127 }
128
129 template <int __instanceLine, class __elementType>
133 throw (std::runtime_error) {
134 if (N+1 != static_cast<CISSTNETLIB_INTEGER>(X.size())) {
135 cmnThrow(std::runtime_error("nmrFminSolver Solve: Size used for Allocate was different"));
136 }
137 /* set the user functions */
138 UserFn.user_ef = callBack.FunctionFdonlp2;
139 donlp2_setuserfunctions(&UserFn);
141 alinRowPointers.SetSize(alin.rows());
142 for (unsigned int i = 0; i < alin.rows(); i++) {
143 alinRowPointers(i) = alin.Row(i).Pointer();
144 }
145 donlp2_setuserinit(X.Pointer(), lbound.Pointer(), ubound.Pointer(), alinRowPointers.Pointer());
146 /* call the DONLP2 C function */
147 donlp2();
148 CISSTNETLIB_DOUBLE fx;
149 donlp2_getresult(X.Pointer(), &fx);
150 }
151
152 template <int __instanceLineF, class __elementTypeF, int __instanceLineC, class __elementTypeC>
156 throw (std::runtime_error) {
157 if (N+1 != static_cast<CISSTNETLIB_INTEGER>(X.size())) {
158 cmnThrow(std::runtime_error("nmrFminSolver Solve: Size used for Allocate was different"));
159 }
160 /* set the user functions */
161 UserFn.user_ef = callBackF.FunctionFdonlp2;
162 UserFn.user_econ = callBackC.FunctionFdonlp2;
163 donlp2_setuserfunctions(&UserFn);
164 donlp2_setuserinit(X.Pointer(), lbound.Pointer(), ubound.Pointer(), NULL);
165 /* call the DONLP2 C function */
166 donlp2();
167 CISSTNETLIB_DOUBLE fx;
168 donlp2_getresult(X.Pointer(), &fx);
169 }
170
171 template <int __instanceLineF, class __elementTypeF, int __instanceLineC, class __elementTypeC>
176 // vctDynamicMatrix<CISSTNETLIB_DOUBLE> &alin)
177 throw (std::runtime_error) {
178 if (N+1 != static_cast<CISSTNETLIB_INTEGER>(X.size())) {
179 cmnThrow(std::runtime_error("nmrFminSolver Solve: Size used for Allocate was different"));
180 }
181 /* set the user functions */
182 UserFn.user_ef = callBackF.FunctionFdonlp2;
183 UserFn.user_econ = callBackC.FunctionFdonlp2;
184 donlp2_setuserfunctions(&UserFn);
186 alinRowPointers.SetSize(alin.rows());
187 for (unsigned int i = 0; i < alin.rows(); i++) {
188 alinRowPointers(i) = alin.Row(i).Pointer();
189 }
190 donlp2_setuserinit(X.Pointer(), lbound.Pointer(), ubound.Pointer(), alinRowPointers.Pointer());
191 //donlp2_setuserinit(X.Pointer(), lbound.Pointer(), ubound.Pointer(), alina);
192 /* call the DONLP2 C function */
193 donlp2();
194 CISSTNETLIB_DOUBLE fx;
195 donlp2_getresult(X.Pointer(), &fx);
196 }
197
198 inline void Solve(void (*efFunctionPointer)(CISSTNETLIB_INTEGER,CISSTNETLIB_DOUBLE[],CISSTNETLIB_DOUBLE*),
200 void (*econFunctionPointer)(CISSTNETLIB_INTEGER,CISSTNETLIB_INTEGER,CISSTNETLIB_INTEGER,CISSTNETLIB_INTEGER[],CISSTNETLIB_DOUBLE[],CISSTNETLIB_DOUBLE[],CISSTNETLIB_INTEGER[]),
203 vctDynamicMatrix<CISSTNETLIB_DOUBLE> &alin) throw (std::runtime_error) {
204 if (N+1 != static_cast<CISSTNETLIB_INTEGER>(X.size())) {
205 cmnThrow(std::runtime_error("nmrFminSolver Solve: Size used for Allocate was different"));
206 }
207 UserFn.user_ef = efFunctionPointer;
208 UserFn.user_econ = econFunctionPointer;
209 donlp2_setuserfunctions(&UserFn);
211 alinRowPointers.SetSize(alin.rows());
212 for (unsigned int i = 0; i < alin.rows(); i++) {
213 alinRowPointers(i) = alin.Row(i).Pointer();
214 }
215 donlp2_setuserinit(X.Pointer(), lbound.Pointer(), ubound.Pointer(), alinRowPointers.Pointer());
216 //donlp2_setuserinit(X.Pointer(), lbound.Pointer(), ubound.Pointer(), NULL);
217 /* call the DONLP2 C function */
218 donlp2();
219 CISSTNETLIB_DOUBLE fx;
220 donlp2_getresult(X.Pointer(), &fx);
221 }
222
223#if 0
224 template <int __instanceLineF, class __elementTypeF, int __instanceLineG, class __elementTypeG,
225 int __instanceLineC, class __elementTypeC, int __instanceLineCG, class __elementTypeCG>
229 nmrCallBackFunctionCG<__instanceLineCG, __elementTypeCG> &callBackCG,
233 throw (std::runtime_error) {
234 if (N != static_cast<CISSTNETLIB_INTEGER>(X.size())) {
235 cmnThrow(std::runtime_error("nmrFminSolver Solve: Size used for Allocate was different"));
236 }
237 /* set the user functions */
238 UserFn.user_ef = callBackF.FunctionFdonlp2;
239 UserFn.user_egradf = callBackG.FunctionFdonlp2;
240 UserFn.user_econ = callBackC.FunctionFdonlp2;
241 UserFn.user_econgrad = callBackCG.FunctionFdonlp2;
242 donlp2_setuserfunctions(&UserFn);
243 donlp2_setuserinit(X.Pointer, Lb.Pointer(), Ub.Pointer(), ALin.Pointer());
244 donlp2();
245 /* call the DONLP2 C function */
246 }
247#endif
248};
249
250#endif // _nmrFminSolver_h
251
Definition nmrCallBack.h:204
Definition nmrCallBack.h:169
Definition nmrCallBack.h:45
CISSTNETLIB_INTEGER NLin
Definition nmrFminSolver.h:51
void Solve(nmrCallBackFunctionF1< __instanceLineF, __elementTypeF > &callBackF, vctDynamicVector< CISSTNETLIB_DOUBLE > &X, nmrCallBackFunctionC< __instanceLineC, __elementTypeC > &callBackC, vctDynamicVector< CISSTNETLIB_DOUBLE > &lbound, vctDynamicVector< CISSTNETLIB_DOUBLE > &ubound, vctDynamicMatrix< CISSTNETLIB_DOUBLE > &alin)
Definition nmrFminSolver.h:172
CISSTNETLIB_INTEGER NNonLin
Definition nmrFminSolver.h:51
void Solve(nmrCallBackFunctionF1< __instanceLine, __elementType > &callBack, vctDynamicVector< CISSTNETLIB_DOUBLE > &X)
Definition nmrFminSolver.h:114
nmrFminSolver(CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nlin, CISSTNETLIB_INTEGER nnonlin, CISSTNETLIB_INTEGER niter, CISSTNETLIB_INTEGER nstep)
Definition nmrFminSolver.h:76
void Allocate(CISSTNETLIB_INTEGER n, CISSTNETLIB_INTEGER nlin, CISSTNETLIB_INTEGER nnonlin, CISSTNETLIB_INTEGER niter, CISSTNETLIB_INTEGER nstep)
Definition nmrFminSolver.h:91
struct O8USERCONFIG & GetOptions(void)
Definition nmrFminSolver.h:99
void Solve(nmrCallBackFunctionF1< __instanceLineF, __elementTypeF > &callBackF, vctDynamicVector< CISSTNETLIB_DOUBLE > &X, nmrCallBackFunctionC< __instanceLineC, __elementTypeC > &callBackC, vctDynamicVector< CISSTNETLIB_DOUBLE > &lbound, vctDynamicVector< CISSTNETLIB_DOUBLE > &ubound)
Definition nmrFminSolver.h:153
CISSTNETLIB_INTEGER NStep
Definition nmrFminSolver.h:51
void Solve(void(*efFunctionPointer)(CISSTNETLIB_INTEGER, CISSTNETLIB_DOUBLE[], CISSTNETLIB_DOUBLE *), vctDynamicVector< CISSTNETLIB_DOUBLE > &X, void(*econFunctionPointer)(CISSTNETLIB_INTEGER, CISSTNETLIB_INTEGER, CISSTNETLIB_INTEGER, CISSTNETLIB_INTEGER[], CISSTNETLIB_DOUBLE[], CISSTNETLIB_DOUBLE[], CISSTNETLIB_INTEGER[]), vctDynamicVector< CISSTNETLIB_DOUBLE > &lbound, vctDynamicVector< CISSTNETLIB_DOUBLE > &ubound, vctDynamicMatrix< CISSTNETLIB_DOUBLE > &alin)
Definition nmrFminSolver.h:198
~nmrFminSolver()
Definition nmrFminSolver.h:81
struct O8USERCONFIG UserCfg
Definition nmrFminSolver.h:50
void SetOptions(struct O8USERCONFIG &userCfg)
Definition nmrFminSolver.h:104
nmrFminSolver(void)
Definition nmrFminSolver.h:58
void PrintOptions(void)
Definition nmrFminSolver.h:109
void Solve(nmrCallBackFunctionF1< __instanceLine, __elementType > &callBack, vctDynamicVector< CISSTNETLIB_DOUBLE > &X, vctDynamicVector< CISSTNETLIB_DOUBLE > &lbound, vctDynamicVector< CISSTNETLIB_DOUBLE > &ubound, vctDynamicMatrix< CISSTNETLIB_DOUBLE > &alin)
Definition nmrFminSolver.h:130
struct O8USERFUNC UserFn
Definition nmrFminSolver.h:49
CISSTNETLIB_INTEGER N
Definition nmrFminSolver.h:51
CISSTNETLIB_INTEGER NIter
Definition nmrFminSolver.h:51
Definition vctForwardDeclarations.h:157
Definition vctForwardDeclarations.h:131
void cmnThrow(const _exceptionType &except, cmnLogLevel lod=CMN_LOG_LEVEL_INIT_ERROR)
Definition cmnThrow.h:76
Declaration of nmrCallBackLSNonLinSolver.
const value_type & X(void) const
Definition vctDynamicConstVectorBase.h:263
Declaration of vctDynamicMatrix.