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