CONOPT
Loading...
Searching...
No Matches
minimax04.f90
Go to the documentation of this file.
1!> @file minimax04.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Minimax04 is a minimax model with multiple minimax variables.
6!!
7!! We solve the following nonlinear minimax model:
8!!
9!! \f[
10!! \min \sum_k \max_{i} |res_{ik}| \\
11!! \sum_{j} (a_{ijk}x_{jk} + b_{ijk}x_{jk}^2) + res_{ik} = obs_{ik} \quad \forall i, k
12!! \f]
13!!
14!! where \f$a\f$, \f$b\f$, and \f$obs\f$ are known data, and \f$res\f$ and \f$x\f$ are the
15!! variables of the model.
16!!
17!! To get a model with continuous derivatives we introduce an
18!! objective variable, \f$z\f$, and the constraints
19!! \f[
20!! res_{ik} - z_k \leq 0 \\
21!! -res_{ik} - z_k \leq 0
22!! \f]
23!! Note opposite sign from Minimax01 and minimize \f$\sum_k z_k\f$
24!!
25!!
26!! For more information about the individual callbacks, please have a look at the source code.
27
28#if defined(_WIN32) && !defined(_WIN64)
29#define dec_directives_win32
30#endif
31
32REAL FUNCTION rndx( )
33!
34! Defines a pseudo random number between 0 and 1
35!
36 IMPLICIT NONE
37
38 Integer, save :: seed = 12359
39
40 seed = mod(seed*1027+25,1048576)
41 rndx = float(seed)/float(1048576)
42
43END FUNCTION rndx
44
45subroutine defdata
46!
47! Define values for A, B, and Obs
48!
49 Use lsq_7k
50 IMPLICIT NONE
51
52 Integer :: i, j, k
53 real*8, Parameter :: xtarg = -1.0
54 real*8, Parameter :: noise = 1.0
55 Real, External :: Rndx
56 real*8 :: o
57
58 do i = 1, nobs
59 o = 0.d0
60 do k = 1, dimk
61 do j = 1, dimx
62 a(i,k,j) = rndx()
63 b(i,k,j) = rndx()
64 o = o + a(i,k,j) * xtarg + b(i,k,j) * xtarg**2
65 enddo
66 obs(i,k) = o + noise * rndx()
67 enddo
68 enddo
69
70end subroutine defdata
71!
72! Main program.
73!
74!> Main program. A simple setup and call of CONOPT
75!!
76Program minimax04
77
79 Use conopt
80 Use lsq_7k
81 implicit None
82!
83! Declare the user callback routines as Integer, External:
84!
85 Integer, External :: mm_readmatrix ! Mandatory Matrix definition routine defined below
86 Integer, External :: mm_fdeval ! Function and Derivative evaluation routine
87 ! needed a nonlinear model.
88 Integer, External :: std_status ! Standard callback for displaying solution status
89 Integer, External :: std_solution ! Standard callback for displaying solution values
90 Integer, External :: std_message ! Standard callback for managing messages
91 Integer, External :: std_errmsg ! Standard callback for managing error messages
92#ifdef dec_directives_win32
93!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_ReadMatrix
94!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
95!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
96!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
97!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
98!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
99#endif
100!
101! Control vector
102!
103 INTEGER, Dimension(:), Pointer :: cntvect
104 INTEGER :: coi_error
105
106 call startup
107!
108! Define data
109!
110 Call defdata
111!
112! Create and initialize a Control Vector
113!
114 coi_error = coi_create( cntvect )
115!
116! Tell CONOPT about the size of the model by populating the Control Vector:
117!
118 coi_error = max( coi_error, coidef_numvar( cntvect, nobs*dimk + dimx*dimk + dimk ) )
119 coi_error = max( coi_error, coidef_numcon( cntvect, 3*nobs*dimk+1 ) )
120 coi_error = max( coi_error, coidef_numnz( cntvect, nobs * dimx * dimk + 5* nobs*dimk + dimk ) )
121 coi_error = max( coi_error, coidef_numnlnz( cntvect, nobs * dimx * dimk ) )
122 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
123 coi_error = max( coi_error, coidef_objcon( cntvect, 3*nobs*dimk + 1 ) ) ! Objective is last constraint
124 coi_error = max( coi_error, coidef_optfile( cntvect, 'Minimax04.opt' ) )
125!
126! Tell CONOPT about the callback routines:
127!
128 coi_error = max( coi_error, coidef_readmatrix( cntvect, mm_readmatrix ) )
129 coi_error = max( coi_error, coidef_fdeval( cntvect, mm_fdeval ) )
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! Save the solution so we can check the duals:
148!
149 do_allocate = .true.
150!
151! Start CONOPT:
152!
153 coi_error = coi_solve( cntvect )
154
155 write(*,*)
156 write(*,*) 'End of Minimax04 example 1. Return code=',coi_error
157
158 If ( coi_error /= 0 ) then
159 call flog( "Errors encountered during solution", 1 )
160 elseif ( stacalls == 0 .or. solcalls == 0 ) then
161 call flog( "Status or Solution routine was not called", 1 )
162 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) ) then
163 call flog( "Solver or Model status was not as expected (1,2)", 1 )
164! elseif ( abs( OBJ - 19.44434311d0 ) > 1.d-7 ) then
165! call flog( "Incorrect objective returned", 1 )
166 Else
167 Call checkdual( 'Minimax04', minimize )
168 endif
169
170 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
171
172 call flog( "Successful Solve", 0 )
173
174End Program minimax04
175!
176! ============================================================================
177! Define information about the model:
178!
179
180!> Define information about the model
181!!
182!! @include{doc} readMatrix_params.dox
183Integer Function mm_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
184 colsta, rowno, value, nlflag, n, m, nz, &
185 usrmem )
186#ifdef dec_directives_win32
187!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_ReadMatrix
188#endif
189 Use lsq_7k
190 implicit none
191 integer, intent (in) :: n ! number of variables
192 integer, intent (in) :: m ! number of constraints
193 integer, intent (in) :: nz ! number of nonzeros
194 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
195 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
196 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
197 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
198 ! (not defined here)
199 integer, intent (out), dimension(m) :: type ! vector of equation types
200 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
201 ! (not defined here)
202 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
203 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
204 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
205 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
206 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
207 real*8 usrmem(*) ! optional user memory
208
209 Integer :: i, j, k, l, nzc
210!
211! Information about Variables:
212! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
213! Default: the status information in Vsta is not used.
214!
215 l = 0
216 do i = 1, dimx
217 do k = 1, dimk
218 l = l + 1
219 curr(l) = -0.8d0
220 enddo
221 enddo
222!
223! Information about Constraints:
224! Default: Rhs = 0
225! Default: the status information in Esta and the function
226! value in FV are not used.
227! Default: Type: There is no default.
228! 0 = Equality,
229! 1 = Greater than or equal,
230! 2 = Less than or equal,
231! 3 = Non binding.
232!
233! Constraints 1 to Nobs*Dimk:
234! Rhs = Obs(i,k) and type Equality
235!
236 l = 0
237 do i = 1, nobs
238 do k = 1, dimk
239 l = l + 1
240 rhs(l) = obs(i,k)
241 type(l) = 0
242 enddo
243 enddo
244!
245! Constraint Nobs*Dimk+1 to 2*Nobs*Dimk
246! Rhs = 0 (default) and type Less than or equal
247!
248 do i = nobs*dimk+1, 2*nobs*dimk
249 type(i) = 2
250 enddo
251!
252! Constraint 2*Nobs*Dimk+1 to 3*Nobs*Dimk
253! Rhs = 0 (default) and type Greater than or equal
254!
255 do i = 2*nobs*dimk+1, 3*nobs*dimk
256 type(i) = 1
257 enddo
258!
259! Objective, type Nonbinding
260!
261 type(3*nobs*dimk+1) = 3
262!
263! Information about the Jacobian. CONOPT expects a columnwise
264! representation in Rowno, Value, Nlflag and Colsta.
265!
266! Colsta = Start of column indices (No Defaults):
267! Rowno = Row indices
268! Value = Value of derivative (by default only linear
269! derivatives are used)
270! Nlflag = 0 for linear and 1 for nonlinear derivative
271! (not needed for completely linear models)
272!
273!
274! Indices
275! x(j,k) res(i,k) z(k)
276! i,k: NL L=1
277! i,k: L=1 L=-1
278! i,k: L=1 L=+1
279! obj L=+1
280!
281! We map (i,k) -> k+DimK*(i-1)
282! and (j,k) -> k+DimK*(j-1)
283!
284 nzc = 1
285 do j = 1, dimx ! x(j,k)
286 do k = 1, dimk
287 l = k+dimk*(j-1)
288 colsta(l) = nzc
289 do i = 1, nobs
290 rowno(nzc) = k+dimk*(i-1)
291 nlflag(nzc) = 1
292 nzc = nzc + 1
293 enddo
294 enddo
295 enddo
296 do i = 1, nobs ! res(i,k)
297 do k = 1, dimk
298 colsta(dimx*dimk+k+dimk*(i-1)) = nzc
299 rowno(nzc) = k+dimk*(i-1)
300 nlflag(nzc) = 0
301 value(nzc) = 1.d0
302 nzc = nzc + 1
303 rowno(nzc) = nobs*dimk+k+dimk*(i-1)
304 nlflag(nzc) = 0
305 value(nzc) = 1.d0
306 nzc = nzc + 1
307 rowno(nzc) = 2*nobs*dimk+k+dimk*(i-1)
308 nlflag(nzc) = 0
309 value(nzc) = 1.d0
310 nzc = nzc + 1
311 enddo
312 enddo
313 do k = 1, dimk ! z(k)
314 colsta(dimx*dimk+nobs*dimk+k) = nzc
315 do i = 1, nobs
316 rowno(nzc) = nobs*dimk++k+dimk*(i-1)
317 nlflag(nzc) = 0
318 value(nzc) = -1.d0
319 nzc = nzc + 1
320 rowno(nzc) = 2*nobs*dimk+k+dimk*(i-1)
321 nlflag(nzc) = 0
322 value(nzc) = +1.d0
323 nzc = nzc + 1
324 enddo
325 rowno(nzc) = 3*nobs*dimk+1
326 nlflag(nzc) = 0
327 value(nzc) = +1.d0
328 nzc = nzc + 1
329 enddo
330 colsta(dimx*dimk+nobs*dimk+dimk+1) = nzc
332 mm_readmatrix = 0 ! Return value means OK
333
334end Function mm_readmatrix
335!
336!==========================================================================
337! Compute nonlinear terms and non-constant Jacobian elements
338!
339
340!> Compute nonlinear terms and non-constant Jacobian elements
341!!
342!! @include{doc} fdeval_params.dox
343Integer Function mm_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
344 n, nzc, thread, usrmem )
345#ifdef dec_directives_win32
346!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
347#endif
348 use lsq_7k
349 implicit none
350 integer, intent (in) :: n ! number of variables
351 integer, intent (in) :: rowno ! number of the row to be evaluated
352 integer, intent (in) :: nzc ! number of nonzceros in this row
353 real*8, intent (in), dimension(n) :: x ! vector of current solution values
354 real*8, intent (in out) :: g ! constraint value
355 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
356 integer, intent (in), dimension(nzc) :: jcnm ! list of variables that appear nonlinearly
357 ! in this row. Ffor information only.
358 integer, intent (in) :: mode ! evaluation mode: 1 = function value
359 ! 2 = derivatives, 3 = both
360 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
361 ! as errcnt is incremented
362 integer, intent (in out) :: errcnt ! error counter to be incremented in case
363 ! of function evaluation errors.
364 integer, intent (in) :: thread
365 real*8 usrmem(*) ! optional user memory
366
367 integer :: i,j,k,l
368 real*8 :: s
369
370 mm_fdeval = 0
371 if ( rowno .le. nobs*dimk ) then
372!
373! We map (i,k): rowno -> k+DimK*(i-1) so
374! i = (Dimk+rowno-1))/Dimk
375! k = rowno-Dimk*(i-1)
376! and (j,k) -> k+DimK*(j-1)
377! Mode = 1 or 3: Function value - x-part only
378!
379 i = (dimk+rowno-1)/dimk
380 k = rowno-dimk*(i-1)
381 if ( mode .eq. 1 .or. mode .eq. 3 ) then
382 s = 0.d0
383 do j = 1, dimx
384 l = k+dimk*(j-1)
385 s = s + a(i,k,j)*x(l) + b(i,k,j)*x(l)**2
386 enddo
387 g = s
388 endif
389!
390! Mode = 2 or 3: Derivatives
391!
392 if ( mode .eq. 2 .or. mode .eq. 3 ) then
393 do j = 1, dimx
394 l = k+dimk*(j-1)
395 jac(l) = a(i,k,j) + 2.d0*b(i,k,j)*x(l)
396 enddo
397 endif
398 Else
399 mm_fdeval = 1 ! Illegal row number
400 endif
401
402end Function mm_fdeval
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
subroutine checkdual(case, minmax)
Definition comdecl.f90:394
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_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
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
#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 minimax04
Main program. A simple setup and call of CONOPT.
Definition minimax04.f90:78
integer function mm_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition minimax.f90:289
integer function mm_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition minimax.f90:171
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer, parameter minimize
Definition comdecl.f90:31
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41