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