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