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 )
197!
198! Free solution memory
199!
200 call finalize
202End Program force02
203!
204! ============================================================================
205! Define information about the model:
206!
207
208!> Define information about the model
209!!
210!! @include{doc} readMatrix_params.dox
211Integer Function force_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
212 colsta, rowno, value, nlflag, n, m, nz, &
213 usrmem )
214#ifdef dec_directives_win32
215!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_ReadMatrix
216#endif
217 Use force02data
218 implicit none
219 integer, intent (in) :: n ! number of variables
220 integer, intent (in) :: m ! number of constraints
221 integer, intent (in) :: nz ! number of nonzeros
222 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
223 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
224 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
225 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
226 ! (not defined here)
227 integer, intent (out), dimension(m) :: type ! vector of equation types
228 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
229 ! (not defined here)
230 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
231 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
232 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
233 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
234 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
235 real*8 usrmem(*) ! optional user memory
236!
237! Information about Variables:
238! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
239! Default: the status information in Vsta is not used.
240!
241! The model uses defaults
242!
243! Information about Constraints:
244! Default: Rhs = 0
245! Default: the status information in Esta and the function
246! value in FV are not used.
247! Default: Type: There is no default.
248! 0 = Equality,
249! 1 = Greater than or equal,
250! 2 = Less than or equal,
251! 3 = Non binding.
252!
253! Constraint 1: e1
254! Rhs = CaseRhs and type Equality
255!
256 rhs(1) = caserhs(casenum)
257 type(1) = 0
258!
259! Constraint 2: e2
260! Rhs = 0.0 and type Less than or equal
261!
262 type(2) = 2
263!
264! Constraint 3: e3
265! Rhs = 0.0 and type Equality
266!
267 type(3) = 0
268!
269! Non-default Bounds
270!
271 lower(2) = 0.0d0
272 lower(3) = 0.0d0
273 lower(4) = 0.0d0
274 lower(5) = 0.0d0
275!
276! Information about the Jacobian. CONOPT expects a columnwise
277! representation in Rowno, Value, Nlflag and Colsta.
278!
279! Colsta = Start of column indices (No Defaults):
280! Rowno = Row indices
281! Value = Value of derivative (by default only linear
282! derivatives are used)
283! Nlflag = 0 for linear and 1 for nonlinear derivative
284! (not needed for completely linear models)
285!
286! Indices
287! x(1) x(2) x(3) x(4) x(5) x(6)
288! 1: 1
289! 2: 2 3 5 7 9
290! 3: 4 6 8 10 11
291!
292 colsta(1) = 1
293 colsta(2) = 3
294 colsta(3) = 5
295 colsta(4) = 7
296 colsta(5) = 9
297 colsta(6) = 11
298 colsta(7) = 12
299 rowno(1) = 1
300 rowno(2) = 2
301 rowno(3) = 2
302 rowno(4) = 3
303 rowno(5) = 2
304 rowno(6) = 3
305 rowno(7) = 2
306 rowno(8) = 3
307 rowno(9) = 2
308 rowno(10) = 3
309 rowno(11) = 3
310!
311! Nonlinearity Structure: Model is linear
312!
313!
314! Value (Linear only)
315! x(1) x(2) x(3) x(4) x(5) x(6)
316! 1: 1.0
317! 2: -1.0 1.0 2.0 3.0 4.0
318! 3: 1.0 1.0 1.0 1.0 -1.0
319!
320 value(1) = 1.d0
321 value(2) = -1.d0
322 value(3) = 1.d0
323 value(4) = 1.d0
324 value(5) = 2.d0
325 value(6) = 1.d0
326 value(7) = 3.d0
327 value(8) = 1.d0
328 value(9) = 4.d0
329 value(10) = 1.d0
330 value(11) = -1.d0
331
332 force_readmatrix = 0 ! Return value means OK
333
334end Function force_readmatrix
335!
336!==========================================================================
337! Compute nonlinear terms and non-constant Jacobian elements
338!
339
340!> Compute nonlinear terms and non-constant Jacobian elements
341!!
342!! @include{doc} fdeval_params.dox
343Integer Function force_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
344 n, nz, thread, usrmem )
345#ifdef dec_directives_win32
346!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_FDEval
347#endif
348 implicit none
349 integer, intent (in) :: n ! number of variables
350 integer, intent (in) :: rowno ! number of the row to be evaluated
351 integer, intent (in) :: nz ! number of nonzeros in this row
352 real*8, intent (in), dimension(n) :: x ! vector of current solution values
353 real*8, intent (in out) :: g ! constraint value
354 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
355 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
356 ! in this row. Ffor information only.
357 integer, intent (in) :: mode ! evaluation mode: 1 = function value
358 ! 2 = derivatives, 3 = both
359 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
360 ! as errcnt is incremented
361 integer, intent (in out) :: errcnt ! error counter to be incremented in case
362 ! of function evaluation errors.
363 integer, intent (in) :: thread
364 real*8 usrmem(*) ! optional user memory
365!
366! The model is linear and FDEval should not be called.
367!
368 force_fdeval = 1
369
370end Function force_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
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:327
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
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:162
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:306
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
subroutine finalize
Definition comdecl.f90:79
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