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