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