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