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