CONOPT
Loading...
Searching...
No Matches
vpost01.f90
Go to the documentation of this file.
1!> @file vpost01.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!! x.up(i) = 10; y.up(i) = 20; z.up(i) = 30; v.up(b) = 40;
18!! @endverbatim
19!!
20!! For fixed values of the variables v the model is recursive. Used to
21!! test various initialization procedures.
22!!
23!!
24!! For more information about the individual callbacks, please have a look at the source code.
25
26#if defined(_WIN32) && !defined(_WIN64)
27#define dec_directives_win32
28#endif
29
30Module vpost01data
31 integer, Parameter :: Ni = 5
32 integer, Parameter :: Nb = 2
33 real*8, Dimension(Ni,Nb) :: a
34End Module vpost01data
36!> Main program. A simple setup and call of CONOPT
37!!
38Program vpost01
39
41 Use conopt
42 Use vpost01data
43 implicit None
44!
45! Declare the user callback routines as Integer, External:
46!
47 Integer, External :: vp_readmatrix ! Mandatory Matrix definition routine defined below
48 Integer, External :: vp_fdeval ! Function and Derivative evaluation routine
49 ! needed a nonlinear model.
50 Integer, External :: std_status ! Standard callback for displaying solution status
51 Integer, External :: std_solution ! Standard callback for displaying solution values
52 Integer, External :: std_message ! Standard callback for managing messages
53 Integer, External :: std_errmsg ! Standard callback for managing error messages
54#ifdef dec_directives_win32
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_ReadMatrix
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_FDEval
57!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
58!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
59!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
60!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
61#endif
62!
63! Control vector
64!
65 INTEGER, Dimension(:), Pointer :: cntvect
66 INTEGER :: coi_error
67!
68! Misc local variables
69!
70 Integer :: i, k
71!
72! Initialize Vpost01Data for the model
73!
74 do i = 1, ni
75 do k = 1, nb
76 a(i,k) = 0.1d0*i/ni*k*(-1)**(i+k)
77 enddo
78 enddo
79!
80! Create and initialize a Control Vector
81!
82 call startup
83
84 coi_error = coi_create( 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, 'vpost01.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(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
106 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_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 Vpost01 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 Else
134 do i = 1, ni
135 if ( abs(uprim(i)-i) > 1.e-7 .or. abs(uprim(ni+i)-2*i) > 1.e-7 .or. &
136 abs(uprim(ni+ni+i)-3*i) > 1.e-7 ) Then
137 call flog( "Incorrect activity level for an equation.", 1 )
138 endif
139 enddo
140 Call checkdual( 'Vpost01', maximize )
141 endif
142
143 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
144
145 call flog( "Successful Solve", 0 )
146!
147! Free solution memory
148!
149 call finalize
150
151End Program vpost01
152!
153! ============================================================================
154! Define information about the model:
155!
156
157!> Define information about the model
158!!
159!! @include{doc} readMatrix_params.dox
160Integer Function vp_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
161 colsta, rowno, value, nlflag, n, m, nz, &
162 usrmem )
163#ifdef dec_directives_win32
164!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_ReadMatrix
165#endif
166 use vpost01data
167 implicit none
168 integer, intent (in) :: n ! number of variables
169 integer, intent (in) :: m ! number of constraints
170 integer, intent (in) :: nz ! number of nonzeros
171 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
172 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
173 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
174 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
175 ! (not defined here)
176 integer, intent (out), dimension(m) :: type ! vector of equation types
177 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
178 ! (not defined here)
179 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
180 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
181 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
182 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
183 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
184 real*8 usrmem(*) ! optional user memory
185!
186 integer i, j, k
187!
188! Information about Variables:
189! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
190! Default: the status information in Vsta is not used.
191!
192! Lower bound = 0 on all variables
193!
194 do i = 1, n
195 lower(i) = 0.0d0
196 enddo
197 do i = 1, ni
198 upper(i) = 10.0d0
199 upper(ni+i) = 20.0d0
200 upper(ni+ni+i) = 30.0d0
201 enddo
202 do j = 1, nb
203 upper(3*ni+j) = 40.d0
204 enddo
205!
206! Information about Constraints:
207! Default: Rhs = 0
208! Default: the status information in Esta and the function
209! value in FV are not used.
210! Default: Type: There is no default.
211! 0 = Equality,
212! 1 = Greater than or equal,
213! 2 = Less than or equal,
214! 3 = Non binding.
215!
216 do i = 1, ni
217 rhs(i) = i
218 rhs(ni+i) = 2*i
219 rhs(ni+ni+i) = 3*i
220 enddo
221 do i = 1, 3*ni
222 type(i) = 0
223 enddo
224!
225! Constraint 3*ni+1 (Objective)
226! Rhs = 0 and type Non binding
227!
228 type(3*ni+1) = 3
229! positive variable x(i), y(i), z(i), v(b), obj;
230! equation e1(i), e2(i), e3(i), objdef
231! e1(i) .. x(i) + sum(b, a(i,b)*v(b)) =E= 1*ord(i);
232! e2(i) .. x(i) + y(i) - sum(b, a(i,b)*v(b)) =E= 2*ord(i);
233! e3(i) .. x(i) + y(i) + z(i) + sum(b, a(i,b)*v(b)) =E= 3*ord(i);
234! objdef .. obj =E= sum(b, ord(b)*v(b) );
235!
236! Information about the Jacobian. CONOPT expects a columnwise
237! representation in Rowno, Value, Nlflag and Colsta.
238!
239! Colsta = Start of column indices (No Defaults):
240! Rowno = Row indices
241! Value = Value of derivative (by default only linear
242! derivatives are used)
243! Nlflag = 0 for linear and 1 for nonlinear derivative
244! (not needed for completely linear models)
245!
246 k = 1
247 do i = 1, ni ! x variables
248 colsta(i) = k
249 rowno(k) = i
250 value(k) = 1.d0
251 k = k + 1
252 rowno(k) = ni+i
253 value(k) = 1.d0
254 k = k + 1
255 rowno(k) = ni+ni+i
256 value(k) = 1.d0
257 k = k + 1
258 enddo
259 do i = 1, ni ! y variables
260 colsta(ni+i) = k
261 rowno(k) = ni+i
262 value(k) = 1.d0
263 k = k + 1
264 rowno(k) = ni+ni+i
265 value(k) = 1.d0
266 k = k + 1
267 enddo
268 do i = 1, ni ! z variables
269 colsta(ni+ni+i) = k
270 rowno(k) = ni+ni+i
271 value(k) = 1.d0
272 k = k + 1
273 enddo
274 do j = 1, nb
275 colsta(3*ni+j) = k
276 do i = 1, ni
277 rowno(k) = i
278 value(k) = a(i,j)
279 k = k + 1
280 enddo
281 do i = 1, ni
282 rowno(k) = ni+i
283 value(k) = -a(i,j)
284 k = k + 1
285 enddo
286 do i = 1, ni
287 rowno(k) = ni+ni+i
288 value(k) = a(i,j)
289 k = k + 1
290 enddo
291 rowno(k) = 3*ni+1
292 value(k) = j
293 k = k + 1
294 enddo
295 colsta(3*ni+nb+1) = k
297 vp_readmatrix = 0 ! Return value means OK
298
299end Function vp_readmatrix
300!
301!==========================================================================
302! Compute nonlinear terms and non-constant Jacobian elements
303!
304
305!> Compute nonlinear terms and non-constant Jacobian elements
306!!
307!! @include{doc} fdeval_params.dox
308Integer Function vp_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
309 n, nz, thread, usrmem )
310#ifdef dec_directives_win32
311!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: VP_FDEval
312#endif
313 implicit none
314 integer, intent (in) :: n ! number of variables
315 integer, intent (in) :: rowno ! number of the row to be evaluated
316 integer, intent (in) :: nz ! number of nonzeros in this row
317 real*8, intent (in), dimension(n) :: x ! vector of current solution values
318 real*8, intent (in out) :: g ! constraint value
319 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
320 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
321 ! in this row. Ffor information only.
322 integer, intent (in) :: mode ! evaluation mode: 1 = function value
323 ! 2 = derivatives, 3 = both
324 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
325 ! as errcnt is incremented
326 integer, intent (in out) :: errcnt ! error counter to be incremented in case
327 ! of function evaluation errors.
328 integer, intent (in) :: thread
329 real*8 usrmem(*) ! optional user memory
330
331 vp_fdeval = 1 ! this routine should never be called for a linear model
332
333end Function vp_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
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(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
subroutine finalize
Definition comdecl.f90:79
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
real *8, dimension(:), pointer uprim
Definition comdecl.f90:24
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
integer, parameter nb
Definition vpost01.f90:34
real *8, dimension(ni, nb) a
Definition vpost01.f90:35
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:154
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:298
program vpost01
Main program. A simple setup and call of CONOPT.
Definition vpost01.f90:40