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