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