CONOPT
Loading...
Searching...
No Matches
pen01.f90
Go to the documentation of this file.
1!> @file pen01.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!! Test model pen01.gms -- a model with penalty pairs.
5!!
6!! \f{eqnarray*}{
7!! \max &x1&&&&&&&&&&&&& \\
8!! &x1& &x2& &x3& &x4& &x5& &x6& &x7& \\
9!! 1: &1& &-1& &-1& &&&&&&&& = 0\\
10!! 2: && &1& && &-10& &-5& && && = 0\\
11!! 3: &&&&&& &-1& && &1& &1& \leq 10\\
12!! 4: &&&&&&&& &-1& &1& &-1& \leq -10\\
13!! 5: &&&& &1& &&&&&&&& \leq 8\\
14!! && &x2&, &x3&, &x4&, &x5&, &x6&, &x7& >= 0
15!! \f}
16!!
17!! The base-case has a penalty-pair with two structural variables and
18!! one with a structural and a slack.
19!!
20!! In case 2 variable x6 and x7 have an upper bound and can therefore
21!! not be used a a penalty variable. However, the effect is that x5
22!! becomes implied free and can be used as post-triangular.
23!!
24!! In case 3 variable x6 and x7 have different upper bounds and can again
25!! not be used a a penalty variable, and this time the slack is used
26!! so we have two structural/slack-pairs.
27!!
28!!
29!! For more information about the individual callbacks, please have a look at the source code.
30
31
32!> Main program. A simple setup and call of CONOPT
33!!
34Program pen01
35
36 Use proginfo
37 Use coidef
38 Use casedata_num
39 implicit None
40!
41! Declare the user callback routines as Integer, External:
42!
43 Integer, External :: pen01_readmatrix ! Mandatory Matrix definition routine defined below
44 Integer, External :: std_status ! Standard callback for displaying solution status
45 Integer, External :: std_solution ! Standard callback for displaying solution values
46 Integer, External :: std_message ! Standard callback for managing messages
47 Integer, External :: std_errmsg ! Standard callback for managing error messages
48#if defined(itl)
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen01_ReadMatrix
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
54#endif
55!
56! Control vector
57!
58 INTEGER :: numcallback
59 INTEGER, Dimension(:), Pointer :: cntvect
60 INTEGER :: coi_error
61
62!
63! Create and initialize a Control Vector
64!
65 call startup
66
67 numcallback = coidef_size()
68 Allocate( cntvect(numcallback) )
69 coi_error = coidef_inifort( cntvect )
70!
71! Tell CONOPT about the size of the model by populating the Control Vector:
72!
73 coi_error = max( coi_error, coidef_numvar( cntvect, 7 ) ) ! # variables
74 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! # constraints
75 coi_error = max( coi_error, coidef_numnz( cntvect,13 ) ) ! # nonzeros in the Jacobian
76 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
77 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
78 coi_error = max( coi_error, coidef_objvar( cntvect, 1 ) ) ! Objective is variable 1
79 coi_error = max( coi_error, coidef_optfile( cntvect, 'pen01.opt' ) )
80!
81! Tell CONOPT about the callback routines:
82!
83 coi_error = max( coi_error, coidef_readmatrix( cntvect, pen01_readmatrix ) )
84 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
85 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
86 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
87 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
88
89#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
90 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
91#endif
92
93 If ( coi_error .ne. 0 ) THEN
94 write(*,*)
95 write(*,*) '**** Fatal Error while loading CONOPT4 Callback routines.'
96 write(*,*)
97 call flog( "Skipping Solve due to setup errors", 1 )
98 ENDIF
99!
100! Save the solution so we can check the duals:
101!
102 do_allocate = .true.
103!
104! Start CONOPT:
105!
106 casenum = 1
107 coi_error = coi_solve( cntvect )
108 If ( coi_error /= 0 ) then
109 call flog( "Case 1: Errors encountered during solution", 1 )
110 elseif ( stacalls == 0 .or. solcalls == 0 ) then
111 call flog( "Case 1: Status or Solution routine was not called", 1 )
112 elseif ( sstat /= 1 ) then
113 call flog( "Case 1: Solver Status was 1 not as expected.", 1 )
114 endif
115!
116! Solve the second case:
117!
118 stacalls = 0; solcalls = 0;
119 casenum = 2
120 coi_error = coi_solve( cntvect )
121 If ( coi_error /= 0 ) then
122 call flog( "Case 2: Errors encountered during solution", 1 )
123 elseif ( stacalls == 0 .or. solcalls == 0 ) then
124 call flog( "Case 2: Status or Solution routine was not called", 1 )
125 elseif ( sstat /= 1 ) then
126 call flog( "Case 2: Solver Status was 1 not as expected.", 1 )
127 endif
128!
129! And the third:
130!
131 stacalls = 0; solcalls = 0;
132 casenum = 3
133 coi_error = coi_solve( cntvect )
134 If ( coi_error /= 0 ) then
135 call flog( "Case 3: Errors encountered during solution", 1 )
136 elseif ( stacalls == 0 .or. solcalls == 0 ) then
137 call flog( "Case 3: Status or Solution routine was not called", 1 )
138 elseif ( sstat /= 1 ) then
139 call flog( "Case 3: Solver Status was 1 not as expected.", 1 )
140 endif
141
142 write(*,*)
143 write(*,*) 'End of pen01 example. Return code=',coi_error
144
145 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
146
147 call flog( "Successful Solve", 0 )
148
149End Program pen01
150!
151! ============================================================================
152! Define information about the model:
153!
154
155!> Define information about the model
156!!
157!! @include{doc} readMatrix_params.dox
158Integer Function pen01_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
159 colsta, rowno, value, nlflag, n, m, nz, &
160 usrmem )
161#if defined(itl)
162!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen01_ReadMatrix
163#endif
164 use casedata_num
165 implicit none
166 integer, intent (in) :: n ! number of variables
167 integer, intent (in) :: m ! number of constraints
168 integer, intent (in) :: nz ! number of nonzeros
169 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
170 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
171 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
172 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
173 ! (not defined here)
174 integer, intent (out), dimension(m) :: type ! vector of equation types
175 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
176 ! (not defined here)
177 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
178 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
179 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
180 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
181 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
182 real*8 usrmem(*) ! optional user memory
183!
184! Information about Variables:
185! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
186! Default: the status information in Vsta is not used.
187!
188 lower(2) = 0.0d0
189 lower(3) = 0.0d0
190 lower(4) = 0.0d0
191 lower(5) = 0.0d0
192 lower(6) = 0.0d0
193 lower(7) = 0.0d0
194 if ( casenum == 2 ) Then
195 upper(6) = 12.0d0
196 upper(7) = 7.0d0
197 Endif
198 if ( casenum == 3 ) Then
199 upper(6) = 7.0d0
200 upper(7) = 12.0d0
201 Endif
202!
203! Information about Constraints:
204! Default: Rhs = 0
205! Default: the status information in Esta and the function
206! value in FV are not used.
207! Default: Type: There is no default.
208! 0 = Equality,
209! 1 = Greater than or equal,
210! 2 = Less than or equal,
211! 3 = Non binding.
212!
213 type(1) = 0
214 type(2) = 0
215 type(3) = 2
216 type(4) = 2
217 type(5) = 2
218 rhs(3) = 10.d0
219 rhs(4) = -10.d0
220 rhs(5) = 8.d0
221!
222! Information about the Jacobian. We use the standard method with
223! Rowno, Value, Nlflag and Colsta and we do not use Colno.
224!
225! Colsta = Start of column indices (No Defaults):
226! Rowno = Row indices
227! Value = Value of derivative (by default only linear
228! derivatives are used)
229! Nlflag = 0 for linear and 1 for nonlinear derivative
230! (not needed for completely linear models)
231!
232! Indices
233! x1 x2 x3 x4 x5 x6 x7
234! 1: 1 2 4
235! 2: 3 6 8
236! 3: 7 10 12
237! 4: 9 11 13
238! 5: 5
239!
240 colsta(1) = 1
241 colsta(2) = 2
242 colsta(3) = 4
243 colsta(4) = 6
244 colsta(5) = 8
245 colsta(6) =10
246 colsta(7) =12
247 colsta(8) =14
248 rowno(1) = 1
249 rowno(2) = 1
250 rowno(3) = 2
251 rowno(4) = 1
252 rowno(5) = 5
253 rowno(6) = 2
254 rowno(7) = 3
255 rowno(8) = 2
256 rowno(9) = 4
257 rowno(10) = 3
258 rowno(11) = 4
259 rowno(12) = 3
260 rowno(13) = 4
261!
262! Nonlinearity Structure: Not needed for a linear model
263!
264! Value (Linear only)
265! x1 x2 x3 x4 x5 x6 x7
266! 1: 1 -1 -1
267! 2: 1 -10 -5
268! 3: -1 1 1
269! 4: -1 1 -1
270! 5: 1
271!
272 value(1) = 1.d0
273 value(2) = -1.d0
274 value(3) = 1.d0
275 value(4) = -1.d0
276 value(5) = 1.d0
277 value(6) =-10.d0
278 value(7) = -1.d0
279 value(8) = -5.d0
280 value(9) = -1.d0
281 value(10) = 1.d0
282 value(11) = 1.d0
283 value(12) = 1.d0
284 value(13) = -1.d0
285
286 pen01_readmatrix = 0 ! Return value means OK
287
288end Function pen01_readmatrix
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
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_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_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_objvar(cntvect, objvar)
defines the Objective Variable.
Definition coistart.f90:586
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
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
subroutine startup
Definition comdecl.f90:35
integer function pen01_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition pen01.f90:161
program pen01
Main program. A simple setup and call of CONOPT.
Definition pen01.f90:34