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