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