CONOPT
Loading...
Searching...
No Matches
identnl01.f90
Go to the documentation of this file.
1!> @file identnl01.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!!
6!! This is a CONOPT implementation of the GAMS model:
7!! @verbatim
8!! Set i Rows / 1*m/
9!! Set j cols / 1*n/
10!! parameter a(j); a(j) = sqrt(ord(j))
11!! parameter w(i); w(i) = power(-1,ord(i)+1)*sqrt(ord(i));
12!! variable x(j), y(i), obj
13!! positive variable x(j);
14!! equation e(i), objdef;
15!! e(i) .. w(i)*sum(j,a(j)*x(j)) +sqr(y(i)) =E= w(i);
16!! ey(i) .. y(i) =E= 0;
17!! objdef .. obj =E= sum(j,x(j));
18!! model m / all /; solve m using nlp maximizing obj;
19!! @endverbatim
20!!
21!! The model is similar to ident01 but with a nonlinear term, that becomes fixed (0)
22!! after the pre-triangular constraints have been solved.
23!!
24!!
25!! For more information about the individual callbacks, please have a look at the source code.
26
27#if defined(_WIN32) && !defined(_WIN64)
28#define dec_directives_win32
29#endif
30
31!> Main program. A simple setup and call of CONOPT
32!!
33Program identnl01
34
36 Use conopt
37 Use ident
38 implicit None
39!
40! Declare the user callback routines as Integer, External:
41!
42 Integer, External :: ident_readmatrix ! Mandatory Matrix definition routine defined below
43 Integer, External :: ident_fdeval ! Function and Derivative evaluation routine
44 Integer, External :: std_status ! Standard callback for displaying solution status
45 Integer, External :: std_solution ! Standard callback for displaying solution values
46 Integer, External :: std_message ! Standard callback for managing messages
47 Integer, External :: std_errmsg ! Standard callback for managing error messages
48#ifdef dec_directives_win32
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
50!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_FDEval
51!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
52!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
53!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
54!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
55#endif
56!
57! Control vector
58!
59 INTEGER, Dimension(:), Pointer :: cntvect
60 INTEGER :: coi_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 coi_error = max( coi_error, coidef_numvar( cntvect, ncol+nrow ) )
71 coi_error = max( coi_error, coidef_numcon( cntvect, 2*nrow+1 ) )
72 coi_error = max( coi_error, coidef_numnz( cntvect, (nrow+1)*ncol+2*nrow ) )
73 coi_error = max( coi_error, coidef_numnlnz( cntvect, nrow ) )
74 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! maximize
75 coi_error = max( coi_error, coidef_objcon( cntvect, 2*nrow + 1 ) ) ! Objective is last constraint
76 coi_error = max( coi_error, coidef_optfile( cntvect, 'identnl01.opt' ) )
77!
78! Tell CONOPT about the callback routines:
79!
80 coi_error = max( coi_error, coidef_readmatrix( cntvect, ident_readmatrix ) )
81 coi_error = max( coi_error, coidef_fdeval( cntvect, ident_fdeval ) )
82 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
83 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
84 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
85 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
86
87#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
88 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
89#endif
90
91 If ( coi_error .ne. 0 ) THEN
92 write(*,*)
93 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
94 write(*,*)
95 call flog( "Skipping Solve due to setup errors", 1 )
96 ENDIF
97!
98! Save the solution so we can check the duals:
99!
100 do_allocate = .true.
101!
102! Start CONOPT:
103!
104 coi_error = coi_solve( cntvect )
105
106 write(*,*)
107 write(*,*) 'End of Ident example 1. Return code=',coi_error
108
109 If ( coi_error /= 0 ) then
110 call flog( "Errors encountered during solution", 1 )
111 elseif ( stacalls == 0 .or. solcalls == 0 ) then
112 call flog( "Status or Solution routine was not called", 1 )
113 elseif ( .not. ( sstat == 1 .and. mstat == 1 ) ) then
114 call flog( "Solver or Model status was not as expected (1,1)", 1 )
115 elseif ( abs( obj - 1.0d0 ) > 1.d-7 ) then
116 call flog( "Incorrect objective returned", 1 )
117 Else
118 Call checkdual( 'Identnl01', maximize )
119 endif
120
121 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
122
123 call flog( "Successful Solve", 0 )
124
125End Program identnl01
126!
127! ============================================================================
128! Define information about the model:
129!
130
131!> Define information about the model
132!!
133!! @include{doc} readMatrix_params.dox
134Integer Function ident_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
135 colsta, rowno, value, nlflag, n, m, nz, &
136 usrmem )
137#ifdef dec_directives_win32
138!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
139#endif
140 Use ident
141 implicit none
142 integer, intent (in) :: n ! number of variables
143 integer, intent (in) :: m ! number of constraints
144 integer, intent (in) :: nz ! number of nonzeros
145 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
146 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
147 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
148 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
149 ! (not defined here)
150 integer, intent (out), dimension(m) :: type ! vector of equation types
151 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
152 ! (not defined here)
153 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
154 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
155 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
156 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
157 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
158 real*8 usrmem(*) ! optional user memory
159
160 real*8, dimension(Nrow) :: w
161 real*8, dimension(Ncol) :: a
162 Integer :: i, j, k
163!
164! Information about Variables:
165! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
166! Default: the status information in Vsta is not used.
167!
168 do j = 1, ncol
169 lower(j) = 0.0d0
170 a(j) = sqrt(1.0d0*j)
171 enddo
172!
173! Information about Constraints:
174! Default: Rhs = 0
175! Default: the status information in Esta and the function
176! value in FV are not used.
177! Default: Type: There is no default.
178! 0 = Equality,
179! 1 = Greater than or equal,
180! 2 = Less than or equal,
181! 3 = Non binding.
182!
183! Constraints 1 to Nrow:
184! Rhs = w(i) and type Equality
185! Constraints Nrow+1 to 2*Nrow
186! Rhs = 0 and type Equality
187!
188 do i = 1, nrow
189 w(i) = (-1)**(i+1)*sqrt(1.0d0*i);
190 rhs(i) = w(i)
191 type(i) = 0
192 type(nrow+i) = 0
193 enddo
194!
195! Constraint 2*Nrow + 1 (Objective)
196! Rhs = 0 and type Non binding
197!
198 type(2*nrow+1) = 3
199!
200! Information about the Jacobian. CONOPT expects a columnwise
201! representation in Rowno, Value, Nlflag and Colsta.
202!
203! Colsta = Start of column indices (No Defaults):
204! Rowno = Row indices
205! Value = Value of derivative (by default only linear
206! derivatives are used)
207! Nlflag = 0 for linear and 1 for nonlinear derivative
208! (not needed for completely linear models)
209!
210!
211! Indices
212! x(j) y(i)
213! e(i): L=w(i)*a(j) NL
214! ey(i): 1
215! obj: L=1
216! Indices
217!
218 k = 1
219 do j = 1, ncol
220 colsta(j) = k
221 do i = 1, nrow
222 rowno(k) = i
223 nlflag(k) = 0
224 value(k) = w(i)*a(j)
225 k = k + 1
226 enddo
227 rowno(k) = 2*nrow+1
228 nlflag(k) = 0
229 value(k) = 1.0d0
230 k = k + 1
231 enddo
232 do i = 1, nrow
233 j = ncol+i
234 colsta(j) = k
235 rowno(k) = i
236 nlflag(k) = 1 ! Nonlinear
237 k = k + 1
238 rowno(k) = nrow+i
239 nlflag(k) = 0
240 value(k) = +1.0d0
241 k = k + 1
242 enddo
243 colsta(ncol+nrow+1) = k
244
245 ident_readmatrix = 0 ! Return value means OK
246
247end Function ident_readmatrix
248
249
250!> Compute nonlinear terms and non-constant Jacobian elements
251!!
252!! @include{doc} fdeval_params.dox
253Integer Function ident_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
254 n, nz, thread, usrmem )
255#ifdef dec_directives_win32
256!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_FDEval
257#endif
258 Use ident
259 implicit none
260 integer, intent (in) :: n ! number of variables
261 integer, intent (in) :: rowno ! number of the row to be evaluated
262 integer, intent (in) :: nz ! number of nonzeros in this row
263 real*8, intent (in), dimension(n) :: x ! vector of current solution values
264 real*8, intent (in out) :: g ! constraint value
265 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
266 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
267 ! in this row. Ffor information only.
268 integer, intent (in) :: mode ! evaluation mode: 1 = function value
269 ! 2 = derivatives, 3 = both
270 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
271 ! as errcnt is incremented
272 integer, intent (in out) :: errcnt ! error counter to be incremented in case
273 ! of function evaluation errors.
274 integer, intent (in) :: thread
275 real*8 usrmem(*) ! optional user memory
276
277 integer :: i,j
278!
279! Row i, add sqr(x(ncol+i))
280!
281 if ( rowno .le. nrow ) then
282 i = rowno; j = ncol+i
283!
284! Mode = 1 or 3. Function value: sqr(x(ncol+i))
285!
286 if ( mode .eq. 1 .or. mode .eq. 3 ) then
287 g = x(j)*x(j)
288 endif
289!
290! Mode = 2 or 3: Derivative values:
291!
292 if ( mode .eq. 2 .or. mode .eq. 3 ) then
293 jac(j) = 2*x(j)
294 endif
295 ident_fdeval = 0
296 else
297 ident_fdeval = 1 ! Illegal row number
298 endif
299
300end Function ident_fdeval
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(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_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
Definition conopt.f90:1135
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
integer function ident_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition ident01.f90:134
integer function ident_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition ident20.f90:221
program identnl01
Main program. A simple setup and call of CONOPT.
Definition identnl01.f90:35
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer stacalls
Definition comdecl.f90:14
subroutine flog(msg, code)
Definition comdecl.f90:62
logical do_allocate
Definition comdecl.f90:27
integer, parameter maximize
Definition comdecl.f90:31
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41