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