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