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