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