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