CONOPT
Loading...
Searching...
No Matches
vpost03.f90
Go to the documentation of this file.
1!> @file vpost03.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the GAMS model:
6!!
7!! @verbatim
8!! set i / i1*i5 /
9!! b / b1*b2 /
10!! parameter a(i,b)
11!! positive variable x(i), y(i), v(b), obj;
12!! equation e1(i), e2(i), e3(i), objdef
13!! e1(i) .. x(i) + sum(b, a(i,b)*v(b)) =E= 1*ord(i);
14!! e2(i) .. x(i) + y(i) - sum(b, a(i,b)*v(b)) =E= 2*ord(i);
15!! e3(i) .. x(i) + y(i) + sum(b, a(i,b)*v(b)) =L= 3*ord(i);
16!! objdef .. obj =E= sum(b, ord(b)*v(b) );
17!! @endverbatim
18!!
19!! For fixed values of the variables v the model is recursive. Used to
20!! test various initialization procedures.
21!!
22!! Similar to vpost02, but with the z-variables being replaced by
23!! inequalities in e3.
24!!
25!!
26!! For more information about the individual callbacks, please have a look at the source code.
27
29 integer, Parameter :: ni = 5
30 integer, Parameter :: nb = 2
31 real*8, Dimension(Ni,Nb) :: a
32End Module vpost03data
33
34!> Main program. A simple setup and call of CONOPT
35!!
36Program vpost03
37
38 Use proginfo
39 Use coidef
40 Use vpost03data
41 implicit None
42!
43! Declare the user callback routines as Integer, External:
44!
45 Integer, External :: vp_readmatrix ! Mandatory Matrix definition routine defined below
46 Integer, External :: vp_fdeval ! Function and Derivative evaluation routine
47 ! needed a nonlinear model.
48 Integer, External :: std_status ! Standard callback for displaying solution status
49 Integer, External :: std_solution ! Standard callback for displaying solution values
50 Integer, External :: std_message ! Standard callback for managing messages
51 Integer, External :: std_errmsg ! Standard callback for managing error messages
52#if defined(itl)
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_ReadMatrix
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_FDEval
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
59#endif
60!
61! Control vector
62!
63 INTEGER :: numcallback
64 INTEGER, Dimension(:), Pointer :: cntvect
65 INTEGER :: coi_error
66!
67! Misc local variables
68!
69 Integer :: i, k
70!
71! Initialize Vpost03Data for the model
72!
73 do i = 1, ni
74 do k = 1, nb
75 a(i,k) = 0.1d0*i/ni*k
76 enddo
77 enddo
78!
79! Create and initialize a Control Vector
80!
81 call startup
82
83 numcallback = coidef_size()
84 Allocate( cntvect(numcallback) )
85 coi_error = coidef_inifort( cntvect )
86!
87! Tell CONOPT about the size of the model by populating the Control Vector:
88!
89 coi_error = max( coi_error, coidef_numvar( cntvect, 2*ni+nb ) ) ! # variables
90 coi_error = max( coi_error, coidef_numcon( cntvect, 3*ni+1 ) ) ! # constraints
91 coi_error = max( coi_error, coidef_numnz( cntvect, ni*(5+3*nb)+nb ) ) ! # nonzeros in the Jacobian
92 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
93 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
94 coi_error = max( coi_error, coidef_objcon( cntvect, 3*ni+1 ) ) ! Objective is constraint #
95 coi_error = max( coi_error, coidef_optfile( cntvect, 'vpost03.opt' ) )
96!
97! Tell CONOPT about the callback routines:
98!
99 coi_error = max( coi_error, coidef_readmatrix( cntvect, vp_readmatrix ) )
100 coi_error = max( coi_error, coidef_fdeval( cntvect, vp_fdeval ) )
101 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
102 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
103 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
104 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
105
106#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
107 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
108#endif
109
110 If ( coi_error .ne. 0 ) THEN
111 write(*,*)
112 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
113 write(*,*)
114 call flog( "Skipping Solve due to setup errors", 1 )
115 ENDIF
116!
117! Save the solution so we can check the duals:
118!
119 do_allocate = .true.
120!
121! Start CONOPT:
122!
123 coi_error = coi_solve( cntvect )
124
125 write(*,*)
126 write(*,*) 'End of Vpost03 example. Return code=',coi_error
127
128 If ( coi_error /= 0 ) then
129 call flog( "Errors encountered during solution", 1 )
130 elseif ( stacalls == 0 .or. solcalls == 0 ) then
131 call flog( "Status or Solution routine was not called", 1 )
132 elseif ( sstat /= 1 .or. mstat /= 1 ) then
133 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
134! elseif ( abs( OBJ-0.572943d0 ) > 0.000001d0 ) then
135! call flog( "Incorrect objective returned", 1 )
136 Else
137 Call checkdual( 'Vpost03', maximize )
138 endif
139
140 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
141
142 call flog( "Successful Solve", 0 )
143
144End Program vpost03
145!
146! ============================================================================
147! Define information about the model:
148!
149
150!> Define information about the model
151!!
152!! @include{doc} readMatrix_params.dox
153Integer Function vp_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
154 colsta, rowno, value, nlflag, n, m, nz, &
155 usrmem )
156#if defined(itl)
157!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_ReadMatrix
158#endif
159 use vpost03data
160 implicit none
161 integer, intent (in) :: n ! number of variables
162 integer, intent (in) :: m ! number of constraints
163 integer, intent (in) :: nz ! number of nonzeros
164 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
165 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
166 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
167 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
168 ! (not defined here)
169 integer, intent (out), dimension(m) :: type ! vector of equation types
170 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
171 ! (not defined here)
172 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
173 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
174 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
175 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
176 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
177 real*8 usrmem(*) ! optional user memory
178!
179 integer i, j, k
180!
181! Information about Variables:
182! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
183! Default: the status information in Vsta is not used.
184!
185! Lower bound = 0 on all variables
186!
187 do i = 1, n
188 lower(i) = 0.0d0
189 enddo
190!
191! Information about Constraints:
192! Default: Rhs = 0
193! Default: the status information in Esta and the function
194! value in FV are not used.
195! Default: Type: There is no default.
196! 0 = Equality,
197! 1 = Greater than or equal,
198! 2 = Less than or equal,
199! 3 = Non binding.
200!
201 do i = 1, ni
202 rhs(i) = i
203 rhs(ni+i) = 2*i
204 rhs(ni+ni+i) = 3*i
205 enddo
206 do i = 1, ni
207 type(i) = 0
208 type(ni+i) = 0
209 type(ni+ni+i) = 2
210 enddo
211!
212! Constraint 3*ni+1 (Objective)
213! Rhs = 0 and type Non binding
214!
215 type(3*ni+1) = 3
216!
217! positive variable x(i), y(i), v(b), obj;
218! equation e1(i), e2(i), e3(i), objdef
219! e1(i) .. x(i) + sum(b, a(i,b)*v(b)) =E= 1*ord(i);
220! e2(i) .. x(i) + y(i) - sum(b, a(i,b)*v(b)) =E= 2*ord(i);
221! e3(i) .. x(i) + y(i) + sum(b, a(i,b)*v(b)) =L= 3*ord(i);
222! objdef .. obj =E= sum(b, ord(b)*v(b) );
223!
224! Information about the Jacobian. We use the standard method with
225! Rowno, Value, Nlflag and Colsta and we do not use Colno.
226!
227! Colsta = Start of column indices (No Defaults):
228! Rowno = Row indices
229! Value = Value of derivative (by default only linear
230! derivatives are used)
231! Nlflag = 0 for linear and 1 for nonlinear derivative
232! (not needed for completely linear models)
233!
234 k = 1
235 do i = 1, ni ! x variables
236 colsta(i) = k
237 rowno(k) = i
238 value(k) = 1.d0
239 k = k + 1
240 rowno(k) = ni+i
241 value(k) = 1.d0
242 k = k + 1
243 rowno(k) = ni+ni+i
244 value(k) = 1.d0
245 k = k + 1
246 enddo
247 do i = 1, ni ! y variables
248 colsta(ni+i) = k
249 rowno(k) = ni+i
250 value(k) = 1.d0
251 k = k + 1
252 rowno(k) = ni+ni+i
253 value(k) = 1.d0
254 k = k + 1
255 enddo
256 do j = 1, nb
257 colsta(2*ni+j) = k
258 do i = 1, ni
259 rowno(k) = i
260 value(k) = a(i,j)
261 k = k + 1
262 enddo
263 do i = 1, ni
264 rowno(k) = ni+i
265 value(k) = -a(i,j)
266 k = k + 1
267 enddo
268 do i = 1, ni
269 rowno(k) = ni+ni+i
270 value(k) = a(i,j)
271 k = k + 1
272 enddo
273 rowno(k) = 3*ni+1
274 value(k) = j
275 k = k + 1
276 enddo
277 colsta(2*ni+nb+1) = k
278
279 vp_readmatrix = 0 ! Return value means OK
280
281end Function vp_readmatrix
282!
283!==========================================================================
284! Compute nonlinear terms and non-constant Jacobian elements
285!
286
287!> Compute nonlinear terms and non-constant Jacobian elements
288!!
289!! @include{doc} fdeval_params.dox
290Integer Function vp_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
291 n, nz, thread, usrmem )
292#if defined(itl)
293!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_FDEval
294#endif
295 implicit none
296 integer, intent (in) :: n ! number of variables
297 integer, intent (in) :: rowno ! number of the row to be evaluated
298 integer, intent (in) :: nz ! number of nonzeros in this row
299 real*8, intent (in), dimension(n) :: x ! vector of current solution values
300 real*8, intent (in out) :: g ! constraint value
301 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
302 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
303 ! in this row. Ffor information only.
304 integer, intent (in) :: mode ! evaluation mode: 1 = function value
305 ! 2 = derivatives, 3 = both
306 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
307 ! as errcnt is incremented
308 integer, intent (in out) :: errcnt ! error counter to be incremented in case
309 ! of function evaluation errors.
310 integer, intent (in) :: thread
311 real*8 usrmem(*) ! optional user memory
312
313 vp_fdeval = 1 ! this routine should never be called for a linear model
314
315end Function vp_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
subroutine checkdual(case, minmax)
Definition comdecl.f90:365
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition coistart.f90:629
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition coistart.f90:552
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
#define ni
Definition mp_trans.c:45
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
integer, parameter maximize
Definition comdecl.f90:25
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35
integer, parameter nb
Definition vpost03.f90:30
real *8, dimension(ni, nb) a
Definition vpost03.f90:31
integer function vp_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition vpost01.f90:158
integer function vp_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition vpost01.f90:305
program vpost03
Main program. A simple setup and call of CONOPT.
Definition vpost03.f90:36