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