CONOPT
Loading...
Searching...
No Matches
triabad13.f90
Go to the documentation of this file.
1!> @file triabad13.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Triangular Demo model 13
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!!
9!! @verbatim
10!! variable x1, x2, x3, x4 ;
11!! equation e1, e2, e3, e4 ;
12!!
13!! e1 .. x1 + power(x2,3) =e= 9;
14!!
15!! e2 .. sqr(x2) =e= 4;
16!!
17!! e3 .. x3 =e= x2 + x1 + sqr(x4);
18!!
19!! e4 .. x2 =g= 0.01;
20!!
21!! model badtria13 / all /;
22!!
23!! solve badtria13 using nlp minimizing x3;
24!! @endverbatim
25!!
26!! This model will not solve given the initial values. Initially,
27!! equation e2 should be solved with respect to variable x2, but the
28!! derivative in the initial point is 0 and CONOPT cannot determine
29!! how to change x2.
30!!
31!! However, the implicit bound on x2 in e4 should provide a better
32!! initial point, and using intervals, we can even determine that
33!! the triangular solution is unique.
34!!
35!!
36!! For more information about the individual callbacks, please have a look at the source code.
37
38#if defined(_WIN32) && !defined(_WIN64)
39#define dec_directives_win32
40#endif
41
42!> Main program. A simple setup and call of CONOPT
43!!
44Program badtria13
45
47 Use conopt
48 implicit None
49!
50! Declare the user callback routines as Integer, External:
51!
52 Integer, External :: tria_readmatrix ! Mandatory Matrix definition routine defined below
53 Integer, External :: tria_fdeval ! Function and Derivative evaluation routine
54 ! needed a nonlinear model.
55 Integer, External :: std_status ! Standard callback for displaying solution status
56 Integer, External :: std_solution ! Standard callback for displaying solution values
57 Integer, External :: std_message ! Standard callback for managing messages
58 Integer, External :: std_errmsg ! Standard callback for managing error messages
59#ifdef dec_directives_win32
60!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
61!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
62!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
63!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
64!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
65!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
66#endif
67!
68! Control vector
69!
70 INTEGER, Dimension(:), Pointer :: cntvect
71 INTEGER :: coi_error
72!
73! Create and initialize a Control Vector
74!
75 call startup
76
77 coi_error = coi_create( cntvect )
78!
79! Tell CONOPT about the size of the model by populating the Control Vector:
80!
81 coi_error = max( coi_error, coidef_numvar( cntvect, 4 ) ) ! 3 variables
82 coi_error = max( coi_error, coidef_numcon( cntvect, 4 ) ) ! 3 constraints
83 coi_error = max( coi_error, coidef_numnz( cntvect, 8 ) ) ! 6 nonzeros in the Jacobian
84 coi_error = max( coi_error, coidef_numnlnz( cntvect, 3 ) ) ! 3 of which are nonlinear
85 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
86 coi_error = max( coi_error, coidef_objvar( cntvect, 3 ) ) ! Objective is variable 3
87 coi_error = max( coi_error, coidef_optfile( cntvect, 'triabad13.opt' ) )
88!
89! Tell CONOPT about the callback routines:
90!
91 coi_error = max( coi_error, coidef_readmatrix( cntvect, tria_readmatrix ) )
92 coi_error = max( coi_error, coidef_fdeval( cntvect, tria_fdeval ) )
93 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
94 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
95 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
96 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
97
98#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
99 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
100#endif
101
102 If ( coi_error .ne. 0 ) THEN
103 write(*,*)
104 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
105 write(*,*)
106 call flog( "Skipping Solve due to setup errors", 1 )
107 ENDIF
108!
109! Save the solution so we can check the duals:
110!
111 do_allocate = .true.
112!
113! Start CONOPT:
114!
115 coi_error = coi_solve( cntvect )
116
117 write(*,*)
118 write(*,*) 'End of First Call. Return code=',coi_error
119
120 If ( coi_error /= 0 ) then
121 call flog( "Errors encountered during solution", 1 )
122 elseif ( stacalls == 0 .or. solcalls == 0 ) then
123 call flog( "Status or Solution routine was not called", 1 )
124 elseif ( sstat /= 1 .or. mstat /= 2 ) then
125 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
126 elseif ( abs( obj-3.0d0 ) > 0.000001d0 ) then
127 call flog( "Incorrect objective returned", 1 )
128 Else
129 Call checkdual( 'Triabad13', infeasible )
130 endif
131
132 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
133
134 call flog( "Successful Solve", 0 )
135
136End Program badtria13
137!
138! ============================================================================
139! Define information about the model:
140!
141
142!> Define information about the model
143!!
144!! @include{doc} readMatrix_params.dox
145Integer Function tria_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
146 colsta, rowno, value, nlflag, n, m, nz, &
147 usrmem )
148#ifdef dec_directives_win32
149!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_ReadMatrix
150#endif
151 implicit none
152 integer, intent (in) :: n ! number of variables
153 integer, intent (in) :: m ! number of constraints
154 integer, intent (in) :: nz ! number of nonzeros
155 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
156 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
157 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
158 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
159 ! (not defined here)
160 integer, intent (out), dimension(m) :: type ! vector of equation types
161 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
162 ! (not defined here)
163 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
164 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
165 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
166 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
167 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
168 real*8 usrmem(*) ! optional user memory
169!
170! Information about Variables:
171! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
172! Default: the status information in Vsta is not used.
173!
174! The model uses defaults, except for x2 where the starting value is
175! default in first call, +10 in the second, and -10 in the third
176!
177 integer, save :: ncall = 0
178 if ( ncall == 0 ) then
179 ncall = 1
180 else if ( ncall == 1 ) then
181 curr(2) = +10.d0; ncall = 2
182 else if ( ncall == 2 ) then
183 curr(2) = -10.d0; ncall = 3
184 else
185 tria_readmatrix = 1; return
186 endif
187!
188! Information about Constraints:
189! Default: Rhs = 0
190! Default: the status information in Esta and the function
191! value in FV are not used.
192! Default: Type: There is no default.
193! 0 = Equality,
194! 1 = Greater than or equal,
195! 2 = Less than or equal,
196! 3 = Non binding.
197!
198! Constraint 1: e1
199! Rhs = 9.0 and type Equality
200!
201 rhs(1) = 9.0d0
202 type(1) = 0
203!
204! Constraint 2: e2
205! Rhs = 4.0 and type Equality
206!
207 rhs(2) = 4.0d0
208 type(2) = 0
209!
210! Constraint 3: e3
211! Rhs = 0.0 and type Equality
212!
213 type(3) = 0
214!
215! Constraint 4: e4
216! Rhs = 0.01 and type Greater than
217!
218 rhs(4) = 0.01d0
219 type(4) = 1
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) x(2) x(3) x(4)
233! 1: 1 3
234! 2: 4
235! 3: 2 5 7 8
236! 4: 6
237!
238 colsta(1) = 1
239 colsta(2) = 3
240 colsta(3) = 7
241 colsta(4) = 8
242 colsta(5) = 9
243 rowno(1) = 1
244 rowno(2) = 3
245 rowno(3) = 1
246 rowno(4) = 2
247 rowno(5) = 3
248 rowno(6) = 4
249 rowno(7) = 3
250 rowno(8) = 3
251!
252! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
253! x(1) x(2) x(3) x(4)
254! 1: L NL
255! 2: NL
256! 3: L L L NL
257! 4: L
258!
259 nlflag(1) = 0
260 nlflag(2) = 0
261 nlflag(3) = 1
262 nlflag(4) = 1
263 nlflag(5) = 0
264 nlflag(6) = 0
265 nlflag(7) = 0
266 nlflag(8) = 1
267!
268! Value (Linear only)
269! x(1) x(2) x(3) x(4)
270! 1: 1.0 NL
271! 2: 5.0
272! 3: -1.0 -1.0 1.0 NL
273! 4: 1.0
274!
275 value(1) = 1.d0
276 value(2) = -1.d0
277 value(4) = 5.d0
278 value(5) = -1.d0
279 value(6) = 1.d0
280 value(7) = 1.d0
282 tria_readmatrix = 0 ! Return value means OK
283
284end Function tria_readmatrix
285!
286!==========================================================================
287! Compute nonlinear terms and non-constant Jacobian elements
288!
289
290!> Compute nonlinear terms and non-constant Jacobian elements
291!!
292!! @include{doc} fdeval_params.dox
293Integer Function tria_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
294 n, nz, thread, usrmem )
295#ifdef dec_directives_win32
296!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Tria_FDEval
297#endif
298 implicit none
299 integer, intent (in) :: n ! number of variables
300 integer, intent (in) :: rowno ! number of the row to be evaluated
301 integer, intent (in) :: nz ! number of nonzeros in this row
302 real*8, intent (in), dimension(n) :: x ! vector of current solution values
303 real*8, intent (in out) :: g ! constraint value
304 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
305 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
306 ! in this row. Ffor information only.
307 integer, intent (in) :: mode ! evaluation mode: 1 = function value
308 ! 2 = derivatives, 3 = both
309 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
310 ! as errcnt is incremented
311 integer, intent (in out) :: errcnt ! error counter to be incremented in case
312 ! of function evaluation errors.
313 integer, intent (in) :: thread
314 real*8 usrmem(*) ! optional user memory
315!
316! Row 1: e1 .. x1 + power(x2,3) =e= 5;
317!
318 if ( rowno .eq. 1 ) then
319!
320! Mode = 1 or 3. G = power(x2,3)
321!
322 if ( mode .eq. 1 .or. mode .eq. 3 ) then
323 g = x(2)*x(2)*x(2)
324 endif
325!
326! Mode = 2 or 3: Derivative values:
327!
328 if ( mode .eq. 2 .or. mode .eq. 3 ) then
329 jac(2) = 3.d0*x(2)*x(2)
330 endif
331 tria_fdeval = 0
332!
333! Row 2: e2 .. sqr(x2) =e= 4;
334!
335 elseif ( rowno .eq. 2 ) then
336!
337! Mode = 1 or 3. G = sqr(x2)
338!
339 if ( mode .eq. 1 .or. mode .eq. 3 ) then
340 g = x(2)*x(2)
341 endif
342!
343! Mode = 2 or 3: Derivative values:
344!
345 if ( mode .eq. 2 .or. mode .eq. 3 ) then
346 jac(2) = 2.d0*x(2)
347 endif
348 tria_fdeval = 0
349!
350! Row 2: e3 .. x3 =e= x2 + x1 + sqr(x4);
351!
352 elseif ( rowno .eq. 3 ) then
353!
354! Mode = 1 or 3. G = -sqr(x4)
355!
356 if ( mode .eq. 1 .or. mode .eq. 3 ) then
357 g = -x(4)*x(4)
358 endif
359!
360! Mode = 2 or 3: Derivative values:
361!
362 if ( mode .eq. 2 .or. mode .eq. 3 ) then
363 jac(4) = -2.d0*x(4)
364 endif
365 tria_fdeval = 0
366 Else
367!
368! Illegal row number
369!
370 tria_fdeval = 1
371 endif
372
373end Function tria_fdeval
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_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_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
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 mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
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:253
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:136
program badtria13
Main program. A simple setup and call of CONOPT.
Definition triabad13.f90:46