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 )
155!
156! Free solution memory
157!
158 call finalize
160End Program force01
161!
162! ============================================================================
163! Define information about the model:
164!
165
166!> Define information about the model
167!!
168!! @include{doc} readMatrix_params.dox
169Integer Function force_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
170 colsta, rowno, value, nlflag, n, m, nz, &
171 usrmem )
172#ifdef dec_directives_win32
173!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_ReadMatrix
174#endif
175 implicit none
176 integer, intent (in) :: n ! number of variables
177 integer, intent (in) :: m ! number of constraints
178 integer, intent (in) :: nz ! number of nonzeros
179 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
180 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
181 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
182 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
183 ! (not defined here)
184 integer, intent (out), dimension(m) :: type ! vector of equation types
185 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
186 ! (not defined here)
187 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
188 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
189 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
190 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
191 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
192 real*8 usrmem(*) ! optional user memory
193!
194! Information about Variables:
195! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
196! Default: the status information in Vsta is not used.
197!
198! The model uses defaults
199!
200! Information about Constraints:
201! Default: Rhs = 0
202! Default: the status information in Esta and the function
203! value in FV are not used.
204! Default: Type: There is no default.
205! 0 = Equality,
206! 1 = Greater than or equal,
207! 2 = Less than or equal,
208! 3 = Non binding.
209!
210! Constraint 1: e1
211! Rhs = 2.0 and type Equality
212!
213 rhs(1) = 2.0d0
214 type(1) = 0
215!
216! Constraint 2: e2
217! Rhs = 6.0 and type Equality
218!
219 rhs(2) = 6.0d0
220 type(2) = 0
221!
222! Constraint 3: e3
223! Rhs = 1.0 and type Less than or equal
224!
225 rhs(3) = 1.0d0
226 type(3) = 2
227!
228! Constraint 4: e4
229! Rhs = 1.0 and type Less than or equal
230!
231 rhs(4) = 1.0d0
232 type(4) = 2
233!
234! Constraint 5: e5
235! Rhs = 1.0 and type Less than or equal
236!
237 rhs(5) = 1.0d0
238 type(5) = 2
239!
240! Non-default Bounds
241!
242 lower(3) = 2.0d0
243 upper(3) = 2.0d0
244 lower(4) = 0.0d0
245 lower(5) = 0.0d0
246 lower(6) = 0.0d0
247!
248! Information about the Jacobian. CONOPT expects a columnwise
249! representation in Rowno, Value, Nlflag and Colsta.
250!
251! Colsta = Start of column indices (No Defaults):
252! Rowno = Row indices
253! Value = Value of derivative (by default only linear
254! derivatives are used)
255! Nlflag = 0 for linear and 1 for nonlinear derivative
256! (not needed for completely linear models)
257!
258! Indices
259! x(1) x(2) x(3) x(4) x(5) x(6)
260! 1: 1 3
261! 2: 4 6
262! 3: 7
263! 4: 5 8
264! 5: 2 9 10
265!
266 colsta(1) = 1
267 colsta(2) = 3
268 colsta(3) = 6
269 colsta(4) = 7
270 colsta(5) = 9
271 colsta(6) = 10
272 colsta(7) = 11
273 rowno(1) = 1
274 rowno(2) = 5
275 rowno(3) = 1
276 rowno(4) = 2
277 rowno(5) = 4
278 rowno(6) = 2
279 rowno(7) = 3
280 rowno(8) = 4
281 rowno(9) = 5
282 rowno(10) = 5
283!
284! Nonlinearity Structure: Model is linear
285!
286!
287! Value (Linear only)
288! x(1) x(2) x(3) x(4) x(5) x(6)
289! 1: 1.0 1.0
290! 2: 4.0 1.0
291! 3: 1.0
292! 4: 1.0 1.0
293! 5: 1.0 1.0 1.0
294!
295 value(1) = 1.d0
296 value(2) = 1.d0
297 value(3) = 1.d0
298 value(4) = 4.d0
299 value(5) = 1.d0
300 value(6) = 1.d0
301 value(7) = 1.d0
302 value(8) = 1.d0
303 value(9) = 1.d0
304 value(10) = 1.d0
305
306 force_readmatrix = 0 ! Return value means OK
307
308end Function force_readmatrix
309!
310!==========================================================================
311! Compute nonlinear terms and non-constant Jacobian elements
312!
313
314!> Compute nonlinear terms and non-constant Jacobian elements
315!!
316!! @include{doc} fdeval_params.dox
317Integer Function force_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
318 n, nz, thread, usrmem )
319#ifdef dec_directives_win32
320!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Force_FDEval
321#endif
322 implicit none
323 integer, intent (in) :: n ! number of variables
324 integer, intent (in) :: rowno ! number of the row to be evaluated
325 integer, intent (in) :: nz ! number of nonzeros in this row
326 real*8, intent (in), dimension(n) :: x ! vector of current solution values
327 real*8, intent (in out) :: g ! constraint value
328 real*8, intent (in out), dimension(n) :: jac ! vector of derivatives for current constraint
329 integer, intent (in), dimension(nz) :: jcnm ! list of variables that appear nonlinearly
330 ! in this row. Ffor information only.
331 integer, intent (in) :: mode ! evaluation mode: 1 = function value
332 ! 2 = derivatives, 3 = both
333 integer, intent (in) :: ignerr ! if 1 then errors can be ignored as long
334 ! as errcnt is incremented
335 integer, intent (in out) :: errcnt ! error counter to be incremented in case
336 ! of function evaluation errors.
337 integer, intent (in) :: thread
338 real*8 usrmem(*) ! optional user memory
339!
340! The model is linear and FDEval should not be called.
341!
342 force_fdeval = 1
343
344end Function force_fdeval
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:170
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:126
subroutine checkdual(case, minmax)
Definition comdecl.f90:432
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:243
integer function std_triord(mode, type, status, irow, icol, inf, value, resid, usrmem)
Definition comdecl.f90:327
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:286
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:162
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:306
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
subroutine finalize
Definition comdecl.f90:79
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