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!> Main program. A simple setup and call of CONOPT
31!!
32Program range10
33
34 Use proginfo
35 Use coidef
36 implicit None
37!
38! Declare the user callback routines as Integer, External:
39!
40 Integer, External :: rng_readmatrix ! Mandatory Matrix definition routine defined below
41 Integer, External :: std_status ! Standard callback for displaying solution status
42 Integer, External :: std_solution ! Standard callback for displaying solution values
43 Integer, External :: std_message ! Standard callback for managing messages
44 Integer, External :: std_errmsg ! Standard callback for managing error messages
45#if defined(itl)
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rng_ReadMatrix
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
51#endif
52!
53! Control vector
54!
55 INTEGER, Dimension(:), Pointer :: cntvect
56 INTEGER :: coi_error
57!
58! Create and initialize a Control Vector
59!
60 call startup
61
62 coi_error = coi_createfort( cntvect )
63!
64! Tell CONOPT about the size of the model by populating the Control Vector:
65!
66 coi_error = max( coi_error, coidef_numvar( cntvect, 5 ) ) ! 5 variables
67 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) ) ! 5 constraints
68 coi_error = max( coi_error, coidef_numnz( cntvect, 11 ) ) ! 11 nonzeros in the Jacobian
69 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! 0 of which are nonlinear
70 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
71 coi_error = max( coi_error, coidef_objvar( cntvect, 3 ) ) ! Objective is variable 3
72 coi_error = max( coi_error, coidef_optfile( cntvect, 'Range10.opt' ) )
73!
74! Tell CONOPT about the callback routines:
75!
76 coi_error = max( coi_error, coidef_readmatrix( cntvect, rng_readmatrix ) )
77 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
78 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
79 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
80 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
81
82#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
83 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
84#endif
85
86 If ( coi_error .ne. 0 ) THEN
87 write(*,*)
88 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
89 write(*,*)
90 call flog( "Skipping Solve due to setup errors", 1 )
91 ENDIF
92!
93! Save the solution so we can check the duals:
94!
95 do_allocate = .true.
96!
97! Start CONOPT:
98!
99 coi_error = coi_solve( cntvect )
100
101 write(*,*)
102 write(*,*) 'End of Range10 example A. Return code=',coi_error
103
104 If ( coi_error /= 0 ) then
105 call flog( "Errors encountered during solution A", 1 )
106 elseif ( stacalls == 0 .or. solcalls == 0 ) then
107 call flog( "Status or Solution routine was not called A", 1 )
108 elseif ( sstat /= 1 .or. mstat /= 1 ) then ! Linear model
109 call flog( "Solver and Model Status was not as expected (1,1) A", 1 )
110 elseif ( abs( obj-2.0d0 ) > 0.000001d0 ) then
111 call flog( "Incorrect objective returned A", 1 )
112 Else
113 Call checkdual( 'Range10A', minimize )
114 endif
115!
116! We now reverse the direction of optimization and check that the dual still
117! are OK
118!
119 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
120 coi_error = coi_solve( cntvect )
121
122 write(*,*)
123 write(*,*) 'End of Range10 example B. Return code=',coi_error
124
125 If ( coi_error /= 0 ) then
126 call flog( "Errors encountered during solution B", 1 )
127 elseif ( stacalls == 0 .or. solcalls == 0 ) then
128 call flog( "Status or Solution routine was not called B", 1 )
129 elseif ( sstat /= 1 .or. mstat /= 1 ) then ! Linear model
130 call flog( "Solver and Model Status was not as expected (1,1) B", 1 )
131 elseif ( abs( obj-2.0d0 ) > 0.000001d0 ) then
132 call flog( "Incorrect objective returned B", 1 )
133 Else
134 Call checkdual( 'Range10B', maximize )
135 endif
136
137 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
138
139 call flog( "Successful Solve", 0 )
140
141End Program range10
142!
143! ============================================================================
144! Define information about the model:
145!
146
147!> Define information about the model
148!!
149!! @include{doc} readMatrix_params.dox
150Integer Function rng_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
151 colsta, rowno, value, nlflag, n, m, nz, &
152 usrmem )
153#if defined(itl)
154!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Rng_ReadMatrix
155#endif
156 implicit none
157 integer, intent (in) :: n ! number of variables
158 integer, intent (in) :: m ! number of constraints
159 integer, intent (in) :: nz ! number of nonzeros
160 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
161 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
162 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
163 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
164 ! (not defined here)
165 integer, intent (out), dimension(m) :: type ! vector of equation types
166 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
167 ! (not defined here)
168 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
169 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
170 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
171 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
172 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
173 real*8 usrmem(*) ! optional user memory
174!
175! Information about Variables:
176! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
177! Default: the status information in Vsta is not used.
178!
179 lower(1) = 0.0d0
180 lower(2) = 0.0d0
181 lower(3) = 0.0d0
182 lower(4) = 0.0d0
183 lower(5) = 0.0d0
184 upper(4) = 1.0d0
185!
186! Information about Constraints:
187! Default: Rhs = 0
188! Default: the status information in Esta and the function
189! value in FV are not used.
190! Default: Type: There is no default.
191! 0 = Equality,
192! 1 = Greater than or equal,
193! 2 = Less than or equal,
194! 3 = Non binding.
195!
196 type(1) = 2
197 type(2) = 2; rhs(2) = 2.0d0
198 type(3) = 1; rhs(3) = 4.0d0
199 type(4) = 0; rhs(4) = 3.0d0
200 type(5) = 2; rhs(5) = 2.0d0
201!
202! Information about the Jacobian. We use the standard method with
203! Rowno, Value, Nlflag and Colsta and we do not use Colno.
204!
205! Colsta = Start of column indices (No Defaults):
206! Rowno = Row indices
207! Value = Value of derivative (by default only linear
208! derivatives are used)
209! Nlflag = 0 for linear and 1 for nonlinear derivative
210! (not needed for completely linear models)
211!
212! e4.. x3 + x5 =E= 3;
213! e5.. x4 + x5 =L= 2;
214! Indices
215! x(1) x(2) x(3) x(4) x(5)
216! 1: 1 2 6
217! 2: 3 7
218! 3: 4 8
219! 4: 5 10
220! 5: 9 11
221!
222 colsta(1) = 1
223 colsta(2) = 2
224 colsta(3) = 3
225 colsta(4) = 6
226 colsta(5) = 10
227 colsta(6) = 12
228 rowno(1) = 1
229 rowno(2) = 1
230 rowno(3) = 2
231 rowno(4) = 3
232 rowno(5) = 4
233 rowno(6) = 1
234 rowno(7) = 2
235 rowno(8) = 3
236 rowno(9) = 5
237 rowno(10) = 4
238 rowno(11) = 5
239!
240! Nonlinearity Structure: Model is linear and nlfalg is not needed
241!
242! e4.. x3 + x5 =E= 3;
243! e5.. x4 + x5 =L= 2;
244! Value (Linear only)
245! x(1) x(2) x(3) x(4) x(5)
246! 1: +1 +1 +1
247! 2: +1 +1
248! 3: +2 +2
249! 4: +1 +1
250! 5: +1 +1
251!
252 value(1) = 1.d0
253 value(2) = 1.d0
254 value(3) = 1.d0
255 value(4) = 2.d0
256 value(5) = 1.d0
257 value(6) = 1.d0
258 value(7) = 1.d0
259 value(8) = 2.d0
260 value(9) = 1.d0
261 value(10) = 1.d0
262 value(11) = 1.d0
263
264 rng_readmatrix = 0 ! Return value means OK
265
266end 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 range10
Main program. A simple setup and call of CONOPT.
Definition range10.f90:32