CONOPT
Loading...
Searching...
No Matches
range10.f90
Go to the documentation of this file.
1!> @file range10.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Range10:
6!! Model where some constraints can be combined into a single equality constraint.
7!!
8!! This is a CONOPT implementation of the GAMS model:
9!!
10!! @verbatim
11!! positive variable x1, x2, x3, x4, x5
12!! equation e1, e2, e3, e4;
13!! e1.. x1 + x2 x4 =L= 0;
14!! e2.. x3 + x4 =L= 2;
15!! e3.. 2x3 +2x4 =G= 4;
16!! e4.. x3 + x5 =E= 3;
17!! e5.. x4 + x5 =L= 2;
18!! x4.up = 1;
19!! minimize x3;
20!! @endverbatim
21!!
22!! e2 and e3 are combined into an equality constraint. After e1 has forced x1, x2, and x4
23!! to zero we have a derived equality with row count 1 and it should translate into x3 being
24!! fixed.
25!! We have added some extra rows to prevent x3 from becoming post-traingular as in range09.
26!!
27!!
28!! For more information about the individual callbacks, please have a look at the source code.
29
30#if defined(_WIN32) && !defined(_WIN64)
31#define dec_directives_win32
32#endif
33
34!> Main program. A simple setup and call of CONOPT
35!!
36Program range10
37
39 Use conopt
40 implicit None
41!
42! Declare the user callback routines as Integer, External:
43!
44 Integer, External :: rng_readmatrix ! Mandatory Matrix definition routine defined below
45 Integer, External :: std_status ! Standard callback for displaying solution status
46 Integer, External :: std_solution ! Standard callback for displaying solution values
47 Integer, External :: std_message ! Standard callback for managing messages
48 Integer, External :: std_errmsg ! Standard callback for managing error messages
49#ifdef dec_directives_win32
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rng_ReadMatrix
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
55#endif
56!
57! Control vector
58!
59 INTEGER, Dimension(:), Pointer :: cntvect
60 INTEGER :: coi_error
61!
62! Create and initialize a Control Vector
63!
64 call startup
65
66 coi_error = coi_create( cntvect )
67!
68! Tell CONOPT about the size of the model by populating the Control Vector:
69!
70 coi_error = max( coi_error, coidef_numvar( cntvect, 5 ) ) ! 5 variables
71 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! 5 constraints
72 coi_error = max( coi_error, coidef_numnz( cntvect, 11 ) ) ! 11 nonzeros in the Jacobian
73 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! 0 of which are nonlinear
74 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
75 coi_error = max( coi_error, coidef_objvar( cntvect, 3 ) ) ! Objective is variable 3
76 coi_error = max( coi_error, coidef_optfile( cntvect, 'Range10.opt' ) )
77!
78! Tell CONOPT about the callback routines:
79!
80 coi_error = max( coi_error, coidef_readmatrix( cntvect, rng_readmatrix ) )
81 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
82 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
83 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
84 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
85
86#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
87 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
88#endif
89
90 If ( coi_error .ne. 0 ) THEN
91 write(*,*)
92 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
93 write(*,*)
94 call flog( "Skipping Solve due to setup errors", 1 )
95 ENDIF
96!
97! Save the solution so we can check the duals:
98!
99 do_allocate = .true.
100!
101! Start CONOPT:
102!
103 coi_error = coi_solve( cntvect )
104
105 write(*,*)
106 write(*,*) 'End of Range10 example A. Return code=',coi_error
107
108 If ( coi_error /= 0 ) then
109 call flog( "Errors encountered during solution A", 1 )
110 elseif ( stacalls == 0 .or. solcalls == 0 ) then
111 call flog( "Status or Solution routine was not called A", 1 )
112 elseif ( sstat /= 1 .or. mstat /= 1 ) then ! Linear model
113 call flog( "Solver and Model Status was not as expected (1,1) A", 1 )
114 elseif ( abs( obj-2.0d0 ) > 0.000001d0 ) then
115 call flog( "Incorrect objective returned A", 1 )
116 Else
117 Call checkdual( 'Range10A', minimize )
118 endif
119!
120! We now reverse the direction of optimization and check that the dual still
121! are OK
122!
123 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
124 coi_error = coi_solve( cntvect )
125
126 write(*,*)
127 write(*,*) 'End of Range10 example B. Return code=',coi_error
128
129 If ( coi_error /= 0 ) then
130 call flog( "Errors encountered during solution B", 1 )
131 elseif ( stacalls == 0 .or. solcalls == 0 ) then
132 call flog( "Status or Solution routine was not called B", 1 )
133 elseif ( sstat /= 1 .or. mstat /= 1 ) then ! Linear model
134 call flog( "Solver and Model Status was not as expected (1,1) B", 1 )
135 elseif ( abs( obj-2.0d0 ) > 0.000001d0 ) then
136 call flog( "Incorrect objective returned B", 1 )
137 Else
138 Call checkdual( 'Range10B', maximize )
139 endif
140
141 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
142
143 call flog( "Successful Solve", 0 )
144!
145! Free solution memory
146!
147 call finalize
148
149End Program range10
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 rng_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
159 colsta, rowno, value, nlflag, n, m, nz, &
160 usrmem )
161#ifdef dec_directives_win32
162!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rng_ReadMatrix
163#endif
164 implicit none
165 integer, intent (in) :: n ! number of variables
166 integer, intent (in) :: m ! number of constraints
167 integer, intent (in) :: nz ! number of nonzeros
168 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
169 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
170 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
171 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
172 ! (not defined here)
173 integer, intent (out), dimension(m) :: type ! vector of equation types
174 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
175 ! (not defined here)
176 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
177 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
178 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
179 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
180 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
181 real*8 usrmem(*) ! optional user memory
182!
183! Information about Variables:
184! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
185! Default: the status information in Vsta is not used.
186!
187 lower(1) = 0.0d0
188 lower(2) = 0.0d0
189 lower(3) = 0.0d0
190 lower(4) = 0.0d0
191 lower(5) = 0.0d0
192 upper(4) = 1.0d0
193!
194! Information about Constraints:
195! Default: Rhs = 0
196! Default: the status information in Esta and the function
197! value in FV are not used.
198! Default: Type: There is no default.
199! 0 = Equality,
200! 1 = Greater than or equal,
201! 2 = Less than or equal,
202! 3 = Non binding.
203!
204 type(1) = 2
205 type(2) = 2; rhs(2) = 2.0d0
206 type(3) = 1; rhs(3) = 4.0d0
207 type(4) = 0; rhs(4) = 3.0d0
208 type(5) = 2; rhs(5) = 2.0d0
209!
210! Information about the Jacobian. CONOPT expects a columnwise
211! representation in Rowno, Value, Nlflag and Colsta.
212!
213! Colsta = Start of column indices (No Defaults):
214! Rowno = Row indices
215! Value = Value of derivative (by default only linear
216! derivatives are used)
217! Nlflag = 0 for linear and 1 for nonlinear derivative
218! (not needed for completely linear models)
219!
220! e4.. x3 + x5 =E= 3;
221! e5.. x4 + x5 =L= 2;
222! Indices
223! x(1) x(2) x(3) x(4) x(5)
224! 1: 1 2 6
225! 2: 3 7
226! 3: 4 8
227! 4: 5 10
228! 5: 9 11
229!
230 colsta(1) = 1
231 colsta(2) = 2
232 colsta(3) = 3
233 colsta(4) = 6
234 colsta(5) = 10
235 colsta(6) = 12
236 rowno(1) = 1
237 rowno(2) = 1
238 rowno(3) = 2
239 rowno(4) = 3
240 rowno(5) = 4
241 rowno(6) = 1
242 rowno(7) = 2
243 rowno(8) = 3
244 rowno(9) = 5
245 rowno(10) = 4
246 rowno(11) = 5
247!
248! Nonlinearity Structure: Model is linear and nlfalg is not needed
249!
250! e4.. x3 + x5 =E= 3;
251! e5.. x4 + x5 =L= 2;
252! Value (Linear only)
253! x(1) x(2) x(3) x(4) x(5)
254! 1: +1 +1 +1
255! 2: +1 +1
256! 3: +2 +2
257! 4: +1 +1
258! 5: +1 +1
259!
260 value(1) = 1.d0
261 value(2) = 1.d0
262 value(3) = 1.d0
263 value(4) = 2.d0
264 value(5) = 1.d0
265 value(6) = 1.d0
266 value(7) = 1.d0
267 value(8) = 2.d0
268 value(9) = 1.d0
269 value(10) = 1.d0
270 value(11) = 1.d0
271
272 rng_readmatrix = 0 ! Return value means OK
273
274end Function rng_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
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_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, parameter minimize
Definition comdecl.f90:31
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
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41
integer function rng_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition range01.f90:139
program range10
Main program. A simple setup and call of CONOPT.
Definition range10.f90:38