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