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