CONOPT
Loading...
Searching...
No Matches
zero01.f90
Go to the documentation of this file.
1!> @file zero01.f90
2!! @ingroup FORT1THREAD_EXAMPLES
3!!
4!!
5!! Zero01. Test model for constraints with coefficient that are identically Zero.
6!!
7!! This is a CONOPT implementation of the GAMS model:
8!! @verbatim
9!! Set i Rows / 1*m/
10!! Set j cols / 1*n/
11!! parameter a(i,j); a(i,j) = 1$(uniform(0,1)>0.5);
12!! variable x(j), obj
13!! positive variable x(j);
14!! equation e(i), objdef;
15!! e(i) .. sum(j,a(i,j)*x(j)) =L= 1;
16!! objdef .. obj =E= sum(j,x(j));
17!! model m / all /; solve m using nlp maximizing obj;
18!! @endverbatim
19!!
20!! All coefficients are added independent of whether they are zero or not.
21!!
22!!
23!! For more information about the individual callbacks, please have a look at the source code.
24
25module zero
26 Integer, parameter :: nrow = 5
27 Integer, parameter :: ncol = 6
28end module zero
29
30!> Main program. A simple setup and call of CONOPT
31!!
32Program zero01
33
34 Use proginfo
35 Use coidef
36 Use zero
37 implicit None
38!
39! Declare the user callback routines as Integer, External:
40!
41 Integer, External :: zero_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#if defined(itl)
47!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Zero_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 :: numcallback
57 INTEGER, Dimension(:), Pointer :: cntvect
58 INTEGER :: coi_error
59
60 call startup
61!
62! Create and initialize a Control Vector
63!
64 numcallback = coidef_size()
65 Allocate( cntvect(numcallback) )
66 coi_error = coidef_inifort( cntvect )
67!
68! Tell CONOPT about the size of the model by populating the Control Vector:
69!
70 coi_error = max( coi_error, coidef_numvar( cntvect, ncol ) )
71 coi_error = max( coi_error, coidef_numcon( cntvect, nrow+1 ) )
72 coi_error = max( coi_error, coidef_numnz( cntvect, (nrow+1)*ncol ) )
73 coi_error = max( coi_error, coidef_numnlnz( cntvect, 0 ) )
74 coi_error = max( coi_error, coidef_optdir( cntvect, +1 ) ) ! maximize
75 coi_error = max( coi_error, coidef_objcon( cntvect, nrow + 1 ) ) ! Objective is last constraint
76 coi_error = max( coi_error, coidef_optfile( cntvect, 'Zero01.opt' ) )
77!
78! Tell CONOPT about the callback routines:
79!
80 coi_error = max( coi_error, coidef_readmatrix( cntvect, zero_readmatrix ) )
81 coi_error = max( coi_error, coidef_status( cntvect, std_status ) )
82 coi_error = max( coi_error, coidef_solution( cntvect, std_solution ) )
83 coi_error = max( coi_error, coidef_message( cntvect, std_message ) )
84 coi_error = max( coi_error, coidef_errmsg( cntvect, std_errmsg ) )
85
86#if defined(LICENSE_INT_1) && defined(LICENSE_INT_2) && defined(LICENSE_INT_3) && defined(LICENSE_TEXT)
87 coi_error = max( coi_error, coidef_license( cntvect, license_int_1, license_int_2, license_int_3, license_text) )
88#endif
89
90 If ( coi_error .ne. 0 ) THEN
91 write(*,*)
92 write(*,*) '**** Fatal Error while loading CONOPT Callback routines.'
93 write(*,*)
94 call flog( "Skipping Solve due to setup errors", 1 )
95 ENDIF
96!
97! Save the solution so we can check the duals:
98!
99 do_allocate = .true.
100!
101! Start CONOPT:
102!
103 coi_error = coi_solve( cntvect )
104
105 write(*,*)
106 write(*,*) 'End of Zero example 1. Return code=',coi_error
107
108 If ( coi_error /= 0 ) then
109 call flog( "Errors encountered during solution", 1 )
110 elseif ( stacalls == 0 .or. solcalls == 0 ) then
111 call flog( "Status or Solution routine was not called", 1 )
112 elseif ( .not. ( sstat == 1 .and. mstat == 1 ) ) then
113 call flog( "Solver or Model status was not as expected (1,1)", 1 )
114 elseif ( obj < 1.0d0-1.d-7 ) then
115 call flog( "Incorrect objective returned", 1 )
116 Else
117 Call checkdual( 'Zero01', maximize )
118 endif
119
120 if ( coi_free(cntvect) /= 0 ) call flog( "Error while freeing control vector",1)
121
122 call flog( "Successful Solve", 0 )
123
124End Program zero01
125
126REAL FUNCTION rndx( )
127!
128! Defines a pseudo random number between 0 and 1
129!
130 IMPLICIT NONE
131
132 Integer, save :: seed = 12359
133
134 seed = mod(seed*1027+25,1048576)
135 rndx = float(seed)/float(1048576)
136
137END FUNCTION rndx
138!
139! ============================================================================
140! Define information about the model:
141!
142
143!> Define information about the model
144!!
145!! @include{doc} readMatrix_params.dox
146Integer Function zero_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
147 colsta, rowno, value, nlflag, n, m, nz, &
148 usrmem )
149#if defined(itl)
150!DEC$ ATTRIBUTES STDCALL, REFERENCE, NOMIXED_STR_LEN_ARG :: Zero_ReadMatrix
151#endif
152 Use zero
153 implicit none
154 integer, intent (in) :: n ! number of variables
155 integer, intent (in) :: m ! number of constraints
156 integer, intent (in) :: nz ! number of nonzeros
157 real*8, intent (in out), dimension(n) :: lower ! vector of lower bounds
158 real*8, intent (in out), dimension(n) :: curr ! vector of initial values
159 real*8, intent (in out), dimension(n) :: upper ! vector of upper bounds
160 integer, intent (in out), dimension(n) :: vsta ! vector of initial variable status
161 ! (not defined here)
162 integer, intent (out), dimension(m) :: type ! vector of equation types
163 integer, intent (in out), dimension(m) :: esta ! vector of initial equation status
164 ! (not defined here)
165 real*8, intent (in out), dimension(m) :: rhs ! vector of right hand sides
166 integer, intent (in out), dimension(n+1) :: colsta ! vector with start of column indices
167 integer, intent (out), dimension(nz) :: rowno ! vector of row numbers
168 integer, intent (in out), dimension(nz) :: nlflag ! vector of nonlinearity flags
169 real*8, intent (in out), dimension(nz) :: value ! vector of matrix values
170 real*8 usrmem(*) ! optional user memory
171
172 real rndx
173 Integer :: i, j, k
174!
175! Information about Variables:
176! Default: Lower = -Inf, Curr = 0, and Upper = +inf.
177! Default: the status information in Vsta is not used.
178!
179 do j = 1, ncol
180 lower(j) = 0.0d0
181 enddo
182!
183! Information about Constraints:
184! Default: Rhs = 0
185! Default: the status information in Esta and the function
186! value in FV are not used.
187! Default: Type: There is no default.
188! 0 = Equality,
189! 1 = Greater than or equal,
190! 2 = Less than or equal,
191! 3 = Non binding.
192!
193! Constraints 1 to Nrow:
194! Rhs = Obs(i) and type Equality
195!
196 do i = 1, nrow
197 rhs(i) = 1.0d0
198 type(i) = 2
199 enddo
200!
201! Constraint Nrow + 1 (Objective)
202! Rhs = 0 and type Non binding
203!
204 type(nrow+1) = 3
205!
206! Information about the Jacobian. We use the standard method with
207! Rowno, Value, Nlflag and Colsta and we do not use Colno.
208!
209! Colsta = Start of column indices (No Defaults):
210! Rowno = Row indices
211! Value = Value of derivative (by default only linear
212! derivatives are used)
213! Nlflag = 0 for linear and 1 for nonlinear derivative
214! (not needed for completely linear models)
215!
216!
217! Indices
218! x(j)
219! i: L=w(i)*a(j)
220! obj: L=1
221!
222 k = 1
223 do j = 1, ncol
224 colsta(j) = k
225 do i = 1, nrow
226 rowno(k) = i
227 nlflag(k) = 0
228 If ( rndx() > 0.5 ) then
229 value(k) = 1.0d0
230 else
231 value(k) = 0.0d0
232 endif
233 k = k + 1
234 enddo
235 rowno(k) = nrow+1
236 value(k) = 1.0d0
237 k = k + 1
238 enddo
239 colsta(ncol+1) = k
240
241 zero_readmatrix = 0 ! Return value means OK
242
243end Function zero_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
float rndx()
Defines a pseudo random number between 0 and 1.
Definition leastsq.c:23
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
integer, parameter ncol
Definition zero01.f90:27
integer, parameter nrow
Definition zero01.f90:26
integer function zero_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
Definition zero01.f90:149
program zero01
Main program. A simple setup and call of CONOPT.
Definition zero01.f90:32