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#if defined(_WIN32) && !defined(_WIN64)
28#define dec_directives_win32
29#endif
30
31!> Main program. A simple setup and call of CONOPT
32!!
33Program ident23
34
36 Use conopt
37 implicit None
38!
39! Declare the user callback routines as Integer, External:
40!
41 Integer, External :: ident_readmatrix ! Mandatory Matrix definition routine defined below
42 Integer, External :: ident_fdeval ! Function and Derivative evaluation routine
43 Integer, External :: std_status ! Standard callback for displaying solution status
44 Integer, External :: std_solution ! Standard callback for displaying solution values
45 Integer, External :: std_message ! Standard callback for managing messages
46 Integer, External :: std_errmsg ! Standard callback for managing error messages
47#ifdef dec_directives_win32
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_FDEval
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
54#endif
55!
56! Control vector
57!
58 INTEGER, Dimension(:), Pointer :: cntvect
59 INTEGER :: coi_error
60
61 call startup
62!
63! Create and initialize a Control Vector
64!
65 coi_error = coi_create( cntvect )
66!
67! Tell CONOPT about the size of the model by populating the Control Vector:
68!
69 coi_error = max( coi_error, coidef_numvar( cntvect, 5 ) )
70 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) )
71 coi_error = max( coi_error, coidef_numnz( cntvect, 13 ) )
72 coi_error = max( coi_error, coidef_numnlnz( cntvect, 4 ) )
73 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! minimize
74 coi_error = max( coi_error, coidef_objvar( cntvect, 5 ) ) ! Objective is x5
75 coi_error = max( coi_error, coidef_optfile( cntvect, 'Ident23.opt' ) )
76!
77! Tell CONOPT about the callback routines:
78!
79 coi_error = max( coi_error, coidef_readmatrix( cntvect, ident_readmatrix ) )
80 coi_error = max( coi_error, coidef_fdeval( cntvect, ident_fdeval ) )
81 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
82 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
83 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
84 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
85
86#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
87 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
88#endif
89
90 If ( coi_error .ne. 0 ) THEN
91 write(*,*)
92 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
93 write(*,*)
94 call flog( "Skipping Solve due to setup errors", 1 )
95 ENDIF
96!
97! Save the solution so we can check the duals:
98!
99 do_allocate = .true.
100!
101! Start CONOPT:
102!
103 coi_error = coi_solve( cntvect )
104
105 write(*,*)
106 write(*,*) 'End of Ident example 1. Return code=',coi_error
107
108 If ( coi_error /= 0 ) then
109 call flog( "Errors encountered during solution", 1 )
110 elseif ( stacalls == 0 .or. solcalls == 0 ) then
111 call flog( "Status or Solution routine was not called", 1 )
112 elseif ( .not. ( sstat == 1 .and. mstat == 2 ) ) then
113 call flog( "Solver or Model status was not as expected (1,2)", 1 )
114 elseif ( abs( obj - 0.0d0 ) > 1.d-7 ) then
115 call flog( "Incorrect objective returned", 1 )
116 Else
117 Call checkdual( 'Ident23', minimize )
118 endif
119
120 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
121
122 call flog( "Successful Solve", 0 )
123!
124! Free solution memory
125!
126 call finalize
127
128End Program ident23
129!
130! ============================================================================
131! Define information about the model:
132!
133
134!> Define information about the model
135!!
136!! @include{doc} readMatrix_params.dox
137Integer Function ident_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
138 colsta, rowno, value, nlflag, n, m, nz, &
139 usrmem )
140#ifdef dec_directives_win32
141!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
142#endif
143 implicit none
144 integer, intent (in) :: n ! number of variables
145 integer, intent (in) :: m ! number of constraints
146 integer, intent (in) :: nz ! number of nonzeros
147 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
148 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
149 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
150 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
151 ! (not defined here)
152 integer, intent (out), dimension(m) :: type ! vector of equation types
153 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
154 ! (not defined here)
155 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
156 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
157 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
158 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
159 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
160 real*8 usrmem(*) ! optional user memory
161
162!
163! Information about Variables:
164! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
165! Default: the status information in Vsta is not used.
166!
167! Information about Constraints:
168! Default: Rhs = 0
169! Default: the status information in Esta and the function
170! value in FV are not used.
171! Default: Type: There is no default.
172! 0 = Equality,
173! 1 = Greater than or equal,
174! 2 = Less than or equal,
175! 3 = Non binding.
176!
177! Constraints 1 to Nrow:
178! Rhs = Obs(i) and type Equality
179!
180 type(1) = 0
181 type(2) = 0
182 type(3) = 0
183 type(4) = 0
184 type(5) = 0
185 rhs(3) = 1.0d0
186 rhs(5) = 1.0d0
187!
188! Information about the Jacobian. CONOPT expects a columnwise
189! representation in Rowno, Value, Nlflag and Colsta.
190!
191! Colsta = Start of column indices (No Defaults):
192! Rowno = Row indices
193! Value = Value of derivative (by default only linear
194! derivatives are used)
195! Nlflag = 0 for linear and 1 for nonlinear derivative
196! (not needed for completely linear models)
197!
198! x1 x2 x3 x4 x5
199! e1 1 8
200! e2 5 10
201! e3 2 6
202! e4 3 7 9 11 13
203! e5 4 12
204
205! e1.. x1 =E= x3;
206! e2.. x2 + x4 =E= 0;
207! e3.. x1 + 2*x2 =E= 1;
208! e4.. x5 =E= sqr(x1+x2+x3+x4);
209! e5.. x1 - 2*x4 =E= 1;
210!
211! x1 x2 x3 x4 x5
212! e1 1.0 -1.0
213! e2 1.0 1.0
214! e3 1.0 2.0
215! e4 NL NL NL NL 1.0
216! e5 1.0 -2.0
217!
218 colsta(1) = 1
219 colsta(2) = 5
220 colsta(3) = 8
221 colsta(4) = 10
222 colsta(5) = 13
223 colsta(6) = 14
224 rowno(1) = 1; nlflag(1) = 0; value(1) = 1.0d0
225 rowno(2) = 3; nlflag(2) = 0; value(2) = 1.0d0
226 rowno(3) = 4; nlflag(3) = 1;
227 rowno(4) = 5; nlflag(4) = 0; value(4) = 1.0d0
228 rowno(5) = 2; nlflag(5) = 0; value(5) = 1.0d0
229 rowno(6) = 3; nlflag(6) = 0; value(6) = 2.0d0
230 rowno(7) = 4; nlflag(7) = 1;
231 rowno(8) = 1; nlflag(8) = 0; value(8) = -1.0d0
232 rowno(9) = 4; nlflag(9) = 1;
233 rowno(10) = 2; nlflag(10) = 0; value(10) = 1.0d0
234 rowno(11) = 4; nlflag(11) = 1;
235 rowno(12) = 5; nlflag(12) = 0; value(12) = -2.0
236 rowno(13) = 4; nlflag(13) = 0; value(13) = 1.0d0
237
238 ident_readmatrix = 0 ! Return value means OK
239
240end Function ident_readmatrix
241
242
243!> Compute nonlinear terms and non-constant Jacobian elements
244!!
245!! @include{doc} fdeval_params.dox
246Integer Function ident_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 :: Ident_FDEval
250#endif
251 Use ident
252 implicit none
253 integer, intent (in) :: n ! number of variables
254 integer, intent (in) :: rowno ! number of the row to be evaluated
255 integer, intent (in) :: nz ! number of nonzeros in this row
256 real*8, intent (in), dimension(n) :: x ! vector of current solution values
257 real*8, intent (in out) :: g ! constraint value
258 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
259 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
260 ! in this row. Ffor information only.
261 integer, intent (in) :: mode ! evaluation mode: 1 = function value
262 ! 2 = derivatives, 3 = both
263 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
264 ! as errcnt is incremented
265 integer, intent (in out) :: errcnt ! error counter to be incremented in case
266 ! of function evaluation errors.
267 integer, intent (in) :: thread
268 real*8 usrmem(*) ! optional user memory
269!
270 if ( rowno .eq. 4 ) then
271!
272! Mode = 1 or 3. Function value: -sqr(x1+x2+x3+x4)
273!
274 if ( mode .eq. 1 .or. mode .eq. 3 ) then
275 g = -(x(1)+x(2)+x(3)+x(4))**2
276 endif
277!
278! Mode = 2 or 3: Derivative values:
279!
280 if ( mode .eq. 2 .or. mode .eq. 3 ) then
281 jac(1) = -2.d0*(x(1)+x(2)+x(3)+x(4))
282 jac(2) = -2.d0*(x(1)+x(2)+x(3)+x(4))
283 jac(3) = -2.d0*(x(1)+x(2)+x(3)+x(4))
284 jac(4) = -2.d0*(x(1)+x(2)+x(3)+x(4))
285 endif
286 ident_fdeval = 0
287 else
288 ident_fdeval = 1 ! Illegal row number
289 endif
290
291end Function ident_fdeval
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_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_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 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:138
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:225
program ident23
Main program. A simple setup and call of CONOPT.
Definition ident23.f90:35
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