CONOPT
Loading...
Searching...
No Matches
minimax03.f90
Go to the documentation of this file.
1!> @file minimax03.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! MiniMax03 is a small version of minimax used for quick turnaround.
6!!
7!! We solve the following nonlinear minimax model:
8!!
9!! \f[
10!! \min \max_{i} |res_i| \\
11!! \sum_{j} (a_{ij}x_{j} + b_{ij}x_j^2) + res_i = obs_i \quad \forall i
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_i - z \leq 0 \\
21!! -res_i - z \leq 0
22!! \f]
23!!
24!! Similar to MiniMax02 but with objective minimize \f$\sqrt{z}\f$
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_7
50 IMPLICIT NONE
51
52 Integer :: i, j
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 j = 1, dimx
61 a(i,j) = rndx()
62 b(i,j) = rndx()
63 o = o + a(i,j) * xtarg + b(i,j) * xtarg**2
64 enddo
65 obs(i) = o + noise * rndx()
66 enddo
67
68end subroutine defdata
69!
70! Main program.
71!
72!> Main program. A simple setup and call of CONOPT
73!!
74Program minimax03
75
77 Use conopt
78 Use lsq_7
79 implicit None
80!
81! Declare the user callback routines as Integer, External:
82!
83 Integer, External :: mm_readmatrix ! Mandatory Matrix definition routine defined below
84 Integer, External :: mm_fdeval ! Function and Derivative evaluation routine
85 ! needed a nonlinear model.
86 Integer, External :: std_status ! Standard callback for displaying solution status
87 Integer, External :: std_solution ! Standard callback for displaying solution values
88 Integer, External :: std_message ! Standard callback for managing messages
89 Integer, External :: std_errmsg ! Standard callback for managing error messages
90#ifdef dec_directives_win32
91!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_ReadMatrix
92!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
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 + 1 ) )
117 coi_error = max( coi_error, coidef_numcon( cntvect, 3*nobs+1 ) )
118 coi_error = max( coi_error, coidef_numnz( cntvect, nobs * (dimx+5)+1 ) )
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, 3*nobs+1 ) ) ! Objective is last constraint
122 coi_error = max( coi_error, coidef_optfile( cntvect, 'MiniMax03.opt' ) )
123!
124! Tell CONOPT about the callback routines:
125!
126 coi_error = max( coi_error, coidef_readmatrix( cntvect, mm_readmatrix ) )
127 coi_error = max( coi_error, coidef_fdeval( cntvect, mm_fdeval ) )
128 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
129 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
130 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
131 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
132 coi_error = max( coi_error, coidef_debugfv( cntvect, 0 ) ) ! Debug Fdeval on or off
133
134#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
135 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
136#endif
137
138 If ( coi_error .ne. 0 ) THEN
139 write(*,*)
140 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
141 write(*,*)
142 call flog( "Skipping Solve due to setup errors", 1 )
143 ENDIF
144!
145! Save the solution so we can check the duals:
146!
147 do_allocate = .true.
148!
149! Start CONOPT:
150!
151 coi_error = coi_solve( cntvect )
152
153 write(*,*)
154 write(*,*) 'End of MiniMax03 example 1. Return code=',coi_error
155
156 If ( coi_error /= 0 ) then
157 call flog( "Errors encountered during solution", 1 )
158 elseif ( stacalls == 0 .or. solcalls == 0 ) then
159 call flog( "Status or Solution routine was not called", 1 )
160 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) ) then
161 call flog( "Solver or Model status was not as expected (1,2)", 1 )
162! elseif ( abs( OBJ - 19.44434311d0 ) > 1.d-7 ) then
163! call flog( "Incorrect objective returned", 1 )
164 Else
165 Call checkdual( 'MiniMax03', minimize )
166 endif
167
168 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
169
170 call flog( "Successful Solve", 0 )
171!
172! Free solution memory
173!
174 call finalize
175
176End Program minimax03
177!
178! ============================================================================
179! Define information about the model:
180!
181
182!> Define information about the model
183!!
184!! @include{doc} readMatrix_params.dox
185Integer Function mm_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
186 colsta, rowno, value, nlflag, n, m, nz, &
187 usrmem )
188#ifdef dec_directives_win32
189!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_ReadMatrix
190#endif
191 Use lsq_7
192 implicit none
193 integer, intent (in) :: n ! number of variables
194 integer, intent (in) :: m ! number of constraints
195 integer, intent (in) :: nz ! number of nonzeros
196 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
197 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
198 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
199 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
200 ! (not defined here)
201 integer, intent (out), dimension(m) :: type ! vector of equation types
202 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
203 ! (not defined here)
204 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
205 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
206 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
207 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
208 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
209 real*8 usrmem(*) ! optional user memory
210
211 Integer :: i, j, k
212!
213! Information about Variables:
214! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
215! Default: the status information in Vsta is not used.
216!
217 do j = 1, dimx
218 curr(j) = -0.8d0
219 enddo
220!
221! Information about Constraints:
222! Default: Rhs = 0
223! Default: the status information in Esta and the function
224! value in FV are not used.
225! Default: Type: There is no default.
226! 0 = Equality,
227! 1 = Greater than or equal,
228! 2 = Less than or equal,
229! 3 = Non binding.
230!
231! Constraints 1 to Nobs:
232! Rhs = Obs(i) and type Equality
233!
234 do i = 1, nobs
235 rhs(i) = obs(i)
236 type(i) = 0
237 enddo
238!
239! Constraint Nobs+1 to 3*Nobs
240! Rhs = 0 (default) and type Less than or equal
241!
242 do i = nobs+1, 3*nobs
243 type(i) = 2
244 enddo
245 Type(3*nobs+1) = 3
246!
247! Information about the Jacobian. CONOPT expects a columnwise
248! representation in Rowno, Value, Nlflag and Colsta.
249!
250! Colsta = Start of column indices (No Defaults):
251! Rowno = Row indices
252! Value = Value of derivative (by default only linear
253! derivatives are used)
254! Nlflag = 0 for linear and 1 for nonlinear derivative
255! (not needed for completely linear models)
256!
257!
258! Indices
259! x(j) res(i) z
260! i: NL L=1
261! i: L=1 L=-1
262! i: L=-1 L=-1
263! obj NL
264!
265 k = 1
266 do j = 1, dimx
267 colsta(j) = k
268 do i = 1, nobs
269 rowno(k) = i
270 nlflag(k) = 1
271 k = k + 1
272 enddo
273 enddo
274 do i = 1, nobs
275 colsta(dimx+i) = k
276 rowno(k) = i
277 nlflag(k) = 0
278 value(k) = 1.d0
279 k = k + 1
280 rowno(k) = i+nobs
281 nlflag(k) = 0
282 value(k) = 1.d0
283 k = k + 1
284 rowno(k) = i+2*nobs
285 nlflag(k) = 0
286 value(k) = -1.d0
287 k = k + 1
288 enddo
289 colsta(dimx+nobs+1) = k
290 do i = 1, 2*nobs
291 rowno(k) = nobs+i
292 nlflag(k) = 0
293 value(k) = -1.d0
294 k = k + 1
295 enddo
296 rowno(k) = 3*nobs+1 ! Nonlinear objective term
297 nlflag(k) = 1
298 k = k + 1
299 colsta(dimx+nobs+2) = k
301 mm_readmatrix = 0 ! Return value means OK
302
303end Function mm_readmatrix
304!
305!==========================================================================
306! Compute nonlinear terms and non-constant Jacobian elements
307!
308
309!> Compute nonlinear terms and non-constant Jacobian elements
310!!
311!! @include{doc} fdeval_params.dox
312Integer Function mm_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
313 n, nz, thread, usrmem )
314#ifdef dec_directives_win32
315!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
316#endif
317 use lsq_7
318 implicit none
319 integer, intent (in) :: n ! number of variables
320 integer, intent (in) :: rowno ! number of the row to be evaluated
321 integer, intent (in) :: nz ! number of nonzeros in this row
322 real*8, intent (in), dimension(n) :: x ! vector of current solution values
323 real*8, intent (in out) :: g ! constraint value
324 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
325 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
326 ! in this row. Ffor information only.
327 integer, intent (in) :: mode ! evaluation mode: 1 = function value
328 ! 2 = derivatives, 3 = both
329 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
330 ! as errcnt is incremented
331 integer, intent (in out) :: errcnt ! error counter to be incremented in case
332 ! of function evaluation errors.
333 integer, intent (in) :: thread
334 real*8 usrmem(*) ! optional user memory
335
336 integer :: j
337 real*8 :: s
338!
339! Row Nobs+1: the objective function is nonlinear
340!
341 mm_fdeval = 0
342 if ( rowno .le. nobs ) then
343!
344! Mode = 1 or 3: Function value - x-part only
345!
346 if ( mode .eq. 1 .or. mode .eq. 3 ) then
347 s = 0.d0
348 do j = 1, dimx
349 s = s + a(rowno,j)*x(j) + b(rowno,j)*x(j)**2
350 enddo
351 g = s
352 endif
353!
354! Mode = 2 or 3: Derivatives
355!
356 if ( mode .eq. 2 .or. mode .eq. 3 ) then
357 do j = 1, dimx
358 jac(j) = a(rowno,j) + 2.d0*b(rowno,j)*x(j)
359 enddo
360 endif
361 Else if ( rowno == 3*nobs+1 ) then
362!
363! Mode = 1 or 3: Objective Function = z**2
364!
365 if ( mode .eq. 1 .or. mode .eq. 3 ) then
366 g = x(nobs+dimx+1)**2
367 endif
368!
369! Mode = 2 or 3: Derivatives
370!
371 if ( mode .eq. 2 .or. mode .eq. 3 ) then
372 jac(nobs+dimx+1) = 2.0d0*x(nobs+dimx+1)
373 endif
374
375 Else
376 mm_fdeval = 1 ! Illegal row number
377 endif
378
379end Function mm_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
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 minimax03
Main program. A simple setup and call of CONOPT.
Definition minimax03.f90:76
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:293
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:175
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
subroutine finalize
Definition comdecl.f90:79
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