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