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