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