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