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