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