CONOPT
Loading...
Searching...
No Matches
force05.f90
Go to the documentation of this file.
1!> @file force05.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the GAMS model:
6!!
7!! @verbatim
8!! variable x1, x2, x3;
9!! equation e1, e2, e3, e4;
10!!
11!! e1 .. -x1 + x2 =G= 0;
12!! e2 .. x2 =L= 1;
13!! e3 .. -x1 =L= -1;
14!! e4 .. x1 + x2 - x3 =E= 0;
15!! model m / all /;
16!! solve m using Lp minimizing x3;
17!! solve m using Lp maximizing x3;
18!! @endverbatim
19!!
20!!
21!! For more information about the individual callbacks, please have a look at the source code.
22
23!> Main program. A simple setup and call of CONOPT
24!!
25Program force05
26
27 Use proginfo
28 Use coidef
29 implicit None
30!
31! Declare the user callback routines as Integer, External:
32!
33 Integer, External :: force_readmatrix ! Mandatory Matrix definition routine defined below
34 Integer, External :: force_fdeval ! Function and Derivative evaluation routine
35 ! needed a nonlinear model.
36 Integer, External :: std_status ! Standard callback for displaying solution status
37 Integer, External :: std_solution ! Standard callback for displaying solution values
38 Integer, External :: std_message ! Standard callback for managing messages
39 Integer, External :: std_errmsg ! Standard callback for managing error messages
40 Integer, External :: std_triord ! Standard callback for Forcengular order
41#if defined(itl)
42!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_ReadMatrix
43!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_FDEval
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_TriOrd
49#endif
50!
51! Control vector
52!
53 INTEGER :: numcallback
54 INTEGER, Dimension(:), Pointer :: cntvect
55 INTEGER :: coi_error
56
57 call startup
58!
59! Create and initialize a Control Vector
60!
61 numcallback = coidef_size()
62 Allocate( cntvect(numcallback) )
63 coi_error = coidef_inifort( cntvect )
64!
65! Tell CONOPT about the size of the model by populating the Control Vector:
66!
67 coi_error = max( coi_error, coidef_numvar( cntvect, 3 ) ) ! # variables
68 coi_error = max( coi_error, coidef_numcon( cntvect, 4 ) ) ! # constraints
69 coi_error = max( coi_error, coidef_numnz( cntvect, 7 ) ) ! # nonzeros in the Jacobian
70 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! # of which are nonlinear
71 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
72 coi_error = max( coi_error, coidef_objvar( cntvect, 3 ) ) ! Objective variable #
73 coi_error = max( coi_error, coidef_optfile( cntvect, 'Force05.opt' ) )
74!
75! Tell CONOPT about the callback routines:
76!
77 coi_error = max( coi_error, coidef_readmatrix( cntvect, force_readmatrix ) )
78 coi_error = max( coi_error, coidef_fdeval( cntvect, force_fdeval ) )
79 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
80 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
81 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
82 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
83 coi_error = max( coi_error, coidef_triord( cntvect, std_triord ) )
84
85#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
86 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
87#endif
88
89 If ( coi_error .ne. 0 ) THEN
90 write(*,*)
91 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
92 write(*,*)
93 call flog( "Skipping Solve due to setup errors", 1 )
94 ENDIF
95!
96! Save the solution so we can check the duals:
97!
98 do_allocate = .true.
99!
100! Start CONOPT:
101!
102 coi_error = coi_solve( cntvect )
103
104 write(*,*)
105 write(*,*) 'End of Force05 example Minimize. Return code=',coi_error
106
107 If ( coi_error /= 0 ) then
108 call flog( "Errors encountered during solution", 1 )
109 elseif ( stacalls == 0 .or. solcalls == 0 ) then
110 call flog( "Status or Solution routine was not called", 1 )
111 elseif ( sstat /= 1 .or. mstat /= 1 ) then
112 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
113 elseif ( abs( obj-2.0d0 ) > 0.000001d0 ) then
114 call flog( "Incorrect objective returned", 1 )
115 Else
116 Call checkdual( 'Force05', minimize )
117 endif
118!
119! Repeat with Maximize
120!
121 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! Maximize
122 coi_error = coi_solve( cntvect )
123
124 write(*,*)
125 write(*,*) 'End of Force05 example Maximize. Return code=',coi_error
126
127 If ( coi_error /= 0 ) then
128 call flog( "Errors encountered during solution", 1 )
129 elseif ( stacalls == 0 .or. solcalls == 0 ) then
130 call flog( "Status or Solution routine was not called", 1 )
131 elseif ( sstat /= 1 .or. mstat /= 1 ) then
132 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
133 elseif ( abs( obj-2.0d0 ) > 0.000001d0 ) then
134 call flog( "Incorrect objective returned", 1 )
135 Else
136 Call checkdual( 'Force05', maximize )
137 endif
138
139 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
140
141 call flog( "Successful Solve", 0 )
142
143End Program force05
144!
145! ============================================================================
146! Define information about the model:
147!
148
149!> Define information about the model
150!!
151!! @include{doc} readMatrix_params.dox
152Integer Function force_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
153 colsta, rowno, value, nlflag, n, m, nz, &
154 usrmem )
155#if defined(itl)
156!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_ReadMatrix
157#endif
158 implicit none
159 integer, intent (in) :: n ! number of variables
160 integer, intent (in) :: m ! number of constraints
161 integer, intent (in) :: nz ! number of nonzeros
162 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
163 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
164 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
165 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
166 ! (not defined here)
167 integer, intent (out), dimension(m) :: type ! vector of equation types
168 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
169 ! (not defined here)
170 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
171 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
172 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
173 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
174 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
175 real*8 usrmem(*) ! optional user memory
176!
177! Information about Variables:
178! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
179! Default: the status information in Vsta is not used.
180!
181! The model uses defaults
182!
183! Information about Constraints:
184! Default: Rhs = 0
185! Default: the status information in Esta and the function
186! value in FV are not used.
187! Default: Type: There is no default.
188! 0 = Equality,
189! 1 = Greater than or equal,
190! 2 = Less than or equal,
191! 3 = Non binding.
192!
193!
194! Constraint 1: e1
195! Rhs = 0.0 and type Greater than or equal
196!
197 type(1) = 1
198! Constraint 2: e2
199! Rhs = 1.0 and type Less than or equal
200!
201 rhs(2) = 1.0d0
202 type(2) = 2
203!
204! Constraint 3: e3
205! Rhs = -1.0 and type Less than or equal
206!
207 rhs(3) = -1.0d0
208 type(3) = 2
209!
210! Constraint 4: e4
211! Rhs = 0.0 and type Equality
212!
213 type(4) = 0
214!
215! Non-default Bounds
216!
217! Information about the Jacobian. We use the standard method with
218! Rowno, Value, Nlflag and Colsta and we do not use Colno.
219!
220! Colsta = Start of column indices (No Defaults):
221! Rowno = Row indices
222! Value = Value of derivative (by default only linear
223! derivatives are used)
224! Nlflag = 0 for linear and 1 for nonlinear derivative
225! (not needed for completely linear models)
226!
227! Indices
228! x(1) x(2) x(3)
229! 1: 1 4
230! 2: 5
231! 3: 2
232! 4: 3 6 7
233!
234 colsta(1) = 1
235 colsta(2) = 4
236 colsta(3) = 7
237 colsta(4) = 8
238 rowno(1) = 1
239 rowno(2) = 3
240 rowno(3) = 4
241 rowno(4) = 1
242 rowno(5) = 2
243 rowno(6) = 4
244 rowno(7) = 4
245!
246! Nonlinearity Structure: Model is linear
247!
248!
249! Value (Linear only)
250! x(1) x(2) x(3)
251! 1: -1.0 1.0
252! 2: 1.0
253! 3: -1.0
254! 4: 1.0 1.0 -1.0
255!
256 value(1) = -1.d0
257 value(2) = -1.d0
258 value(3) = 1.d0
259 value(4) = 1.d0
260 value(5) = 1.d0
261 value(6) = 1.d0
262 value(7) = -1.d0
263
264 force_readmatrix = 0 ! Return value means OK
265
266end Function force_readmatrix
267!
268!==========================================================================
269! Compute nonlinear terms and non-constant Jacobian elements
270!
271
272!> Compute nonlinear terms and non-constant Jacobian elements
273!!
274!! @include{doc} fdeval_params.dox
275Integer Function force_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
276 n, nz, thread, usrmem )
277#if defined(itl)
278!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_FDEval
279#endif
280 implicit none
281 integer, intent (in) :: n ! number of variables
282 integer, intent (in) :: rowno ! number of the row to be evaluated
283 integer, intent (in) :: nz ! number of nonzeros in this row
284 real*8, intent (in), dimension(n) :: x ! vector of current solution values
285 real*8, intent (in out) :: g ! constraint value
286 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
287 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
288 ! in this row. Ffor information only.
289 integer, intent (in) :: mode ! evaluation mode: 1 = function value
290 ! 2 = derivatives, 3 = both
291 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
292 ! as errcnt is incremented
293 integer, intent (in out) :: errcnt ! error counter to be incremented in case
294 ! of function evaluation errors.
295 integer, intent (in) :: thread
296 real*8 usrmem(*) ! optional user memory
297!
298! The model is linear and FDEval should not be called.
299!
300 force_fdeval = 1
301
302end Function force_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
subroutine checkdual(case, minmax)
Definition comdecl.f90:365
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:291
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:167
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:314
program force05
Main program. A simple setup and call of CONOPT.
Definition force05.f90:25
integer function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_triord(cntvect, coi_triord)
define callback routine for providing the triangular order information.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition coistart.f90:552
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_objvar(cntvect, objvar)
defines the Objective Variable.
Definition coistart.f90:586
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
real *8 obj
Definition comdecl.f90:10
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer, parameter minimize
Definition comdecl.f90:25
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
integer, parameter maximize
Definition comdecl.f90:25
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35