CONOPT
Loading...
Searching...
No Matches
leastsq10.f90
Go to the documentation of this file.
1!> @file leastsq10.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This model is similar to leastsq5, but this time we define 2nd order
6!! directional information in the form of both an `2DDirIni` routine
7!! where the bulk of the work is done and a `2DDir` routine that just
8!! copies the information.
9!!
10!! We solve the following nonlinear least squares model:
11!!
12!! \f[
13!! \min \sum_i res_{i}^2 \\
14!!
15!! \sum_j ( a_{ij}x_j + b_{ij}x_j^2 ) + res_i = obs_i
16!! \f]
17!!
18!! where \f$a\f$, \f$b\f$, and \f$obs\f$ are known data, and \f$res\f$ and \f$x\f$ are the
19!! variables of the model.
20!!
21!!
22!! For more information about the individual callbacks, please have a look at the source code.
23
24#if defined(_WIN32) && !defined(_WIN64)
25#define dec_directives_win32
26#endif
27
28REAL FUNCTION rndx( )
29!
30! Defines a pseudo random number between 0 and 1
31!
32 IMPLICIT NONE
33
34 Integer, save :: seed = 12359
35
36 seed = mod(seed*1027+25,1048576)
37 rndx = float(seed)/float(1048576)
38
39END FUNCTION rndx
40
41subroutine defdata
42!
43! Define values for A, B, and Obs
44!
45 Use lsq_700
46 IMPLICIT NONE
47
48 Integer :: i, j
49 real*8, Parameter :: xtarg = -1.0
50 real*8, Parameter :: noise = 1.0
51 Real, External :: Rndx
52 real*8 :: o
53
54 do i = 1, nobs
55 o = 0.d0
56 do j = 1, dimx
57 a(i,j) = rndx()
58 b(i,j) = rndx()
59 o = o + a(i,j) * xtarg + b(i,j) * xtarg**2
60 enddo
61 obs(i) = o + noise * rndx()
62 enddo
63
64end subroutine defdata
65!
66! Main program.
67!
68!> Main program. A simple setup and call of CONOPT
69!!
70Program leastsquare
71
73 Use conopt
74 Use lsq_700
75 implicit None
76!
77! Declare the user callback routines as Integer, External:
78!
79 Integer, External :: lsq_readmatrix ! Mandatory Matrix definition routine defined below
80 Integer, External :: lsq_fdeval ! Function and Derivative evaluation routine
81 ! needed a nonlinear model.
82 Integer, External :: lsq_2ddirini ! Initialization routine for Directional 2nd derivatives
83 Integer, External :: lsq_2ddir ! Directional 2nd derivative routine
84 Integer, External :: std_status ! Standard callback for displaying solution status
85 Integer, External :: std_solution ! Standard callback for displaying solution values
86 Integer, External :: std_message ! Standard callback for managing messages
87 Integer, External :: std_errmsg ! Standard callback for managing error messages
88#ifdef dec_directives_win32
89!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_ReadMatrix
90!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_FDEval
91!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_2DDirIni
92!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_2DDir
93!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
94!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
95!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
96!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
97#endif
98!
99! Control vector
100!
101 INTEGER, Dimension(:), Pointer :: cntvect
102 INTEGER :: coi_error
103
104 call startup
105!
106! Define data
107!
108 Call defdata
109!
110! Create and initialize a Control Vector
111!
112 coi_error = coi_create( cntvect )
113!
114! Tell CONOPT about the size of the model by populating the Control Vector:
115!
116 coi_error = max( coi_error, coidef_numvar( cntvect, nobs + dimx ) )
117 coi_error = max( coi_error, coidef_numcon( cntvect, nobs + 1 ) )
118 coi_error = max( coi_error, coidef_numnz( cntvect, nobs * (dimx+2) ) )
119 coi_error = max( coi_error, coidef_numnlnz( cntvect, nobs * (dimx+1) ) )
120 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
121 coi_error = max( coi_error, coidef_objcon( cntvect, nobs + 1 ) ) ! Objective is last constraint
122 coi_error = max( coi_error, coidef_optfile( cntvect, 'leastsq10.opt' ) )
123!
124! Tell CONOPT about the callback routines:
125!
126 coi_error = max( coi_error, coidef_readmatrix( cntvect, lsq_readmatrix ) )
127 coi_error = max( coi_error, coidef_fdeval( cntvect, lsq_fdeval ) )
128 coi_error = max( coi_error, coidef_2ddirini( cntvect, lsq_2ddirini ) )
129 coi_error = max( coi_error, coidef_2ddir( cntvect, lsq_2ddir ) )
130 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
131 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
132 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
133 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
134 coi_error = max( coi_error, coidef_debugfv( cntvect, 0 ) ) ! Debug Fdeval on or off
135
136#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
137 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
138#endif
139
140 If ( coi_error .ne. 0 ) THEN
141 write(*,*)
142 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
143 write(*,*)
144 call flog( "Skipping Solve due to setup errors", 1 )
145 ENDIF
146!
147! Start CONOPT:
148!
149 coi_error = coi_solve( cntvect )
150
151 write(*,*)
152 write(*,*) 'End of Least Square example 10. Return code=',coi_error
153
154 If ( coi_error /= 0 ) then
155 call flog( "Errors encountered during solution", 1 )
156 elseif ( stacalls == 0 .or. solcalls == 0 ) then
157 call flog( "Status or Solution routine was not called", 1 )
158 elseif ( sstat /= 1 .or. mstat /= 2 ) then
159 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
160 elseif ( abs( obj - 19.44434311d0 ) > 1.d-7 ) then
161 call flog( "Incorrect objective returned", 1 )
162 endif
163
164 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
165
166 call flog( "Successful Solve", 0 )
167
168End Program leastsquare
169!
170! ============================================================================
171! Define information about the model:
172!
173
174!> Define information about the model
175!!
176!! @include{doc} readMatrix_params.dox
177Integer Function lsq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
178 colsta, rowno, value, nlflag, n, m, nz, &
179 usrmem )
180#ifdef dec_directives_win32
181!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_ReadMatrix
182#endif
183 Use lsq_700
184 implicit none
185 integer, intent (in) :: n ! number of variables
186 integer, intent (in) :: m ! number of constraints
187 integer, intent (in) :: nz ! number of nonzeros
188 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
189 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
190 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
191 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
192 ! (not defined here)
193 integer, intent (out), dimension(m) :: type ! vector of equation types
194 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
195 ! (not defined here)
196 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
197 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
198 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
199 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
200 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
201 real*8 usrmem(*) ! optional user memory
202
203 Integer :: i, j, k
204!
205! Information about Variables:
206! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
207! Default: the status information in Vsta is not used.
208!
209 do i = 1, dimx
210 curr(i) = -0.8d0
211 enddo
212!
213! Information about Constraints:
214! Default: Rhs = 0
215! Default: the status information in Esta and the function
216! value in FV are not used.
217! Default: Type: There is no default.
218! 0 = Equality,
219! 1 = Greater than or equal,
220! 2 = Less than or equal,
221! 3 = Non binding.
222!
223! Constraints 1 to Nobs:
224! Rhs = Obs(i) and type Equality
225!
226 do i = 1, nobs
227 rhs(i) = obs(i)
228 type(i) = 0
229 enddo
230!
231! Constraint Nobs + 1 (Objective)
232! Rhs = 0 and type Non binding
233!
234 type(nobs+1) = 3
235!
236! Information about the Jacobian. CONOPT expects a columnwise
237! representation in Rowno, Value, Nlflag and Colsta.
238!
239! Colsta = Start of column indices (No Defaults):
240! Rowno = Row indices
241! Value = Value of derivative (by default only linear
242! derivatives are used)
243! Nlflag = 0 for linear and 1 for nonlinear derivative
244! (not needed for completely linear models)
245!
246!
247! Indices
248! x(i) res(j)
249! j: NL L=1
250! obj: NL
251! 3:
252!
253 k = 1
254 do j = 1, dimx
255 colsta(j) = k
256 do i = 1, nobs
257 rowno(k) = i
258 nlflag(k) = 1
259 k = k + 1
260 enddo
261 enddo
262 do i = 1, nobs
263 colsta(dimx+i) = k
264 rowno(k) = i
265 nlflag(k) = 0
266 value(k) = 1.d0
267 k = k + 1
268 rowno(k) = nobs+1
269 nlflag(k) = 1
270 k = k + 1
271 enddo
272 colsta(dimx+nobs+1) = k
273
274 lsq_readmatrix = 0 ! Return value means OK
275
276end Function lsq_readmatrix
277!
278!==========================================================================
279! Compute nonlinear terms and non-constant Jacobian elements
280!
281
282!> Compute nonlinear terms and non-constant Jacobian elements
283!!
284!! @include{doc} fdeval_params.dox
285Integer Recursive function lsq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
286 n, nz, thread, usrmem )
287#ifdef dec_directives_win32
288!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_FDEval
289#endif
290 use lsq_700
291 implicit none
292 integer, intent (in) :: n ! number of variables
293 integer, intent (in) :: rowno ! number of the row to be evaluated
294 integer, intent (in) :: nz ! number of nonzeros in this row
295 real*8, intent (in), dimension(n) :: x ! vector of current solution values
296 real*8, intent (in out) :: g ! constraint value
297 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
298 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
299 ! in this row. Ffor information only.
300 integer, intent (in) :: mode ! evaluation mode: 1 = function value
301 ! 2 = derivatives, 3 = both
302 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
303 ! as errcnt is incremented
304 integer, intent (in out) :: errcnt ! error counter to be incremented in case
305 ! of function evaluation errors.
306 integer, intent (in) :: thread
307 real*8 usrmem(*) ! optional user memory
308
309 integer :: i,j
310 real*8 :: s
311!
312! Row 1: the objective function is nonlinear
313!
314 if ( rowno .eq. nobs+1 ) then
315!
316! Mode = 1 or 3. Function value: G = sum(i, res(i)**2)
317!
318 if ( mode .eq. 1 .or. mode .eq. 3 ) then
319 s = 0.d0
320 do i = 1, nobs
321 s = s + x(dimx+i)**2
322 enddo
323 g = s
324 endif
325!
326! Mode = 2 or 3: Derivative values:
327!
328 if ( mode .eq. 2 .or. mode .eq. 3 ) then
329 do i = 1, nobs
330 jac(dimx+i) = 2*x(dimx+i)
331 enddo
332 endif
333!
334! Row 1 to Nobs: The observation definitions:
335!
336 else
337!
338! Mode = 1 or 3: Function value - x-part only
339!
340 if ( mode .eq. 1 .or. mode .eq. 3 ) then
341 s = 0.d0
342 do j = 1, dimx
343 s = s + a(rowno,j)*x(j) + b(rowno,j)*x(j)**2
344 enddo
345 g = s
346 endif
348! Mode = 2 or 3: Derivatives
349!
350 if ( mode .eq. 2 .or. mode .eq. 3 ) then
351 do j = 1, dimx
352 jac(j) = a(rowno,j) + 2.d0*b(rowno,j)*x(j)
353 enddo
354 endif
355 endif
356 lsq_fdeval = 0
357
358end Function lsq_fdeval
359
360
361!> Computes the second derivative of a constraint in a direction
362!!
363!! @include{doc} 2DDir_params.dox
364Integer Function lsq_2ddir( X, DX, D2G, ROWNO, JCNM, NODRV, N, NJ, THREAD, USRMEM )
365#ifdef dec_directives_win32
366!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_2DDir
367#endif
368 Use lsq_700
369 implicit none
370 INTEGER, Intent(IN) :: rowno, n, nj, thread
371 INTEGER, Intent(IN OUT) :: nodrv
372 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
373 real*8, Dimension(N), Intent(IN) :: x
374 real*8, Dimension(N), Intent(IN) :: dx
375 real*8, Dimension(N), Intent(OUT) :: d2g
376 real*8, Intent(IN OUT) :: usrmem(*)
377 integer :: i, j
378
379 if ( rowno .eq. nobs+1 ) then
380!
381! Objective row: sum(i, res(i)**2 )
382!
383 do j = 1, dimx
384 d2g(j) = 0.d0
385 enddo
386 do i = 1, nobs
387 d2g(dimx+i) = 2.d0*dx(dimx+i)
388 enddo
389 else
390!
391! Constraint row with b(i,j)*x(j)**2 as only nonlinear term
392!
393 do j = 1, dimx
394 d2g(j) = c(rowno,j) ! Extract the value computed in 2DDirIni below
395 enddo
396 do i = 1, nobs
397 d2g(dimx+i) = 0.d0
398 enddo
399 endif
400
401 lsq_2ddir = 0
402
403End Function lsq_2ddir
404
405
406!> Computes the second derivative of a constraint in a direction
407!!
408!! @include{doc} 2DDir_params.dox
409
410!> Initialises the second derivative in a direction evaluation. Called each time the point or the direction changes
411!!
412!! @include{doc} 2DDirIni_params.dox
413Integer Function lsq_2ddirini( X, DX, RowList, ListSize, NumThread, NewPT, NODRV, NumVar, USRMEM )
414#ifdef dec_directives_win32
415!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Lsq_2DDirIni
416#endif
417 Use lsq_700
418 implicit none
419 INTEGER, Intent(IN) :: listsize, numthread, newpt, numvar
420 Integer, Intent(IN OUT) :: nodrv
421 real*8, Dimension(NumVar), Intent(IN) :: x
422 real*8, Dimension(NumVar), Intent(IN) :: dx
423 Integer, Dimension(ListSize),Intent(IN) :: rowlist
424 real*8, Intent(IN OUT) :: usrmem(*)
425
426 integer :: i, j
427
428!
429! Constraint row i has b(i,j)*x(j)**2 as only nonlinear term and the
430! directional derivative is 2*b(i,j)*dx(j). The value is saved here and
431! used in the subsequent calls to Lsq_2DDir
432!
433 write(10,*) 'Inside 2DDirIni: NewPt=',newpt
434 Do i = 1, nobs
435 do j = 1, dimx
436 c(i,j) = 2.d0*b(i,j)*dx(j)
437 enddo
438 enddo
439
440 lsq_2ddirini = 0
441
442End Function lsq_2ddirini
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:132
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:205
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
Definition conopt.f90:1265
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
Definition conopt.f90:1238
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
Definition conopt.f90:1212
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
Definition conopt.f90:1111
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
Definition conopt.f90:1291
integer(c_int) function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
Definition conopt.f90:1135
integer(c_int) function coidef_2ddirini(cntvect, coi_2ddirini)
define callback routine for initializing the computation of second derivatives for a constraint in a ...
Definition conopt.f90:1453
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
integer(c_int) function coidef_2ddir(cntvect, coi_2ddir)
define callback routine for computing the second derivative for a constraint in a direction.
Definition conopt.f90:1421
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_debugfv(cntvect, debugfv)
turn Debugging of FDEval on and off.
Definition conopt.f90:387
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition conopt.f90:97
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition conopt.f90:121
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition conopt.f90:167
integer(c_int) function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition conopt.f90:213
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition conopt.f90:144
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
Definition conopt.f90:1726
integer(c_int) function coi_free(cntvect)
frees the control vector.
Definition conopt.f90:1749
integer(c_int) function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition conopt.f90:1625
integer function lsq_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition leastl1.f90:166
integer function lsq_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition leastl1.f90:287
integer function lsq_2ddirini(x, dx, rowlist, listsize, numthread, newpt, nodrv, numvar, usrmem)
Computes the second derivative of a constraint in a direction.
integer function lsq_2ddir(x, dx, d2g, rowno, jcnm, nodrv, n, nj, thread, usrmem)
Computes the second derivative of a constraint in a direction.
#define dimx
Definition leastsq.c:16
#define nobs
Definition leastsq.c:15
void defdata()
Defines the data for the problem.
Definition leastsq.c:35
float rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.c:22
program leastsquare
Main program. A simple setup and call of CONOPT.
Definition leastsq.f90:68
#define nj
Definition mp_trans.c:46
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41