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