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
204end Program pinadd2err
205!
206! =====================================================================
207! Define information about the model structure
208!
209
210!> Define information about the model
211!!
212!! @include{doc} readMatrix_params.dox
213Integer Function pin_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
214 colsta, rowno, value, nlflag, n, m, nz, usrmem )
215#ifdef dec_directives_win32
216!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_ReadMatrix
217#endif
219 implicit none
220 integer, intent (in) :: n ! number of variables
221 integer, intent (in) :: m ! number of constraints
222 integer, intent (in) :: nz ! number of nonzeros
223 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
224 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
225 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
226 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
227 ! Defined during restarts
228 integer, intent (out), dimension(m) :: type ! vector of equation types
229 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
230 ! Defined during restarts
231 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
232 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
233 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
234 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
235 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
236 real*8 usrmem(*) ! optional user memory
237
238 Integer :: it, is, i, icol, iz, iold
239!
240! Define the information for the columns.
241!
242! We should not supply status information, vsta.
243!
244! We order the variables as follows:
245! td, cs, s, d, r, p, and rev. All variables for period 1 appears
246! first followed by all variables for period 2 etc.
247!
248! td, cs, s, and d have lower bounds of 0, r and p have lower
249! bounds of 1, and rev has no lower bound.
250! All have infinite upper bounds (default).
251! The initial value of td is 18, s is 7, cs is 7*t, d is td-s,
252! p is 14, and r is r(t-1)-d. No initial value for rev.
253!
254 do it = 1, t
255 is = vpp*(it-1)
256 lower(is+1) = 0.d0
257 lower(is+2) = 0.d0
258 lower(is+3) = 0.d0
259 lower(is+4) = 0.d0
260 lower(is+5) = 1.d0
261 lower(is+6) = 1.d0
262 curr(is+1) = 18.d0
263 curr(is+2) = 7.d0*it
264 curr(is+3) = 7.d0
265 curr(is+4) = curr(is+1) - curr(is+3)
266 if ( it .gt. 1 ) then
267 curr(is+5) = curr(is+5-vpp) - curr(is+4)
268 else
269 curr(is+5) = 500.d0 - curr(is+4)
270 endif
271 curr(is+6) = 14.d0
272 enddo
273 If ( t > tmin ) Then
274!
275! This is a restart: Use the initial values from last solve for
276! the variables in the first periods and extrapolate the last
277! period using a linear extrapolation between the last two
278!
279 iold = vpp*(t-1)
280 curr(1:iold) = xkeep(1:iold)
281 do i = 1, vpp
282 curr(iold+i) = curr(iold+i-vpp) + (curr(iold+i-vpp)-curr(iold+i-2*vpp))
283 enddo
284!
285! The variables from the last solve are given the old status and
286! the variables in the new period are given those in the last
287! pariod. Similarly with the Equation status:
288!
289 vsta(1:iold) = xstas(1:iold)
290 do i = 1, vpp
291 vsta(iold+i) = xstas(iold+i-vpp)
292 enddo
293 iold = 6*(t-1)+1
294 esta(1:iold) = estat(1:iold)
295 do i = 1, epp
296 esta(iold+i) = estat(iold+i-epp)
297 enddo
298 endif
299!
300! Define the information for the rows
301!
302! We order the constraints as follows: The objective is first,
303! followed by tddef, sdef, csdef, ddef, rdef, and revdef for
304! the first period, the same for the second period, etc.
305!
306! The objective is a nonbinding constraint:
307!
308 type(1) = 3
309!
310! All others are equalities:
311!
312 do i = 2, m
313 type(i) = 0
314 enddo
315!
316! Right hand sides: In all periods except the first, only tddef
317! has a nonzero right hand side of 1+2.3*1.015**(t-1).
318! In the initial period there are contributions from lagged
319! variables in the constraints that have lagged variables.
320!
321 do it = 1, t
322 is = 1 + 6*(it-1)
323 rhs(is+1) = 1.d0+2.3d0*1.015d0**(it-1)
324 enddo
325!
326! tddef: + 0.87*td(0)
327!
328 rhs(2) = rhs(2) + 0.87d0*18.d0
329!
330! sdef: +0.75*s(0)
331!
332 rhs(3) = 0.75d0*6.5d0
333!
334! csdef: +1*cs(0)
335!
336 rhs(4) = 0.d0
337!
338! rdef: +1*r(0)
339!
340 rhs(6) = 500.d0
341!
342! Define the structure and content of the Jacobian:
343! To help define the Jacobian pattern and values it can be useful to
344! make a picture of the Jacobian. We describe the variables for one
345! period and the constraints they are part of:
346!
347! td cs s d r p rev
348! Obj (1+r)**(1-t)
349! Period t:
350! tddef 1.0 0.13
351! sdef NL 1.0 NL
352! csdef 1.0 -1.0
353! ddef -1.0 1.0 1.0
354! rdef 1.0 1.0
355! revdef NL NL NL 1.0
356! Period t+1:
357! tddef -0.87
358! sdef -0.75
359! csdef -1.0
360! ddef
361! rdef -1.0
362! revdef
363!
364! The Jacobian has to be sorted column-wise so we will just define
365! the elements column by column according to the table above:
366!
367 iz = 1
368 icol = 1
369 do it = 1, t
370!
371! is points to the position before the first equation for the period
372!
373 is = 1 + 6*(it-1)
374!
375! Column td:
376!
377 colsta(icol) = iz
378 icol = icol + 1
379 rowno(iz) = is+1
380 value(iz) = +1.d0
381 nlflag(iz) = 0
382 iz = iz + 1
383 rowno(iz) = is+4
384 value(iz) = -1.d0
385 nlflag(iz) = 0
386 iz = iz + 1
387 if ( it .lt. t ) then
388 rowno(iz) = is+7
389 value(iz) = -0.87d0
390 nlflag(iz) = 0
391 iz = iz + 1
392 endif
393!
394! Column cs
395!
396 colsta(icol) = iz
397 icol = icol + 1
398 rowno(iz) = is+2
399 nlflag(iz) = 1
400 iz = iz + 1
401 rowno(iz) = is+3
402 value(iz) = +1.d0
403 nlflag(iz) = 0
404 iz = iz + 1
405 if ( it .lt. t ) then
406 rowno(iz) = is+9
407 value(iz) = -1.d0
408 nlflag(iz) = 0
409 iz = iz + 1
410 endif
411!
412! Column s
413!
414 colsta(icol) = iz
415 icol = icol + 1
416 rowno(iz) = is+2
417 value(iz) = +1.d0
418 nlflag(iz) = 0
419 iz = iz + 1
420 rowno(iz) = is+3
421 value(iz) = -1.d0
422 nlflag(iz) = 0
423 iz = iz + 1
424 rowno(iz) = is+4
425 value(iz) = +1.d0
426 nlflag(iz) = 0
427 iz = iz + 1
428 if ( it .lt. t ) then
429 rowno(iz) = is+8
430 value(iz) = -0.75d0
431 nlflag(iz) = 0
432 iz = iz + 1
433 endif
434!
435! Column d:
436!
437 colsta(icol) = iz
438 icol = icol + 1
439 rowno(iz) = is+4
440 value(iz) = +1.d0
441 nlflag(iz) = 0
442 iz = iz + 1
443 rowno(iz) = is+5
444 value(iz) = +1.d0
445 nlflag(iz) = 0
446 iz = iz + 1
447 rowno(iz) = is+6
448 nlflag(iz) = 1
449 iz = iz + 1
450!
451! Column r:
452!
453 colsta(icol) = iz
454 icol = icol + 1
455 rowno(iz) = is+5
456 value(iz) = +1.d0
457 nlflag(iz) = 0
458 iz = iz + 1
459 rowno(iz) = is+6
460 nlflag(iz) = 1
461 iz = iz + 1
462 if ( it .lt. t ) then
463 rowno(iz) = is+11
464 value(iz) = -1.d0
465 nlflag(iz) = 0
466 iz = iz + 1
467 endif
468!
469! Column p:
470!
471 colsta(icol) = iz
472 icol = icol + 1
473 rowno(iz) = is+1
474 value(iz) = +0.13d0
475 nlflag(iz) = 0
476 iz = iz + 1
477 rowno(iz) = is+2
478 nlflag(iz) = 1
479 iz = iz + 1
480 rowno(iz) = is+6
481 nlflag(iz) = 1
482 iz = iz + 1
483!
484! Column rev:
485!
486 colsta(icol) = iz
487 icol = icol + 1
488 rowno(iz) = +1
489 value(iz) = 1.05d0**(1-it)
490 nlflag(iz) = 0
491 iz = iz + 1
492 rowno(iz) = is+6
493 value(iz) = 1.d0
494 nlflag(iz) = 0
495 iz = iz + 1
496 enddo
497 colsta(icol) = iz
498
500
501end Function pin_readmatrix
502!
503! =====================================================================
504! Compute nonlinear terms and non-constant Jacobian elements
505!
506
507!> Compute nonlinear terms and non-constant Jacobian elements
508!!
509!! @include{doc} fdeval_params.dox
510Integer Function pin_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
511 n, nz, thread, usrmem )
512#ifdef dec_directives_win32
513!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_FDEval
514#endif
516 implicit none
517 integer, intent (in) :: n ! number of variables
518 integer, intent (in) :: rowno ! number of the row to be evaluated
519 integer, intent (in) :: nz ! number of nonzeros in this row
520 real*8, intent (in), dimension(n) :: x ! vector of current solution values
521 real*8, intent (in out) :: g ! constraint value
522 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
523 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
524 ! in this row. Ffor information only.
525 integer, intent (in) :: mode ! evaluation mode: 1 = function value
526 ! 2 = derivatives, 3 = both
527 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
528 ! as errcnt is incremented
529 integer, intent (in out) :: errcnt ! error counter to be incremented in case
530 ! of function evaluation errors.
531 integer, intent (in) :: thread
532 real*8 usrmem(*) ! optional user memory
533
534 integer :: it, is
535 real*8 :: h1, h2
536!
537! Compute the number of the period
538!
539 it = (rowno+4) / epp
540 is = vpp*(it-1)
541 if ( rowno == (it-1)*epp+3 ) then
542!
543! sdef equation. Nonlinear term = -(1.1+0.1*p)*1.02**(-cs/7)
544!
545 h1 = (1.1d0+0.1d0*x(is+6))
546 h2 = 1.02d0**(-x(is+2)/7.d0)
547 if ( mode == 1 .or. mode == 3 ) then
548 g = -h1*h2
549 endif
550 if ( mode == 2 .or. mode == 3 ) then
551 jac(is+2) = h1*h2*log(1.02d0)/7.d0
552 jac(is+6) = -h2*0.1d0
553 endif
554 elseif ( rowno == (it-1)*epp+7 ) then
555!
556! revdef equation. Nonlinear term = -d*(p-250/r)
557!
558 if ( mode == 1 .or. mode == 3 ) then
559 g = -x(is+4)*(x(is+6)-250.d0/x(is+5))
560 endif
561 if ( mode == 2 .or. mode == 3 ) then
562 jac(is+4) = -(x(is+6)-250.d0/x(is+5))
563 jac(is+5) = -x(is+4)*250d0/x(is+5)**2
564 jac(is+6) = -x(is+4) ;
565! jac(is+6) = +x(is+4) ! Potential error
566 endif
567 else
568!
569! Error - this equation is not nonlinear
570!
571 write(*,*)
572 write(*,*) 'Error. FDEval called with rowno=',rowno
573 write(*,*)
574 pin_fdeval = 1
575 Return
576 endif
577 pin_fdeval = 0
578
579end Function pin_fdeval
580
581Integer Function pin_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
582#ifdef dec_directives_win32
583!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_Solution
584#endif
585
587 Use proginfo
588 IMPLICIT NONE
589 INTEGER, Intent(IN) :: n, m
590 INTEGER, Intent(IN), Dimension(N) :: xbas, xsta
591 INTEGER, Intent(IN), Dimension(M) :: ybas, ysta
592 real*8, Intent(IN), Dimension(N) :: xval, xmar
593 real*8, Intent(IN), Dimension(M) :: yval, ymar
594 real*8, Intent(IN OUT) :: usrmem(*)
595 character*6, parameter, dimension(7) :: vname = (/'td ','cs ','s ','d ','r ','p ','rev '/)
596 character*6, parameter, dimension(6) :: ename = (/'tddef ','sdef ','csdef ','ddef ','rdef ','revdef'/)
597
598 INTEGER :: i, it, i1
599 CHARACTER*5, Parameter, Dimension(4) :: stat = (/ 'Lower','Upper','Basic','Super' /)
600
601 solcalls = solcalls + 1
602 If ( t < tmax ) Then
603 Write(*,"(A,i3)") 'Saving primal solution and status information for T =',t
604 xkeep(1:n) = xval(1:n)
605 xstas(1:n) = xbas(1:n)
606 estat(1:m) = ybas(1:m)
607 pin_solution = 0
608 Return
609 Endif
610
611 WRITE(10,"(/' Variable Solution value Reduced cost B-stat'/)")
612 i = 0
613 do it = 1, t
614 DO i1 = 1, vpp
615 i = i + 1
616 WRITE(10,"(1X,A6,i2,1p,E20.6,E16.6,4X,A5 )") vname(i1), it, xval(i), xmar(i), stat(1+xbas(i))
617 ENDDO
618 enddo
619
620 WRITE(10,"(/' Constrnt Activity level Marginal cost B-stat'/)")
621 i = 1
622 WRITE(10,"(1x,'Objective',1P,E19.6,E16.6,4X,A5 )") yval(i), ymar(i), stat(1+ybas(i))
623 do it = 1, t
624 do i1 = 1, epp
625 i = i + 1
626 WRITE(10,"(1x,A6,i2,1P,E20.6,E16.6,4X,A5 )") ename(i1),it, yval(i), ymar(i), stat(1+ybas(i))
627 enddo
628 ENDDO
629
630 pin_solution = 0
631
632END Function pin_solution
633
634
635!> Specify the structure of the Lagrangian of the Hessian
636!!
637!! @include{doc} 2DLagrStr_params.dox
638Integer Function pin_2dlagrstr( HSRW, HSCL, NODRV, N, M, NHESS, UsrMem )
639#ifdef dec_directives_win32
640!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrStr
641#endif
643 Implicit None
644
645 Integer, Intent (IN) :: n, m, nhess
646 Integer, Intent (IN OUT) :: nodrv
647 Integer, Dimension(NHess), Intent (Out) :: hsrw, hscl
648 real*8, Intent(IN OUT) :: usrmem(*)
649
650 Integer :: it, is, ic
651!
652! There are two nonlinear constraints, Sdef and RevDef and they have
653! the following Hessians (for each time period):
654!
655! Revdef equation. Nonlinear term = -d*(p-250/r)
656! Hessian (diagonal and lower triangle): A total of 3 nonzero elements per period
657! 4 5 6
658! d r p
659! 4 : d
660! 5 : r -250/r**2 500*d/r**3
661! 6 : p -1
662!
663! Sdef: Nonlinear term = -(1.1+0.1p)*1.02**(-cs/7)
664! Hessian (diagonal and lower triangle): A total of 2 nonzero elements per period
665! 2 6
666! cs p
667! 2 : cs -(1.1+0.1p)*1.02**(-cs/7)*(log(1.02)/7)**2
668! 6 : p 0.1*1.02**(-cs/7)*(log(1.02)72)
669!
670! The two Hessian matrices do not overlap so the total number of nonzeros is
671! 5 per period. The structure using numerical indices for rows and columns and
672! running indices for the Hessian element is:
673!
674! 2 4 5 6
675! 2 1
676! 4
677! 5 3 5
678! 6 2 4
679!
680! and the same with names of variables and contributing equations
681! cs d r p
682! cs sdef
683! d
684! r revdef revdef
685! p sdef revdef
686!
687!
688! Define structure of Hessian
689!
690 if ( n /= vpp*t ) then
691 write(*,*) 'Struct: Number of variables should be=',vpp*t,' but CONOPT uses',n
692 write(*,*) 'Number of periods=',t
693 pin_2dlagrstr = 1; Return
694 stop
695 endif
696 if ( m /= epp*t+1 ) then
697 write(*,*) 'Struct: Number of equations should be=',epp*t+1,' but CONOPT uses',m
698 write(*,*) 'Number of periods=',t
699 pin_2dlagrstr = 1; Return
700 endif
701 Do it = 1, t
702 is = hpp*(it-1)
703 ic = vpp*(it-1)
704 hscl(is+1) = ic+2; hsrw(is+1) = ic+2
705 hscl(is+2) = ic+2; hsrw(is+2) = ic+6
706 hscl(is+3) = ic+4; hsrw(is+3) = ic+5
707 hsrw(is+3) = ic+4; ! Potential Error
708 hscl(is+4) = ic+4; hsrw(is+4) = ic+6
709 hscl(is+5) = ic+5; hsrw(is+5) = ic+5
710 Enddo
711
712 pin_2dlagrstr = 0
713
714End Function pin_2dlagrstr
715
716
717!> Compute the Lagrangian of the Hessian
718!!
719!! @include{doc} 2DLagrVal_params.dox
720Integer Function pin_2dlagrval( X, U, HSRW, HSCL, HSVL, NODRV, N, M, NHESS, UsrMem )
721#ifdef dec_directives_win32
722!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pin_2DLagrVal
723#endif
725 Implicit None
726
727 Integer, Intent (IN) :: n, m, nhess
728 Integer, Intent (IN OUT) :: nodrv
729 real*8, Dimension(N), Intent (IN) :: x
730 real*8, Dimension(M), Intent (IN) :: u
731 Integer, Dimension(Nhess), Intent (IN) :: hsrw, hscl
732 real*8, Dimension(NHess), Intent (Out) :: hsvl
733 real*8, Intent(IN OUT) :: usrmem(*)
734
735 Integer :: it, is, ic, ie
736 real*8 :: cs, d, r, p
737 real*8 :: h1, h2
738!
739! There are two nonlinear constraints, Sdef and RevDef and they have
740! the following Hessians (for each time period):
741!
742! Revdef equation. Nonlinear term = -d*(p-250/r)
743! Hessian (diagonal and lower triangle): A total of 3 nonzero elements per period
744! 4 5 6
745! d r p
746! 4 : d
747! 5 : r -250/r**2 500*d/r**3
748! 6 : p -1
749!
750! Sdef: Nonlinear term = -(1.1+0.1p)*1.02**(-cs/7)
751! Hessian (diagonal and lower triangle): A total of 2 nonzero elements per period
752! 2 6
753! cs p
754! 2 : cs -(1.1+0.1p)*1.02**(-cs/7)*(log(1.02)/7)**2
755! 6 : p 0.1*1.02**(-cs/7)*(log(1.02)72)
756!
757! The two Hessian matrices do not overlap so the total number of nonzeros is
758! 5 per period. The structure using numerical indices for rows and columns and
759! running indices for the Hessian element is:
760!
761! 2 4 5 6
762! 2 1
763! 4
764! 5 3 5
765! 6 2 4
766!
767! and the same with names of variables and contributing equations
768! cs d r p
769! cs sdef
770! d
771! r revdef revdef
772! p sdef revdef
773!
774!
775! Normal Evaluation mode
776!
777 if ( n /= vpp*t ) then
778 write(*,*) 'Eval: Number of variables should be=',vpp*t,' but CONOPT uses',n
779 write(*,*) 'Number of periods=',t
780 pin_2dlagrval = 1; REturn
781 endif
782 if ( m /= epp*t+1 ) then
783 write(*,*) 'Eval: Number of equations should be=',epp*t+1,' but CONOPT uses',m
784 write(*,*) 'Number of periods=',t
785 pin_2dlagrval = 1; Return
786 endif
787 Do it = 1, t
788 is = hpp*(it-1)
789 ic = vpp*(it-1)
790 ie = epp*(it-1) + 1
791 cs = x(ic+2)
792 d = x(ic+4)
793 r = x(ic+5)
794 p = x(ic+6)
795 h1 = 1.1d0+0.1d0*p
796 h2 = 1.02d0**(-cs/7.d0)
797 hsvl(is+1) = -h1*h2*(log(1.02)/7.d0)**2 * u(ie+2) ! Sdef = equation 2
798 hsvl(is+2) = 0.1d0*h2*(log(1.02)/7.d0) * u(ie+2)
799 hsvl(is+3) = -250.d0/r**2 * u(ie+6) ! Revdef = equation 6
800 hsvl(is+4) = -1.d0 * u(ie+6)
801 hsvl(is+5) = 500.d0*d/ r**3 * u(ie+6)
802 enddo
803
804 pin_2dlagrval = 0
805
806End Function pin_2dlagrval
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
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 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:425
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:157
integer function pin_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition fvboth.f90:531
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
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:607
integer function pin_2dlagrval(x, u, hsrw, hscl, hsvl, nodrv, n, m, nhess, usrmem)
Compute the Lagrangian of the Hessian.
Definition pinadd2.f90:685
program pinadd2err
Main program. A simple setup and call of CONOPT.