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
165End Program ex01
166!
167! ============================================================================
168! Define information about the model:
169!
170
171!> Define information about the model
172!!
173!! @include{doc} readMatrix_params.dox
174Integer Function ex01_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
175 colsta, rowno, value, nlflag, n, m, nz, usrmem )
176#ifdef dec_directives_win32
177!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ex01_ReadMatrix
178#endif
179 implicit none
180 integer, intent (in) :: n ! number of variables
181 integer, intent (in) :: m ! number of constraints
182 integer, intent (in) :: nz ! number of nonzeros
183 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
184 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
185 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
186 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
187 ! (not defined here)
188 integer, intent (out), dimension(m) :: type ! vector of equation types
189 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
190 ! (not defined here)
191 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
192 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
193 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
194 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
195 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
196 real*8 usrmem(*) ! optional user memory
197!
198! Define information about the Variables:
199!
200! Curr is not defined. We will use the default starting values of
201! zero.
202!
203! By default, we do not define the status argument Vsta.
204!
205! The first 5 variables are Positive, i.e. have a lower bound of
206! zero. They must be defined. The remaining two variables are Free,
207! i.e. have a lower bound of -infinity, which is the default, and we
208! do not define them:
209!
210 lower(1) = 0.d0
211 lower(2) = 0.d0
212 lower(3) = 0.d0
213 lower(4) = 0.d0
214 lower(5) = 0.d0
215!
216! Tinvest (number 6) has an upper bound of 10 and all other variables
217! are unbounded, i.e. have an upper bound of +infinity, which is the
218! default, so we do not define them:
219!
220 upper(6) = 10.d0
221!
222! Define information about the Constraints:
223!
224! By default, we do not define the status argument Esta.
225!
226! Groupmin: Type >= or 1:
227!
228 type(1) = 1
229!
230! Rdef and Mdef: Type <= or 2:
231!
232 type(2) = 2
233 type(3) = 2
234!
235! Tdef and Idef: Type = or 0:
236!
237 type(4) = 0
238 type(5) = 0
239!
240! Right hand sides: Only Groupmin (number 1) has a nonzero Right hand
241! side so it is sufficient to define this one. The default is zero:
242!
243 rhs(1) = 4.d0
244!
245! Define the structure and content of the Jacobian
246!
247! To help define the Jacobian pattern and values it can be useful to
248! make a picture of the Jacobian. Since the model is small and linear
249! we can put everything in one small table:
250!
251! inv1 inv2 inv3 inv4 inv5 tinvest return
252! groupmin 1 1 1
253! rdef 2 5 2 1 1 -1.4
254! mdef 9 2 15 4 3 -5.0
255! tdef 1 1 1 1 1 -1
256! idef 0.043 0.045 0.027 0.025 0.022 -1
257!
258! The model is linear so we do not have the define the nonlinearity
259! flag, Nlflag.
260! The Jacobian has to be sorted column-wise so we will just define
261! the elements column by column according to the table above:
262!
263! Column 1, Inv1. Start in position 1.
264!
265 colsta(1) = 1
266 rowno(1) = 2
267 value(1) = 2.d0
268 rowno(2) = 3
269 value(2) = 9.d0
270 rowno(3) = 4
271 value(3) = 1.d0
272 rowno(4) = 5
273 value(4) = 0.043d0
274!
275! Column 2, Inv2. Start in position 5:
276!
277 colsta(2) = 5
278 rowno(5) = 2
279 value(5) = 5.d0
280 rowno(6) = 3
281 value(6) = 2.d0
282 rowno(7) = 4
283 value(7) = 1.d0
284 rowno(8) = 5
285 value(8) = 0.045d0
286!
287! Column 3, Inv3. Start in position 9:
288!
289 colsta(3) = 9
290 rowno(9) = 1
291 value(9) = 1.d0
292 rowno(10) = 2
293 value(10) = 2.d0
294 rowno(11) = 3
295 value(11) = 15.d0
296 rowno(12) = 4
297 value(12) = 1.d0
298 rowno(13) = 5
299 value(13) = 0.027d0
300!
301! Column 4, Inv4. Start in position 14:
302!
303 colsta(4) = 14
304 rowno(14) = 1
305 value(14) = 1.d0
306 rowno(15) = 2
307 value(15) = 1.d0
308 rowno(16) = 3
309 value(16) = 4.d0
310 rowno(17) = 4
311 value(17) = 1.d0
312 rowno(18) = 5
313 value(18) = 0.025d0
314!
315! Column 5, Inv5. Start in position 19:
316!
317 colsta(5) = 19
318 rowno(19) = 1
319 value(19) = 1.d0
320 rowno(20) = 2
321 value(20) = 1.d0
322 rowno(21) = 3
323 value(21) = 3.d0
324 rowno(22) = 4
325 value(22) = 1.d0
326 rowno(23) = 5
327 value(23) = 0.022d0
328!
329! Column 6, Tinvest, Start in position 24:
330!
331 colsta(6) = 24
332 rowno(24) = 2
333 value(24) = -1.4d0
334 rowno(25) = 3
335 value(25) = -5.0d0
336 rowno(26) = 4
337 value(26) = -1.d0
338!
339! Column 7, Return, Start in position 27:
340!
341 colsta(7) = 27
342 rowno(27) = 5
343 value(27) = -1.d0
344!
345! End of columns, the next free position is 28 = number of elements+1:
346! The number of elements, 27, was the one reported in Ipsz(3) in
347! Coipsz.
348!
349 colsta(8) = 28
350
352
353end Function ex01_readmatrix
354!
355! ======================================================================
356! This was the end of the model definition. The next three routines are
357! standard routines used to report the solution back and write the
358! messages.
359! ======================================================================
360Integer Function ex01_solution( XVAL, XMAR, XBAS, XSTA, YVAL, YMAR, YBAS, YSTA, N, M, USRMEM )
361#ifdef dec_directives_win32
362!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ex01_Solution
363#endif
364
365 Use proginfo
366 IMPLICIT NONE
367 INTEGER, Intent(IN) :: n, m
368 INTEGER, Intent(IN), Dimension(N) :: xbas, xsta
369 INTEGER, Intent(IN), Dimension(M) :: ybas, ysta
370 real*8, Intent(IN), Dimension(N) :: xval, xmar
371 real*8, Intent(IN), Dimension(M) :: yval, ymar
372 real*8, Intent(IN OUT) :: usrmem(*)
373
374 INTEGER i
375 CHARACTER*5, Parameter, Dimension(4) :: stat = (/ 'Lower','Upper','Basic','Super' /)
376
377 Character*9, Parameter, Dimension(7) :: vname = (/ 'MUNICIP-A', 'MUNICIP-B', 'CORPORATE', 'US-SER-E ', 'US-SER-F ', &
378 'Tinvest ', 'Return ' /)
379 Character*9, Parameter, Dimension(5) :: cname = (/ 'GroupMin ', 'RDef ', 'Mdef ', 'Tdef ', 'Idef ' /)
380
381 WRITE(*,"(/' Variable Solution value Reduced cost B-stat'/)")
382 DO i = 1, n
383 WRITE(*,"(1P,1X,A9,E20.6,E16.6,4X,A5 )") vname(i), xval(i), xmar(i), stat(1+xbas(i))
384 ENDDO
385
386 WRITE(*,"(/' Constraint Activity level Marginal cost B-stat'/)")
387 DO i = 1, m
388 WRITE(*,"(1P,1X,A9,E20.6,E16.6,4X,A5 )") cname(i), yval(i), ymar(i), stat(1+ybas(i))
389 ENDDO
390
391 solcalls = solcalls + 1
392 ex01_solution = 0
393
394END Function ex01_solution
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:88
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
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:178
integer function ex01_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition ex01.f90:363
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
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