CONOPT
Loading...
Searching...
No Matches
ident23.f90
Go to the documentation of this file.
1!> @file ident23.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Model with identical variables, i.e. `a*xi =E= b*xj`
6!! Similar to ident20, but with an extra redundant identity.
7!!
8!! This is a CONOPT implementation of the GAMS model:
9!!
10!! @verbatim
11!! variable x1, x2, x3, x4, x5;
12!! equation e1, e2, e3, e4, e5;
13!!
14!! e1.. x1 =E= x3;
15!! e2.. x2 + x4 =E= 0;
16!! e3.. x1 + 2*x2 =E= 1;
17!! e4.. x5 =E= sqr(x1+x2+x3+x4);
18!! e5.. x1 - 2*x4 =E= 1;
19!!
20!! model m / all /;
21!! solve m using nlp minimizing x5;
22!! @endverbatim
23!!
24!!
25!! For more information about the individual callbacks, please have a look at the source code.
26
27!> Main program. A simple setup and call of CONOPT
28!!
29Program ident23
30
31 Use proginfo
32 Use coidef
33 implicit None
34!
35! Declare the user callback routines as Integer, External:
36!
37 Integer, External :: ident_readmatrix ! Mandatory Matrix definition routine defined below
38 Integer, External :: ident_fdeval ! Function and Derivative evaluation routine
39 Integer, External :: std_status ! Standard callback for displaying solution status
40 Integer, External :: std_solution ! Standard callback for displaying solution values
41 Integer, External :: std_message ! Standard callback for managing messages
42 Integer, External :: std_errmsg ! Standard callback for managing error messages
43#if defined(itl)
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_FDEval
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
50#endif
51!
52! Control vector
53!
54 INTEGER :: numcallback
55 INTEGER, Dimension(:), Pointer :: cntvect
56 INTEGER :: coi_error
57
58 call startup
59!
60! Create and initialize a Control Vector
61!
62 numcallback = coidef_size()
63 Allocate( cntvect(numcallback) )
64 coi_error = coidef_inifort( cntvect )
65!
66! Tell CONOPT about the size of the model by populating the Control Vector:
67!
68 coi_error = max( coi_error, coidef_numvar( cntvect, 5 ) )
69 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) )
70 coi_error = max( coi_error, coidef_numnz( cntvect, 13 ) )
71 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) )
72 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! minimize
73 coi_error = max( coi_error, coidef_objvar( cntvect, 5 ) ) ! Objective is x5
74 coi_error = max( coi_error, coidef_optfile( cntvect, 'Ident23.opt' ) )
75!
76! Tell CONOPT about the callback routines:
77!
78 coi_error = max( coi_error, coidef_readmatrix( cntvect, ident_readmatrix ) )
79 coi_error = max( coi_error, coidef_fdeval( cntvect, ident_fdeval ) )
80 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
81 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
82 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
83 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
84
85#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
86 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
87#endif
88
89 If ( coi_error .ne. 0 ) THEN
90 write(*,*)
91 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
92 write(*,*)
93 call flog( "Skipping Solve due to setup errors", 1 )
94 ENDIF
95!
96! Save the solution so we can check the duals:
97!
98 do_allocate = .true.
99!
100! Start CONOPT:
101!
102 coi_error = coi_solve( cntvect )
103
104 write(*,*)
105 write(*,*) 'End of Ident example 1. Return code=',coi_error
106
107 If ( coi_error /= 0 ) then
108 call flog( "Errors encountered during solution", 1 )
109 elseif ( stacalls == 0 .or. solcalls == 0 ) then
110 call flog( "Status or Solution routine was not called", 1 )
111 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) ) then
112 call flog( "Solver or Model status was not as expected (1,2)", 1 )
113 elseif ( abs( obj - 0.0d0 ) > 1.d-7 ) then
114 call flog( "Incorrect objective returned", 1 )
115 Else
116 Call checkdual( 'Ident23', minimize )
117 endif
118
119 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
120
121 call flog( "Successful Solve", 0 )
122
123End Program ident23
124!
125! ============================================================================
126! Define information about the model:
127!
128
129!> Define information about the model
130!!
131!! @include{doc} readMatrix_params.dox
132Integer Function ident_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
133 colsta, rowno, value, nlflag, n, m, nz, &
134 usrmem )
135#if defined(itl)
136!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
137#endif
138 implicit none
139 integer, intent (in) :: n ! number of variables
140 integer, intent (in) :: m ! number of constraints
141 integer, intent (in) :: nz ! number of nonzeros
142 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
143 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
144 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
145 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
146 ! (not defined here)
147 integer, intent (out), dimension(m) :: type ! vector of equation types
148 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
149 ! (not defined here)
150 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
151 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
152 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
153 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
154 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
155 real*8 usrmem(*) ! optional user memory
156
157!
158! Information about Variables:
159! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
160! Default: the status information in Vsta is not used.
161!
162! Information about Constraints:
163! Default: Rhs = 0
164! Default: the status information in Esta and the function
165! value in FV are not used.
166! Default: Type: There is no default.
167! 0 = Equality,
168! 1 = Greater than or equal,
169! 2 = Less than or equal,
170! 3 = Non binding.
171!
172! Constraints 1 to Nrow:
173! Rhs = Obs(i) and type Equality
174!
175 type(1) = 0
176 type(2) = 0
177 type(3) = 0
178 type(4) = 0
179 type(5) = 0
180 rhs(3) = 1.0d0
181 rhs(5) = 1.0d0
182!
183! Information about the Jacobian. We use the standard method with
184! Rowno, Value, Nlflag and Colsta and we do not use Colno.
185!
186! Colsta = Start of column indices (No Defaults):
187! Rowno = Row indices
188! Value = Value of derivative (by default only linear
189! derivatives are used)
190! Nlflag = 0 for linear and 1 for nonlinear derivative
191! (not needed for completely linear models)
192!
193! x1 x2 x3 x4 x5
194! e1 1 8
195! e2 5 10
196! e3 2 6
197! e4 3 7 9 11 13
198! e5 4 12
199
200! e1.. x1 =E= x3;
201! e2.. x2 + x4 =E= 0;
202! e3.. x1 + 2*x2 =E= 1;
203! e4.. x5 =E= sqr(x1+x2+x3+x4);
204! e5.. x1 - 2*x4 =E= 1;
205!
206! x1 x2 x3 x4 x5
207! e1 1.0 -1.0
208! e2 1.0 1.0
209! e3 1.0 2.0
210! e4 NL NL NL NL 1.0
211! e5 1.0 -2.0
212!
213 colsta(1) = 1
214 colsta(2) = 5
215 colsta(3) = 8
216 colsta(4) = 10
217 colsta(5) = 13
218 colsta(6) = 14
219 rowno(1) = 1; nlflag(1) = 0; value(1) = 1.0d0
220 rowno(2) = 3; nlflag(2) = 0; value(2) = 1.0d0
221 rowno(3) = 4; nlflag(3) = 1;
222 rowno(4) = 5; nlflag(4) = 0; value(4) = 1.0d0
223 rowno(5) = 2; nlflag(5) = 0; value(5) = 1.0d0
224 rowno(6) = 3; nlflag(6) = 0; value(6) = 2.0d0
225 rowno(7) = 4; nlflag(7) = 1;
226 rowno(8) = 1; nlflag(8) = 0; value(8) = -1.0d0
227 rowno(9) = 4; nlflag(9) = 1;
228 rowno(10) = 2; nlflag(10) = 0; value(10) = 1.0d0
229 rowno(11) = 4; nlflag(11) = 1;
230 rowno(12) = 5; nlflag(12) = 0; value(12) = -2.0
231 rowno(13) = 4; nlflag(13) = 0; value(13) = 1.0d0
232
233 ident_readmatrix = 0 ! Return value means OK
234
235end Function ident_readmatrix
236
237
238!> Compute nonlinear terms and non-constant Jacobian elements
239!!
240!! @include{doc} fdeval_params.dox
241Integer Function ident_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
242 n, nz, thread, usrmem )
243#if defined(itl)
244!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_FDEval
245#endif
246 Use ident
247 implicit none
248 integer, intent (in) :: n ! number of variables
249 integer, intent (in) :: rowno ! number of the row to be evaluated
250 integer, intent (in) :: nz ! number of nonzeros in this row
251 real*8, intent (in), dimension(n) :: x ! vector of current solution values
252 real*8, intent (in out) :: g ! constraint value
253 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
254 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
255 ! in this row. Ffor information only.
256 integer, intent (in) :: mode ! evaluation mode: 1 = function value
257 ! 2 = derivatives, 3 = both
258 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
259 ! as errcnt is incremented
260 integer, intent (in out) :: errcnt ! error counter to be incremented in case
261 ! of function evaluation errors.
262 integer, intent (in) :: thread
263 real*8 usrmem(*) ! optional user memory
264!
265 if ( rowno .eq. 4 ) then
266!
267! Mode = 1 or 3. Function value: -sqr(x1+x2+x3+x4)
268!
269 if ( mode .eq. 1 .or. mode .eq. 3 ) then
270 g = -(x(1)+x(2)+x(3)+x(4))**2
271 endif
272!
273! Mode = 2 or 3: Derivative values:
274!
275 if ( mode .eq. 2 .or. mode .eq. 3 ) then
276 jac(1) = -2.d0*(x(1)+x(2)+x(3)+x(4))
277 jac(2) = -2.d0*(x(1)+x(2)+x(3)+x(4))
278 jac(3) = -2.d0*(x(1)+x(2)+x(3)+x(4))
279 jac(4) = -2.d0*(x(1)+x(2)+x(3)+x(4))
280 endif
281 ident_fdeval = 0
282 else
283 ident_fdeval = 1 ! Illegal row number
284 endif
285
286end Function ident_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
subroutine checkdual(case, minmax)
Definition comdecl.f90:365
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition coistart.f90:552
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_objvar(cntvect, objvar)
defines the Objective Variable.
Definition coistart.f90:586
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
integer function ident_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition ident01.f90:131
integer function ident_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition ident20.f90:232
program ident23
Main program. A simple setup and call of CONOPT.
Definition ident23.f90:29
real *8 obj
Definition comdecl.f90:10
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer, parameter minimize
Definition comdecl.f90:25
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35