CONOPT
Loading...
Searching...
No Matches
minimax01.f90
Go to the documentation of this file.
1!> @file minimax01.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! MiniMax01 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!! For more information about the individual callbacks, please have a look at the source code.
25
26#if defined(_WIN32) && !defined(_WIN64)
27#define dec_directives_win32
28#endif
29
30REAL FUNCTION rndx( )
31!
32! Defines a pseudo random number between 0 and 1
33!
34 IMPLICIT NONE
35
36 Integer, save :: seed = 12359
37
38 seed = mod(seed*1027+25,1048576)
39 rndx = float(seed)/float(1048576)
40
41END FUNCTION rndx
42
43subroutine defdata
44!
45! Define values for A, B, and Obs
46!
47 Use lsq_7
48 IMPLICIT NONE
49
50 Integer :: i, j
51 real*8, Parameter :: xtarg = -1.0
52 real*8, Parameter :: noise = 1.0
53 Real, External :: Rndx
54 real*8 :: o
55
56 do i = 1, nobs
57 o = 0.d0
58 do j = 1, dimx
59 a(i,j) = rndx()
60 b(i,j) = rndx()
61 o = o + a(i,j) * xtarg + b(i,j) * xtarg**2
62 enddo
63 obs(i) = o + noise * rndx()
64 enddo
65
66end subroutine defdata
67!
68! Main program.
69!
70!> Main program. A simple setup and call of CONOPT
71!!
72Program minimax01
73
75 Use conopt
76 Use lsq_7
77 implicit None
78!
79! Declare the user callback routines as Integer, External:
80!
81 Integer, External :: mm_readmatrix ! Mandatory Matrix definition routine defined below
82 Integer, External :: mm_fdeval ! Function and Derivative evaluation routine
83 ! needed a nonlinear model.
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 :: MM_ReadMatrix
90!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
91!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
92!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
93!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
94!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
95#endif
96!
97! Control vector
98!
99 INTEGER, Dimension(:), Pointer :: cntvect
100 INTEGER :: coi_error
101
102 call startup
103!
104! Define data
105!
106 Call defdata
107!
108! Create and initialize a Control Vector
109!
110 coi_error = coi_create( cntvect )
111!
112! Tell CONOPT about the size of the model by populating the Control Vector:
113!
114 coi_error = max( coi_error, coidef_numvar( cntvect, nobs + dimx + 1 ) )
115 coi_error = max( coi_error, coidef_numcon( cntvect, 3*nobs ) )
116 coi_error = max( coi_error, coidef_numnz( cntvect, nobs * (dimx+5) ) )
117 coi_error = max( coi_error, coidef_numnlnz( cntvect, nobs * dimx ) )
118 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
119 coi_error = max( coi_error, coidef_objvar( cntvect, nobs + dimx + 1 ) ) ! Objective is last variable
120 coi_error = max( coi_error, coidef_optfile( cntvect, 'MiniMax01.opt' ) )
121!
122! Tell CONOPT about the callback routines:
123!
124 coi_error = max( coi_error, coidef_readmatrix( cntvect, mm_readmatrix ) )
125 coi_error = max( coi_error, coidef_fdeval( cntvect, mm_fdeval ) )
126 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
127 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
128 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
129 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
130 coi_error = max( coi_error, coidef_debugfv( cntvect, 0 ) ) ! Debug Fdeval on or off
131
132#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
133 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
134#endif
135
136 If ( coi_error .ne. 0 ) THEN
137 write(*,*)
138 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
139 write(*,*)
140 call flog( "Skipping Solve due to setup errors", 1 )
141 ENDIF
142!
143! Save the solution so we can check the duals:
144!
145 do_allocate = .true.
146!
147! Start CONOPT:
148!
149 coi_error = coi_solve( cntvect )
150
151 write(*,*)
152 write(*,*) 'End of MiniMax01 example 1. 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 ( .not. ( sstat == 1 .and. mstat == 2 ) ) then
159 call flog( "Solver or 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 Else
163 Call checkdual( 'MiniMax01', minimize )
164 endif
165
166 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
167
168 call flog( "Successful Solve", 0 )
169!
170! Free solution memory
171!
172 call finalize
173
174End Program minimax01
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_7
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
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 do j = 1, dimx
216 curr(j) = -0.8d0
217 enddo
218!
219! Information about Constraints:
220! Default: Rhs = 0
221! Default: the status information in Esta and the function
222! value in FV are not used.
223! Default: Type: There is no default.
224! 0 = Equality,
225! 1 = Greater than or equal,
226! 2 = Less than or equal,
227! 3 = Non binding.
228!
229! Constraints 1 to Nobs:
230! Rhs = Obs(i) and type Equality
231!
232 do i = 1, nobs
233 rhs(i) = obs(i)
234 type(i) = 0
235 enddo
236!
237! Constraint Nobs+1 to 3*Nobs
238! Rhs = 0 (default) and type Less than or equal
239!
240 do i = nobs+1, 3*nobs
241 type(i) = 2
242 enddo
243!
244! Information about the Jacobian. CONOPT expects a columnwise
245! representation in Rowno, Value, Nlflag and Colsta.
246!
247! Colsta = Start of column indices (No Defaults):
248! Rowno = Row indices
249! Value = Value of derivative (by default only linear
250! derivatives are used)
251! Nlflag = 0 for linear and 1 for nonlinear derivative
252! (not needed for completely linear models)
253!
254!
255! Indices
256! x(j) res(i) z
257! i: NL L=1
258! i: L=1 L=-1
259! i: L=-1 L=-1
260!
261 k = 1
262 do j = 1, dimx
263 colsta(j) = k
264 do i = 1, nobs
265 rowno(k) = i
266 nlflag(k) = 1
267 k = k + 1
268 enddo
269 enddo
270 do i = 1, nobs
271 colsta(dimx+i) = k
272 rowno(k) = i
273 nlflag(k) = 0
274 value(k) = 1.d0
275 k = k + 1
276 rowno(k) = i+nobs
277 nlflag(k) = 0
278 value(k) = 1.d0
279 k = k + 1
280 rowno(k) = i+2*nobs
281 nlflag(k) = 0
282 value(k) = -1.d0
283 k = k + 1
284 enddo
285 colsta(dimx+nobs+1) = k
286 do i = 1, 2*nobs
287 rowno(k) = nobs+i
288 nlflag(k) = 0
289 value(k) = -1.d0
290 k = k + 1
291 enddo
292 colsta(dimx+nobs+2) = k
294 mm_readmatrix = 0 ! Return value means OK
295
296end Function mm_readmatrix
297!
298!==========================================================================
299! Compute nonlinear terms and non-constant Jacobian elements
300!
301
302!> Compute nonlinear terms and non-constant Jacobian elements
303!!
304!! @include{doc} fdeval_params.dox
305Integer Function mm_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
306 n, nz, thread, usrmem )
307#ifdef dec_directives_win32
308!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
309#endif
310 use lsq_7
311 implicit none
312 integer, intent (in) :: n ! number of variables
313 integer, intent (in) :: rowno ! number of the row to be evaluated
314 integer, intent (in) :: nz ! number of nonzeros in this row
315 real*8, intent (in), dimension(n) :: x ! vector of current solution values
316 real*8, intent (in out) :: g ! constraint value
317 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
318 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
319 ! in this row. Ffor information only.
320 integer, intent (in) :: mode ! evaluation mode: 1 = function value
321 ! 2 = derivatives, 3 = both
322 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
323 ! as errcnt is incremented
324 integer, intent (in out) :: errcnt ! error counter to be incremented in case
325 ! of function evaluation errors.
326 integer, intent (in) :: thread
327 real*8 usrmem(*) ! optional user memory
328
329 integer :: j
330 real*8 :: s
331!
332! Row Nobs+1: the objective function is nonlinear
333!
334 mm_fdeval = 0
335 if ( rowno .le. nobs ) then
336!
337! Mode = 1 or 3: Function value - x-part only
338!
339 if ( mode .eq. 1 .or. mode .eq. 3 ) then
340 s = 0.d0
341 do j = 1, dimx
342 s = s + a(rowno,j)*x(j) + b(rowno,j)*x(j)**2
343 enddo
344 g = s
345 endif
346!
347! Mode = 2 or 3: Derivatives
348!
349 if ( mode .eq. 2 .or. mode .eq. 3 ) then
350 do j = 1, dimx
351 jac(j) = a(rowno,j) + 2.d0*b(rowno,j)*x(j)
352 enddo
353 endif
354 Else
355 mm_fdeval = 1 ! Illegal row number
356 endif
357
358end 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_objvar(cntvect, objvar)
defines the Objective Variable.
Definition conopt.f90:257
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 minimax01
Main program. A simple setup and call of CONOPT.
Definition minimax01.f90:74
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