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$\max x1\f$
7!! <table>
8!! <tr><th> <th>x1 <th>x2 <th>x3 <th>x4 <th>x5 <th>x6 <th>x7 <th>
9!! <tr><td>1: <td>1 <td>-1 <td>-1 <td> <td> <td> <td> <td>= 0
10!! <tr><td>2: <td> <td>1 <td> -10 <td>-5 <td> <td> <td> <td>= 0
11!! <tr><td>3: <td> <td> <td> <td>-1 <td> <td>1 <td>1 <td>\f$\leq\f$ 10
12!! <tr><td>4: <td> <td> <td> <td> <td>-1 <td>1 <td>-1 <td>\f$\leq\f$ -10
13!! <tr><td>5: <td> <td> <td>1 <td> <td> <td> <td> <td>\f$\leq\f$ 8
14!! <tr><td> <td> <td>x2 <td>x3 <td>x4 <td>x5 <td>x6 <td>x7 <td>\f$\geq\f$ 0
15!! </table>
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#if defined(_WIN32) && !defined(_WIN64)
32#define dec_directives_win32
33#endif
34
35!> Main program. A simple setup and call of CONOPT
36!!
37Program pen01
38
40 Use conopt
41 Use casedata_num
42 implicit None
43!
44! Declare the user callback routines as Integer, External:
45!
46 Integer, External :: pen01_readmatrix ! Mandatory Matrix definition routine defined below
47 Integer, External :: std_status ! Standard callback for displaying solution status
48 Integer, External :: std_solution ! Standard callback for displaying solution values
49 Integer, External :: std_message ! Standard callback for managing messages
50 Integer, External :: std_errmsg ! Standard callback for managing error messages
51#ifdef dec_directives_win32
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen01_ReadMatrix
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
55!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
56!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
57#endif
58!
59! Control vector
60!
61 INTEGER, Dimension(:), Pointer :: cntvect
62 INTEGER :: coi_error
63
64!
65! Create and initialize a Control Vector
66!
67 call startup
68
69 coi_error = coi_create( 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(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
90 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_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!
149! Free solution memory
150!
151 call finalize
152
153End Program pen01
154!
155! ============================================================================
156! Define information about the model:
157!
158
159!> Define information about the model
160!!
161!! @include{doc} readMatrix_params.dox
162Integer Function pen01_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
163 colsta, rowno, value, nlflag, n, m, nz, &
164 usrmem )
165#ifdef dec_directives_win32
166!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Pen01_ReadMatrix
167#endif
168 use casedata_num
169 implicit none
170 integer, intent (in) :: n ! number of variables
171 integer, intent (in) :: m ! number of constraints
172 integer, intent (in) :: nz ! number of nonzeros
173 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
174 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
175 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
176 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
177 ! (not defined here)
178 integer, intent (out), dimension(m) :: type ! vector of equation types
179 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
180 ! (not defined here)
181 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
182 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
183 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
184 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
185 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
186 real*8 usrmem(*) ! optional user memory
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(2) = 0.0d0
193 lower(3) = 0.0d0
194 lower(4) = 0.0d0
195 lower(5) = 0.0d0
196 lower(6) = 0.0d0
197 lower(7) = 0.0d0
198 if ( casenum == 2 ) Then
199 upper(6) = 12.0d0
200 upper(7) = 7.0d0
201 Endif
202 if ( casenum == 3 ) Then
203 upper(6) = 7.0d0
204 upper(7) = 12.0d0
205 Endif
206!
207! Information about Constraints:
208! Default: Rhs = 0
209! Default: the status information in Esta and the function
210! value in FV are not used.
211! Default: Type: There is no default.
212! 0 = Equality,
213! 1 = Greater than or equal,
214! 2 = Less than or equal,
215! 3 = Non binding.
216!
217 type(1) = 0
218 type(2) = 0
219 type(3) = 2
220 type(4) = 2
221 type(5) = 2
222 rhs(3) = 10.d0
223 rhs(4) = -10.d0
224 rhs(5) = 8.d0
225!
226! Information about the Jacobian. CONOPT expects a columnwise
227! representation in Rowno, Value, Nlflag and Colsta.
228!
229! Colsta = Start of column indices (No Defaults):
230! Rowno = Row indices
231! Value = Value of derivative (by default only linear
232! derivatives are used)
233! Nlflag = 0 for linear and 1 for nonlinear derivative
234! (not needed for completely linear models)
235!
236! Indices
237! x1 x2 x3 x4 x5 x6 x7
238! 1: 1 2 4
239! 2: 3 6 8
240! 3: 7 10 12
241! 4: 9 11 13
242! 5: 5
243!
244 colsta(1) = 1
245 colsta(2) = 2
246 colsta(3) = 4
247 colsta(4) = 6
248 colsta(5) = 8
249 colsta(6) =10
250 colsta(7) =12
251 colsta(8) =14
252 rowno(1) = 1
253 rowno(2) = 1
254 rowno(3) = 2
255 rowno(4) = 1
256 rowno(5) = 5
257 rowno(6) = 2
258 rowno(7) = 3
259 rowno(8) = 2
260 rowno(9) = 4
261 rowno(10) = 3
262 rowno(11) = 4
263 rowno(12) = 3
264 rowno(13) = 4
265!
266! Nonlinearity Structure: Not needed for a linear model
267!
268! Value (Linear only)
269! x1 x2 x3 x4 x5 x6 x7
270! 1: 1 -1 -1
271! 2: 1 -10 -5
272! 3: -1 1 1
273! 4: -1 1 -1
274! 5: 1
275!
276 value(1) = 1.d0
277 value(2) = -1.d0
278 value(3) = 1.d0
279 value(4) = -1.d0
280 value(5) = 1.d0
281 value(6) =-10.d0
282 value(7) = -1.d0
283 value(8) = -5.d0
284 value(9) = -1.d0
285 value(10) = 1.d0
286 value(11) = 1.d0
287 value(12) = 1.d0
288 value(13) = -1.d0
289
290 pen01_readmatrix = 0 ! Return value means OK
291
292end Function pen01_readmatrix
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
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_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
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
subroutine startup
Definition comdecl.f90:41
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:167
program pen01
Main program. A simple setup and call of CONOPT.
Definition pen01.f90:39