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