CONOPT
Loading...
Searching...
No Matches
cns03.f90
Go to the documentation of this file.
1!> @file cns03.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!! This is a CONOPT implementation of the GAMS model:
5!!
6!! @verbatim
7!! variable x1,x2,x3;
8!! equation e1,e2;
9!!
10!! e1 .. x1 =e= 5;
11!! e2 .. x1 + x2 +x3 =e= 7;
12!!
13!! x1.fx = 5;
14!!
15!! model m / all /;
16!! @endverbatim
17!!
18!! * Case 1: with holdfixed=0, GAMS passes on a square 2x2 model
19!! in x2 and x3 - x1 is ignored since it is fixed
20!! @verbatim
21!! m.holdfixed = 0;
22!! solve m using cns;
23!! abort$(m.solvestat <> 1) 'bad solvestat';
24!! if { (m.modelstat = 5),
25!! * locally infeasible, should report a row that way
26!! abort$(m.numinfes < 1) 'wrong .numinfes';
27!! * this next check may be too ambitious - we'll see how it flies
28!! abort$(m.numdepnd < 1) 'wrong .numdepnd';
29!! elseif (m.modelstat = 17),
30!! * solved singular, should indicate that one dependency exists
31!! havesol = 1;
32!! abort$(m.numdepnd <> 1) 'wrong .numdepnd';
33!! else
34!! havesol = 1;
35!! abort$(m.modelstat <> 16) 'bad modelstat';
36!! };
37!! if {havesol,
38!! abort$(abs(x2.l+x3.l-2) > tol) 'bad x2.l+x3.l';
39!! abort$(abs(e1.l-5) > tol) 'bad e1.l';
40!! abort$(abs(e2.l-7) > tol) 'bad e2.l';
41!! };
42!! @endverbatim
43!!
44!!
45!! For more information about the individual callbacks, please have a look at the source code.
46
47
48
49!> Main program. A simple setup and call of CONOPT
50!!
51Program cns03
52
53 Use proginfo
54 Use coidef
55 implicit None
56!
57! Declare the user callback routines as Integer, External:
58!
59 Integer, External :: cns03_readmatrix ! Mandatory Matrix definition routine defined below
60 Integer, External :: std_status ! Standard callback for displaying solution status
61 Integer, External :: std_solution ! Standard callback for displaying solution values
62 Integer, External :: std_message ! Standard callback for managing messages
63 Integer, External :: std_errmsg ! Standard callback for managing error messages
64#if defined(itl)
65!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Cns03_ReadMatrix
66!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
67!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
68!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
69!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
70#endif
71!
72! Control vector
73!
74 INTEGER :: numcallback
75 INTEGER, Dimension(:), Pointer :: cntvect
76 INTEGER :: coi_error
77
78 Logical :: havesol
79!
80! Create and initialize a Control Vector
81!
82 call startup
83
84 numcallback = coidef_size()
85 Allocate( cntvect(numcallback) )
86 coi_error = coidef_inifort( cntvect )
87!
88! Tell CONOPT about the size of the model by populating the Control Vector:
89!
90 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! # variables
91 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
92 coi_error = max( coi_error, coidef_numnz( cntvect, 4 ) ) ! # nonzeros in the Jacobian
93 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
94 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! Square system
95 coi_error = max( coi_error, coidef_optfile( cntvect, 'cns03.opt' ) )
96!
97! Tell CONOPT about the callback routines:
98!
99 coi_error = max( coi_error, coidef_readmatrix( cntvect, cns03_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(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
106 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, 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!
116! Save the solution so we can check the duals:
117!
118 do_allocate = .true.
119!
120! Start CONOPT:
121!
122 coi_error = coi_solve( cntvect )
123
124 havesol = .false.
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 ) then
130 call flog( "Solver Status was not 1 as expected.", 1 )
131 else
132 if ( mstat == 5 ) then ! Locally infeasible
133 if ( c_infeas == 0 ) then
134 call flog( "Mstat = 5 and Count of infeasibilities was not positive.", 1 )
135 elseif ( c_nonopt == 0 ) then ! Dependencies are flagged as non-optimal.
136 call flog( "Mstat = 4 and Count of dependencies was not positive.", 1 )
137 endif
138 elseif ( mstat == 17 ) then ! solved singular
139 if ( c_infeas /= 0 ) then
140 call flog( "Mstat = 17 and Count of infeasibilities was not zero.", 1 )
141 elseif ( c_nonopt == 0 ) then ! Dependencies are flagged as non-optimal.
142 call flog( "Mstat = 17 and Count of dependencies was not positive.", 1 )
143 endif
144 havesol = .true.
145 elseif ( mstat == 16 ) then ! Solved
146 havesol = .true.
147 Else
148 call flog( "Model Status was not as expected 5, 16, or 17.", 1 )
149 endif
150 endif
151 if ( havesol ) then
152 if ( abs(xprim(2)+xprim(3)-2.0d0) > 1.d-8 ) then
153 call flog( "Bad levels for x2 + x3.", 1 )
154 else if ( abs(uprim(1)-5.0d0) > 1.d-8 ) then
155 call flog( "Bad e1 level.", 1 )
156 else if ( abs(uprim(2)-7.0d0) > 1.d-8 ) then
157 call flog( "Bad e2 level.", 1 )
158 endif
159 Endif
160
161 write(*,*)
162 write(*,*) 'End of Cns03 example. Return code=',coi_error
163
164 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
165
166 call flog( "Successful Solve", 0 )
167
168End Program cns03
169!
170! ============================================================================
171! Define information about the model:
172!
173
174!> Define information about the model
175!!
176!! @include{doc} readMatrix_params.dox
177Integer Function cns03_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
178 colsta, rowno, value, nlflag, n, m, nz, &
179 usrmem )
180#if defined(itl)
181!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Cns03_ReadMatrix
182#endif
183 implicit none
184 integer, intent (in) :: n ! number of variables
185 integer, intent (in) :: m ! number of constraints
186 integer, intent (in) :: nz ! number of nonzeros
187 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
188 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
189 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
190 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
191 ! (not defined here)
192 integer, intent (out), dimension(m) :: type ! vector of equation types
193 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
194 ! (not defined here)
195 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
196 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
197 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
198 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
199 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
200 real*8 usrmem(*) ! optional user memory
201!
202! Information about Variables:
203! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
204! Default: the status information in Vsta is not used.
205!
206 lower(1) = 5.0d0
207 upper(1) = 5.0d0
208 curr(1) = 5.0d0
209!
210! Information about Constraints:
211! Default: Rhs = 0
212! Default: the status information in Esta and the function
213! value in FV are not used.
214! Default: Type: There is no default.
215! 0 = Equality,
216! 1 = Greater than or equal,
217! 2 = Less than or equal,
218! 3 = Non binding.
219!
220 type(1) = 0
221 rhs(1) = 5.0d0
222 type(2) = 0
223 rhs(2) = 7.0d0
224!
225! Information about the Jacobian. We use the standard method with
226! Rowno, Value, Nlflag and Colsta and we do not use Colno.
227!
228! Colsta = Start of column indices (No Defaults):
229! Rowno = Row indices
230! Value = Value of derivative (by default only linear
231! derivatives are used)
232! Nlflag = 0 for linear and 1 for nonlinear derivative
233! (not needed for completely linear models)
234!
235! Indices
236! x(1) x(2) x(3)
237! 1: 1
238! 2: 2 3 4
239!
240 colsta(1) = 1
241 colsta(2) = 3
242 colsta(3) = 4
243 colsta(4) = 5
244 rowno(1) = 1
245 rowno(2) = 2
246 rowno(3) = 2
247 rowno(4) = 2
248!
249! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
250! Not reported for a linear model
251! x(1) x(2) x(3)
252! 1: L
253! 2: L L L
254!
255! nlflag(1) = 0
256! nlflag(2) = 0
257! nlflag(3) = 0
258! nlflag(4) = 0
259!
260! Value (Linear only)
261! x(1) x(2) x(3)
262! 1: 1
263! 2: 1 1 1
264!
265 value(1) = 1.0d0
266 value(2) = 1.0d0
267 value(3) = 1.0d0
268 value(4) = 1.0d0
269
270 cns03_readmatrix = 0 ! Return value means OK
271
272end Function cns03_readmatrix
integer function cns03_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition cns03.f90:180
program cns03
Main program. A simple setup and call of CONOPT.
Definition cns03.f90:51
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_square(cntvect, square)
square models.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
integer c_nonopt
Definition comdecl.f90:15
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer c_infeas
Definition comdecl.f90:14
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
real *8, dimension(:), pointer xprim
Definition comdecl.f90:17
real *8, dimension(:), pointer uprim
Definition comdecl.f90:18
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35