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
109End Program roseq
110!
111! ============================================================================
112! Define information about the model:
113!
114
115!> Define information about the model
116!!
117!! @include{doc} readMatrix_params.dox
118Integer Function ros_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
119 colsta, rowno, value, nlflag, n, m, nz, &
120 usrmem )
121#ifdef dec_directives_win32
122!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ros_ReadMatrix
123#endif
124 IMPLICIT NONE
125 integer, intent (in) :: n ! number of variables
126 integer, intent (in) :: m ! number of constraints
127 integer, intent (in) :: nz ! number of nonzeros
128 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
129 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
130 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
131 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
132 ! (not defined here)
133 integer, intent (out), dimension(m) :: type ! vector of equation types
134 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
135 ! (not defined here)
136 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
137 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
138 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
139 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
140 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
141 real*8 usrmem(*) ! optional user memory
142
143!
144! Information about Variables:
145! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
146! Default: the status information in Vsta is not used.
147!
148 curr(1) = -1.d0
149 curr(2) = +1.1d0
150!
151! Information about Constraints:
152! Default: Rhs = 0
153! Default: the status information in Esta and the function
154! value in FV are not used.
155! Default: Type: There is no default.
156! 0 = Equality,
157! 1 = Greater than or equal,
158! 2 = Less than or equal,
159! 3 = Non binding.
160!
161 type(1) = 0
162 type(2) = 0
163!
164! Information about the Jacobian. We have to define Rowno, Value,
165! Nlflag and Colsta.
166!
167! Colsta = Start of column indices (No Defaults):
168! Rowno = Row indices
169! Value = Value of derivative (by default only linear
170! derivatives are used)
171! Nlflag = 0 for linear and 1 for nonlinear derivative
172! (not needed for completely linear models)
173!
174! Indices
175! x(1) x(2)
176! 1: 1 3
177! 2: 2 4
178!
179! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
180! x(1) x(2)
181! 1: NL NL
182! 2: NL 200
183!
184! Value (Linear only)
185!
186 value(4) = 200.d0
187 colsta(1) = 1
188 colsta(2) = 3
189 colsta(3) = 5
190 rowno(1) = 1
191 rowno(2) = 2
192 rowno(3) = 1
193 rowno(4) = 2
194 nlflag(1) = 1
195 nlflag(2) = 1
196 nlflag(3) = 1
197 nlflag(4) = 0
199 ros_readmatrix = 0 ! Return value means OK
200
201end Function ros_readmatrix
202!
203!==========================================================================
204! Compute nonlinear terms and non-constant Jacobian elements
205!
206
207!> Compute nonlinear terms and non-constant Jacobian elements
208!!
209!! @include{doc} fdeval_params.dox
210Integer Function ros_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
211 n, nz, thread, usrmem )
212#ifdef dec_directives_win32
213!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ros_FDEval
214#endif
215 IMPLICIT NONE
216 integer, intent (in) :: n ! number of variables
217 integer, intent (in) :: rowno ! number of the row to be evaluated
218 integer, intent (in) :: nz ! number of nonzeros in this row
219 real*8, intent (in), dimension(n) :: x ! vector of current solution values
220 real*8, intent (in out) :: g ! constraint value
221 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
222 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
223 ! in this row. Ffor information only.
224 integer, intent (in) :: mode ! evaluation mode: 1 = function value
225 ! 2 = derivatives, 3 = both
226 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
227 ! as errcnt is incremented
228 integer, intent (in out) :: errcnt ! error counter to be incremented in case
229 ! of function evaluation errors.
230 integer, intent (in) :: thread
231 real*8 usrmem(*) ! optional user memory
232
233 ros_fdeval = 0
234!
235!
236 if ( rowno .eq. 1 ) then
237!
238! Row 1: 2*(x1-1) + 200*(x2-x1**2)*(-2*x1) = 0
239! Mode = 1 or 3. Function value:
240!
241 if ( mode .eq. 1 .or. mode .eq. 3 ) then
242 g = 2.d0*(x(1)-1.d0) + 200.d0*(x(2)-x(1)**2)*(-2.d0*x(1))
243! write(10,"('Row 1: X=',1p,2e20.10,' G =',1p,e20.10)") x, g
244 endif
245!
246! Mode = 2 or 3: Derivative values:
247!
248 if ( mode .eq. 2 .or. mode .eq. 3 ) then
249 jac(1) = 2.d0 - 400.d0*(x(2)-x(1)**2) + 200.d0*(-2.d0*x(1))**2
250 jac(2) = -400.d0*x(1)
251! write(10,"('Row 1: X=',1p,2e20.10,' Jac=',1p,2e20.10)") x, jac
252 endif
253 elseif ( rowno .eq. 2 ) then
254!
255! Row 2: 200*(x2-x1**2) = 0
256! Mode = 1 or 3. Function value: Note that we include linear terms after a call to COIDEF_FVincLin.
257!
258 if ( mode .eq. 1 .or. mode .eq. 3 ) then
259 g = 200.d0*(x(2)-x(1)**2)
260! write(10,"('Row 2: X=',1p,2e20.10,' G =',1p,e20.10)") x, g
261 endif
262!
263! Mode = 2 or 3: Derivative values: Only jac(1) is nonlinear
264!
265 if ( mode .eq. 2 .or. mode .eq. 3 ) then
266 jac(1) = -400.d0*x(1)
267! write(10,"('Row 2: X=',1p,2e20.10,' Jac=',1p,2e20.10)") x, jac
268 endif
269 else
270 ros_fdeval = 1
271 endif
272
273end Function ros_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
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_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
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:112
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:200