CONOPT
Loading...
Searching...
No Matches
force02.f90
Go to the documentation of this file.
1!> @file force02.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!! This is a CONOPT implementation of the GAMS model:
5!!
6!! @verbatim
7!! set i / i1*i4 /;
8!! variable x, z
9!! positive variable y(i);
10!!
11!! equation pre, force, objdef;
12!! scalar rhs;
13!!
14!! pre .. x =E= rhs;
15!! force .. sum(i, i*y(i) ) - x =L= 0;
16!! objdef .. sum(i, y(i) ) - z =E= 0;
17!!
18!! Model m / all /
19!! set case / c1*c5 /;
20!! parameter rhsc(case) / c1 1.0, c2 5.0e-13, c3 0.0, c4 -5.0e-13, c5 -1 /
21!!
22!! Loop(case,
23!! rhs = rhsc(case);
24!! solve m using lp maximizing z;
25!! );
26!! @endverbatim
27!!
28!! The model status should be
29!! c1 : Feasible (force is not forcing and y(1) = 1)
30!! c2 : Feasible (force is forcing all y are zero)
31!! c3 : Feasible (force is forcing all y are zero)
32!! c4 : Feasible (force is forcing all y are zero)
33!! c5 : Force is infeasible.
34!!
35!! @verbatim
36!! Loop(case,
37!! rhs = rhsc(case);
38!! solve m using lp minimizing z;
39!! );
40!! @endverbatim
41!!
42!! The model status should be
43!! c1 : Feasible (force is not forcing but all y's are still zero)
44!! c2 : Feasible (force is forcing all y are zero)
45!! c3 : Feasible (force is forcing all y are zero)
46!! c4 : Feasible (force is forcing all y are zero)
47!! c5 : Force is infeasible.
48!!
49!!
50!! For more information about the individual callbacks, please have a look at the source code.
51
52#if defined(_WIN32) && !defined(_WIN64)
53#define dec_directives_win32
54#endif
55
56module force02data
57 Integer, Parameter :: MaxCase = 5
58 real*8, Parameter, dimension(MaxCase) :: caserhs = &
59 (/ 1.0d0, 5.0d-13, 0.0d0, -5.0d-13, -1.0d0 /)
60 Integer, Parameter, dimension(MaxCase) :: casemstat = &
61 (/ 1, 1, 1, 1, 4 /)
62 real*8, Parameter, dimension(MaxCase) :: caseobj1 = &
63 (/ 1.0d0, 0.0d0, 0.0d0, 0.0d0, 0.0d0 /)
64 real*8, Parameter, dimension(MaxCase) :: caseobj2 = &
65 (/ 0.0d0, 0.0d0, 0.0d0, 0.0d0, 0.0d0 /)
66 Integer :: casenum
67end module force02data
69!> Main program. A simple setup and call of CONOPT
70!!
71Program force02
72
74 Use conopt
75 Use force02data
76 implicit None
77!
78! Declare the user callback routines as Integer, External:
79!
80 Integer, External :: force_readmatrix ! Mandatory Matrix definition routine defined below
81 Integer, External :: force_fdeval ! Function and Derivative evaluation routine
82 ! needed a nonlinear model.
83 Integer, External :: std_status ! Standard callback for displaying solution status
84 Integer, External :: std_solution ! Standard callback for displaying solution values
85 Integer, External :: std_message ! Standard callback for managing messages
86 Integer, External :: std_errmsg ! Standard callback for managing error messages
87 Integer, External :: std_triord ! Standard callback for Forcengular order
88#ifdef dec_directives_win32
89!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_ReadMatrix
90!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_FDEval
91!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
92!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
93!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
94!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
95!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
96#endif
97!
98! Control vector
99!
100 INTEGER, Dimension(:), Pointer :: cntvect
101 INTEGER :: coi_error
102
103 call startup
104!
105! Create and initialize a Control Vector
106!
107 coi_error = coi_create( cntvect )
108!
109! Tell CONOPT about the size of the model by populating the Control Vector:
110!
111 coi_error = max( coi_error, coidef_numvar( cntvect, 6 ) ) ! # variables
112 coi_error = max( coi_error, coidef_numcon( cntvect, 3 ) ) ! # constraints
113 coi_error = max( coi_error, coidef_numnz( cntvect, 11 ) ) ! # nonzeros in the Jacobian
114 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
115 coi_error = max( coi_error, coidef_optdir( cntvect, 1 ) ) ! Maximize
116 coi_error = max( coi_error, coidef_objvar( cntvect, 6 ) ) ! Objective variable #
117 coi_error = max( coi_error, coidef_optfile( cntvect, 'Force02.opt' ) )
118!
119! Tell CONOPT about the callback routines:
120!
121 coi_error = max( coi_error, coidef_readmatrix( cntvect, force_readmatrix ) )
122 coi_error = max( coi_error, coidef_fdeval( cntvect, force_fdeval ) )
123 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
124 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
125 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
126 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
127 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
128
129#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
130 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
131#endif
132
133 If ( coi_error .ne. 0 ) THEN
134 write(*,*)
135 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
136 write(*,*)
137 call flog( "Skipping Solve due to setup errors", 1 )
138 ENDIF
139!
140! Save the solution so we can check the duals:
141!
142 do_allocate = .true.
143 DO casenum = 1, maxcase
144!
145! Start CONOPT:
146!
147 coi_error = coi_solve( cntvect )
148
149 write(*,*)
150 write(*,*) 'End of Force02 case',casenum,' - Maximize. Return code=',coi_error
151
152 If ( coi_error /= 0 ) then
153 call flog( "Errors encountered during solution", 1 )
154 elseif ( stacalls == 0 .or. solcalls == 0 ) then
155 call flog( "Status or Solution routine was not called", 1 )
156 elseif ( sstat /= 1 .or. mstat /= casemstat(casenum) ) then
157 call flog( "Solver and Model Status was not as expected", 1 )
158 elseif ( mstat == 1 .and. abs( obj-caseobj1(casenum) ) > 0.000001d0 ) then
159 call flog( "Incorrect objective returned", 1 )
160 Elseif ( mstat == 1 ) Then
161 Call checkdual( 'Force02', maximize )
162 Elseif ( mstat == 4 ) Then
163 Call checkdual( 'Force02', infeasible )
164 endif
165 EndDo ! end Casenum loop
166!
167! Change direction of optimization and run the loop again
168!
169 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
170 DO casenum = 1, maxcase
171!
172! Start CONOPT:
173!
174 coi_error = coi_solve( cntvect )
175
176 write(*,*)
177 write(*,*) 'End of Force02 case',casenum,' - Minimize. Return code=',coi_error
178
179 If ( coi_error /= 0 ) then
180 call flog( "Errors encountered during solution", 1 )
181 elseif ( stacalls == 0 .or. solcalls == 0 ) then
182 call flog( "Status or Solution routine was not called", 1 )
183 elseif ( sstat /= 1 .or. mstat /= casemstat(casenum) ) then
184 call flog( "Solver and Model Status was not as expected", 1 )
185 elseif ( mstat == 1 .and. abs( obj-caseobj2(casenum) ) > 0.000001d0 ) then
186 call flog( "Incorrect objective returned", 1 )
187 Elseif ( mstat == 1 ) Then
188 Call checkdual( 'Force02', minimize )
189 Elseif ( mstat == 4 ) Then
190 Call checkdual( 'Force02', infeasible )
191 endif
192 EndDo ! end Casenum loop
193
194 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
195
196 call flog( "Successful Solve", 0 )
198End Program force02
199!
200! ============================================================================
201! Define information about the model:
202!
203
204!> Define information about the model
205!!
206!! @include{doc} readMatrix_params.dox
207Integer Function force_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
208 colsta, rowno, value, nlflag, n, m, nz, &
209 usrmem )
210#ifdef dec_directives_win32
211!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_ReadMatrix
212#endif
213 Use force02data
214 implicit none
215 integer, intent (in) :: n ! number of variables
216 integer, intent (in) :: m ! number of constraints
217 integer, intent (in) :: nz ! number of nonzeros
218 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
219 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
220 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
221 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
222 ! (not defined here)
223 integer, intent (out), dimension(m) :: type ! vector of equation types
224 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
225 ! (not defined here)
226 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
227 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
228 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
229 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
230 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
231 real*8 usrmem(*) ! optional user memory
232!
233! Information about Variables:
234! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
235! Default: the status information in Vsta is not used.
236!
237! The model uses defaults
238!
239! Information about Constraints:
240! Default: Rhs = 0
241! Default: the status information in Esta and the function
242! value in FV are not used.
243! Default: Type: There is no default.
244! 0 = Equality,
245! 1 = Greater than or equal,
246! 2 = Less than or equal,
247! 3 = Non binding.
248!
249! Constraint 1: e1
250! Rhs = CaseRhs and type Equality
251!
252 rhs(1) = caserhs(casenum)
253 type(1) = 0
254!
255! Constraint 2: e2
256! Rhs = 0.0 and type Less than or equal
257!
258 type(2) = 2
259!
260! Constraint 3: e3
261! Rhs = 0.0 and type Equality
262!
263 type(3) = 0
264!
265! Non-default Bounds
266!
267 lower(2) = 0.0d0
268 lower(3) = 0.0d0
269 lower(4) = 0.0d0
270 lower(5) = 0.0d0
271!
272! Information about the Jacobian. CONOPT expects a columnwise
273! representation in Rowno, Value, Nlflag and Colsta.
274!
275! Colsta = Start of column indices (No Defaults):
276! Rowno = Row indices
277! Value = Value of derivative (by default only linear
278! derivatives are used)
279! Nlflag = 0 for linear and 1 for nonlinear derivative
280! (not needed for completely linear models)
281!
282! Indices
283! x(1) x(2) x(3) x(4) x(5) x(6)
284! 1: 1
285! 2: 2 3 5 7 9
286! 3: 4 6 8 10 11
287!
288 colsta(1) = 1
289 colsta(2) = 3
290 colsta(3) = 5
291 colsta(4) = 7
292 colsta(5) = 9
293 colsta(6) = 11
294 colsta(7) = 12
295 rowno(1) = 1
296 rowno(2) = 2
297 rowno(3) = 2
298 rowno(4) = 3
299 rowno(5) = 2
300 rowno(6) = 3
301 rowno(7) = 2
302 rowno(8) = 3
303 rowno(9) = 2
304 rowno(10) = 3
305 rowno(11) = 3
306!
307! Nonlinearity Structure: Model is linear
308!
309!
310! Value (Linear only)
311! x(1) x(2) x(3) x(4) x(5) x(6)
312! 1: 1.0
313! 2: -1.0 1.0 2.0 3.0 4.0
314! 3: 1.0 1.0 1.0 1.0 -1.0
315!
316 value(1) = 1.d0
317 value(2) = -1.d0
318 value(3) = 1.d0
319 value(4) = 1.d0
320 value(5) = 2.d0
321 value(6) = 1.d0
322 value(7) = 3.d0
323 value(8) = 1.d0
324 value(9) = 4.d0
325 value(10) = 1.d0
326 value(11) = -1.d0
327
328 force_readmatrix = 0 ! Return value means OK
329
330end Function force_readmatrix
331!
332!==========================================================================
333! Compute nonlinear terms and non-constant Jacobian elements
334!
335
336!> Compute nonlinear terms and non-constant Jacobian elements
337!!
338!! @include{doc} fdeval_params.dox
339Integer Function force_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
340 n, nz, thread, usrmem )
341#ifdef dec_directives_win32
342!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_FDEval
343#endif
344 implicit none
345 integer, intent (in) :: n ! number of variables
346 integer, intent (in) :: rowno ! number of the row to be evaluated
347 integer, intent (in) :: nz ! number of nonzeros in this row
348 real*8, intent (in), dimension(n) :: x ! vector of current solution values
349 real*8, intent (in out) :: g ! constraint value
350 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
351 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
352 ! in this row. Ffor information only.
353 integer, intent (in) :: mode ! evaluation mode: 1 = function value
354 ! 2 = derivatives, 3 = both
355 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
356 ! as errcnt is incremented
357 integer, intent (in out) :: errcnt ! error counter to be incremented in case
358 ! of function evaluation errors.
359 integer, intent (in) :: thread
360 real*8 usrmem(*) ! optional user memory
361!
362! The model is linear and FDEval should not be called.
363!
364 force_fdeval = 1
365
366end Function force_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_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:289
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function force_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition force01.f90:158
integer function force_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
Definition force01.f90:302
program force02
Main program. A simple setup and call of CONOPT.
Definition force02.f90:73
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_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
Definition conopt.f90:1371
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 casenum
Definition force02.f90:68
real *8, dimension(maxcase), parameter caseobj2
Definition force02.f90:66
real *8, dimension(maxcase), parameter caserhs
Definition force02.f90:60
integer, dimension(maxcase), parameter casemstat
Definition force02.f90:62
integer, parameter maxcase
Definition force02.f90:59
real *8, dimension(maxcase), parameter caseobj1
Definition force02.f90:64
real *8 obj
Definition comdecl.f90:16
integer solcalls
Definition comdecl.f90:15
integer sstat
Definition comdecl.f90:18
integer, parameter infeasible
Definition comdecl.f90:31
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
integer, parameter maximize
Definition comdecl.f90:31
integer mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41