25#if defined(_WIN32) && !defined(_WIN64)
26#define dec_directives_win32
30 Integer,
parameter :: Nobs = 14
31 Integer,
parameter :: DimX = 10
32 real*8,
dimension(Nobs,DimX) ::
a,
b
33 real*8,
dimension(Nobs) ::
obs
46 Integer,
save :: seed = 12359
48 seed = mod(seed*1027+25,1048576)
49 rndx = float(seed)/float(1048576)
61 real*8,
Parameter :: xtarg = -1.0
62 real*8,
Parameter :: noise = 1.0
63 Real,
External :: Rndx
71 o = o +
a(i,j) * xtarg +
b(i,j) * xtarg**2
73 obs(i) = o + noise * rndx()
101#ifdef dec_directives_win32
115 INTEGER,
Dimension(:),
Pointer :: cntvect
119 Character(Len=132) :: text
120 real*8 :: obj_run1, obj_run3
141 coi_error = max( coi_error,
coidef_optfile( cntvect,
'leastsq7.opt' ) )
155#if defined(CONOPT_LICENSE_INT_1) && defined(CONOPT_LICENSE_INT_2) && defined(CONOPT_LICENSE_INT_3) && defined(CONOPT_LICENSE_TEXT)
156 coi_error = max( coi_error,
coidef_license( cntvect, conopt_license_int_1, conopt_license_int_2, conopt_license_int_3, conopt_license_text) )
159 If ( coi_error .ne. 0 )
THEN
161 write(*,*)
'**** Fatal Error while loading CONOPT Callback routines.'
163 call flog(
"Skipping Solve due to setup errors", 1 )
175 If ( coi_error /= 0 )
then
176 call flog(
"Run 1: Errors encountered during solution", 1 )
178 call flog(
"Run 1: Status or Solution routine was not called", 1 )
179 elseif ( .not. (
sstat == 1 .and.
mstat == 2 ) )
then
180 call flog(
"Run 1: Solver or Model status was not as expected (1,2)", 1 )
186 write(text,*)
'Run 1: Dual and Primal did not match in constraint',i,
' P=',
xprim(
dimx+i),
' D=',
udual(i)
193 obj_run3 = obj_run1 + 1.0d-3*
udual(1)
201 If ( coi_error /= 0 )
then
202 call flog(
"Run 2: Errors encountered during solution", 1 )
204 call flog(
"Run 2: Status or Solution routine was not called", 1 )
205 elseif ( .not. (
sstat == 1 .and.
mstat == 2 ) )
then
206 call flog(
"Run 2: Solver or Model status was not as expected (1,2)", 1 )
207 elseif ( abs(
obj - obj_run1 ) > 1.d-7 )
then
208 call flog(
"Run 2: Incorrect objective returned", 1 )
212 write(text,*)
'Run 2: Dual and Primal did not match in constraint',i,
' P=',
xprim(
dimx+i),
' D=',
udual(i)
224 write(10,*)
'Actual Objective for run 1=',obj_run1
225 write(10,*)
'Expected Objective for run 3=',obj_run3
227 write(10,*)
'Actual Objective for run 3=',
obj
228 write(10,*)
'Expected Objective for run 3=',obj_run3
229 write(10,*)
'Diffrens Objective for run 3=', abs(obj_run3-
obj)
231 If ( coi_error /= 0 )
then
232 call flog(
"Run 3: Errors encountered during solution", 1 )
234 call flog(
"Run 3: Status or Solution routine was not called", 1 )
235 elseif ( .not. (
sstat == 1 .and.
mstat == 2 ) )
then
236 call flog(
"Run 3: Solver or Model status was not as expected (1,2)", 1 )
237 elseif ( abs(
obj - obj_run3 ) > 1.d-2 )
then
238 call flog(
"Run 3: Incorrect objective returned in run3 ", 1 )
244 write(*,*)
'End of Least Square example 7. Return code=',coi_error
246 if (
coi_free(cntvect) /= 0 )
call flog(
"Error while freeing control vector",1)
248 call flog(
"Successful Solve", 0 )
259Integer Function lsq_readmatrix( lower, curr, upper, vsta, type, rhs, esta, &
260 colsta, rowno, value, nlflag, n, m, nz, &
262#ifdef dec_directives_win32
267 integer,
intent (in) :: n
268 integer,
intent (in) :: m
269 integer,
intent (in) :: nz
270 real*8,
intent (in out),
dimension(n) :: lower
271 real*8,
intent (in out),
dimension(n) :: curr
272 real*8,
intent (in out),
dimension(n) :: upper
273 integer,
intent (in out),
dimension(n) :: vsta
275 integer,
intent (out),
dimension(m) ::
type
276 integer,
intent (in out),
dimension(m) :: esta
278 real*8,
intent (in out),
dimension(m) :: rhs
279 integer,
intent (in out),
dimension(n+1) :: colsta
280 integer,
intent (out),
dimension(nz) :: rowno
281 integer,
intent (in out),
dimension(nz) :: nlflag
282 real*8,
intent (in out),
dimension(nz) ::
value
296 lower(
dimx+i) = -1000.0d0
297 upper(
dimx+i) = +1000.0d0
372Integer Function lsq_fdeval( x, g, jac, rowno, jcnm, mode, ignerr, errcnt, &
373 n, nz, thread, usrmem )
374#ifdef dec_directives_win32
379 integer,
intent (in) :: n
380 integer,
intent (in) :: rowno
381 integer,
intent (in) :: nz
382 real*8,
intent (in),
dimension(n) :: x
383 real*8,
intent (in out) :: g
384 real*8,
intent (in out),
dimension(n) :: jac
385 integer,
intent (in),
dimension(nz) :: jcnm
387 integer,
intent (in) :: mode
389 integer,
intent (in) :: ignerr
391 integer,
intent (in out) :: errcnt
393 integer,
intent (in) :: thread
401 if ( rowno .eq.
nobs+1 )
then
405 if ( mode .eq. 1 .or. mode .eq. 3 )
then
415 if ( mode .eq. 2 .or. mode .eq. 3 )
then
427 if ( mode .eq. 1 .or. mode .eq. 3 )
then
430 s = s +
a(rowno,j)*x(j) +
b(rowno,j)*x(j)**2
437 if ( mode .eq. 2 .or. mode .eq. 3 )
then
439 jac(j) =
a(rowno,j) + 2.d0*
b(rowno,j)*x(j)
451Integer Function lsq_2dlagrstr( HSRW, HSCL, NODRV, N, M, NHESS, UsrMem )
452#ifdef dec_directives_win32
458 Integer,
Intent (IN) :: n, m, nhess
459 Integer,
Intent (IN OUT) :: nodrv
460 Integer,
Dimension(Nhess),
Intent (Out) :: hsrw, hscl
461 real*8,
Intent(IN OUT) :: usrmem(*)
491Integer Function lsq_2dlagrval( X, U, HSRW, HSCL, HSVL, NODRV, N, M, NHESS, UsrMem )
492#ifdef dec_directives_win32
498 Integer,
Intent (IN) :: n, m, nhess
499 Integer,
Intent (IN OUT) :: nodrv
500 real*8,
Dimension(N),
Intent (IN) :: x
501 real*8,
Dimension(M),
Intent (IN) :: u
502 Integer,
Dimension(Nhess),
Intent (In) :: hsrw, hscl
503 real*8,
Dimension(NHess),
Intent (Out) :: hsvl
504 real*8,
Intent(IN OUT) :: usrmem(*)
535Integer Function lsq_option( ncall, rval, ival, lval, usrmem, name )
536#ifdef dec_directives_win32
539 integer ncall, ival, lval
540 character(Len=*) :: name
integer function std_solution(xval, xmar, xbas, xsta, yval, ymar, ybas, ysta, n, m, usrmem)
integer function std_status(modsta, solsta, iter, objval, usrmem)
subroutine checkdual(case, minmax)
integer function std_message(smsg, dmsg, nmsg, llen, usrmem, msgv)
integer function std_errmsg(rowno, colno, posno, msglen, usrmem, msg)
integer(c_int) function coidef_message(cntvect, coi_message)
define callback routine for handling messages returned during the solution process.
integer(c_int) function coidef_solution(cntvect, coi_solution)
define callback routine for returning the final solution values.
integer(c_int) function coidef_status(cntvect, coi_status)
define callback routine for returning the completion status.
integer(c_int) function coidef_readmatrix(cntvect, coi_readmatrix)
define callback routine for providing the matrix data to CONOPT.
integer(c_int) function coidef_errmsg(cntvect, coi_errmsg)
define callback routine for returning error messages for row, column or Jacobian elements.
integer(c_int) function coidef_fdeval(cntvect, coi_fdeval)
define callback routine for performing function and derivative evaluations.
integer(c_int) function coidef_2dlagrstr(cntvect, coi_2dlagrstr)
define callback routine for providing the structure of the second derivatives of the Lagrangian.
integer(c_int) function coidef_optfile(cntvect, optfile)
define callback routine for defining an options file.
integer(c_int) function coidef_option(cntvect, coi_option)
define callback routine for defining runtime options.
integer(c_int) function coidef_2dlagrval(cntvect, coi_2dlagrval)
define callback routine for computing the values of the second derivatives of the Lagrangian.
integer(c_int) function coidef_license(cntvect, licint1, licint2, licint3, licstring)
define the License Information.
integer(c_int) function coidef_numvar(cntvect, numvar)
defines the number of variables in the model.
integer(c_int) function coidef_numcon(cntvect, numcon)
defines the number of constraints in the model.
integer(c_int) function coidef_numnlnz(cntvect, numnlnz)
defines the Number of Nonlinear Nonzeros.
integer(c_int) function coidef_optdir(cntvect, optdir)
defines the Optimization Direction.
integer(c_int) function coidef_numnz(cntvect, numnz)
defines the number of nonzero elements in the Jacobian.
integer(c_int) function coidef_numhess(cntvect, numhess)
defines the Number of Hessian Nonzeros.
integer(c_int) function coidef_objcon(cntvect, objcon)
defines the Objective Constraint.
integer(c_int) function coi_create(cntvect)
initializes CONOPT and creates the control vector.
integer(c_int) function coi_free(cntvect)
frees the control vector.
integer(c_int) function coi_solve(cntvect)
method for starting the solving process of CONOPT.
integer function lsq_readmatrix(lower, curr, upper, vsta, type, rhs, esta, colsta, rowno, value, nlflag, n, m, nz, usrmem)
Define information about the model.
integer function lsq_fdeval(x, g, jac, rowno, jcnm, mode, ignerr, errcnt, n, nz, thread, usrmem)
Compute nonlinear terms and non-constant Jacobian elements.
integer function lsq_2dlagrstr(hsrw, hscl, nodrv, n, m, nhess, usrmem)
Specify the structure of the Lagrangian of the Hessian.
integer function lsq_2dlagrval(x, u, hsrw, hscl, hsvl, nodrv, n, m, nhess, usrmem)
Compute the Lagrangian of the Hessian.
integer function lsq_option(ncall, rval, ival, lval, usrmem, name)
Sets runtime options.
void defdata()
Defines the data for the problem.
float rndx()
Defines a pseudo random number between 0 and 1.
program leastsquare
Main program. A simple setup and call of CONOPT.
real *8, dimension(nobs, dimx) a
real *8, dimension(nobs, dimx) b
real *8, dimension(nobs) obs
real *8, dimension(:), pointer udual
integer, parameter minimize
subroutine flog(msg, code)
real *8, dimension(:), pointer xprim