CONOPT
Loading...
Searching...
No Matches
minimax.f90
Go to the documentation of this file.
1!> @file minimax.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! We solve the following nonlinear minimax model:
6!!
7!! \f[
8!! \min \max_{i} |res_i| \\
9!! \sum_{j} (a_{ij}x_{j} + b_{ij}x_j^2) + res_i = obs_i \quad \forall i
10!! \f]
11!!
12!! where \f$a\f$, \f$b\f$, and \f$obs\f$ are known data, and \f$res\f$ and \f$x\f$ are the
13!! variables of the model.
14!!
15!! To get a model with continuous derivatives we introduce an
16!! objective variable, \f$z\f$, and the constraints
17!! \f[
18!! res_i - z \leq 0 \\
19!! -res_i - z \leq 0
20!! \f]
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_70
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 minimax
71
73 Use conopt
74 Use lsq_70
75 implicit None
76!
77! Declare the user callback routines as Integer, External:
78!
79 Integer, External :: mm_readmatrix ! Mandatory Matrix definition routine defined below
80 Integer, External :: mm_fdeval ! Function and Derivative evaluation routine
81 ! needed a nonlinear model.
82 Integer, External :: std_status ! Standard callback for displaying solution status
83 Integer, External :: std_solution ! Standard callback for displaying solution values
84 Integer, External :: std_message ! Standard callback for managing messages
85 Integer, External :: std_errmsg ! Standard callback for managing error messages
86#ifdef dec_directives_win32
87!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_ReadMatrix
88!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
89!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
90!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
91!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
92!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
93#endif
94!
95! Control vector
96!
97 INTEGER, Dimension(:), Pointer :: cntvect
98 INTEGER :: coi_error
99
100 call startup
101!
102! Define data
103!
104 Call defdata
105!
106! Create and initialize a Control Vector
107!
108 coi_error = coi_create( cntvect )
109!
110! Tell CONOPT about the size of the model by populating the Control Vector:
111!
112 coi_error = max( coi_error, coidef_numvar( cntvect, nobs + dimx + 1 ) )
113 coi_error = max( coi_error, coidef_numcon( cntvect, 3*nobs ) )
114 coi_error = max( coi_error, coidef_numnz( cntvect, nobs * (dimx+5) ) )
115 coi_error = max( coi_error, coidef_numnlnz( cntvect, nobs * dimx ) )
116 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
117 coi_error = max( coi_error, coidef_objvar( cntvect, nobs + dimx + 1 ) ) ! Objective is last variable
118 coi_error = max( coi_error, coidef_optfile( cntvect, 'leastsq.opt' ) )
119!
120! Tell CONOPT about the callback routines:
121!
122 coi_error = max( coi_error, coidef_readmatrix( cntvect, mm_readmatrix ) )
123 coi_error = max( coi_error, coidef_fdeval( cntvect, mm_fdeval ) )
124 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
125 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
126 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
127 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
128 coi_error = max( coi_error, coidef_debugfv( cntvect, 0 ) ) ! Debug Fdeval on or off
129
130#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
131 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
132#endif
133
134 If ( coi_error .ne. 0 ) THEN
135 write(*,*)
136 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
137 write(*,*)
138 call flog( "Skipping Solve due to setup errors", 1 )
139 ENDIF
140!
141! Save the solution so we can check the duals:
142!
143 do_allocate = .true.
144!
145! Start CONOPT:
146!
147 coi_error = coi_solve( cntvect )
148
149 write(*,*)
150 write(*,*) 'End of Minimax example 1. Return code=',coi_error
151
152 If ( coi_error /= 0 ) then
153 call flog( "Errors encountered during solution", 1 )
154 elseif ( stacalls == 0 .or. solcalls == 0 ) then
155 call flog( "Status or Solution routine was not called", 1 )
156 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) ) then
157 call flog( "Solver or Model status was not as expected (1,2)", 1 )
158! elseif ( abs( OBJ - 19.44434311d0 ) > 1.d-7 ) then
159! call flog( "Incorrect objective returned", 1 )
160 Else
161 Call checkdual( 'Minimax', minimize )
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 minimax
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 mm_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 :: MM_ReadMatrix
182#endif
183 Use lsq_70
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 to 3*Nobs
232! Rhs = 0 (default) and type Less than or equal
233!
234 do i = nobs+1, 3*nobs
235 type(i) = 2
236 enddo
237!
238! Information about the Jacobian. CONOPT expects a columnwise
239! representation in Rowno, Value, Nlflag and Colsta.
240!
241! Colsta = Start of column indices (No Defaults):
242! Rowno = Row indices
243! Value = Value of derivative (by default only linear
244! derivatives are used)
245! Nlflag = 0 for linear and 1 for nonlinear derivative
246! (not needed for completely linear models)
247!
248!
249! Indices
250! x(i) res(j) z
251! j: NL L=1
252! j: L=1 L=-1
253! j: L=-1 L=-1
254!
255 k = 1
256 do j = 1, dimx
257 colsta(j) = k
258 do i = 1, nobs
259 rowno(k) = i
260 nlflag(k) = 1
261 k = k + 1
262 enddo
263 enddo
264 do i = 1, nobs
265 colsta(dimx+i) = k
266 rowno(k) = i
267 nlflag(k) = 0
268 value(k) = 1.d0
269 k = k + 1
270 rowno(k) = i+nobs
271 nlflag(k) = 0
272 value(k) = 1.d0
273 k = k + 1
274 rowno(k) = i+2*nobs
275 nlflag(k) = 0
276 value(k) = -1.d0
277 k = k + 1
278 enddo
279 colsta(dimx+nobs+1) = k
280 do i = 1, 2*nobs
281 rowno(k) = nobs+i
282 nlflag(k) = 0
283 value(k) = -1.d0
284 k = k + 1
285 enddo
286 colsta(dimx+nobs+2) = k
288 mm_readmatrix = 0 ! Return value means OK
289
290end Function mm_readmatrix
291!
292!==========================================================================
293! Compute nonlinear terms and non-constant Jacobian elements
294!
295
296!> Compute nonlinear terms and non-constant Jacobian elements
297!!
298!! @include{doc} fdeval_params.dox
299Integer Function mm_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
300 n, nz, thread, usrmem )
301#ifdef dec_directives_win32
302!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
303#endif
304 use lsq_70
305 implicit none
306 integer, intent (in) :: n ! number of variables
307 integer, intent (in) :: rowno ! number of the row to be evaluated
308 integer, intent (in) :: nz ! number of nonzeros in this row
309 real*8, intent (in), dimension(n) :: x ! vector of current solution values
310 real*8, intent (in out) :: g ! constraint value
311 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
312 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
313 ! in this row. Ffor information only.
314 integer, intent (in) :: mode ! evaluation mode: 1 = function value
315 ! 2 = derivatives, 3 = both
316 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
317 ! as errcnt is incremented
318 integer, intent (in out) :: errcnt ! error counter to be incremented in case
319 ! of function evaluation errors.
320 integer, intent (in) :: thread
321 real*8 usrmem(*) ! optional user memory
322
323 integer :: j
324 real*8 :: s
325!
326! Row Nobs+1: the objective function is nonlinear
327!
328 mm_fdeval = 0
329 if ( rowno .le. nobs ) then
330!
331! Mode = 1 or 3: Function value - x-part only
332!
333 if ( mode .eq. 1 .or. mode .eq. 3 ) then
334 s = 0.d0
335 do j = 1, dimx
336 s = s + a(rowno,j)*x(j) + b(rowno,j)*x(j)**2
337 enddo
338 g = s
339 endif
340!
341! Mode = 2 or 3: Derivatives
342!
343 if ( mode .eq. 2 .or. mode .eq. 3 ) then
344 do j = 1, dimx
345 jac(j) = a(rowno,j) + 2.d0*b(rowno,j)*x(j)
346 enddo
347 endif
348 Else
349 mm_fdeval = 1 ! Illegal row number
350 endif
351
352end 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_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
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
program minimax
Main program. A simple setup and call of CONOPT.
Definition minimax.f90:72
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