CONOPT
Loading...
Searching...
No Matches
post.f90
Go to the documentation of this file.
1!> @file post.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the GAMS model:
6!!
7!! @verbatim
8!! variable x1, x2, x3, x4, obj;
9!! equations e1, p1, objdef;
10!!
11!! scalar value;
12!!
13!! e1 .. sqr(x1) + sqr(x2) =E= 1;
14!!
15!! p1 .. sqr(x1-1) + sqr(x2-2) + value*x3 + x4 =E= 0;
16!!
17!! objdef .. sqr(x3-1) + sqr(x4-1) - obj =E= 0;
18!!
19!! model m / all /;
20!! m.optfile=1;
21!! x1.l = 0.5; x2.l = 0.5;
22!! option limrow = 0, limcol = 0;
23!! option sysout = on;
24!! set iter / it1*it3 /;
25!! parameter rep(*,iter);
26!! loop( iter,
27!! value = 100*power(0.1, ord(iter) );
28!! solve m using nlp minimizing obj;
29!! rep('x1',iter) = x1.l;
30!! rep('x2',iter) = x2.l;
31!! rep('x3',iter) = x3.l;
32!! rep('x4',iter) = x4.l;
33!! rep('obj',iter) = obj.l;
34!! );
35!! display rep;
36!! @endverbatim
37!!
38!! For more information about the individual callbacks, please have a look at the source code.
39
40
41!> Main program. A simple setup and call of CONOPT
42!!
43Program postpiv
44
45 Use proginfo
46 Use coidef
47 Use casedata_num
48 implicit None
49!
50! Declare the user callback routines as Integer, External:
51!
52 Integer, External :: post_readmatrix ! Mandatory Matrix definition routine defined below
53 Integer, External :: post_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 Integer, External :: post_option ! Option defining callback routine
60#if defined(itl)
61!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_ReadMatrix
62!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_FDEval
63!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
64!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
65!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
66!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
67!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_Option
68#endif
69!
70! Control vector
71!
72 INTEGER, Dimension(:), Pointer :: cntvect
73 INTEGER :: coi_error
74!
75! Create and initialize a Control Vector
76!
77 call startup
78
79 coi_error = coi_createfort( cntvect )
80!
81! Tell CONOPT about the size of the model by populating the Control Vector:
82!
83 coi_error = max( coi_error, coidef_numvar( cntvect, 5 ) ) ! 4 variables
84 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! 3 constraints
85 coi_error = max( coi_error, coidef_numnz( cntvect, 9 ) ) ! 9 nonzeros in the Jacobian
86 coi_error = max( coi_error, coidef_numnlnz( cntvect, 6 ) ) ! 6 of which are nonlinear
87 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
88 coi_error = max( coi_error, coidef_objvar( cntvect, 5 ) ) ! Objective is variable 5
89 coi_error = max( coi_error, coidef_optfile( cntvect, 'postpiv.opt' ) )
90!
91! Tell CONOPT about the callback routines:
92!
93 coi_error = max( coi_error, coidef_readmatrix( cntvect, post_readmatrix ) )
94 coi_error = max( coi_error, coidef_fdeval( cntvect, post_fdeval ) )
95 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
96 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
97 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
98 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
99 coi_error = max( coi_error, coidef_option( cntvect, post_option ) )
100
101#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
102 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
103#endif
104
105 If ( coi_error .ne. 0 ) THEN
106 write(*,*)
107 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
108 write(*,*)
109 call flog( "Skipping Solve due to setup errors", 1 )
110 ENDIF
111!
112! Save the solution so we can check the duals:
113!
114 do_allocate = .true.
115!
116! Start CONOPT:
117!
118 DO casenum = 1, 3
119 coi_error = coi_solve( cntvect )
120
121 write(*,*)
122 write(*,*) 'End of PostPiv example with Casenum=',casenum,'. Return code=',coi_error
123
124 If ( coi_error /= 0 ) then
125 call flog( "Errors encountered during solution", 1 )
126 elseif ( stacalls == 0 .or. solcalls == 0 ) then
127 call flog( "Status or Solution routine was not called", 1 )
128 elseif ( sstat /= 1 .or. mstat /= 2 ) then
129 call flog( "Solver and Model Status was not as expected (1,2)", 1 )
130 Else
131 Call checkdual( 'PostPiv', minimize )
132 endif
133 if ( casenum == 1 ) Then ! Variable X3 must be post-triangular and therefore basic and X4 superbasic
134 if ( xbasc(3) /= bsbasic .or. xbasc(4) /= bssuper ) Then
135 call flog( "Basis / Superbasis status was not as expected", 1 )
136 Endif
137 elseif ( casenum == 3 ) Then ! Variable X4 must be post-triangular and therefore basic and X3 superbasic
138 if ( xbasc(4) /= bsbasic .or. xbasc(3) /= bssuper ) Then
139 call flog( "Basis / Superbasis status was not as expected", 1 )
140 Endif
141 Endif
142 Enddo
143
144 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
145
146 call flog( "Successful Solve", 0 )
147
148End Program postpiv
149!
150! ============================================================================
151! Define information about the model:
152!
153
154!> Define information about the model
155!!
156!! @include{doc} readMatrix_params.dox
157Integer Function post_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
158 colsta, rowno, value, nlflag, n, m, nz, &
159 usrmem )
160#if defined(itl)
161!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_ReadMatrix
162#endif
163 Use casedata_num
164 implicit none
165 integer, intent (in) :: n ! number of variables
166 integer, intent (in) :: m ! number of constraints
167 integer, intent (in) :: nz ! number of nonzeros
168 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
169 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
170 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
171 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
172 ! (not defined here)
173 integer, intent (out), dimension(m) :: type ! vector of equation types
174 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
175 ! (not defined here)
176 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
177 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
178 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
179 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
180 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
181 real*8 usrmem(*) ! optional user memory
182!
183! Information about Variables:
184! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
185! Default: the status information in Vsta is not used.
186!
187! All variables are free.
188! X1 and X2 have initial value 0.5
189!
190 curr(1) = 0.5d0
191 curr(2) = 0.5d0
192!
193! Information about Constraints:
194! Default: Rhs = 0
195! Default: the status information in Esta and the function
196! value in FV are not used.
197! Default: Type: There is no default.
198! 0 = Equality,
199! 1 = Greater than or equal,
200! 2 = Less than or equal,
201! 3 = Non binding.
202!
203! Constraint 1
204! Rhs = 1 and type Equality
205!
206 rhs(1) = 1.0d0
207 type(1) = 0
208!
209! Constraint 2
210! Rhs = 0 and type Equality
211!
212 type(2) = 0
213!
214! Constraint 3
215! Rhs = 0 and type Equality
216!
217 type(3) = 0
218!
219! Information about the Jacobian. We use the standard method with
220! Rowno, Value, Nlflag and Colsta and we do not use Colno.
221!
222! Colsta = Start of column indices (No Defaults):
223! Rowno = Row indices
224! Value = Value of derivative (by default only linear
225! derivatives are used)
226! Nlflag = 0 for linear and 1 for nonlinear derivative
227! (not needed for completely linear models)
228!
229! Indices
230! x(1) x(2) x(3) x(4) x(5)
231! 1: 1 3
232! 2: 2 4 5 7
233! 3: 6 8 9
234!
235 colsta(1) = 1
236 colsta(2) = 3
237 colsta(3) = 5
238 colsta(4) = 7
239 colsta(5) = 9
240 colsta(6) = 10
241 rowno(1) = 1
242 rowno(2) = 2
243 rowno(3) = 1
244 rowno(4) = 2
245 rowno(5) = 2
246 rowno(6) = 3
247 rowno(7) = 2
248 rowno(8) = 3
249 rowno(9) = 3
250!
251! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
252! x(1) x(2) x(3) x(4) x(5)
253! 1: NL NL
254! 2: NL NL L L
255! 3: NL NL L
256!
257 nlflag(1) = 1
258 nlflag(2) = 1
259 nlflag(3) = 1
260 nlflag(4) = 1
261 nlflag(5) = 0
262 nlflag(6) = 1
263 nlflag(7) = 0
264 nlflag(8) = 1
265 nlflag(9) = 0
266!
267! Value (Linear only)
268! x(1) x(2) x(3) x(4) x(5)
269! 1: NL NL
270! 2: NL NL S 1 where s changes from case to case
271! 3: NL NL -1
272!
273 value(5) = 100.d0 * 0.1d0**casenum ! 10, 1, and 0.1 in the three cases
274 value(7) = 1.d0
275 value(9) = -1.d0
276
277 post_readmatrix = 0 ! Return value means OK
278
279end Function post_readmatrix
280!
281!==========================================================================
282! Compute nonlinear terms and non-constant Jacobian elements
283!
284
285!> Compute nonlinear terms and non-constant Jacobian elements
286!!
287!! @include{doc} fdeval_params.dox
288Integer Function post_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
289 n, nz, thread, usrmem )
290#if defined(itl)
291!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_FDEval
292#endif
293 implicit none
294 integer, intent (in) :: n ! number of variables
295 integer, intent (in) :: rowno ! number of the row to be evaluated
296 integer, intent (in) :: nz ! number of nonzeros in this row
297 real*8, intent (in), dimension(n) :: x ! vector of current solution values
298 real*8, intent (in out) :: g ! constraint value
299 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
300 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
301 ! in this row. Ffor information only.
302 integer, intent (in) :: mode ! evaluation mode: 1 = function value
303 ! 2 = derivatives, 3 = both
304 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
305 ! as errcnt is incremented
306 integer, intent (in out) :: errcnt ! error counter to be incremented in case
307 ! of function evaluation errors.
308 integer, intent (in) :: thread
309 real*8 usrmem(*) ! optional user memory
310!
311! Row 1: e1 .. sqr(x1) + sqr(x2) =E= 1;
312!
313 if ( rowno .eq. 1 ) then
314!
315! Mode = 1 or 3. Function value: G = P * Out
316!
317 if ( mode .eq. 1 .or. mode .eq. 3 ) then
318 g = x(1)*x(1) + x(2)*x(2)
319 endif
320!
321! Mode = 2 or 3: Derivative values:
322!
323 if ( mode .eq. 2 .or. mode .eq. 3 ) then
324 jac(1) = 2.d0*x(1)
325 jac(2) = 2.d0*x(2)
326 endif
327!
328! Row 2: p1 .. sqr(x1-1) + sqr(x2-2) + value*x3 + x4 =E= 0;
329!
330 elseif ( rowno .eq. 2 ) then
331!
332! Mode = 1 or 3: Function value
333!
334 if ( mode .eq. 1 .or. mode .eq. 3 ) then
335 g = (x(1)-1.d0)**2 + (x(2)-2.d0)**2
336 endif
337!
338! Mode = 2 or 3: Derivatives
339!
340 if ( mode .eq. 2 .or. mode .eq. 3 ) then
341 jac(1) = 2.d0*(x(1)-1.d0)
342 jac(2) = 2.d0*(x(2)-2.d0)
343 endif
344!
345! Row = 3: objdef .. sqr(x3-1) + sqr(x4-1) - obj =E= 0;
346!
347 elseif ( rowno .eq. 3 ) then
348!
349! Mode = 1 or 3: Function value
350!
351 if ( mode .eq. 1 .or. mode .eq. 3 ) then
352 g = (x(3)-1.d0)**2 + (x(4)-1.d0)**2
353 endif
354!
355! Mode = 2 or 3: Derivatives
356!
357 if ( mode .eq. 2 .or. mode .eq. 3 ) then
358 jac(3) = 2.d0*(x(3)-1.d0)
359 jac(4) = 2.d0*(x(4)-1.d0)
360 endif
361 endif
362 post_fdeval = 0
363
364end Function post_fdeval
365
366
367!> Sets runtime options
368!!
369!! @include{doc} option_params.dox
370Integer Function post_option( ncall, rval, ival, lval, usrmem, name )
371#if defined(itl)
372!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_Option
373#endif
374 integer ncall, ival, lval
375 character(Len=*) :: name
376 real*8 rval
377 real*8 usrmem(*) ! optional user memory
378
379 Select case (ncall)
380 case (1)
381 name = 'lotria'
382 ival = 1 ! Report on triangular equations
383 case default
384 name = ' '
385 end Select
386 post_option = 0
387
388end Function post_option
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_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_option(cntvect, coi_option)
define callback routine for defining runtime options.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
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 coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
integer, parameter bssuper
Definition comdecl.f90:27
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer, dimension(:), pointer xbasc
Definition comdecl.f90:19
integer, parameter bsbasic
Definition comdecl.f90:27
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 post_option(ncall, rval, ival, lval, usrmem, name)
Sets runtime options.
Definition post.f90:371
integer function post_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition post.f90:290
program postpiv
Main program. A simple setup and call of CONOPT.
Definition post.f90:43
integer function post_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition post.f90:160