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