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