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