CONOPT
Loading...
Searching...
No Matches
ident13.f90
Go to the documentation of this file.
1!> @file ident13.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the GAMS model:
6!!
7!!
8!! @verbatim
9!! positive variable x1, x2, x3, x4
10!! variable obj;
11!! equation e1, e2, e3, objdef;
12!! e1.. x2 =G= x1 + x4;
13!! e2.. x2 =L= x1 + x4;
14!! e3.. x1 + 2*x4 =G= 2;
15!! objdef .. obj =E= x2 + x3;
16!! model m / all /;
17!! solve m minimizing obj using lp;
18!! @endverbatim
19!!
20!! e1 and e2 are combined into an equality and becomes post-triangular.
21!!
22!!
23!! For more information about the individual callbacks, please have a look at the source code.
24
25#if defined(_WIN32) && !defined(_WIN64)
26#define dec_directives_win32
27#endif
28
29!> Main program. A simple setup and call of CONOPT
30!!
31Program ident13
32
34 Use conopt
35 implicit None
36!
37! Declare the user callback routines as Integer, External:
38!
39 Integer, External :: ident_readmatrix ! Mandatory Matrix definition routine defined below
40 Integer, External :: std_status ! Standard callback for displaying solution status
41 Integer, External :: std_solution ! Standard callback for displaying solution values
42 Integer, External :: std_message ! Standard callback for managing messages
43 Integer, External :: std_errmsg ! Standard callback for managing error messages
44#ifdef dec_directives_win32
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
48!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
49!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
50#endif
51!
52! Control vector
53!
54 INTEGER, Dimension(:), Pointer :: cntvect
55 INTEGER :: coi_error
56!
57! Create and initialize a Control Vector
58!
59 call startup
60
61 coi_error = coi_create( cntvect )
62!
63! Tell CONOPT about the size of the model by populating the Control Vector:
64!
65 coi_error = max( coi_error, coidef_numvar( cntvect, 5 ) ) ! 5 variables
66 coi_error = max( coi_error, coidef_numcon( cntvect, 4 ) ) ! 4 constraints
67 coi_error = max( coi_error, coidef_numnz( cntvect, 11 ) ) ! 11 nonzeros in the Jacobian
68 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) ) ! 0 of which are nonlinear
69 coi_error = max( coi_error, coidef_optdir( cntvect, -1 ) ) ! Minimize
70 coi_error = max( coi_error, coidef_objvar( cntvect, 5 ) ) ! Objective is variable 3
71 coi_error = max( coi_error, coidef_optfile( cntvect, 'Ident13.opt' ) )
72!
73! Tell CONOPT about the callback routines:
74!
75 coi_error = max( coi_error, coidef_readmatrix( cntvect, ident_readmatrix ) )
76 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
77 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
78 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
79 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
80
81#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
82 coi_error = max( coi_error, coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
83#endif
84
85 If ( coi_error .ne. 0 ) THEN
86 write(*,*)
87 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
88 write(*,*)
89 call flog( "Skipping Solve due to setup errors", 1 )
90 ENDIF
91!
92! Save the solution so we can check the duals:
93!
94 do_allocate = .true.
95!
96! Start CONOPT:
97!
98 coi_error = coi_solve( cntvect )
99
100 write(*,*)
101 write(*,*) 'End of Ident13 example. Return code=',coi_error
102
103 If ( coi_error /= 0 ) then
104 call flog( "Errors encountered during solution", 1 )
105 elseif ( stacalls == 0 .or. solcalls == 0 ) then
106 call flog( "Status or Solution routine was not called", 1 )
107 elseif ( sstat /= 1 .or. mstat /= 1 ) then ! Linear model
108 call flog( "Solver and Model Status was not as expected (1,1)", 1 )
109 elseif ( abs( obj-1.0d0 ) > 0.000001d0 ) then
110 call flog( "Incorrect objective returned", 1 )
111 Else
112 Call checkdual( 'Ident13', minimize )
113 endif
114
115 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
116
117 call flog( "Successful Solve", 0 )
118
119End Program ident13
120!
121! ============================================================================
122! Define information about the model:
123!
124
125!> Define information about the model
126!!
127!! @include{doc} readMatrix_params.dox
128Integer Function ident_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
129 colsta, rowno, value, nlflag, n, m, nz, &
130 usrmem )
131#ifdef dec_directives_win32
132!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
133#endif
134 implicit none
135 integer, intent (in) :: n ! number of variables
136 integer, intent (in) :: m ! number of constraints
137 integer, intent (in) :: nz ! number of nonzeros
138 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
139 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
140 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
141 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
142 ! (not defined here)
143 integer, intent (out), dimension(m) :: type ! vector of equation types
144 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
145 ! (not defined here)
146 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
147 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
148 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
149 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
150 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
151 real*8 usrmem(*) ! optional user memory
152!
153! Information about Variables:
154! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
155! Default: the status information in Vsta is not used.
156!
157 lower(1) = 0.0d0
158 lower(2) = 0.0d0
159 lower(3) = 0.0d0
160 lower(4) = 0.0d0
161!
162! Information about Constraints:
163! Default: Rhs = 0
164! Default: the status information in Esta and the function
165! value in FV are not used.
166! Default: Type: There is no default.
167! 0 = Equality,
168! 1 = Greater than or equal,
169! 2 = Less than or equal,
170! 3 = Non binding.
171!
172!
173 type(1) = 1
174 type(2) = 2;
175 type(3) = 1; rhs(3) = 2.0d0
176 type(4) = 0
177!
178! Information about the Jacobian. CONOPT expects a columnwise
179! representation in Rowno, Value, Nlflag and Colsta.
180!
181! Colsta = Start of column indices (No Defaults):
182! Rowno = Row indices
183! Value = Value of derivative (by default only linear
184! derivatives are used)
185! Nlflag = 0 for linear and 1 for nonlinear derivative
186! (not needed for completely linear models)
187!
188! e1.. x2 =G= x1 + x4;
189! e2.. x2 =L= x1 + x4;
190! e3.. x1 + 2*x4 =G= 2;
191! objdef .. obj =E= x2 + x3;
192! Indices
193! x(1) x(2) x(3) x(4) obj
194! 1: 1 4 8
195! 2: 2 5 9
196! 3: 3 10
197! 4: 6 7 11
198!
199 colsta(1) = 1
200 colsta(2) = 4
201 colsta(3) = 7
202 colsta(4) = 8
203 colsta(5) = 11
204 colsta(6) = 12
205 rowno(1) = 1
206 rowno(2) = 2
207 rowno(3) = 3
208 rowno(4) = 1
209 rowno(5) = 2
210 rowno(6) = 4
211 rowno(7) = 4
212 rowno(8) = 1
213 rowno(9) = 2
214 rowno(10) = 3
215 rowno(11) = 4
216!
217! Nonlinearity Structure: Model is linear and nlfalg is not needed
218!
219! Value (Linear only)
220! x(1) x(2) x(3) x(4)
221! 1: -1 +1 -1
222! 2: -1 +1 -1
223! 3: +1 +2
224! 4: -1 -1 +1
225!
226 value(1) = -1.d0
227 value(2) = -1.d0
228 value(3) = +1.d0
229 value(4) = +1.d0
230 value(5) = +1.d0
231 value(6) = -1.d0
232 value(7) = -1.d0
233 value(8) = -1.d0
234 value(8) = -1.d0
235 value(9) = -1.d0
236 value(10) = +2.d0
237 value(11) = +1.d0
238
239 ident_readmatrix = 0 ! Return value means OK
240
241end Function ident_readmatrix
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_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
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_optfile(cntvect, optfile)
define callback routine for defining an options file.
Definition conopt.f90:928
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 function ident_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition ident01.f90:134
program ident13
Main program. A simple setup and call of CONOPT.
Definition ident13.f90:33
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 mstat
Definition comdecl.f90:17
subroutine startup
Definition comdecl.f90:41