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