CONOPT
Loading...
Searching...
No Matches
eq2d.f90
Go to the documentation of this file.
1!> @file eq2d.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Tests EQ2 constraints that are made post-triangular by transfer of bounds.
6!!
7!! @verbatim
8!! positive variable x1, x2, x3, x4, x5, x6, x7;
9!! variable obj;
10!!
11!! equation e1, e2, e3, e4, objdef;
12!!
13!! e1.. x1 + x3 =E= 1;
14!! e2.. x1 + x2 =L= 2;
15!! e3.. x2 =G= 1.5 + x6 + x7;
16!! e4.. x2 =L= 1.5 - x4 - x5;
17!! objdef .. obj =E= 7*x1 + 8*x2 + 100*x3;
18!! @endverbatim
19!!
20!!
21!! For more information about the individual callbacks, please have a look at the source code.
22
23!> Main program. A simple setup and call of CONOPT
24!!
25Program eq2d
26!
27! Declare the user callback routines as Integer, External:
28!
29 Use proginfo
30 Use coidef
31 implicit None
32
33 Integer, External :: eq2d_readmatrix ! Mandatory Matrix definition routine defined below
34 Integer, External :: std_status ! Standard callback for displaying solution status
35 Integer, External :: std_solution ! Standard callback for displaying solution values
36 Integer, External :: std_message ! Standard callback for managing messages
37 Integer, External :: std_errmsg ! Standard callback for managing error messages
38#if defined(itl)
39!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: eq2d_ReadMatrix
40!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
41!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
43!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
44#endif
45!
46! Control vector
47!
48 INTEGER :: numcallback
49 INTEGER, Dimension(:), Pointer :: cntvect
50 INTEGER :: coi_error
51
52 real*8, dimension(7) :: xsol1 = (/ 0.5d0, 1.5d0, 0.5d0, 0.0d0, 0.0d0, 0.0d0, 0.0d0 /)
53 real*8, dimension(7) :: xdua1 = (/ 0.0d0, 0.0d0, 0.0d0, 0.0d0, 0.0d0, 101.0d0, 101.0d0 /)
54 real*8, dimension(4) :: udua1 = (/ 100.0d0, -93.d0, 101.d0, 0.d0 /)
55 Integer :: i
56 Logical :: error
57
58 Call startup
59!
60! Create and initialize a Control Vector
61!
62 numcallback = coidef_size()
63 Allocate( cntvect(numcallback) )
64 coi_error = coidef_inifort( cntvect )
65!
66! Tell CONOPT about the size of the model by populating the Control Vector:
67!
68! Number of variables. Variable obj i the GAMS model is not included.
69!
70 coi_error = max( coi_error, coidef_numvar( cntvect, 7 ) )
71!
72! Number of equations
73!
74 coi_error = max( coi_error, coidef_numcon( cntvect, 5 ) )
75!
76! Number of nonzeros in the Jacobian.
77!
78 coi_error = max( coi_error, coidef_numnz( cntvect,13 ) )
79!
80! Number of nonlinear nonzeros.
81!
82 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) )
83!
84! Optimization direction is Minimize = -1
85!
86 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) )
87!
88! Objective is constraint 5
89!
90 coi_error = max( coi_error, coidef_objcon( cntvect, 5 ) )
91!
92! Define the options file as 'eq2d.opt'
93!
94 coi_error = max( coi_error, coidef_optfile( cntvect, 'eq2d.opt' ) )
95!
96! Tell CONOPT about the callback routines:
97!
98 coi_error = max( coi_error, coidef_readmatrix( cntvect, eq2d_readmatrix ) )
99 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
100 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
101 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
102 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
103
104#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
105 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
106#endif
107
108 If ( coi_error .ne. 0 ) THEN
109 write(*,*)
110 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
111 write(*,*)
112 call flog( "Skipping Solve due to setup errors", 1 )
113 ENDIF
114 do_allocate = .true.
115!
116! Start CONOPT:
117!
118 coi_error = coi_solve( cntvect )
119
120 write(*,*)
121 write(*,*) 'End of example eq2d'
122
123 If ( coi_error /= 0 ) then
124 call flog( "Errors encountered during solution", 1 )
125 elseif ( stacalls == 0 .or. solcalls == 0 ) then
126 call flog( "Status or Solution routine was not called", 1 )
127 elseif ( sstat /= 1 .or. mstat /= 1 ) then ! This is an LP model
128 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
129 elseif ( abs( obj-65.5d0 ) > 0.000001d0 ) then
130 call flog( "Incorrect objective returned", 1 )
131 Else
132!
133! Check the primal and dual solution itself
134!
135 error = .false.
136 do i = 1, 7
137 if ( abs(xprim(i)-xsol1(i)) > 1.d-7 ) error = .true.
138 if ( abs(xdual(i)-xdua1(i)) > 1.d-7 ) error = .true.
139 enddo
140 do i = 1, 4
141 if ( abs(udual(i)-udua1(i)) > 1.d-7 ) error = .true.
142 enddo
143 if ( error ) call flog( "Eq2d: Numerical solution was not as expected.", 1 )
144 Call checkdual( 'Eq2d', minimize )
145 endif
146
147 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
148
149 call flog( "Successful Solve", 0 )
150
151End Program eq2d
152!
153! ============================================================================
154! Define information about the model:
155!
156
157!> Define information about the model
158!!
159!! @include{doc} readMatrix_params.dox
160Integer Function eq2d_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
161 colsta, rowno, value, nlflag, n, m, nz, usrmem )
162#if defined(itl)
163!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: eq2d_ReadMatrix
164#endif
165 implicit none
166 integer, intent (in) :: n ! number of variables
167 integer, intent (in) :: m ! number of constraints
168 integer, intent (in) :: nz ! number of nonzeros
169 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
170 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
171 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
172 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
173 ! (not defined here)
174 integer, intent (out), dimension(m) :: type ! vector of equation types
175 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
176 ! (not defined here)
177 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
178 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
179 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
180 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
181 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
182 real*8 usrmem(*) ! optional user memory
183!
184! Define information about the Variables:
185!
186! Curr is not defined. We will use the default starting values of
187! zero.
188!
189! By default, we do not define the status argument Vsta.
190!
191! The variables are Positive, i.e. have a lower bound of zero.
192!
193 lower(1) = 0.d0
194 lower(2) = 0.d0
195 lower(3) = 0.d0
196 lower(4) = 0.d0
197 lower(5) = 0.d0
198 lower(6) = 0.d0
199 lower(7) = 0.d0
200!
201! Define information about the Constraints:
202!
203 rhs(1) = 1.0d0
204 type(1) = 0 ! =E=
205 rhs(2) = 2.0d0
206 type(2) = 2 ! =L=
207 rhs(3) = 1.5d0
208 type(3) = 1 ! =G=
209 rhs(4) = 1.5d0
210 type(4) = 2 ! =L=
211 type(5) = 3 ! =N=
212!
213! The Jacobian has to be sorted column-wise so we will just define
214! the elements column by column according to the table above:
215!
216! x1 x2 x3 x4 x5 x6 x7
217! e1 1 1
218! e2 1 1
219! e3 1 -1 -1
220! e4 1 1 1
221! e5 7 8 100
222!
223 colsta(1) = 1
224 rowno(1) = 1
225 value(1) = 1.d0
226 rowno(2) = 2
227 value(2) = 1.d0
228 rowno(3) = 5
229 value(3) = 7.d0
230!
231 colsta(2) = 4
232 rowno(4) = 2
233 value(4) = 1.0d0
234 rowno(5) = 3
235 value(5) = 1.0d0
236 rowno(6) = 4
237 value(6) = 1.0d0
238 rowno(7) = 5
239 value(7) = 8.0d0
240!
241 colsta(3) = 8
242 rowno(8) = 1
243 value(8) = 1.d0
244 rowno(9) = 5
245 value(9) = 100.0d0
246!
247 colsta(4) = 10
248 rowno(10) = 4
249 value(10) = 1.0d0
250!
251 colsta(5) = 11
252 rowno(11) = 4
253 value(11) = 1.d0
254!
255 colsta(6) = 12
256 rowno(12) = 3
257 value(12) = -1.0d0
258!
259 colsta(7) = 13
260 rowno(13) = 3
261 value(13) = -1.d0
262!
263! End of columns, the next free position is = number of elements+1:
264!
265 colsta(8) = 14
266
268
269end Function eq2d_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 eq2d_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition eq2d.f90:162
program eq2d
Main program. A simple setup and call of CONOPT.
Definition eq2d.f90:25
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_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition coistart.f90:629
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_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
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
real *8, dimension(:), pointer udual
Definition comdecl.f90:18
real *8, dimension(:), pointer xdual
Definition comdecl.f90:17
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
real *8, dimension(:), pointer xprim
Definition comdecl.f90:17
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35