CONOPT
Loading...
Searching...
No Matches
pinadd2err.f90
Go to the documentation of this file.
1!> @file pinadd2err.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the Pindyck model from the
6!! GAMS model library. The implementation is similar to the one
7!! in pindyck, but this time we first solve the model for 16
8!! periods and then we gradually increase the number of periods
9!! one at a time up to 20.
10!!
11!!
12!! For more information about the individual callbacks, please have a look at the source code.
13
14#if defined(_WIN32) && !defined(_WIN64)
15#define dec_directives_win32
16#endif
17
18Module pinadd2errdata
19 integer :: T
20 integer, Parameter :: Tmin = 16, tmax = 20
21 integer, Parameter :: vpp = 7 ! Variables per period
22 integer, Parameter :: epp = 6 ! Equations per period
23 Integer, Parameter :: hpp = 5 ! Hessian elements per period -- see in 2DLagr* routines
24 real*8, Dimension(Tmax*Vpp) :: xkeep ! Store previous solutions
25 Integer, Dimension(Tmax*Vpp) :: xstas ! Store previous status
26 Integer, Dimension(Tmax*Epp) :: estat ! for variables and equations
29!> Main program. A simple setup and call of CONOPT
30!!
31Program pinadd2err
32 Use proginfo
35 Implicit none
36!
37! Declare the user callback routines as Integer, External:
38!
39 Integer, External :: pin_readmatrix ! Mandatory Matrix definition routine defined below
40 Integer, External :: pin_fdeval ! Function and Derivative evaluation routine
41 ! needed a nonlinear model.
42 Integer, External :: std_status ! Standard callback for displaying solution status
43 Integer, External :: pin_solution ! Specialized callback for displaying solution values
44 Integer, External :: std_message ! Standard callback for managing messages
45 Integer, External :: std_errmsg ! Standard callback for managing error messages
46 Integer, External :: pin_2dlagrstr ! Hessian structure evaluation routine
47 Integer, External :: pin_2dlagrval ! Hessian value evaluation routine
48#ifdef dec_directives_win32
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_ReadMatrix
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_FDEval
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_Solution
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrStr
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrVal
57#endif
58!
59! Control vector
60!
61 INTEGER, Dimension(:), Pointer :: cntvect
62 INTEGER :: coi_error
63
64 call startup
65!
66! Create and initialize a Control Vector
67!
68 coi_error = coi_create( cntvect )
69!
70! Define the number of time periods, T.
71!
72 t = tmin
73!
74! Tell CONOPT about the size of the model by populating the Control Vector:
75!
76! Number of variables (excl. slacks): Vpp per period
77!
78 coi_error = max( coi_error, coidef_numvar( cntvect, vpp * t ) )
79!
80! Number of equations: 1 objective + Epp per period
81!
82 coi_error = max( coi_error, coidef_numcon( cntvect, 1 + epp * t ) )
83!
84! Number of nonzeros in the Jacobian. See the counting in ReadMatrix below:
85! For each period there is 1 in the objective, 16 for unlagged
86! variables and 4 for lagged variables.
87!
88 coi_error = max( coi_error, coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
89!
90! Number of nonlinear nonzeros. 5 unlagged for each period.
91!
92 coi_error = max( coi_error, coidef_numnlnz( cntvect, 5 * t ) )
93!
94! Number of nonzeros in the Hessian. 5 for each period.
95!
96 coi_error = max( coi_error, coidef_numhess( cntvect, hpp * t ) )
97!
98! Direction: +1 = maximization.
99!
100 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) )
101!
102! Objective: Constraint no 1
103!
104 coi_error = max( coi_error, coidef_objcon( cntvect, 1 ) )
105!
106! Turn function debugging on in the initial point
107!
108 coi_error = max( coi_error, coidef_debugfv( cntvect, -1 ) )
109!
110! Turn Hessian debugging on in the initial point
111!
112 coi_error = max( coi_error, coidef_debug2d( cntvect, -1 ) )
113!
114! Options file = test.opt
115!
116 coi_error = max( coi_error, coidef_optfile( cntvect, 'test.opt' ) )
117!
118! Tell CONOPT about the callback routines:
119!
120 coi_error = max( coi_error, coidef_readmatrix( cntvect, pin_readmatrix ) )
121 coi_error = max( coi_error, coidef_fdeval( cntvect, pin_fdeval ) )
122 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
123 coi_error = max( coi_error, coidef_solution( cntvect, pin_solution ) )
124 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
125 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
126 coi_error = max( coi_error, coidef_2dlagrstr( cntvect, pin_2dlagrstr ) )
127 coi_error = max( coi_error, coidef_2dlagrval( cntvect, pin_2dlagrval ) )
128
129#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
130 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
131#endif
132
133 If ( coi_error .ne. 0 ) THEN
134 write(*,*)
135 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
136 write(*,*)
137 call flog( "Skipping First Solve due to setup errors", 1 )
138 ENDIF
139!
140! Start CONOPT:
141!
142 coi_error = coi_solve( cntvect )
143!
144! We have an intentional error in Pin_2DLagr and we have turned debugging on.
145! COI_Solve should still return 0, but the Solver status should return 5, Evaluation
146! Error Limit, and the Model status should be Intermediate Infeasible (6) or Intermediate
147! Nonoptimal (7).
148!
149 If ( coi_error .ne. 0 ) THEN
150 write(*,*)
151 write(*,*) '**** Fatal Error while Solving first model.'
152 write(*,*)
153 call flog( "Errors encountered during first Solve.", 1 )
154 elseif ( stacalls == 0 .or. solcalls == 0 ) then
155 call flog( "Status or Solution routine was not called during first Solve", 1 )
156 elseif ( mstat < 6 .or. mstat > 7 .or. sstat /= 5 ) then
157 call flog( "Incorrect Model or Solver Status during first solve", 1 )
158 ENDIF
159!
160! Now increase the time horizon gradually up to 20.
161! All sizes that depend on T must be registered again.
162!
163 Do t = tmin+1, tmax
164 coi_error = max( coi_error, coidef_numvar( cntvect, 7 * t ) )
165 coi_error = max( coi_error, coidef_numcon( cntvect, 1 + 6 * t ) )
166 coi_error = max( coi_error, coidef_numnz( cntvect, 17 * t + 4 * (t-1) ) )
167 coi_error = max( coi_error, coidef_numnlnz( cntvect, 5 * t ) )
168 coi_error = max( coi_error, coidef_numhess( cntvect, hpp * t ) )
169!
170! This time we have status information copied from a previous solve, i.e.
171! IniStat = 2
172!
173 coi_error = max( coi_error, coidef_inistat( cntvect, 2 ) )
174!
175! Turn Hessian debugging off again
176!
177 coi_error = max( coi_error, coidef_debug2d( cntvect, 0 ) )
178 If ( coi_error .ne. 0 ) THEN
179 write(*,*)
180 write(*,*) '**** Fatal Error while revising CONOPT Sizes.'
181 write(*,*)
182 call flog( "Skipping Later Solve due to setup errors", 1 )
183 ENDIF
184 stacalls = 0; solcalls = 0; ! reset so we can test if called.
185 coi_error = coi_solve( cntvect )
186 If ( coi_error .ne. 0 ) THEN
187 write(*,*)
188 write(*,*) '**** Fatal Error while Solving for T=',t
189 write(*,*)
190 call flog( "Errors encountered during later Solve.", 1 )
191 elseif ( stacalls == 0 .or. solcalls == 0 ) then
192 call flog( "Status or Solution routine was not called during later Solve", 1 )
193 else if ( mstat /= 2 .or. sstat /= 1 ) then
194 call flog( "Incorrect Model or Solver Status during later solve", 1 )
195 ENDIF
196 Enddo
197 write(*,*)
198 write(*,*) 'End of Pinadd2err Model. Return code=',coi_error
199
200 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
201
202 call flog( "Successful Solve", 0 )
203!
204! Free solution memory
205!
207
208end Program pinadd2err
209!
210! =====================================================================
211! Define information about the model structure
212!
213
214!> Define information about the model
215!!
216!! @include{doc} readMatrix_params.dox
217Integer Function pin_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
218 colsta, rowno, value, nlflag, n, m, nz, usrmem )
219#ifdef dec_directives_win32
220!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_ReadMatrix
221#endif
223 implicit none
224 integer, intent (in) :: n ! number of variables
225 integer, intent (in) :: m ! number of constraints
226 integer, intent (in) :: nz ! number of nonzeros
227 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
228 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
229 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
230 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
231 ! Defined during restarts
232 integer, intent (out), dimension(m) :: type ! vector of equation types
233 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
234 ! Defined during restarts
235 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
236 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
237 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
238 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
239 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
240 real*8 usrmem(*) ! optional user memory
241
242 Integer :: it, is, i, icol, iz, iold
243!
244! Define the information for the columns.
245!
246! We should not supply status information, vsta.
247!
248! We order the variables as follows:
249! td, cs, s, d, r, p, and rev. All variables for period 1 appears
250! first followed by all variables for period 2 etc.
251!
252! td, cs, s, and d have lower bounds of 0, r and p have lower
253! bounds of 1, and rev has no lower bound.
254! All have infinite upper bounds (default).
255! The initial value of td is 18, s is 7, cs is 7*t, d is td-s,
256! p is 14, and r is r(t-1)-d. No initial value for rev.
257!
258 do it = 1, t
259 is = vpp*(it-1)
260 lower(is+1) = 0.d0
261 lower(is+2) = 0.d0
262 lower(is+3) = 0.d0
263 lower(is+4) = 0.d0
264 lower(is+5) = 1.d0
265 lower(is+6) = 1.d0
266 curr(is+1) = 18.d0
267 curr(is+2) = 7.d0*it
268 curr(is+3) = 7.d0
269 curr(is+4) = curr(is+1) - curr(is+3)
270 if ( it .gt. 1 ) then
271 curr(is+5) = curr(is+5-vpp) - curr(is+4)
272 else
273 curr(is+5) = 500.d0 - curr(is+4)
274 endif
275 curr(is+6) = 14.d0
276 enddo
277 If ( t > tmin ) Then
278!
279! This is a restart: Use the initial values from last solve for
280! the variables in the first periods and extrapolate the last
281! period using a linear extrapolation between the last two
282!
283 iold = vpp*(t-1)
284 curr(1:iold) = xkeep(1:iold)
285 do i = 1, vpp
286 curr(iold+i) = curr(iold+i-vpp) + (curr(iold+i-vpp)-curr(iold+i-2*vpp))
287 enddo
288!
289! The variables from the last solve are given the old status and
290! the variables in the new period are given those in the last
291! pariod. Similarly with the Equation status:
292!
293 vsta(1:iold) = xstas(1:iold)
294 do i = 1, vpp
295 vsta(iold+i) = xstas(iold+i-vpp)
296 enddo
297 iold = 6*(t-1)+1
298 esta(1:iold) = estat(1:iold)
299 do i = 1, epp
300 esta(iold+i) = estat(iold+i-epp)
301 enddo
302 endif
303!
304! Define the information for the rows
305!
306! We order the constraints as follows: The objective is first,
307! followed by tddef, sdef, csdef, ddef, rdef, and revdef for
308! the first period, the same for the second period, etc.
309!
310! The objective is a nonbinding constraint:
311!
312 type(1) = 3
313!
314! All others are equalities:
315!
316 do i = 2, m
317 type(i) = 0
318 enddo
319!
320! Right hand sides: In all periods except the first, only tddef
321! has a nonzero right hand side of 1+2.3*1.015**(t-1).
322! In the initial period there are contributions from lagged
323! variables in the constraints that have lagged variables.
324!
325 do it = 1, t
326 is = 1 + 6*(it-1)
327 rhs(is+1) = 1.d0+2.3d0*1.015d0**(it-1)
328 enddo
329!
330! tddef: + 0.87*td(0)
331!
332 rhs(2) = rhs(2) + 0.87d0*18.d0
333!
334! sdef: +0.75*s(0)
335!
336 rhs(3) = 0.75d0*6.5d0
337!
338! csdef: +1*cs(0)
339!
340 rhs(4) = 0.d0
341!
342! rdef: +1*r(0)
343!
344 rhs(6) = 500.d0
345!
346! Define the structure and content of the Jacobian:
347! To help define the Jacobian pattern and values it can be useful to
348! make a picture of the Jacobian. We describe the variables for one
349! period and the constraints they are part of:
350!
351! td cs s d r p rev
352! Obj (1+r)**(1-t)
353! Period t:
354! tddef 1.0 0.13
355! sdef NL 1.0 NL
356! csdef 1.0 -1.0
357! ddef -1.0 1.0 1.0
358! rdef 1.0 1.0
359! revdef NL NL NL 1.0
360! Period t+1:
361! tddef -0.87
362! sdef -0.75
363! csdef -1.0
364! ddef
365! rdef -1.0
366! revdef
367!
368! The Jacobian has to be sorted column-wise so we will just define
369! the elements column by column according to the table above:
370!
371 iz = 1
372 icol = 1
373 do it = 1, t
374!
375! is points to the position before the first equation for the period
376!
377 is = 1 + 6*(it-1)
378!
379! Column td:
380!
381 colsta(icol) = iz
382 icol = icol + 1
383 rowno(iz) = is+1
384 value(iz) = +1.d0
385 nlflag(iz) = 0
386 iz = iz + 1
387 rowno(iz) = is+4
388 value(iz) = -1.d0
389 nlflag(iz) = 0
390 iz = iz + 1
391 if ( it .lt. t ) then
392 rowno(iz) = is+7
393 value(iz) = -0.87d0
394 nlflag(iz) = 0
395 iz = iz + 1
396 endif
397!
398! Column cs
399!
400 colsta(icol) = iz
401 icol = icol + 1
402 rowno(iz) = is+2
403 nlflag(iz) = 1
404 iz = iz + 1
405 rowno(iz) = is+3
406 value(iz) = +1.d0
407 nlflag(iz) = 0
408 iz = iz + 1
409 if ( it .lt. t ) then
410 rowno(iz) = is+9
411 value(iz) = -1.d0
412 nlflag(iz) = 0
413 iz = iz + 1
414 endif
415!
416! Column s
417!
418 colsta(icol) = iz
419 icol = icol + 1
420 rowno(iz) = is+2
421 value(iz) = +1.d0
422 nlflag(iz) = 0
423 iz = iz + 1
424 rowno(iz) = is+3
425 value(iz) = -1.d0
426 nlflag(iz) = 0
427 iz = iz + 1
428 rowno(iz) = is+4
429 value(iz) = +1.d0
430 nlflag(iz) = 0
431 iz = iz + 1
432 if ( it .lt. t ) then
433 rowno(iz) = is+8
434 value(iz) = -0.75d0
435 nlflag(iz) = 0
436 iz = iz + 1
437 endif
438!
439! Column d:
440!
441 colsta(icol) = iz
442 icol = icol + 1
443 rowno(iz) = is+4
444 value(iz) = +1.d0
445 nlflag(iz) = 0
446 iz = iz + 1
447 rowno(iz) = is+5
448 value(iz) = +1.d0
449 nlflag(iz) = 0
450 iz = iz + 1
451 rowno(iz) = is+6
452 nlflag(iz) = 1
453 iz = iz + 1
454!
455! Column r:
456!
457 colsta(icol) = iz
458 icol = icol + 1
459 rowno(iz) = is+5
460 value(iz) = +1.d0
461 nlflag(iz) = 0
462 iz = iz + 1
463 rowno(iz) = is+6
464 nlflag(iz) = 1
465 iz = iz + 1
466 if ( it .lt. t ) then
467 rowno(iz) = is+11
468 value(iz) = -1.d0
469 nlflag(iz) = 0
470 iz = iz + 1
471 endif
472!
473! Column p:
474!
475 colsta(icol) = iz
476 icol = icol + 1
477 rowno(iz) = is+1
478 value(iz) = +0.13d0
479 nlflag(iz) = 0
480 iz = iz + 1
481 rowno(iz) = is+2
482 nlflag(iz) = 1
483 iz = iz + 1
484 rowno(iz) = is+6
485 nlflag(iz) = 1
486 iz = iz + 1
487!
488! Column rev:
489!
490 colsta(icol) = iz
491 icol = icol + 1
492 rowno(iz) = +1
493 value(iz) = 1.05d0**(1-it)
494 nlflag(iz) = 0
495 iz = iz + 1
496 rowno(iz) = is+6
497 value(iz) = 1.d0
498 nlflag(iz) = 0
499 iz = iz + 1
500 enddo
501 colsta(icol) = iz
502
504
505end Function pin_readmatrix
506!
507! =====================================================================
508! Compute nonlinear terms and non-constant Jacobian elements
509!
510
511!> Compute nonlinear terms and non-constant Jacobian elements
512!!
513!! @include{doc} fdeval_params.dox
514Integer Function pin_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
515 n, nz, thread, usrmem )
516#ifdef dec_directives_win32
517!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_FDEval
518#endif
520 implicit none
521 integer, intent (in) :: n ! number of variables
522 integer, intent (in) :: rowno ! number of the row to be evaluated
523 integer, intent (in) :: nz ! number of nonzeros in this row
524 real*8, intent (in), dimension(n) :: x ! vector of current solution values
525 real*8, intent (in out) :: g ! constraint value
526 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
527 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
528 ! in this row. Ffor information only.
529 integer, intent (in) :: mode ! evaluation mode: 1 = function value
530 ! 2 = derivatives, 3 = both
531 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
532 ! as errcnt is incremented
533 integer, intent (in out) :: errcnt ! error counter to be incremented in case
534 ! of function evaluation errors.
535 integer, intent (in) :: thread
536 real*8 usrmem(*) ! optional user memory
537
538 integer :: it, is
539 real*8 :: h1, h2
540!
541! Compute the number of the period
542!
543 it = (rowno+4) / epp
544 is = vpp*(it-1)
545 if ( rowno == (it-1)*epp+3 ) then
546!
547! sdef equation. Nonlinear term = -(1.1+0.1*p)*1.02**(-cs/7)
548!
549 h1 = (1.1d0+0.1d0*x(is+6))
550 h2 = 1.02d0**(-x(is+2)/7.d0)
551 if ( mode == 1 .or. mode == 3 ) then
552 g = -h1*h2
553 endif
554 if ( mode == 2 .or. mode == 3 ) then
555 jac(is+2) = h1*h2*log(1.02d0)/7.d0
556 jac(is+6) = -h2*0.1d0
557 endif
558 elseif ( rowno == (it-1)*epp+7 ) then
559!
560! revdef equation. Nonlinear term = -d*(p-250/r)
561!
562 if ( mode == 1 .or. mode == 3 ) then
563 g = -x(is+4)*(x(is+6)-250.d0/x(is+5))
564 endif
565 if ( mode == 2 .or. mode == 3 ) then
566 jac(is+4) = -(x(is+6)-250.d0/x(is+5))
567 jac(is+5) = -x(is+4)*250d0/x(is+5)**2
568 jac(is+6) = -x(is+4) ;
569! jac(is+6) = +x(is+4) ! Potential error
570 endif
571 else
572!
573! Error - this equation is not nonlinear
574!
575 write(*,*)
576 write(*,*) 'Error. FDEval called with rowno=',rowno
577 write(*,*)
578 pin_fdeval = 1
579 Return
580 endif
581 pin_fdeval = 0
582
583end Function pin_fdeval
584
585Integer Function pin_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
586#ifdef dec_directives_win32
587!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_Solution
588#endif
589
591 Use proginfo
592 IMPLICIT NONE
593 INTEGER, Intent(IN) :: n, m
594 INTEGER, Intent(IN), Dimension(N) :: xbas, xsta
595 INTEGER, Intent(IN), Dimension(M) :: ybas, ysta
596 real*8, Intent(IN), Dimension(N) :: xval, xmar
597 real*8, Intent(IN), Dimension(M) :: yval, ymar
598 real*8, Intent(IN OUT) :: usrmem(*)
599 character*6, parameter, dimension(7) :: vname = (/'td ','cs ','s ','d ','r ','p ','rev '/)
600 character*6, parameter, dimension(6) :: ename = (/'tddef ','sdef ','csdef ','ddef ','rdef ','revdef'/)
601
602 INTEGER :: i, it, i1
603 CHARACTER*5, Parameter, Dimension(4) :: stat = (/ 'Lower','Upper','Basic','Super' /)
604
605 solcalls = solcalls + 1
606 If ( t < tmax ) Then
607 Write(*,"(A,i3)") 'Saving primal solution and status information for T =',t
608 xkeep(1:n) = xval(1:n)
609 xstas(1:n) = xbas(1:n)
610 estat(1:m) = ybas(1:m)
611 pin_solution = 0
612 Return
613 Endif
614
615 WRITE(10,"(/' Variable Solution value Reduced cost B-stat'/)")
616 i = 0
617 do it = 1, t
618 DO i1 = 1, vpp
619 i = i + 1
620 WRITE(10,"(1X,A6,i2,1p,E20.6,E16.6,4X,A5 )") vname(i1), it, xval(i), xmar(i), stat(1+xbas(i))
621 ENDDO
622 enddo
623
624 WRITE(10,"(/' Constrnt Activity level Marginal cost B-stat'/)")
625 i = 1
626 WRITE(10,"(1x,'Objective',1P,E19.6,E16.6,4X,A5 )") yval(i), ymar(i), stat(1+ybas(i))
627 do it = 1, t
628 do i1 = 1, epp
629 i = i + 1
630 WRITE(10,"(1x,A6,i2,1P,E20.6,E16.6,4X,A5 )") ename(i1),it, yval(i), ymar(i), stat(1+ybas(i))
631 enddo
632 ENDDO
633
634 pin_solution = 0
635
636END Function pin_solution
637
638
639!> Specify the structure of the Lagrangian of the Hessian
640!!
641!! @include{doc} 2DLagrStr_params.dox
642Integer Function pin_2dlagrstr( HSRW, HSCL, NODRV, N, M, NHESS, UsrMem )
643#ifdef dec_directives_win32
644!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrStr
645#endif
647 Implicit None
648
649 Integer, Intent (IN) :: n, m, nhess
650 Integer, Intent (IN OUT) :: nodrv
651 Integer, Dimension(NHess), Intent (Out) :: hsrw, hscl
652 real*8, Intent(IN OUT) :: usrmem(*)
653
654 Integer :: it, is, ic
655!
656! There are two nonlinear constraints, Sdef and RevDef and they have
657! the following Hessians (for each time period):
658!
659! Revdef equation. Nonlinear term = -d*(p-250/r)
660! Hessian (diagonal and lower triangle): A total of 3 nonzero elements per period
661! 4 5 6
662! d r p
663! 4 : d
664! 5 : r -250/r**2 500*d/r**3
665! 6 : p -1
666!
667! Sdef: Nonlinear term = -(1.1+0.1p)*1.02**(-cs/7)
668! Hessian (diagonal and lower triangle): A total of 2 nonzero elements per period
669! 2 6
670! cs p
671! 2 : cs -(1.1+0.1p)*1.02**(-cs/7)*(log(1.02)/7)**2
672! 6 : p 0.1*1.02**(-cs/7)*(log(1.02)72)
673!
674! The two Hessian matrices do not overlap so the total number of nonzeros is
675! 5 per period. The structure using numerical indices for rows and columns and
676! running indices for the Hessian element is:
677!
678! 2 4 5 6
679! 2 1
680! 4
681! 5 3 5
682! 6 2 4
683!
684! and the same with names of variables and contributing equations
685! cs d r p
686! cs sdef
687! d
688! r revdef revdef
689! p sdef revdef
690!
691!
692! Define structure of Hessian
693!
694 if ( n /= vpp*t ) then
695 write(*,*) 'Struct: Number of variables should be=',vpp*t,' but CONOPT uses',n
696 write(*,*) 'Number of periods=',t
697 pin_2dlagrstr = 1; Return
698 stop
699 endif
700 if ( m /= epp*t+1 ) then
701 write(*,*) 'Struct: Number of equations should be=',epp*t+1,' but CONOPT uses',m
702 write(*,*) 'Number of periods=',t
703 pin_2dlagrstr = 1; Return
704 endif
705 Do it = 1, t
706 is = hpp*(it-1)
707 ic = vpp*(it-1)
708 hscl(is+1) = ic+2; hsrw(is+1) = ic+2
709 hscl(is+2) = ic+2; hsrw(is+2) = ic+6
710 hscl(is+3) = ic+4; hsrw(is+3) = ic+5
711 hsrw(is+3) = ic+4; ! Potential Error
712 hscl(is+4) = ic+4; hsrw(is+4) = ic+6
713 hscl(is+5) = ic+5; hsrw(is+5) = ic+5
714 Enddo
715
716 pin_2dlagrstr = 0
717
718End Function pin_2dlagrstr
719
720
721!> Compute the Lagrangian of the Hessian
722!!
723!! @include{doc} 2DLagrVal_params.dox
724Integer Function pin_2dlagrval( X, U, HSRW, HSCL, HSVL, NODRV, N, M, NHESS, UsrMem )
725#ifdef dec_directives_win32
726!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrVal
727#endif
729 Implicit None
730
731 Integer, Intent (IN) :: n, m, nhess
732 Integer, Intent (IN OUT) :: nodrv
733 real*8, Dimension(N), Intent (IN) :: x
734 real*8, Dimension(M), Intent (IN) :: u
735 Integer, Dimension(Nhess), Intent (IN) :: hsrw, hscl
736 real*8, Dimension(NHess), Intent (Out) :: hsvl
737 real*8, Intent(IN OUT) :: usrmem(*)
738
739 Integer :: it, is, ic, ie
740 real*8 :: cs, d, r, p
741 real*8 :: h1, h2
742!
743! There are two nonlinear constraints, Sdef and RevDef and they have
744! the following Hessians (for each time period):
745!
746! Revdef equation. Nonlinear term = -d*(p-250/r)
747! Hessian (diagonal and lower triangle): A total of 3 nonzero elements per period
748! 4 5 6
749! d r p
750! 4 : d
751! 5 : r -250/r**2 500*d/r**3
752! 6 : p -1
753!
754! Sdef: Nonlinear term = -(1.1+0.1p)*1.02**(-cs/7)
755! Hessian (diagonal and lower triangle): A total of 2 nonzero elements per period
756! 2 6
757! cs p
758! 2 : cs -(1.1+0.1p)*1.02**(-cs/7)*(log(1.02)/7)**2
759! 6 : p 0.1*1.02**(-cs/7)*(log(1.02)72)
760!
761! The two Hessian matrices do not overlap so the total number of nonzeros is
762! 5 per period. The structure using numerical indices for rows and columns and
763! running indices for the Hessian element is:
764!
765! 2 4 5 6
766! 2 1
767! 4
768! 5 3 5
769! 6 2 4
770!
771! and the same with names of variables and contributing equations
772! cs d r p
773! cs sdef
774! d
775! r revdef revdef
776! p sdef revdef
777!
778!
779! Normal Evaluation mode
780!
781 if ( n /= vpp*t ) then
782 write(*,*) 'Eval: Number of variables should be=',vpp*t,' but CONOPT uses',n
783 write(*,*) 'Number of periods=',t
784 pin_2dlagrval = 1; REturn
785 endif
786 if ( m /= epp*t+1 ) then
787 write(*,*) 'Eval: Number of equations should be=',epp*t+1,' but CONOPT uses',m
788 write(*,*) 'Number of periods=',t
789 pin_2dlagrval = 1; Return
790 endif
791 Do it = 1, t
792 is = hpp*(it-1)
793 ic = vpp*(it-1)
794 ie = epp*(it-1) + 1
795 cs = x(ic+2)
796 d = x(ic+4)
797 r = x(ic+5)
798 p = x(ic+6)
799 h1 = 1.1d0+0.1d0*p
800 h2 = 1.02d0**(-cs/7.d0)
801 hsvl(is+1) = -h1*h2*(log(1.02)/7.d0)**2 * u(ie+2) ! Sdef = equation 2
802 hsvl(is+2) = 0.1d0*h2*(log(1.02)/7.d0) * u(ie+2)
803 hsvl(is+3) = -250.d0/r**2 * u(ie+6) ! Revdef = equation 6
804 hsvl(is+4) = -1.d0 * u(ie+6)
805 hsvl(is+5) = 500.d0*d/ r**3 * u(ie+6)
806 enddo
807
808 pin_2dlagrval = 0
809
810End Function pin_2dlagrval
Main program. A simple setup and call of CONOPT.
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
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 function pin_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition fvboth.f90:429
integer function pin_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition fvboth.f90:161
integer function pin_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition fvboth.f90:535
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_2dlagrstr(cntvect, coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
Definition conopt.f90:1547
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_2dlagrval(cntvect, coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
Definition conopt.f90:1573
integer(c_int) function coidef_inistat(cntvect, inistat)
handling of the initial status values.
Definition conopt.f90:1023
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition conopt.f90:293
integer(c_int) function coidef_debugfv(cntvect, debugfv)
turn Debugging of FDEval on and off.
Definition conopt.f90:387
integer(c_int) function coidef_debug2d(cntvect, debug2d)
turn debugging of 2nd derivatives on and off.
Definition conopt.f90:534
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_numhess(cntvect, numhess)
defines the Number of Hessian Nonzeros.
Definition conopt.f90:188
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition conopt.f90:239
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, dimension(tmax *epp) estat
integer, parameter tmax
integer, parameter vpp
integer, parameter epp
integer, parameter hpp
integer, parameter tmin
integer, dimension(tmax *vpp) xstas
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
subroutine finalize
Definition comdecl.f90:79
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
integer function pin_2dlagrstr(hsrw, hscl, nodrv, n, m, nhess, usrmem)
Specify the structure of the Lagrangian of the Hessian.
Definition pinadd2.f90:611
integer function pin_2dlagrval(x, u, hsrw, hscl, hsvl, nodrv, n, m, nhess, usrmem)
Compute the Lagrangian of the Hessian.
Definition pinadd2.f90:689