CONOPT
Loading...
Searching...
No Matches
ex01.f90
Go to the documentation of this file.
1!> @file ex01.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is an implementation of the model PORT.GMS (seq 50) in the GAMS
6!! model library. The model is repeated here for easy reference:
7!!
8!!
9!! @verbatim
10!! $TITLE SIMPLE PORTFOLIO MODEL (PORT,SEQ=50)
11!! * THIS SIMPLE PORTFOLIO SELECTION MODEL EXAMINES
12!! * INVESTMENT ALTERNATIVES IN THE BOND MARKET. THE SELECTION
13!! * IS CONSTRAIND BY RATING AND MATURITY CONSIDERATIONS.
14!! *
15!! * REFERENCE: CONTROL DATA CORPORATION, IFPS/OPTIMIM - USERS MANUAL,
16!! * MINNEAPOLIS, 1984.
17!!
18!! SETS B BONDS / MUNICIP-A, MUNICIP-B, CORPORATE, US-SER-E, US-SER-F /
19!! G(B) GROUPING / CORPORATE, US-SER-E, US-SER-F /
20!!
21!! TABLE YDAT(B,*) YIELD DATA
22!!
23!! RATING MATURITY YIELD TAX-RATE
24!! MUNICIP-A 2 9 4.3
25!! MUNICIP-B 5 2 4.5
26!! CORPORATE 2 15 5.4 .5
27!! US-SER-E 1 4 5.0 .5
28!! US-SER-F 1 3 4.4 .5
29!!
30!! VARIABLES INVESTMENT(B)
31!! TINVEST TOTAL INVESTMENT
32!! RETURN
33!! POSITIVE VARIABLE INVESTMENT
34!!
35!! EQUATIONS GROUPMIN MINIMUM INVESTMENT IN GROUP G
36!! RDEF RATING DEFINITION
37!! MDEF MATURITY DEFINITION
38!! IDEF TOTAL RETURN DEFINITION
39!! TDEF TOTAL INVESTMENT DEFINITION ;
40!! $DOUBLE
41!! TINVEST.UP = 10;
42!! GROUPMIN.. SUM(G, INVESTMENT(G)) =G= 4;
43!! RDEF.. SUM(B, YDAT(B,'RATING ')*INVESTMENT(B)) =L= 1.4*TINVEST;
44!! MDEF.. SUM(B, YDAT(B,'MATURITY')*INVESTMENT(B)) =L= 5.0*TINVEST;
45!! TDEF.. TINVEST =E= SUM(B, INVESTMENT(B));
46!! IDEF.. RETURN =E= SUM(B, YDAT(B,'YIELD')/100*(1-YDAT(B,'TAX-RATE'))*INVESTMENT(B));
47!! MODEL PORT / ALL / ; SOLVE PORT MAXIMIZING RETURN USING LP;
48!! @endverbatim
49!!
50!!
51!! For more information about the individual callbacks, please have a look at the source code.
52
53#if defined(_WIN32) && !defined(_WIN64)
54#define dec_directives_win32
55#endif
56
57!> Main program. A simple setup and call of CONOPT
58!!
59Program ex01
60!
61! Declare the user callback routines as Integer, External:
62!
63 Use proginfo
64 Use conopt
65 implicit None
66
67 Integer, External :: ex01_readmatrix ! Mandatory Matrix definition routine defined below
68 Integer, External :: std_status ! Standard callback for displaying solution status
69 Integer, External :: ex01_solution ! Specialized callback for displaying solution values
70 Integer, External :: std_message ! Standard callback for managing messages
71 Integer, External :: std_errmsg ! Standard callback for managing error messages
72#ifdef dec_directives_win32
73!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ex01_ReadMatrix
74!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
75!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ex01_Solution
76!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
77!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
78#endif
79!
80! Control vector
81!
82 INTEGER, Dimension(:), Pointer :: cntvect
83 INTEGER :: coi_error
84
85 Call startup
86!
87! Create and initialize a Control Vector
88!
89 coi_error = coi_create( cntvect )
90!
91! Tell CONOPT about the size of the model by populating the Control Vector:
92!
93! Number of variables (excl. slacks). 5 Investment, Tinvest, and Return
94! for a total of 7:
95!
96 coi_error = max( coi_error, coidef_numvar( cntvect, 7 ) )
97!
98! Number of equations: Groupmin, Rdef, Mdef, Idef, and Tdef for a total
99! of 5:
100!
101 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) )
102!
103! Number of nonzeros in the Jacobian. This is more complicated. See the
104! counting in Ex01_ReadMatrix below:
105!
106 coi_error = max( coi_error, coidef_numnz( cntvect,27 ) )
107!
108! Number of nonlinear nonzeros. The model is linear so the number is
109! zero:
110!
111 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) )
112!
113! Optimization direction is Maximize = 1
114!
115 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) )
116!
117! Objective is variable Return = 7
118!
119 coi_error = max( coi_error, coidef_objvar( cntvect, 7 ) )
120!
121! Define the options file as 'ex01.opt'
122!
123 coi_error = max( coi_error, coidef_optfile( cntvect, 'ex01.opt' ) )
124!
125! Tell CONOPT about the callback routines:
126!
127 coi_error = max( coi_error, coidef_readmatrix( cntvect, ex01_readmatrix ) )
128 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
129 coi_error = max( coi_error, coidef_solution( cntvect, ex01_solution ) )
130 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
131 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
132
133#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
134 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
135#endif
136
137 If ( coi_error .ne. 0 ) THEN
138 write(*,*)
139 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
140 write(*,*)
141 call flog( "Skipping Solve due to setup errors", 1 )
142 ENDIF
143!
144! Start CONOPT:
145!
146 coi_error = coi_solve( cntvect )
147
148 write(*,*)
149 write(*,*) 'End of example ex01'
150
151 If ( coi_error /= 0 ) then
152 call flog( "Errors encountered during solution", 1 )
153 elseif ( stacalls == 0 .or. solcalls == 0 ) then
154 call flog( "Status or Solution routine was not called", 1 )
155 elseif ( sstat /= 1 .or. mstat /= 1 ) then ! This is an LP model
156 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
157 elseif ( abs( obj-0.298364d0 ) > 0.000001d0 ) then
158 call flog( "Incorrect objective returned", 1 )
159 endif
160
161 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
162
163 call flog( "Successful Solve", 0 )
164!
165! Free solution memory
166!
167 call finalize
168
169End Program ex01
170!
171! ============================================================================
172! Define information about the model:
173!
174
175!> Define information about the model
176!!
177!! @include{doc} readMatrix_params.dox
178Integer Function ex01_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
179 colsta, rowno, value, nlflag, n, m, nz, usrmem )
180#ifdef dec_directives_win32
181!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ex01_ReadMatrix
182#endif
183 implicit none
184 integer, intent (in) :: n ! number of variables
185 integer, intent (in) :: m ! number of constraints
186 integer, intent (in) :: nz ! number of nonzeros
187 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
188 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
189 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
190 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
191 ! (not defined here)
192 integer, intent (out), dimension(m) :: type ! vector of equation types
193 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
194 ! (not defined here)
195 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
196 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
197 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
198 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
199 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
200 real*8 usrmem(*) ! optional user memory
201!
202! Define information about the Variables:
203!
204! Curr is not defined. We will use the default starting values of
205! zero.
206!
207! By default, we do not define the status argument Vsta.
208!
209! The first 5 variables are Positive, i.e. have a lower bound of
210! zero. They must be defined. The remaining two variables are Free,
211! i.e. have a lower bound of -infinity, which is the default, and we
212! do not define them:
213!
214 lower(1) = 0.d0
215 lower(2) = 0.d0
216 lower(3) = 0.d0
217 lower(4) = 0.d0
218 lower(5) = 0.d0
219!
220! Tinvest (number 6) has an upper bound of 10 and all other variables
221! are unbounded, i.e. have an upper bound of +infinity, which is the
222! default, so we do not define them:
223!
224 upper(6) = 10.d0
225!
226! Define information about the Constraints:
227!
228! By default, we do not define the status argument Esta.
229!
230! Groupmin: Type >= or 1:
231!
232 type(1) = 1
233!
234! Rdef and Mdef: Type <= or 2:
235!
236 type(2) = 2
237 type(3) = 2
238!
239! Tdef and Idef: Type = or 0:
240!
241 type(4) = 0
242 type(5) = 0
243!
244! Right hand sides: Only Groupmin (number 1) has a nonzero Right hand
245! side so it is sufficient to define this one. The default is zero:
246!
247 rhs(1) = 4.d0
248!
249! Define the structure and content of the Jacobian
250!
251! To help define the Jacobian pattern and values it can be useful to
252! make a picture of the Jacobian. Since the model is small and linear
253! we can put everything in one small table:
254!
255! inv1 inv2 inv3 inv4 inv5 tinvest return
256! groupmin 1 1 1
257! rdef 2 5 2 1 1 -1.4
258! mdef 9 2 15 4 3 -5.0
259! tdef 1 1 1 1 1 -1
260! idef 0.043 0.045 0.027 0.025 0.022 -1
261!
262! The model is linear so we do not have the define the nonlinearity
263! flag, Nlflag.
264! The Jacobian has to be sorted column-wise so we will just define
265! the elements column by column according to the table above:
266!
267! Column 1, Inv1. Start in position 1.
268!
269 colsta(1) = 1
270 rowno(1) = 2
271 value(1) = 2.d0
272 rowno(2) = 3
273 value(2) = 9.d0
274 rowno(3) = 4
275 value(3) = 1.d0
276 rowno(4) = 5
277 value(4) = 0.043d0
278!
279! Column 2, Inv2. Start in position 5:
280!
281 colsta(2) = 5
282 rowno(5) = 2
283 value(5) = 5.d0
284 rowno(6) = 3
285 value(6) = 2.d0
286 rowno(7) = 4
287 value(7) = 1.d0
288 rowno(8) = 5
289 value(8) = 0.045d0
290!
291! Column 3, Inv3. Start in position 9:
292!
293 colsta(3) = 9
294 rowno(9) = 1
295 value(9) = 1.d0
296 rowno(10) = 2
297 value(10) = 2.d0
298 rowno(11) = 3
299 value(11) = 15.d0
300 rowno(12) = 4
301 value(12) = 1.d0
302 rowno(13) = 5
303 value(13) = 0.027d0
304!
305! Column 4, Inv4. Start in position 14:
306!
307 colsta(4) = 14
308 rowno(14) = 1
309 value(14) = 1.d0
310 rowno(15) = 2
311 value(15) = 1.d0
312 rowno(16) = 3
313 value(16) = 4.d0
314 rowno(17) = 4
315 value(17) = 1.d0
316 rowno(18) = 5
317 value(18) = 0.025d0
318!
319! Column 5, Inv5. Start in position 19:
320!
321 colsta(5) = 19
322 rowno(19) = 1
323 value(19) = 1.d0
324 rowno(20) = 2
325 value(20) = 1.d0
326 rowno(21) = 3
327 value(21) = 3.d0
328 rowno(22) = 4
329 value(22) = 1.d0
330 rowno(23) = 5
331 value(23) = 0.022d0
332!
333! Column 6, Tinvest, Start in position 24:
334!
335 colsta(6) = 24
336 rowno(24) = 2
337 value(24) = -1.4d0
338 rowno(25) = 3
339 value(25) = -5.0d0
340 rowno(26) = 4
341 value(26) = -1.d0
342!
343! Column 7, Return, Start in position 27:
344!
345 colsta(7) = 27
346 rowno(27) = 5
347 value(27) = -1.d0
348!
349! End of columns, the next free position is 28 = number of elements+1:
350! The number of elements, 27, was the one reported in Ipsz(3) in
351! Coipsz.
352!
353 colsta(8) = 28
354
356
357end Function ex01_readmatrix
358!
359! ======================================================================
360! This was the end of the model definition. The next three routines are
361! standard routines used to report the solution back and write the
362! messages.
363! ======================================================================
364Integer Function ex01_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
365#ifdef dec_directives_win32
366!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ex01_Solution
367#endif
368
369 Use proginfo
370 IMPLICIT NONE
371 INTEGER, Intent(IN) :: n, m
372 INTEGER, Intent(IN), Dimension(N) :: xbas, xsta
373 INTEGER, Intent(IN), Dimension(M) :: ybas, ysta
374 real*8, Intent(IN), Dimension(N) :: xval, xmar
375 real*8, Intent(IN), Dimension(M) :: yval, ymar
376 real*8, Intent(IN OUT) :: usrmem(*)
377
378 INTEGER i
379 CHARACTER*5, Parameter, Dimension(4) :: stat = (/ 'Lower','Upper','Basic','Super' /)
380
381 Character*9, Parameter, Dimension(7) :: vname = (/ 'MUNICIP-A', 'MUNICIP-B', 'CORPORATE', 'US-SER-E ', 'US-SER-F ', &
382 'Tinvest ', 'Return ' /)
383 Character*9, Parameter, Dimension(5) :: cname = (/ 'GroupMin ', 'RDef ', 'Mdef ', 'Tdef ', 'Idef ' /)
384
385 WRITE(*,"(/' Variable Solution value Reduced cost B-stat'/)")
386 DO i = 1, n
387 WRITE(*,"(1P,1X,A9,E20.6,E16.6,4X,A5 )") vname(i), xval(i), xmar(i), stat(1+xbas(i))
388 ENDDO
389
390 WRITE(*,"(/' Constraint Activity level Marginal cost B-stat'/)")
391 DO i = 1, m
392 WRITE(*,"(1P,1X,A9,E20.6,E16.6,4X,A5 )") cname(i), yval(i), ymar(i), stat(1+ybas(i))
393 ENDDO
394
395 solcalls = solcalls + 1
396 ex01_solution = 0
397
398END Function ex01_solution
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
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
program ex01
Main program. A simple setup and call of CONOPT.
Definition ex01.f90:61
integer function ex01_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition ex01.f90:182
integer function ex01_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition ex01.f90:367
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_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_objvar(cntvect, objvar)
defines the Objective Variable.
Definition conopt.f90:257
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
real *8 obj
Definition comdecl.f90:16
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
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41