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