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