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