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#if defined(_WIN32) && !defined(_WIN64)
48#define dec_directives_win32
49#endif
50
51!> Main program. A simple setup and call of CONOPT
52!!
53Program cns03
54
56 Use conopt
57 implicit None
58!
59! Declare the user callback routines as Integer, External:
60!
61 Integer, External :: cns03_readmatrix ! Mandatory Matrix definition routine defined below
62 Integer, External :: std_status ! Standard callback for displaying solution status
63 Integer, External :: std_solution ! Standard callback for displaying solution values
64 Integer, External :: std_message ! Standard callback for managing messages
65 Integer, External :: std_errmsg ! Standard callback for managing error messages
66#ifdef dec_directives_win32
67!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Cns03_ReadMatrix
68!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
69!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
70!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
71!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
72#endif
73!
74! Control vector
75!
76 INTEGER, Dimension(:), Pointer :: cntvect
77 INTEGER :: coi_error
78
79 Logical :: havesol
80!
81! Create and initialize a Control Vector
82!
83 call startup
84
85 coi_error = coi_create( cntvect )
86!
87! Tell CONOPT about the size of the model by populating the Control Vector:
88!
89 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! # variables
90 coi_error = max( coi_error, coidef_numcon( cntvect, 2 ) ) ! # constraints
91 coi_error = max( coi_error, coidef_numnz( cntvect, 4 ) ) ! # nonzeros in the Jacobian
92 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
93 coi_error = max( coi_error, coidef_square( cntvect, 1 ) ) ! Square system
94 coi_error = max( coi_error, coidef_optfile( cntvect, 'cns03.opt' ) )
95!
96! Tell CONOPT about the callback routines:
97!
98 coi_error = max( coi_error, coidef_readmatrix( cntvect, cns03_readmatrix ) )
99 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
100 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
101 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
102 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
103
104#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
105 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
106#endif
107
108 If ( coi_error .ne. 0 ) THEN
109 write(*,*)
110 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
111 write(*,*)
112 call flog( "Skipping Solve due to setup errors", 1 )
113 ENDIF
114!
115! Save the solution so we can check the duals:
116!
117 do_allocate = .true.
118!
119! Start CONOPT:
120!
121 coi_error = coi_solve( cntvect )
122
123 havesol = .false.
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 ) then
129 call flog( "Solver Status was not 1 as expected.", 1 )
130 else
131 if ( mstat == 5 ) then ! Locally infeasible
132 if ( c_infeas == 0 ) then
133 call flog( "Mstat = 5 and Count of infeasibilities was not positive.", 1 )
134 elseif ( c_nonopt == 0 ) then ! Dependencies are flagged as non-optimal.
135 call flog( "Mstat = 4 and Count of dependencies was not positive.", 1 )
136 endif
137 elseif ( mstat == 17 ) then ! solved singular
138 if ( c_infeas /= 0 ) then
139 call flog( "Mstat = 17 and Count of infeasibilities was not zero.", 1 )
140 elseif ( c_nonopt == 0 ) then ! Dependencies are flagged as non-optimal.
141 call flog( "Mstat = 17 and Count of dependencies was not positive.", 1 )
142 endif
143 havesol = .true.
144 elseif ( mstat == 16 ) then ! Solved
145 havesol = .true.
146 Else
147 call flog( "Model Status was not as expected 5, 16, or 17.", 1 )
148 endif
149 endif
150 if ( havesol ) then
151 if ( abs(xprim(2)+xprim(3)-2.0d0) > 1.d-8 ) then
152 call flog( "Bad levels for x2 + x3.", 1 )
153 else if ( abs(uprim(1)-5.0d0) > 1.d-8 ) then
154 call flog( "Bad e1 level.", 1 )
155 else if ( abs(uprim(2)-7.0d0) > 1.d-8 ) then
156 call flog( "Bad e2 level.", 1 )
157 endif
158 Endif
159
160 write(*,*)
161 write(*,*) 'End of Cns03 example. Return code=',coi_error
162
163 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
164
165 call flog( "Successful Solve", 0 )
166
167End Program cns03
168!
169! ============================================================================
170! Define information about the model:
171!
172
173!> Define information about the model
174!!
175!! @include{doc} readMatrix_params.dox
176Integer Function cns03_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
177 colsta, rowno, value, nlflag, n, m, nz, &
178 usrmem )
179#ifdef dec_directives_win32
180!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Cns03_ReadMatrix
181#endif
182 implicit none
183 integer, intent (in) :: n ! number of variables
184 integer, intent (in) :: m ! number of constraints
185 integer, intent (in) :: nz ! number of nonzeros
186 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
187 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
188 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
189 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
190 ! (not defined here)
191 integer, intent (out), dimension(m) :: type ! vector of equation types
192 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
193 ! (not defined here)
194 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
195 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
196 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
197 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
198 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
199 real*8 usrmem(*) ! optional user memory
200!
201! Information about Variables:
202! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
203! Default: the status information in Vsta is not used.
204!
205 lower(1) = 5.0d0
206 upper(1) = 5.0d0
207 curr(1) = 5.0d0
208!
209! Information about Constraints:
210! Default: Rhs = 0
211! Default: the status information in Esta and the function
212! value in FV are not used.
213! Default: Type: There is no default.
214! 0 = Equality,
215! 1 = Greater than or equal,
216! 2 = Less than or equal,
217! 3 = Non binding.
218!
219 type(1) = 0
220 rhs(1) = 5.0d0
221 type(2) = 0
222 rhs(2) = 7.0d0
223!
224! Information about the Jacobian. CONOPT expects a columnwise
225! representation in Rowno, Value, Nlflag and Colsta.
226!
227! Colsta = Start of column indices (No Defaults):
228! Rowno = Row indices
229! Value = Value of derivative (by default only linear
230! derivatives are used)
231! Nlflag = 0 for linear and 1 for nonlinear derivative
232! (not needed for completely linear models)
233!
234! Indices
235! x(1) x(2) x(3)
236! 1: 1
237! 2: 2 3 4
238!
239 colsta(1) = 1
240 colsta(2) = 3
241 colsta(3) = 4
242 colsta(4) = 5
243 rowno(1) = 1
244 rowno(2) = 2
245 rowno(3) = 2
246 rowno(4) = 2
247!
248! Nonlinearity Structure: L = 0 are linear and NL = 1 are nonlinear
249! Not reported for a linear model
250! x(1) x(2) x(3)
251! 1: L
252! 2: L L L
253!
254! nlflag(1) = 0
255! nlflag(2) = 0
256! nlflag(3) = 0
257! nlflag(4) = 0
258!
259! Value (Linear only)
260! x(1) x(2) x(3)
261! 1: 1
262! 2: 1 1 1
263!
264 value(1) = 1.0d0
265 value(2) = 1.0d0
266 value(3) = 1.0d0
267 value(4) = 1.0d0
268
269 cns03_readmatrix = 0 ! Return value means OK
270
271end 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:181
program cns03
Main program. A simple setup and call of CONOPT.
Definition cns03.f90:55
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
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(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_square(cntvect, square)
square models.
Definition conopt.f90:447
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_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition conopt.f90:144
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
integer c_nonopt
Definition comdecl.f90:21
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer c_infeas
Definition comdecl.f90:20
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
real *8, dimension(:), pointer uprim
Definition comdecl.f90:24
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41