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