CONOPT
Loading...
Searching...
No Matches
minimax08.f90
Go to the documentation of this file.
1!> @file minimax08.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Minimax08 is a minimax model with multiple minimax variables
6!! and some side constraints.
7!! Same as minimax06 except that the quadratic b-term is much
8!! smaller so the SLP part should work well.
9!!
10!! We solve the following nonlinear minimax model:
11!!
12!! @verbatim
13!! min sum(k, max(i, abs(res(i,k)) )
14!!
15!! sum(j, a(i,j,k)*x(j,k) + b(i,j,k)*x(j,k)**2 ) + res(i,k) = obs(i,k)
16!! all k: sum(j,x(j,k)) =L= 1;
17!! @endverbatim
18!!
19!! where a, b, and obs are known data, and res and x are the
20!! variables of the model.
21!!
22!! To get a model with continuous derivatives we introduce an
23!! objective variable, z, and the constraints
24!! @verbatim
25!! +res(i,k) <= z(k) or +res(i,k) - z(k) <= 0
26!! -res(i,k) <= z(k) or +res(i,k) + z(k) >= 0
27!! @endverbatim
28!! Note opposite sign from Minimax01
29!! and minimize sum(k, z(k) )
30!!
31!!
32!! For more information about the individual callbacks, please have a look at the source code.
33
34
35REAL FUNCTION rndx( )
36!
37! Defines a pseudo random number between 0 and 1
38!
39 IMPLICIT NONE
40
41 Integer, save :: seed = 12359
42
43 seed = mod(seed*1027+25,1048576)
44 rndx = float(seed)/float(1048576)
45
46END FUNCTION rndx
47
48subroutine defdata
49!
50! Define values for A, B, and Obs
51!
52 Use lsq_7k
53 IMPLICIT NONE
54
55 Integer :: i, j, k
56 real*8, Parameter :: xtarg = -1.0
57 real*8, Parameter :: noise = 1.0
58 Real, External :: Rndx
59 real*8 :: o
60
61 do i = 1, nobs
62 o = 0.d0
63 do k = 1, dimk
64 do j = 1, dimx
65 a(i,k,j) = rndx()
66 b(i,k,j) = rndx()*0.05d0
67 o = o + a(i,k,j) * xtarg + b(i,k,j) * xtarg**2
68 enddo
69 obs(i,k) = o + noise * rndx()
70 enddo
71 enddo
72
73end subroutine defdata
74!
75! Main program.
76!
77!> Main program. A simple setup and call of CONOPT
78!!
79Program minimax08
80
81 Use proginfo
82 Use coidef
83 Use lsq_7k
84 implicit None
85!
86! Declare the user callback routines as Integer, External:
87!
88 Integer, External :: mm_readmatrix ! Mandatory Matrix definition routine defined below
89 Integer, External :: mm_fdeval ! Function and Derivative evaluation routine
90 ! needed a nonlinear model.
91 Integer, External :: std_status ! Standard callback for displaying solution status
92 Integer, External :: std_solution ! Standard callback for displaying solution values
93 Integer, External :: std_message ! Standard callback for managing messages
94 Integer, External :: std_errmsg ! Standard callback for managing error messages
95#if defined(itl)
96!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_ReadMatrix
97!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
98!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
99!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
100!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
101!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
102#endif
103!
104! Control vector
105!
106 INTEGER :: numcallback
107 INTEGER, Dimension(:), Pointer :: cntvect
108 INTEGER :: coi_error
109
110 call startup
111!
112! Define data
113!
114 Call defdata
115!
116! Create and initialize a Control Vector
117!
118 numcallback = coidef_size()
119 Allocate( cntvect(numcallback) )
120 coi_error = coidef_inifort( cntvect )
121!
122! Tell CONOPT about the size of the model by populating the Control Vector:
123!
124 coi_error = max( coi_error, coidef_numvar( cntvect, nobs*dimk + dimx*dimk + dimk ) )
125 coi_error = max( coi_error, coidef_numcon( cntvect, 3*nobs*dimk+dimk+1 ) )
126 coi_error = max( coi_error, coidef_numnz( cntvect, nobs * dimx * dimk + 5* nobs*dimk + dimk + dimk*dimx ) )
127 coi_error = max( coi_error, coidef_numnlnz( cntvect, nobs * dimx * dimk ) )
128 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
129 coi_error = max( coi_error, coidef_objcon( cntvect, 3*nobs*dimk + dimk+ 1 ) ) ! Objective is last constraint
130 coi_error = max( coi_error, coidef_optfile( cntvect, 'Minimax08.opt' ) )
131!
132! Tell CONOPT about the callback routines:
133!
134 coi_error = max( coi_error, coidef_readmatrix( cntvect, mm_readmatrix ) )
135 coi_error = max( coi_error, coidef_fdeval( cntvect, mm_fdeval ) )
136 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
137 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
138 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
139 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
140 coi_error = max( coi_error, coidef_debugfv( cntvect, 0 ) ) ! Debug Fdeval on or off
141
142#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
143 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
144#endif
145
146 If ( coi_error .ne. 0 ) THEN
147 write(*,*)
148 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
149 write(*,*)
150 call flog( "Skipping Solve due to setup errors", 1 )
151 ENDIF
152!
153! Save the solution so we can check the duals:
154!
155 do_allocate = .true.
156!
157! Start CONOPT:
158!
159 coi_error = coi_solve( cntvect )
160
161 write(*,*)
162 write(*,*) 'End of Minimax08 example 1. Return code=',coi_error
163
164 If ( coi_error /= 0 ) then
165 call flog( "Errors encountered during solution", 1 )
166 elseif ( stacalls == 0 .or. solcalls == 0 ) then
167 call flog( "Status or Solution routine was not called", 1 )
168 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) ) then
169 call flog( "Solver or Model status was not as expected (1,2)", 1 )
170! elseif ( abs( OBJ - 19.44434311d0 ) > 1.d-7 ) then
171! call flog( "Incorrect objective returned", 1 )
172 Else
173 Call checkdual( 'Minimax08', minimize )
174 endif
175
176 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
177
178 call flog( "Successful Solve", 0 )
179
180End Program minimax08
181!
182! ============================================================================
183! Define information about the model:
184!
185
186!> Define information about the model
187!!
188!! @include{doc} readMatrix_params.dox
189Integer Function mm_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
190 colsta, rowno, value, nlflag, n, m, nz, &
191 usrmem )
192#if defined(itl)
193!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_ReadMatrix
194#endif
195 Use lsq_7k
196 implicit none
197 integer, intent (in) :: n ! number of variables
198 integer, intent (in) :: m ! number of constraints
199 integer, intent (in) :: nz ! number of nonzeros
200 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
201 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
202 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
203 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
204 ! (not defined here)
205 integer, intent (out), dimension(m) :: type ! vector of equation types
206 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
207 ! (not defined here)
208 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
209 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
210 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
211 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
212 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
213 real*8 usrmem(*) ! optional user memory
214
215 Integer :: i, j, k, l, nzc
216!
217! Information about Variables:
218! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
219! Default: the status information in Vsta is not used.
220!
221 l = 0
222 do i = 1, dimx
223 do k = 1, dimk
224 l = l + 1
225 curr(l) = +0.9d0 ! Make the initial point infeasible to the sum(j,(x(j,k)) =L= 1;
226 lower(l) = -1.0d0
227 enddo
228 enddo
229!
230! Information about Constraints:
231! Default: Rhs = 0
232! Default: the status information in Esta and the function
233! value in FV are not used.
234! Default: Type: There is no default.
235! 0 = Equality,
236! 1 = Greater than or equal,
237! 2 = Less than or equal,
238! 3 = Non binding.
239!
240! Constraints 1 to Nobs*Dimk:
241! Rhs = Obs(i,k) and type Equality
242!
243 l = 0
244 do i = 1, nobs
245 do k = 1, dimk
246 l = l + 1
247 rhs(l) = obs(i,k)
248 type(l) = 0
249 enddo
250 enddo
251!
252! Constraint Nobs*Dimk+1 to 2*Nobs*Dimk
253! Rhs = 0 (default) and type Less than or equal
254!
255 do i = nobs*dimk+1, 2*nobs*dimk
256 type(i) = 2
257 enddo
258!
259! Constraint 2*Nobs*Dimk+1 to 3*Nobs*Dimk
260! Rhs = 0 (default) and type Greater than or equal
261!
262 do i = 2*nobs*dimk+1, 3*nobs*dimk
263 type(i) = 1
264 enddo
265!
266! Constraint 3*Nobs*Dimk+1 to 3*Nobs*Dimk+Dimk
267! Rhs = 1 and type Less than or equal
268!
269 do i = 3*nobs*dimk+1, 3*nobs*dimk+dimk
270 rhs(i) = 1.0d0
271 type(i) = 2
272 enddo
273!
274! Objective, type Nonbinding
275!
276 type(3*nobs*dimk+dimk+1) = 3
277!
278! Information about the Jacobian. We use the standard method with
279! Rowno, Value, Nlflag and Colsta and we do not use Colno.
280!
281! Colsta = Start of column indices (No Defaults):
282! Rowno = Row indices
283! Value = Value of derivative (by default only linear
284! derivatives are used)
285! Nlflag = 0 for linear and 1 for nonlinear derivative
286! (not needed for completely linear models)
287!
288!
289! Indices
290! x(j,k) res(i,k) z(k)
291! i,k: NL L=1
292! i,k: L=1 L=-1
293! i,k: L=1 L=+1
294! k L=1
295! obj L=+1
296!
297! We map (i,k) -> k+DimK*(i-1)
298! and (j,k) -> k+DimK*(j-1)
299!
300 nzc = 1
301 do j = 1, dimx ! x(j,k)
302 do k = 1, dimk
303 l = k+dimk*(j-1)
304 colsta(l) = nzc
305 do i = 1, nobs
306 rowno(nzc) = k+dimk*(i-1)
307 nlflag(nzc) = 1
308 nzc = nzc + 1
309 enddo
310 rowno(nzc) = 3*nobs*dimk+k
311 nlflag(nzc) = 0
312 value(nzc) = 1.d0
313 nzc = nzc + 1
314 enddo
315 enddo
316 do i = 1, nobs ! res(i,k)
317 do k = 1, dimk
318 colsta(dimx*dimk+k+dimk*(i-1)) = nzc
319 rowno(nzc) = k+dimk*(i-1)
320 nlflag(nzc) = 0
321 value(nzc) = 1.d0
322 nzc = nzc + 1
323 rowno(nzc) = nobs*dimk+k+dimk*(i-1)
324 nlflag(nzc) = 0
325 value(nzc) = 1.d0
326 nzc = nzc + 1
327 rowno(nzc) = 2*nobs*dimk+k+dimk*(i-1)
328 nlflag(nzc) = 0
329 value(nzc) = 1.d0
330 nzc = nzc + 1
331 enddo
332 enddo
333 do k = 1, dimk ! z(k)
334 colsta(dimx*dimk+nobs*dimk+k) = nzc
335 do i = 1, nobs
336 rowno(nzc) = nobs*dimk++k+dimk*(i-1)
337 nlflag(nzc) = 0
338 value(nzc) = -1.d0
339 nzc = nzc + 1
340 rowno(nzc) = 2*nobs*dimk+k+dimk*(i-1)
341 nlflag(nzc) = 0
342 value(nzc) = +1.d0
343 nzc = nzc + 1
344 enddo
345 rowno(nzc) = 3*nobs*dimk+dimk+1 ! Objective
346 nlflag(nzc) = 0
347 value(nzc) = +1.d0
348 nzc = nzc + 1
349 enddo
350 colsta(dimx*dimk+nobs*dimk+dimk+1) = nzc
351
352 mm_readmatrix = 0 ! Return value means OK
353
354end Function mm_readmatrix
355!
356!==========================================================================
357! Compute nonlinear terms and non-constant Jacobian elements
358!
359
360!> Compute nonlinear terms and non-constant Jacobian elements
361!!
362!! @include{doc} fdeval_params.dox
363Integer Function mm_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
364 n, nzc, thread, usrmem )
365#if defined(itl)
366!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: MM_FDEval
367#endif
368 use lsq_7k
369 implicit none
370 integer, intent (in) :: n ! number of variables
371 integer, intent (in) :: rowno ! number of the row to be evaluated
372 integer, intent (in) :: nzc ! number of nonzceros in this row
373 real*8, intent (in), dimension(n) :: x ! vector of current solution values
374 real*8, intent (in out) :: g ! constraint value
375 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
376 integer, intent (in), dimension(nzc) :: jcnm ! list of variables that appear nonlinearly
377 ! in this row. Ffor information only.
378 integer, intent (in) :: mode ! evaluation mode: 1 = function value
379 ! 2 = derivatives, 3 = both
380 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
381 ! as errcnt is incremented
382 integer, intent (in out) :: errcnt ! error counter to be incremented in case
383 ! of function evaluation errors.
384 integer, intent (in) :: thread
385 real*8 usrmem(*) ! optional user memory
386
387 integer :: i,j,k,l
388 real*8 :: s
389
390 mm_fdeval = 0
391 if ( rowno .le. nobs*dimk ) then
392!
393! We map (i,k): rowno -> k+DimK*(i-1) so
394! i = (Dimk+rowno-1))/Dimk
395! k = rowno-Dimk*(i-1)
396! and (j,k) -> k+DimK*(j-1)
397! Mode = 1 or 3: Function value - x-part only
398!
399 i = (dimk+rowno-1)/dimk
400 k = rowno-dimk*(i-1)
401 if ( mode .eq. 1 .or. mode .eq. 3 ) then
402 s = 0.d0
403 do j = 1, dimx
404 l = k+dimk*(j-1)
405 s = s + a(i,k,j)*x(l) + b(i,k,j)*x(l)**2
406 enddo
407 g = s
408 endif
409!
410! Mode = 2 or 3: Derivatives
411!
412 if ( mode .eq. 2 .or. mode .eq. 3 ) then
413 do j = 1, dimx
414 l = k+dimk*(j-1)
415 jac(l) = a(i,k,j) + 2.d0*b(i,k,j)*x(l)
416 enddo
417 endif
418 Else
419 mm_fdeval = 1 ! Illegal row number
420 endif
421
422end Function mm_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
subroutine checkdual(case, minmax)
Definition comdecl.f90:365
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_debugfv(cntvect, debugfv)
turn Debugging of FDEval on and off.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition coistart.f90:629
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition coistart.f90:552
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
#define dimx
Definition leastsq.c:17
#define nobs
Definition leastsq.c:16
void defdata()
Defines the data for the problem.
Definition leastsq.c:36
float rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.c:23
program minimax08
Main program. A simple setup and call of CONOPT.
Definition minimax08.f90:79
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:303
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:182
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer, parameter minimize
Definition comdecl.f90:25
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35