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