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