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 )
151End Program postpiv
152!
153! ============================================================================
154! Define information about the model:
155!
156
157!> Define information about the model
158!!
159!! @include{doc} readMatrix_params.dox
160Integer Function post_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
161 colsta, rowno, value, nlflag, n, m, nz, &
162 usrmem )
163#ifdef dec_directives_win32
164!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_ReadMatrix
165#endif
166 Use casedata_num
167 implicit none
168 integer, intent (in) :: n ! number of variables
169 integer, intent (in) :: m ! number of constraints
170 integer, intent (in) :: nz ! number of nonzeros
171 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
172 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
173 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
174 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
175 ! (not defined here)
176 integer, intent (out), dimension(m) :: type ! vector of equation types
177 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
178 ! (not defined here)
179 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
180 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
181 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
182 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
183 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
184 real*8 usrmem(*) ! optional user memory
185!
186! Information about Variables:
187! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
188! Default: the status information in Vsta is not used.
189!
190! All variables are free.
191! X1 and X2 have initial value 0.5
192!
193 curr(1) = 0.5d0
194 curr(2) = 0.5d0
195!
196! Information about Constraints:
197! Default: Rhs = 0
198! Default: the status information in Esta and the function
199! value in FV are not used.
200! Default: Type: There is no default.
201! 0 = Equality,
202! 1 = Greater than or equal,
203! 2 = Less than or equal,
204! 3 = Non binding.
205!
206! Constraint 1
207! Rhs = 1 and type Equality
208!
209 rhs(1) = 1.0d0
210 type(1) = 0
211!
212! Constraint 2
213! Rhs = 0 and type Equality
214!
215 type(2) = 0
216!
217! Constraint 3
218! Rhs = 0 and type Equality
219!
220 type(3) = 0
221!
222! Information about the Jacobian. CONOPT expects a columnwise
223! representation in Rowno, Value, Nlflag and Colsta.
224!
225! Colsta = Start of column indices (No Defaults):
226! Rowno = Row indices
227! Value = Value of derivative (by default only linear
228! derivatives are used)
229! Nlflag = 0 for linear and 1 for nonlinear derivative
230! (not needed for completely linear models)
231!
232! Indices
233! x(1) x(2) x(3) x(4) x(5)
234! 1: 1 3
235! 2: 2 4 5 7
236! 3: 6 8 9
237!
238 colsta(1) = 1
239 colsta(2) = 3
240 colsta(3) = 5
241 colsta(4) = 7
242 colsta(5) = 9
243 colsta(6) = 10
244 rowno(1) = 1
245 rowno(2) = 2
246 rowno(3) = 1
247 rowno(4) = 2
248 rowno(5) = 2
249 rowno(6) = 3
250 rowno(7) = 2
251 rowno(8) = 3
252 rowno(9) = 3
253!
254! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
255! x(1) x(2) x(3) x(4) x(5)
256! 1: NL NL
257! 2: NL NL L L
258! 3: NL NL L
259!
260 nlflag(1) = 1
261 nlflag(2) = 1
262 nlflag(3) = 1
263 nlflag(4) = 1
264 nlflag(5) = 0
265 nlflag(6) = 1
266 nlflag(7) = 0
267 nlflag(8) = 1
268 nlflag(9) = 0
269!
270! Value (Linear only)
271! x(1) x(2) x(3) x(4) x(5)
272! 1: NL NL
273! 2: NL NL S 1 where s changes from case to case
274! 3: NL NL -1
275!
276 value(5) = 100.d0 * 0.1d0**casenum ! 10, 1, and 0.1 in the three cases
277 value(7) = 1.d0
278 value(9) = -1.d0
279
280 post_readmatrix = 0 ! Return value means OK
281
282end Function post_readmatrix
283!
284!==========================================================================
285! Compute nonlinear terms and non-constant Jacobian elements
286!
287
288!> Compute nonlinear terms and non-constant Jacobian elements
289!!
290!! @include{doc} fdeval_params.dox
291Integer Function post_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
292 n, nz, thread, usrmem )
293#ifdef dec_directives_win32
294!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_FDEval
295#endif
296 implicit none
297 integer, intent (in) :: n ! number of variables
298 integer, intent (in) :: rowno ! number of the row to be evaluated
299 integer, intent (in) :: nz ! number of nonzeros in this row
300 real*8, intent (in), dimension(n) :: x ! vector of current solution values
301 real*8, intent (in out) :: g ! constraint value
302 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
303 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
304 ! in this row. Ffor information only.
305 integer, intent (in) :: mode ! evaluation mode: 1 = function value
306 ! 2 = derivatives, 3 = both
307 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
308 ! as errcnt is incremented
309 integer, intent (in out) :: errcnt ! error counter to be incremented in case
310 ! of function evaluation errors.
311 integer, intent (in) :: thread
312 real*8 usrmem(*) ! optional user memory
313!
314! Row 1: e1 .. sqr(x1) + sqr(x2) =E= 1;
315!
316 if ( rowno .eq. 1 ) then
317!
318! Mode = 1 or 3. Function value: G = P * Out
319!
320 if ( mode .eq. 1 .or. mode .eq. 3 ) then
321 g = x(1)*x(1) + x(2)*x(2)
322 endif
323!
324! Mode = 2 or 3: Derivative values:
325!
326 if ( mode .eq. 2 .or. mode .eq. 3 ) then
327 jac(1) = 2.d0*x(1)
328 jac(2) = 2.d0*x(2)
329 endif
330!
331! Row 2: p1 .. sqr(x1-1) + sqr(x2-2) + value*x3 + x4 =E= 0;
332!
333 elseif ( rowno .eq. 2 ) then
334!
335! Mode = 1 or 3: Function value
336!
337 if ( mode .eq. 1 .or. mode .eq. 3 ) then
338 g = (x(1)-1.d0)**2 + (x(2)-2.d0)**2
339 endif
340!
341! Mode = 2 or 3: Derivatives
342!
343 if ( mode .eq. 2 .or. mode .eq. 3 ) then
344 jac(1) = 2.d0*(x(1)-1.d0)
345 jac(2) = 2.d0*(x(2)-2.d0)
346 endif
347!
348! Row = 3: objdef .. sqr(x3-1) + sqr(x4-1) - obj =E= 0;
349!
350 elseif ( rowno .eq. 3 ) then
351!
352! Mode = 1 or 3: Function value
353!
354 if ( mode .eq. 1 .or. mode .eq. 3 ) then
355 g = (x(3)-1.d0)**2 + (x(4)-1.d0)**2
356 endif
358! Mode = 2 or 3: Derivatives
359!
360 if ( mode .eq. 2 .or. mode .eq. 3 ) then
361 jac(3) = 2.d0*(x(3)-1.d0)
362 jac(4) = 2.d0*(x(4)-1.d0)
363 endif
364 endif
365 post_fdeval = 0
366
367end Function post_fdeval
368
369
370!> Sets runtime options
371!!
372!! @include{doc} option_params.dox
373Integer Function post_option( ncall, rval, ival, lval, usrmem, name )
374#ifdef dec_directives_win32
375!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Post_Option
376#endif
377 integer ncall, ival, lval
378 character(Len=*) :: name
379 real*8 rval
380 real*8 usrmem(*) ! optional user memory
381
382 Select case (ncall)
383 case (1)
384 name = 'lotria'
385 ival = 1 ! Report on triangular equations
386 case default
387 name = ' '
388 end Select
389 post_option = 0
390
391end Function post_option
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_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
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:358
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:280
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:153