CONOPT
Loading...
Searching...
No Matches
ident02.f90
Go to the documentation of this file.
1!> @file ident02.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! This is a CONOPT implementation of the GAMS model:
6!! @verbatim
7!! Set i Rows / 1*m/
8!! Set j cols / 1*n/
9!! parameter a(j); a(j) = sqrt(ord(j))
10!! parameter w(i); w(i) = power(-1,ord(i)+1)*sqrt(ord(i));
11!! variable x(j), obj
12!! positive variable x(j);
13!! equation e(i), objdef;
14!! e(i) .. w(i)*sum(j,a(j)*x(j)) =L= w(i);
15!! objdef .. obj =E= sum(j,x(j));
16!! model m / all /; solve m using nlp maximizing obj;
17!! @endverbatim
18!!
19!! The left hand sides of the constraints are all proportional but they are this time
20!! inequalities. The last two (with the largest size) should be singled out and the last
21!! changed into an equality and the rest should be made redundant.
22!!
23!!
24!! For more information about the individual callbacks, please have a look at the source code.
25
26!> Main program. A simple setup and call of CONOPT
27!!
28Program ident02
29
30 Use proginfo
31 Use coidef
32 Use ident
33 implicit None
34!
35! Declare the user callback routines as Integer, External:
36!
37 Integer, External :: ident_readmatrix ! Mandatory Matrix definition routine defined below
38 Integer, External :: std_status ! Standard callback for displaying solution status
39 Integer, External :: std_solution ! Standard callback for displaying solution values
40 Integer, External :: std_message ! Standard callback for managing messages
41 Integer, External :: std_errmsg ! Standard callback for managing error messages
42#if defined(itl)
43!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
44!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Status
45!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Solution
46!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_Message
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Std_ErrMsg
48#endif
49!
50! Control vector
51!
52 INTEGER :: numcallback
53 INTEGER, Dimension(:), Pointer :: cntvect
54 INTEGER :: coi_error
55
56 call startup
57!
58! Create and initialize a Control Vector
59!
60 numcallback = coidef_size()
61 Allocate( cntvect(numcallback) )
62 coi_error = coidef_inifort( cntvect )
63!
64! Tell CONOPT about the size of the model by populating the Control Vector:
65!
66 coi_error = max( coi_error, coidef_numvar( cntvect, ncol ) )
67 coi_error = max( coi_error, coidef_numcon( cntvect, nrow+1 ) )
68 coi_error = max( coi_error, coidef_numnz( cntvect, (nrow+1)*ncol ) )
69 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) )
70 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! maximize
71 coi_error = max( coi_error, coidef_objcon( cntvect, nrow + 1 ) ) ! Objective is last constraint
72 coi_error = max( coi_error, coidef_optfile( cntvect, 'ident02.opt' ) )
73!
74! Tell CONOPT about the callback routines:
75!
76 coi_error = max( coi_error, coidef_readmatrix( cntvect, ident_readmatrix ) )
77 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
78 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
79 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
80 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
81
82#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
83 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
84#endif
85
86 If ( coi_error .ne. 0 ) THEN
87 write(*,*)
88 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
89 write(*,*)
90 call flog( "Skipping Solve due to setup errors", 1 )
91 ENDIF
92!
93! Save the solution so we can check the duals:
94!
95 do_allocate = .true.
96!
97! Start CONOPT:
98!
99 coi_error = coi_solve( cntvect )
100
101 write(*,*)
102 write(*,*) 'End of Ident example 1. Return code=',coi_error
103
104 If ( coi_error /= 0 ) then
105 call flog( "Errors encountered during solution", 1 )
106 elseif ( stacalls == 0 .or. solcalls == 0 ) then
107 call flog( "Status or Solution routine was not called", 1 )
108 elseif ( .not. ( sstat == 1 .and. mstat == 1 ) ) then
109 call flog( "Solver or Model status was not as expected (1,1)", 1 )
110 elseif ( abs( obj - 1.0d0 ) > 1.d-7 ) then
111 call flog( "Incorrect objective returned", 1 )
112 Else
113 Call checkdual( 'Ident02', maximize )
114 endif
115
116 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
117
118 call flog( "Successful Solve", 0 )
119
120End Program ident02
121!
122! ============================================================================
123! Define information about the model:
124!
125
126!> Define information about the model
127!!
128!! @include{doc} readMatrix_params.dox
129Integer Function ident_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
130 colsta, rowno, value, nlflag, n, m, nz, &
131 usrmem )
132#if defined(itl)
133!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Ident_ReadMatrix
134#endif
135 Use ident
136 implicit none
137 integer, intent (in) :: n ! number of variables
138 integer, intent (in) :: m ! number of constraints
139 integer, intent (in) :: nz ! number of nonzeros
140 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
141 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
142 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
143 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
144 ! (not defined here)
145 integer, intent (out), dimension(m) :: type ! vector of equation types
146 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
147 ! (not defined here)
148 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
149 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
150 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
151 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
152 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
153 real*8 usrmem(*) ! optional user memory
154
155 real*8, dimension(Nrow) :: w
156 real*8, dimension(Ncol) :: a
157 Integer :: i, j, k
158!
159! Information about Variables:
160! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
161! Default: the status information in Vsta is not used.
162!
163 do j = 1, ncol
164 lower(j) = 0.0d0
165 a(j) = sqrt(1.0d0*j)
166 enddo
167!
168! Information about Constraints:
169! Default: Rhs = 0
170! Default: the status information in Esta and the function
171! value in FV are not used.
172! Default: Type: There is no default.
173! 0 = Equality,
174! 1 = Greater than or equal,
175! 2 = Less than or equal,
176! 3 = Non binding.
177!
178! Constraints 1 to Nrow:
179! Rhs = Obs(i) and type Equality
180!
181 do i = 1, nrow
182 w(i) = (-1)**(i+1)*sqrt(1.0d0*i);
183 rhs(i) = w(i)
184 type(i) = 2
185 enddo
186!
187! Constraint Nrow + 1 (Objective)
188! Rhs = 0 and type Non binding
189!
190 type(nrow+1) = 3
191!
192! Information about the Jacobian. We use the standard method with
193! Rowno, Value, Nlflag and Colsta and we do not use Colno.
194!
195! Colsta = Start of column indices (No Defaults):
196! Rowno = Row indices
197! Value = Value of derivative (by default only linear
198! derivatives are used)
199! Nlflag = 0 for linear and 1 for nonlinear derivative
200! (not needed for completely linear models)
201!
202!
203! Indices
204! x(j)
205! i: L=w(i)*a(j)
206! obj: L=1
207!
208 k = 1
209 do j = 1, ncol
210 colsta(j) = k
211 do i = 1, nrow
212 rowno(k) = i
213 nlflag(k) = 0
214 value(k) = w(i)*a(j)
215 k = k + 1
216 enddo
217 rowno(k) = nrow+1
218 value(k) = 1.0d0
219 k = k + 1
220 enddo
221 colsta(ncol+1) = k
222
223 ident_readmatrix = 0 ! Return value means OK
224
225end Function ident_readmatrix
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
Definition comdecl.f90:128
integer function std_status(modsta, solsta, iter, objval, usrmem)
Definition comdecl.f90:82
subroutine checkdual(case, minmax)
Definition comdecl.f90:365
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
Definition comdecl.f90:203
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
Definition comdecl.f90:248
integer function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
Definition coistart.f90:680
integer function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
Definition coistart.f90:358
integer function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
Definition coistart.f90:629
integer function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
Definition coistart.f90:437
integer function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
Definition coistart.f90:552
integer function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
Definition coistart.f90:476
integer function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
Definition coistart.f90:398
integer function coidef_size()
returns the size the Control Vector must have, measured in standard Integer units.
Definition coistart.f90:176
integer function coidef_inifort(cntvect)
initialisation method for Fortran applications.
Definition coistart.f90:314
integer function coi_solve(cntvect)
method for starting the solving process of CONOPT.
Definition coistart.f90:14
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:131
program ident02
Main program. A simple setup and call of CONOPT.
Definition ident02.f90:28
real *8 obj
Definition comdecl.f90:10
integer solcalls
Definition comdecl.f90:9
integer sstat
Definition comdecl.f90:12
integer stacalls
Definition comdecl.f90:8
subroutine flog(msg, code)
Definition comdecl.f90:56
logical do_allocate
Definition comdecl.f90:21
integer, parameter maximize
Definition comdecl.f90:25
integer mstat
Definition comdecl.f90:11
subroutine startup
Definition comdecl.f90:35