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