CONOPT
Loading...
Searching...
No Matches
mono09.f90
Go to the documentation of this file.
1!> @file mono09.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Monotone function to bound conversion example 09
6!!
7!! Inaccurate function values and convergence test -- the log is
8!! computed in single precision
9!!
10!! This is a CONOPT implementation of the GAMS model:
11!!
12!! @verbatim
13!! variable x1
14!! equation e1;
15!!
16!! e1 .. log(x1) =L= 2.01;
17!!
18!! x1.lo = 0.01;
19!! model mono / all /;
20!! solve mono using nlp maximizing x1;
21!! @endverbatim
22!!
23!!
24!!
25!! For more information about the individual callbacks, please have a look at the source code.
26
27#if defined(_WIN32) && !defined(_WIN64)
28#define dec_directives_win32
29#endif
30
31!> Main program. A simple setup and call of CONOPT
32!!
33Program mono09
34
36 Use conopt
37 implicit None
38!
39! Declare the user callback routines as Integer, External:
40!
41 Integer, External :: mono_readmatrix ! Mandatory Matrix definition routine defined below
42 Integer, External :: mono_fdeval ! Function and Derivative evaluation routine
43 ! needed a nonlinear model.
44 Integer, External :: mono_fdinterval ! Function and Derivative evaluation routine
45 ! needed a nonlinear model.
46 Integer, External :: std_status ! Standard callback for displaying solution status
47 Integer, External :: std_solution ! Standard callback for displaying solution values
48 Integer, External :: std_message ! Standard callback for managing messages
49 Integer, External :: std_errmsg ! Standard callback for managing error messages
50 Integer, External :: std_triord ! Standard callback for Monongular order
51#ifdef dec_directives_win32
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_ReadMatrix
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDEval
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDInterval
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
59!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
60#endif
61!
62! Control vector
63!
64 INTEGER, Dimension(:), Pointer :: cntvect
65 INTEGER :: coi_error
66
67 call startup
68!
69! Create and initialize a Control Vector
70!
71 coi_error = coi_create( cntvect )
72!
73! Tell CONOPT about the size of the model by populating the Control Vector:
74!
75 coi_error = max( coi_error, coidef_numvar( cntvect, 1 ) ) ! # variables
76 coi_error = max( coi_error, coidef_numcon( cntvect, 1 ) ) ! # constraints
77 coi_error = max( coi_error, coidef_numnz( cntvect, 1 ) ) ! # nonzeros in the Jacobian
78 coi_error = max( coi_error, coidef_numnlnz( cntvect, 1 ) ) ! # of which are nonlinear
79 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
80 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 3
81 coi_error = max( coi_error, coidef_optfile( cntvect, 'Mono09.opt' ) )
82!
83! Tell CONOPT about the callback routines:
84!
85 coi_error = max( coi_error, coidef_readmatrix( cntvect, mono_readmatrix ) )
86 coi_error = max( coi_error, coidef_fdeval( cntvect, mono_fdeval ) )
87 coi_error = max( coi_error, coidef_fdinterval( cntvect, mono_fdinterval ) )
88 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
89 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
90 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
91 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
92 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
93
94#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
95 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
96#endif
97
98 If ( coi_error .ne. 0 ) THEN
99 write(*,*)
100 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
101 write(*,*)
102 call flog( "Skipping Solve due to setup errors", 1 )
103 ENDIF
104!
105! Save the solution so we can check the duals:
106!
107 do_allocate = .true.
108!
109! Start CONOPT:
110!
111 coi_error = coi_solve( cntvect )
112
113 write(*,*)
114 write(*,*) 'End of Mono09 example. Return code=',coi_error
115
116 If ( coi_error /= 0 ) then
117 call flog( "Errors encountered during solution", 1 )
118 elseif ( stacalls == 0 .or. solcalls == 0 ) then
119 call flog( "Status or Solution routine was not called", 1 )
120 elseif ( sstat /= 1 .or. mstat /= 1 ) then
121 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
122 elseif ( abs( obj-exp(2.01d0) ) > 0.000001d0 ) then
123 call flog( "Incorrect objective returned", 1 )
124 Else
125 Call checkdual( 'Mono09', maximize )
126 endif
127
128 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
129
130 call flog( "Successful Solve", 0 )
131
132End Program mono09
133!
134! ============================================================================
135! Define information about the model:
136!
137
138!> Define information about the model
139!!
140!! @include{doc} readMatrix_params.dox
141Integer Function mono_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
142 colsta, rowno, value, nlflag, n, m, nz, &
143 usrmem )
144#ifdef dec_directives_win32
145!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_ReadMatrix
146#endif
147 implicit none
148 integer, intent (in) :: n ! number of variables
149 integer, intent (in) :: m ! number of constraints
150 integer, intent (in) :: nz ! number of nonzeros
151 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
152 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
153 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
154 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
155 ! (not defined here)
156 integer, intent (out), dimension(m) :: type ! vector of equation types
157 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
158 ! (not defined here)
159 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
160 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
161 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
162 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
163 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
164 real*8 usrmem(*) ! optional user memory
165!
166! Information about Variables:
167! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
168! Default: the status information in Vsta is not used.
169!
170! The model uses defaults
171!
172! Information about Constraints:
173! Default: Rhs = 0
174! Default: the status information in Esta and the function
175! value in FV are not used.
176! Default: Type: There is no default.
177! 0 = Equality,
178! 1 = Greater than or equal,
179! 2 = Less than or equal,
180! 3 = Non binding.
181!
182! Constraint 1: e1
183! Rhs = 2.01 and type Less than or Equal
184!
185 rhs(1) = 2.01d0
186 type(1) = 2
187!
188 lower(1) = 0.01d0
189 curr(1) = 0.01d0
190!
191! Information about the Jacobian. CONOPT expects a columnwise
192! representation in Rowno, Value, Nlflag and Colsta.
193!
194! Colsta = Start of column indices (No Defaults):
195! Rowno = Row indices
196! Value = Value of derivative (by default only linear
197! derivatives are used)
198! Nlflag = 0 for linear and 1 for nonlinear derivative
199! (not needed for completely linear models)
200!
201! Indices
202! x(1)
203! 1: 1
204!
205 colsta(1) = 1
206 colsta(2) = 2
207 rowno(1) = 1
208!
209! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
210! x(1)
211! 1: NL
212!
213 nlflag(1) = 1
214!
215! Value (Linear only)
216! x(1)
217! 1: NL
218!
219 mono_readmatrix = 0 ! Return value means OK
220
221end Function mono_readmatrix
222!
223!==========================================================================
224! Compute nonlinear terms and non-constant Jacobian elements
225!
226
227!> Compute nonlinear terms and non-constant Jacobian elements
228!!
229!! @include{doc} fdeval_params.dox
230Integer Function mono_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
231 n, nz, thread, usrmem )
232#ifdef dec_directives_win32
233!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDEval
234#endif
235 implicit none
236 integer, intent (in) :: n ! number of variables
237 integer, intent (in) :: rowno ! number of the row to be evaluated
238 integer, intent (in) :: nz ! number of nonzeros in this row
239 real*8, intent (in), dimension(n) :: x ! vector of current solution values
240 real*8, intent (in out) :: g ! constraint value
241 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
242 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
243 ! in this row. Ffor information only.
244 integer, intent (in) :: mode ! evaluation mode: 1 = function value
245 ! 2 = derivatives, 3 = both
246 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
247 ! as errcnt is incremented
248 integer, intent (in out) :: errcnt ! error counter to be incremented in case
249 ! of function evaluation errors.
250 integer, intent (in) :: thread
251 real*8 usrmem(*) ! optional user memory
252 real*4 :: xs
253!
254! Row 1: e1
255!
256 if ( rowno .eq. 1 ) then
257!
258! Mode = 1 or 3. G = log(x1)
259!
260 xs = x(1)
261 if ( mode .eq. 1 .or. mode .eq. 3 ) then
262 g = log(xs)
263 endif
264!
265! Mode = 2 or 3: Derivative values:
266!
267 if ( mode .eq. 2 .or. mode .eq. 3 ) then
268 jac(1) = 1.0/xs
269 endif
270 mono_fdeval = 0
271 else
272!
273! There are no other rows:
274!
275 mono_fdeval = 1
276 endif
277
278end Function mono_fdeval
279
280
281!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
282!!
283!! @include{doc} fdinterval_params.dox
284Integer Function mono_fdinterval( XMIN, XMAX, GMIN, GMAX, &
285 JMIN, JMAX, ROWNO, JCNM, &
286 MODE, PINF, N, NJ, USRMEM )
287#ifdef dec_directives_win32
288!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Mono_FDInterval
289#endif
290 Implicit None
291 INTEGER, Intent(IN) :: rowno, mode, n, nj
292 INTEGER, Dimension(NJ), Intent(IN) :: jcnm
293 real*8, Dimension(N), Intent(IN) :: xmin, xmax
294 real*8, Intent(IN OUT) :: gmin, gmax
295 real*8, Dimension(N), Intent(IN OUT) :: jmin, jmax
296 real*8, Intent(IN) :: pinf
297 real*8, Intent(IN OUT) :: usrmem(*)
298
299!
300! Row 1: e1
301!
302 write(10,*) 'Enter Mono_FDInterval. Row=',rowno,' Mode=',mode
303 write(10,*) 'Xmin=',xmin
304 write(10,*) 'Xmax=',xmax
305 if ( rowno .eq. 1 ) then
306!
307! Mode = 1 or 3. G = log(x1)
308!
309 if ( mode .eq. 1 .or. mode .eq. 3 ) then
310 If ( xmin(1) <= 0.0d0 ) then
311 gmin = -pinf
312 else
313 gmin = log(xmin(1))
314 endif
315 If ( xmax(1) <= 0.0d0 ) then
316 gmax = -pinf
317 else
318 gmax = log(xmax(1))
319 endif
320 write(10,*) 'Gmin=',gmin,' Gmax=',gmax
321 endif
322!
323! Mode = 2 or 3: Derivative values:
324!
325 if ( mode .eq. 2 .or. mode .eq. 3 ) then
326 If ( xmin(1) <= 0.0d0 ) then
327 jmin(1) = -pinf
328 jmax(1) = +pinf
329 else
330 jmin(1) = 1.0d0/xmax(1)
331 jmax(1) = 1.0d0/xmin(1)
332 endif
333 write(10,*) 'Jmin=',jmin
334 write(10,*) 'Jmax=',jmax
335 endif
337 else
338!
339! There are no other rows:
340!
342 endif
343
344end Function mono_fdinterval
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_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:289
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_fdinterval(cntvect, coi_fdinterval)
define callback routine for performing function and derivative evaluations on intervals.
Definition conopt.f90:1396
integer(c_int) function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
Definition conopt.f90:1371
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
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
integer function mono_fdinterval(xmin, xmax, gmin, gmax, jmin, jmax, rowno, jcnm, mode, pinf, n, nj, usrmem)
Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing.
Definition mono01.f90:265
integer function mono_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition mono01.f90:130
integer function mono_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition mono01.f90:215
program mono09
Main program. A simple setup and call of CONOPT.
Definition mono09.f90:35
#define nj
Definition mp_trans.c:46
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
integer, parameter maximize
Definition comdecl.f90:31
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41