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