CONOPT
Loading...
Searching...
No Matches
postmo04.f90
Go to the documentation of this file.
1!> @file postmo04.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Model with derivatives that become constant after other variables are fixed.
6!! The model is similar to postmo01 but the bounds are changed.
7!!
8!! This is a CONOPT implementation of the GAMS model:
9!!
10!! @verbatim
11!! e1: log(x1) =E= x2;
12!! e2: exp(x3) =E= x1;
13!! max x2
14!! 0.01 <= x1 x1.l = 1
15!! -1000 <= x2 <= 1; x2.l = 0
16!! -2 <= x3 <= +inf; x3.l = 0;
17!! @endverbatim
18!!
19!! The bounds on x2 prevent e1 from being posttriangular immediately. However, due to
20!! the monotonicity the bounds can be transferred to x1. Similarly, the bounds on x1
21!! can be moved to x2 and the model ends with one variable and no constraints.
22!!
23!! The transfer of bounds work as follows:
24!! e1: the inverval `-1000 <= x2 <= 1` is transferred to `exp(-1000) <= x1 <= exp(1) = 2.718`
25!! where only the upper bound is tighter than the input bound.
26!! e2: the inverval `0.01 <= x1 <= exp(1)` from before is transferred to `-4.605 <= x3 <= 1`
27!! where again only the upper bound is tighter that the input bound.
28!!
29!! The expected solution is `x2=1` (at upper bound), `x1=exp(x2)=exp(1)`, and `x3= log(x1) = 1`;
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
38!> Main program. A simple setup and call of CONOPT
39!!
40Program postmo04
41
43 Use conopt
44 implicit None
45!
46! Declare the user callback routines as Integer, External:
47!
48 Integer, External :: con_readmatrix ! Mandatory Matrix definition routine defined below
49 Integer, External :: con_fdeval ! Function and Derivative evaluation routine
50 ! needed a nonlinear model.
51 Integer, External :: con_fdinterval ! Function and Derivative evaluation routine
52 ! optional for a nonlinear model.
53 Integer, External :: std_status ! Standard callback for displaying solution status
54 Integer, External :: std_solution ! Standard callback for displaying solution values
55 Integer, External :: std_message ! Standard callback for managing messages
56 Integer, External :: std_errmsg ! Standard callback for managing error messages
57#ifdef dec_directives_win32
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
59!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
60!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDInterval
61!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
62!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
63!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
64!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
65#endif
66!
67! Control vector
68!
69 INTEGER, Dimension(:), Pointer :: cntvect
70 INTEGER :: coi_error
71!
72! Create and initialize a Control Vector
73!
74 call startup
75
76 coi_error = coi_create( cntvect )
77!
78! Tell CONOPT about the size of the model by populating the Control Vector:
79!
80 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! # variables
81 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
82 coi_error = max( coi_error, coidef_numnz( cntvect, 4 ) ) ! # nonzeros in the Jacobian
83 coi_error = max( coi_error, coidef_numnlnz( cntvect, 2 ) ) ! # of which are nonlinear
84 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
85 coi_error = max( coi_error, coidef_objvar( cntvect, 2 ) ) ! Objective is variable 2
86 coi_error = max( coi_error, coidef_optfile( cntvect, 'postmo04.opt' ) )
87!
88! Tell CONOPT about the callback routines:
89!
90 coi_error = max( coi_error, coidef_readmatrix( cntvect, con_readmatrix ) )
91 coi_error = max( coi_error, coidef_fdeval( cntvect, con_fdeval ) )
92 coi_error = max( coi_error, coidef_fdinterval( cntvect, con_fdinterval ) )
93 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
94 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
95 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
96 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
97
98#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
99 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
100#endif
101
102 If ( coi_error .ne. 0 ) THEN
103 write(*,*)
104 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
105 write(*,*)
106 call flog( "Skipping Solve due to setup errors", 1 )
107 ENDIF
108!
109! Save the solution so we can check the duals:
110!
111 do_allocate = .true.
112!
113! Start CONOPT:
114!
115 coi_error = coi_solve( cntvect )
116
117 write(*,*)
118 write(*,*) 'End of postmo04 example. Return code=',coi_error
119
120 If ( coi_error /= 0 ) then
121 call flog( "Errors encountered during solution", 1 )
122 elseif ( stacalls == 0 .or. solcalls == 0 ) then
123 call flog( "Status or Solution routine was not called", 1 )
124 elseif ( sstat /= 1 .or. mstat /= 2 ) then
125 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
126 elseif ( abs( obj-1.0d0 ) > 0.000001d0 ) then
127 call flog( "Incorrect objective returned", 1 )
128 Else
129 Call checkdual( 'postmo04', maximize )
130 endif
131
132 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
133
134 call flog( "Successful Solve", 0 )
136End Program postmo04
137!
138! ============================================================================
139! Define information about the model:
140!
141
142!> Define information about the model
143!!
144!! @include{doc} readMatrix_params.dox
145Integer Function con_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
146 colsta, rowno, value, nlflag, n, m, nz, &
147 usrmem )
148#ifdef dec_directives_win32
149!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_ReadMatrix
150#endif
151 implicit none
152 integer, intent (in) :: n ! number of variables
153 integer, intent (in) :: m ! number of constraints
154 integer, intent (in) :: nz ! number of nonzeros
155 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
156 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
157 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
158 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
159 ! (not defined here)
160 integer, intent (out), dimension(m) :: type ! vector of equation types
161 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
162 ! (not defined here)
163 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
164 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
165 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
166 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
167 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
168 real*8 usrmem(*) ! optional user memory
169!
170! Information about Variables:
171! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
172! Default: the status information in Vsta is not used.
173!
174 lower(1) = 0.01d0 ; curr(1) = 1.0d0
175 lower(2) = -1000.0d0; curr(2) = 0.0d0; upper(2) = 1.0d0
176 lower(3) = -2.0d0 ; curr(3) = 0.0d0
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 type(1) = 0
189 type(2) = 0
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) x(2) x(3)
203! 1: 1 3
204! 2: 2 4
205! e1: log(x1) =E= x2;
206! e2: exp(x3) =E= x1;
207!
208 colsta(1) = 1
209 colsta(2) = 3
210 colsta(3) = 4
211 colsta(4) = 5
212 rowno(1) = 1
213 rowno(2) = 2
214 rowno(3) = 1
215 rowno(4) = 2
216!
217! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
218! x(1) x(2) x(3)
219! 1: NL L
220! 2: L NL
221!
222 nlflag(1) = 1
223 nlflag(2) = 0
224 nlflag(3) = 0
225 nlflag(4) = 1
226!
227! Value (Linear only)
228! x(1) x(2) x(3)
229! 1: NL -1.0
230! 2: -1.0 NL
231!
232 value(2) = -1.d0
233 value(3) = -1.d0
234
235 con_readmatrix = 0 ! Return value means OK
236
237end Function con_readmatrix
238!
239!==========================================================================
240! Compute nonlinear terms and non-constant Jacobian elements
241!
242
243!> Compute nonlinear terms and non-constant Jacobian elements
244!!
245!! @include{doc} fdeval_params.dox
246Integer Function con_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
247 n, nz, thread, usrmem )
248#ifdef dec_directives_win32
249!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Con_FDEval
250#endif
251 implicit none
252 integer, intent (in) :: n ! number of variables
253 integer, intent (in) :: rowno ! number of the row to be evaluated
254 integer, intent (in) :: nz ! number of nonzeros in this row
255 real*8, intent (in), dimension(n) :: x ! vector of current solution values
256 real*8, intent (in out) :: g ! constraint value
257 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
258 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
259 ! in this row. Ffor information only.
260 integer, intent (in) :: mode ! evaluation mode: 1 = function value
261 ! 2 = derivatives, 3 = both
262 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
263 ! as errcnt is incremented
264 integer, intent (in out) :: errcnt ! error counter to be incremented in case
265 ! of function evaluation errors.
266 integer, intent (in) :: thread
267 real*8 usrmem(*) ! optional user memory
268 if ( rowno .eq. 1 ) then
269!
270! e1: log(x1) =E= x2;
271!
272! Mode = 1 or 3: Function value
273!
274 if ( mode .eq. 1 .or. mode .eq. 3 ) then
275 g = log(x(1))
276 endif
277!
278! Mode = 2 or 3: Derivatives
279!
280 if ( mode .eq. 2 .or. mode .eq. 3 ) then
281 jac(1) = 1.0d0/x(1)
282 endif
283 con_fdeval = 0
284 else if ( rowno .eq. 2 ) then
285!
286! e2: exp(x3) =E= x1;
287!
288! Mode = 1 or 3: Function value
289!
290 if ( mode .eq. 1 .or. mode .eq. 3 ) then
291 g = exp(x(3))
292 endif
293!
294! Mode = 2 or 3: Derivatives
295!
296 if ( mode .eq. 2 .or. mode .eq. 3 ) then
297 jac(3) = exp(x(3))
298 endif
299 con_fdeval = 0
300 Else
301 con_fdeval = 1 ! Should not happen
302 endif
303
304end Function con_fdeval
305
306
307!> Evaluating nonlinear functions and derivatives on an interval. Used in preprocessing
308!!
309!! @include{doc} fdinterval_params.dox
310Integer Function con_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 :: Con_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 if ( rowno .eq. 1 ) then
326!
327! e1: log(x1) =E= x2;
328!
329! Mode = 1 or 3. Function value
330!
331 if ( mode .eq. 1 .or. mode .eq. 3 ) then
332 gmin = log(xmin(1))
333 gmax = log(xmax(1))
334 endif
335!
336! Mode = 2 or 3: Derivative values:
337!
338 if ( mode .eq. 2 .or. mode .eq. 3 ) then
339 jmin(1) = 1.0d0/xmax(1)
340 jmax(1) = 1.0d0/xmin(1)
341 endif
343 else if ( rowno .eq. 2 ) then
344!
345! e2: exp(x3) =E= x1;
346!
347! Mode = 1 or 3. Function value
348!
349 if ( mode .eq. 1 .or. mode .eq. 3 ) then
350 gmin = exp(xmin(3))
351 gmax = exp(xmax(3))
352 endif
353!
354! Mode = 2 or 3: Derivative values:
355!
356 if ( mode .eq. 2 .or. mode .eq. 3 ) then
357 jmin(3) = exp(xmin(3))
358 jmax(3) = exp(xmax(3))
359 endif
361 else
362!
363! There are no other rows:
364!
366 endif
367
368end Function con_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_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function con_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition const01.f90:127
integer function con_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition const01.f90:229
integer function con_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 const01.f90:279
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_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
#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
program postmo04
Main program. A simple setup and call of CONOPT.
Definition postmo04.f90:42