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
28#if defined(_WIN32) && !defined(_WIN64)
29#define dec_directives_win32
30#endif
31
32Module vpost03data
33 integer, Parameter :: Ni = 5
34 integer, Parameter :: Nb = 2
35 real*8, Dimension(Ni,Nb) :: a
36End Module vpost03data
38!> Main program. A simple setup and call of CONOPT
39!!
40Program vpost03
41
43 Use conopt
44 Use vpost03data
45 implicit None
46!
47! Declare the user callback routines as Integer, External:
48!
49 Integer, External :: vp_readmatrix ! Mandatory Matrix definition routine defined below
50 Integer, External :: vp_fdeval ! Function and Derivative evaluation routine
51 ! needed a nonlinear model.
52 Integer, External :: std_status ! Standard callback for displaying solution status
53 Integer, External :: std_solution ! Standard callback for displaying solution values
54 Integer, External :: std_message ! Standard callback for managing messages
55 Integer, External :: std_errmsg ! Standard callback for managing error messages
56#ifdef dec_directives_win32
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_ReadMatrix
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_FDEval
59!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
60!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
61!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
62!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
63#endif
64!
65! Control vector
66!
67 INTEGER, Dimension(:), Pointer :: cntvect
68 INTEGER :: coi_error
69!
70! Misc local variables
71!
72 Integer :: i, k
73!
74! Initialize Vpost03Data for the model
75!
76 do i = 1, ni
77 do k = 1, nb
78 a(i,k) = 0.1d0*i/ni*k
79 enddo
80 enddo
81!
82! Create and initialize a Control Vector
83!
84 call startup
85
86 coi_error = coi_create( cntvect )
87!
88! Tell CONOPT about the size of the model by populating the Control Vector:
89!
90 coi_error = max( coi_error, coidef_numvar( cntvect, 2*ni+nb ) ) ! # variables
91 coi_error = max( coi_error, coidef_numcon( cntvect, 3*ni+1 ) ) ! # constraints
92 coi_error = max( coi_error, coidef_numnz( cntvect, ni*(5+3*nb)+nb ) ) ! # nonzeros in the Jacobian
93 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
94 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
95 coi_error = max( coi_error, coidef_objcon( cntvect, 3*ni+1 ) ) ! Objective is constraint #
96 coi_error = max( coi_error, coidef_optfile( cntvect, 'vpost03.opt' ) )
97!
98! Tell CONOPT about the callback routines:
99!
100 coi_error = max( coi_error, coidef_readmatrix( cntvect, vp_readmatrix ) )
101 coi_error = max( coi_error, coidef_fdeval( cntvect, vp_fdeval ) )
102 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
103 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
104 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
105 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
106
107#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
108 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
109#endif
110
111 If ( coi_error .ne. 0 ) THEN
112 write(*,*)
113 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
114 write(*,*)
115 call flog( "Skipping Solve due to setup errors", 1 )
116 ENDIF
117!
118! Save the solution so we can check the duals:
119!
120 do_allocate = .true.
121!
122! Start CONOPT:
123!
124 coi_error = coi_solve( cntvect )
125
126 write(*,*)
127 write(*,*) 'End of Vpost03 example. Return code=',coi_error
128
129 If ( coi_error /= 0 ) then
130 call flog( "Errors encountered during solution", 1 )
131 elseif ( stacalls == 0 .or. solcalls == 0 ) then
132 call flog( "Status or Solution routine was not called", 1 )
133 elseif ( sstat /= 1 .or. mstat /= 1 ) then
134 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
135! elseif ( abs( OBJ-0.572943d0 ) > 0.000001d0 ) then
136! call flog( "Incorrect objective returned", 1 )
137 Else
138 Call checkdual( 'Vpost03', maximize )
139 endif
140
141 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
142
143 call flog( "Successful Solve", 0 )
144
145End Program vpost03
146!
147! ============================================================================
148! Define information about the model:
149!
150
151!> Define information about the model
152!!
153!! @include{doc} readMatrix_params.dox
154Integer Function vp_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
155 colsta, rowno, value, nlflag, n, m, nz, &
156 usrmem )
157#ifdef dec_directives_win32
158!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_ReadMatrix
159#endif
160 use vpost03data
161 implicit none
162 integer, intent (in) :: n ! number of variables
163 integer, intent (in) :: m ! number of constraints
164 integer, intent (in) :: nz ! number of nonzeros
165 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
166 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
167 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
168 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
169 ! (not defined here)
170 integer, intent (out), dimension(m) :: type ! vector of equation types
171 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
172 ! (not defined here)
173 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
174 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
175 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
176 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
177 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
178 real*8 usrmem(*) ! optional user memory
179!
180 integer i, j, k
181!
182! Information about Variables:
183! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
184! Default: the status information in Vsta is not used.
185!
186! Lower bound = 0 on all variables
187!
188 do i = 1, n
189 lower(i) = 0.0d0
190 enddo
191!
192! Information about Constraints:
193! Default: Rhs = 0
194! Default: the status information in Esta and the function
195! value in FV are not used.
196! Default: Type: There is no default.
197! 0 = Equality,
198! 1 = Greater than or equal,
199! 2 = Less than or equal,
200! 3 = Non binding.
201!
202 do i = 1, ni
203 rhs(i) = i
204 rhs(ni+i) = 2*i
205 rhs(ni+ni+i) = 3*i
206 enddo
207 do i = 1, ni
208 type(i) = 0
209 type(ni+i) = 0
210 type(ni+ni+i) = 2
211 enddo
212!
213! Constraint 3*ni+1 (Objective)
214! Rhs = 0 and type Non binding
215!
216 type(3*ni+1) = 3
217!
218! positive variable x(i), y(i), v(b), obj;
219! equation e1(i), e2(i), e3(i), objdef
220! e1(i) .. x(i) + sum(b, a(i,b)*v(b)) =E= 1*ord(i);
221! e2(i) .. x(i) + y(i) - sum(b, a(i,b)*v(b)) =E= 2*ord(i);
222! e3(i) .. x(i) + y(i) + sum(b, a(i,b)*v(b)) =L= 3*ord(i);
223! objdef .. obj =E= sum(b, ord(b)*v(b) );
224!
225! Information about the Jacobian. CONOPT expects a columnwise
226! representation in Rowno, Value, Nlflag and Colsta.
227!
228! Colsta = Start of column indices (No Defaults):
229! Rowno = Row indices
230! Value = Value of derivative (by default only linear
231! derivatives are used)
232! Nlflag = 0 for linear and 1 for nonlinear derivative
233! (not needed for completely linear models)
234!
235 k = 1
236 do i = 1, ni ! x variables
237 colsta(i) = k
238 rowno(k) = i
239 value(k) = 1.d0
240 k = k + 1
241 rowno(k) = ni+i
242 value(k) = 1.d0
243 k = k + 1
244 rowno(k) = ni+ni+i
245 value(k) = 1.d0
246 k = k + 1
247 enddo
248 do i = 1, ni ! y variables
249 colsta(ni+i) = k
250 rowno(k) = ni+i
251 value(k) = 1.d0
252 k = k + 1
253 rowno(k) = ni+ni+i
254 value(k) = 1.d0
255 k = k + 1
256 enddo
257 do j = 1, nb
258 colsta(2*ni+j) = k
259 do i = 1, ni
260 rowno(k) = i
261 value(k) = a(i,j)
262 k = k + 1
263 enddo
264 do i = 1, ni
265 rowno(k) = ni+i
266 value(k) = -a(i,j)
267 k = k + 1
268 enddo
269 do i = 1, ni
270 rowno(k) = ni+ni+i
271 value(k) = a(i,j)
272 k = k + 1
273 enddo
274 rowno(k) = 3*ni+1
275 value(k) = j
276 k = k + 1
277 enddo
278 colsta(2*ni+nb+1) = k
280 vp_readmatrix = 0 ! Return value means OK
281
282end Function vp_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 vp_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 :: VP_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 vp_fdeval = 1 ! this routine should never be called for a linear model
315
316end Function vp_fdeval
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_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_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
#define ni
Definition mp_trans.c:45
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
logical do_allocate
Definition comdecl.f90:27
integer, parameter maximize
Definition comdecl.f90:31
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
integer, parameter nb
Definition vpost03.f90:36
real *8, dimension(ni, nb) a
Definition vpost03.f90:37
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:150
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:294
program vpost03
Main program. A simple setup and call of CONOPT.
Definition vpost03.f90:42