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