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