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