本文整理汇总了C#中convexquadraticmodel类的典型用法代码示例。如果您正苦于以下问题:C# convexquadraticmodel类的具体用法?C# convexquadraticmodel怎么用?C# convexquadraticmodel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
convexquadraticmodel类属于命名空间,在下文中一共展示了convexquadraticmodel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: cqmxtadx2
/*************************************************************************
This subroutine evaluates x'*(0.5*alpha*A+tau*D)*x
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static double cqmxtadx2(convexquadraticmodel s,
double[] x)
{
double result = 0;
int n = 0;
int i = 0;
int j = 0;
n = s.n;
alglib.ap.assert(apserv.isfinitevector(x, n), "CQMEval: X is not finite vector");
result = 0.0;
//
// main quadratic term
//
if( (double)(s.alpha)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
for(j=0; j<=n-1; j++)
{
result = result+s.alpha*0.5*x[i]*s.a[i,j]*x[j];
}
}
}
if( (double)(s.tau)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
result = result+0.5*math.sqr(x[i])*s.tau*s.d[i];
}
}
return result;
}
示例2: evaluates
/*************************************************************************
This subroutine evaluates (0.5*alpha*A+tau*D)*x
Y is automatically resized if needed
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmadx(convexquadraticmodel s,
double[] x,
ref double[] y)
{
int n = 0;
int i = 0;
double v = 0;
int i_ = 0;
n = s.n;
alglib.ap.assert(apserv.isfinitevector(x, n), "CQMEval: X is not finite vector");
apserv.rvectorsetlengthatleast(ref y, n);
//
// main quadratic term
//
for(i=0; i<=n-1; i++)
{
y[i] = 0;
}
if( (double)(s.alpha)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
v = 0.0;
for(i_=0; i_<=n-1;i_++)
{
v += s.a[i,i_]*x[i_];
}
y[i] = y[i]+s.alpha*v;
}
}
if( (double)(s.tau)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
y[i] = y[i]+x[i]*s.tau*s.d[i];
}
}
}
示例3: cqmevalx
/*************************************************************************
This subroutine evaluates model at X. Active constraints are ignored.
It returns:
R - model value
Noise- estimate of the numerical noise in data
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmevalx(convexquadraticmodel s,
double[] x,
ref double r,
ref double noise)
{
int n = 0;
int i = 0;
int j = 0;
double v = 0;
double v2 = 0;
double mxq = 0;
double eps = 0;
r = 0;
noise = 0;
n = s.n;
alglib.ap.assert(apserv.isfinitevector(x, n), "CQMEval: X is not finite vector");
r = 0.0;
noise = 0.0;
eps = 2*math.machineepsilon;
mxq = 0.0;
//
// Main quadratic term.
//
// Noise from the main quadratic term is equal to the
// maximum summand in the term.
//
if( (double)(s.alpha)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
for(j=0; j<=n-1; j++)
{
v = s.alpha*0.5*x[i]*s.a[i,j]*x[j];
r = r+v;
noise = Math.Max(noise, eps*Math.Abs(v));
}
}
}
if( (double)(s.tau)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
v = 0.5*math.sqr(x[i])*s.tau*s.d[i];
r = r+v;
noise = Math.Max(noise, eps*Math.Abs(v));
}
}
//
// secondary quadratic term
//
// Noise from the secondary quadratic term is estimated as follows:
// * noise in qi*x-r[i] is estimated as
// Eps*MXQ = Eps*max(|r[i]|, |q[i,j]*x[j]|)
// * noise in (qi*x-r[i])^2 is estimated as
// NOISE = (|qi*x-r[i]|+Eps*MXQ)^2-(|qi*x-r[i]|)^2
// = Eps*MXQ*(2*|qi*x-r[i]|+Eps*MXQ)
//
if( (double)(s.theta)>(double)(0) )
{
for(i=0; i<=s.k-1; i++)
{
v = 0.0;
mxq = Math.Abs(s.r[i]);
for(j=0; j<=n-1; j++)
{
v2 = s.q[i,j]*x[j];
v = v+v2;
mxq = Math.Max(mxq, Math.Abs(v2));
}
r = r+0.5*s.theta*math.sqr(v-s.r[i]);
noise = Math.Max(noise, eps*mxq*(2*Math.Abs(v-s.r[i])+eps*mxq));
}
}
//
// linear term
//
for(i=0; i<=s.n-1; i++)
{
r = r+x[i]*s.b[i];
noise = Math.Max(noise, eps*Math.Abs(x[i]*s.b[i]));
}
//
// Final update of the noise
//
noise = n*noise;
//.........这里部分代码省略.........
示例4: cqmgradunconstrained
/*************************************************************************
This subroutine evaluates gradient of the model; active constraints are
ignored.
INPUT PARAMETERS:
S - convex model
X - point, array[N]
G - possibly preallocated buffer; resized, if too small
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmgradunconstrained(convexquadraticmodel s,
double[] x,
ref double[] g)
{
int n = 0;
int i = 0;
int j = 0;
double v = 0;
int i_ = 0;
n = s.n;
alglib.ap.assert(apserv.isfinitevector(x, n), "CQMEvalGradUnconstrained: X is not finite vector");
apserv.rvectorsetlengthatleast(ref g, n);
for(i=0; i<=n-1; i++)
{
g[i] = 0;
}
//
// main quadratic term
//
if( (double)(s.alpha)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
v = 0.0;
for(j=0; j<=n-1; j++)
{
v = v+s.alpha*s.a[i,j]*x[j];
}
g[i] = g[i]+v;
}
}
if( (double)(s.tau)>(double)(0) )
{
for(i=0; i<=n-1; i++)
{
g[i] = g[i]+x[i]*s.tau*s.d[i];
}
}
//
// secondary quadratic term
//
if( (double)(s.theta)>(double)(0) )
{
for(i=0; i<=s.k-1; i++)
{
v = 0.0;
for(i_=0; i_<=n-1;i_++)
{
v += s.q[i,i_]*x[i_];
}
v = s.theta*(v-s.r[i]);
for(i_=0; i_<=n-1;i_++)
{
g[i_] = g[i_] + v*s.q[i,i_];
}
}
}
//
// linear term
//
for(i=0; i<=n-1; i++)
{
g[i] = g[i]+s.b[i];
}
}
示例5: failure
/*************************************************************************
Internal function, rebuilds "effective" model subject to constraints.
Returns False on failure (non-SPD main quadratic term)
-- ALGLIB --
Copyright 10.05.2011 by Bochkanov Sergey
*************************************************************************/
private static bool cqmrebuild(convexquadraticmodel s)
{
bool result = new bool();
int n = 0;
int nfree = 0;
int k = 0;
int i = 0;
int j = 0;
int ridx0 = 0;
int ridx1 = 0;
int cidx0 = 0;
int cidx1 = 0;
double v = 0;
int i_ = 0;
if( (double)(s.alpha)==(double)(0) && (double)(s.tau)==(double)(0) )
{
//
// Non-SPD model, quick exit
//
result = false;
return result;
}
result = true;
n = s.n;
k = s.k;
//
// Determine number of free variables.
// Fill TXC - array whose last N-NFree elements store constraints.
//
if( s.isactivesetchanged )
{
s.nfree = 0;
for(i=0; i<=n-1; i++)
{
if( !s.activeset[i] )
{
s.nfree = s.nfree+1;
}
}
j = s.nfree;
for(i=0; i<=n-1; i++)
{
if( s.activeset[i] )
{
s.txc[j] = s.xc[i];
j = j+1;
}
}
}
nfree = s.nfree;
//
// Re-evaluate TQ2/TQ1/TQ0, if needed
//
if( s.isactivesetchanged || s.ismaintermchanged )
{
//
// Handle cases Alpha>0 and Alpha=0 separately:
// * in the first case we have dense matrix
// * in the second one we have diagonal matrix, which can be
// handled more efficiently
//
if( (double)(s.alpha)>(double)(0) )
{
//
// Alpha>0, dense QP
//
// Split variables into two groups - free (F) and constrained (C). Reorder
// variables in such way that free vars come first, constrained are last:
// x = [xf, xc].
//
// Main quadratic term x'*(alpha*A+tau*D)*x now splits into quadratic part,
// linear part and constant part:
// ( alpha*Aff+tau*Df alpha*Afc ) ( xf )
// 0.5*( xf' xc' )*( )*( ) =
// ( alpha*Acf alpha*Acc+tau*Dc ) ( xc )
//
// = 0.5*xf'*(alpha*Aff+tau*Df)*xf + (alpha*Afc*xc)'*xf + 0.5*xc'(alpha*Acc+tau*Dc)*xc
//
// We store these parts into temporary variables:
// * alpha*Aff+tau*Df, alpha*Afc, alpha*Acc+tau*Dc are stored into upper
// triangle of TQ2
// * alpha*Afc*xc is stored into TQ1
// * 0.5*xc'(alpha*Acc+tau*Dc)*xc is stored into TQ0
//
// Below comes first part of the work - generation of TQ2:
// * we pass through rows of A and copy I-th row into upper block (Aff/Afc) or
// lower one (Acf/Acc) of TQ2, depending on presence of X[i] in the active set.
//.........这里部分代码省略.........
示例6: cqminit
/*************************************************************************
This subroutine is used to initialize CQM. By default, empty NxN model is
generated, with Alpha=Lambda=Theta=0.0 and zero b.
Previously allocated buffer variables are reused as much as possible.
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqminit(int n,
convexquadraticmodel s)
{
int i = 0;
s.n = n;
s.k = 0;
s.nfree = n;
s.ecakind = -1;
s.alpha = 0.0;
s.tau = 0.0;
s.theta = 0.0;
s.ismaintermchanged = true;
s.issecondarytermchanged = true;
s.islineartermchanged = true;
s.isactivesetchanged = true;
apserv.bvectorsetlengthatleast(ref s.activeset, n);
apserv.rvectorsetlengthatleast(ref s.xc, n);
apserv.rvectorsetlengthatleast(ref s.eb, n);
apserv.rvectorsetlengthatleast(ref s.tq1, n);
apserv.rvectorsetlengthatleast(ref s.txc, n);
apserv.rvectorsetlengthatleast(ref s.tb, n);
apserv.rvectorsetlengthatleast(ref s.b, s.n);
apserv.rvectorsetlengthatleast(ref s.tk1, s.n);
for(i=0; i<=n-1; i++)
{
s.activeset[i] = false;
s.xc[i] = 0.0;
s.b[i] = 0.0;
}
}
示例7: cqmsetb
/*************************************************************************
This subroutine changes linear term of the model
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmsetb(convexquadraticmodel s,
double[] b)
{
int i = 0;
alglib.ap.assert(apserv.isfinitevector(b, s.n), "CQMSetB: B is not finite vector");
apserv.rvectorsetlengthatleast(ref s.b, s.n);
for(i=0; i<=s.n-1; i++)
{
s.b[i] = b[i];
}
s.islineartermchanged = true;
}
示例8: CQMRebuild
/*************************************************************************
This subroutine calls CQMRebuild() and evaluates model at X subject to
active constraints.
It is intended for debug purposes only, because it evaluates model by
means of temporaries, which were calculated by CQMRebuild(). The only
purpose of this function is to check correctness of CQMRebuild() by
comparing results of this function with ones obtained by CQMEval(), which
is used as reference point. The idea is that significant deviation in
results of these two functions is evidence of some error in the
CQMRebuild().
NOTE: suffix T denotes that temporaries marked by T-prefix are used. There
is one more variant of this function, which uses "effective" model
built by CQMRebuild().
NOTE2: in case CQMRebuild() fails (due to model non-convexity), this
function returns NAN.
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static double cqmdebugconstrainedevalt(convexquadraticmodel s,
double[] x)
{
double result = 0;
int n = 0;
int nfree = 0;
int i = 0;
int j = 0;
double v = 0;
n = s.n;
alglib.ap.assert(apserv.isfinitevector(x, n), "CQMDebugConstrainedEvalT: X is not finite vector");
if( !cqmrebuild(s) )
{
result = Double.NaN;
return result;
}
result = 0.0;
nfree = s.nfree;
//
// Reorder variables
//
j = 0;
for(i=0; i<=n-1; i++)
{
if( !s.activeset[i] )
{
alglib.ap.assert(j<nfree, "CQMDebugConstrainedEvalT: internal error");
s.txc[j] = x[i];
j = j+1;
}
}
//
// TQ2, TQ1, TQ0
//
//
if( (double)(s.alpha)>(double)(0) )
{
//
// Dense TQ2
//
for(i=0; i<=nfree-1; i++)
{
for(j=0; j<=nfree-1; j++)
{
result = result+0.5*s.txc[i]*s.tq2dense[i,j]*s.txc[j];
}
}
}
else
{
//
// Diagonal TQ2
//
for(i=0; i<=nfree-1; i++)
{
result = result+0.5*s.tq2diag[i]*math.sqr(s.txc[i]);
}
}
for(i=0; i<=nfree-1; i++)
{
result = result+s.tq1[i]*s.txc[i];
}
result = result+s.tq0;
//
// TK2, TK1, TK0
//
if( s.k>0 && (double)(s.theta)>(double)(0) )
{
for(i=0; i<=s.k-1; i++)
{
v = 0;
for(j=0; j<=nfree-1; j++)
//.........这里部分代码省略.........
示例9: cqmsetd
/*************************************************************************
This subroutine changes diagonal quadratic term of the model.
INPUT PARAMETERS:
S - model
D - array[N], semidefinite diagonal matrix
Tau - multiplier; when Tau=0, D is not referenced at all
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmsetd(convexquadraticmodel s,
double[] d,
double tau)
{
int i = 0;
alglib.ap.assert(math.isfinite(tau) && (double)(tau)>=(double)(0), "CQMSetD: Tau<0 or is not finite number");
alglib.ap.assert((double)(tau)==(double)(0) || apserv.isfinitevector(d, s.n), "CQMSetD: D is not finite Nx1 vector");
s.tau = tau;
if( (double)(tau)>(double)(0) )
{
apserv.rvectorsetlengthatleast(ref s.d, s.n);
apserv.rvectorsetlengthatleast(ref s.ecadiag, s.n);
apserv.rvectorsetlengthatleast(ref s.tq2diag, s.n);
for(i=0; i<=s.n-1; i++)
{
alglib.ap.assert((double)(d[i])>=(double)(0), "CQMSetD: D[i]<0");
s.d[i] = d[i];
}
}
s.ismaintermchanged = true;
}
示例10: CQMSetA
/*************************************************************************
This subroutine drops main quadratic term A from the model. It is same as
call to CQMSetA() with zero A, but gives better performance because
algorithm knows that matrix is zero and can optimize subsequent
calculations.
INPUT PARAMETERS:
S - model
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmdropa(convexquadraticmodel s)
{
s.alpha = 0.0;
s.ismaintermchanged = true;
}
示例11: cqmrewritedensediagonal
/*************************************************************************
This subroutine rewrites diagonal of the main quadratic term of the model
(dense A) by vector Z/Alpha (current value of the Alpha coefficient is
used).
IMPORTANT: in case model has no dense quadratic term, this function
allocates N*N dense matrix of zeros, and fills its diagonal by
non-zero values.
INPUT PARAMETERS:
S - model
Z - new diagonal, array[N]
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmrewritedensediagonal(convexquadraticmodel s,
double[] z)
{
int n = 0;
int i = 0;
int j = 0;
n = s.n;
if( (double)(s.alpha)==(double)(0) )
{
apserv.rmatrixsetlengthatleast(ref s.a, s.n, s.n);
apserv.rmatrixsetlengthatleast(ref s.ecadense, s.n, s.n);
apserv.rmatrixsetlengthatleast(ref s.tq2dense, s.n, s.n);
for(i=0; i<=n-1; i++)
{
for(j=0; j<=n-1; j++)
{
s.a[i,j] = 0.0;
}
}
s.alpha = 1.0;
}
for(i=0; i<=s.n-1; i++)
{
s.a[i,i] = z[i]/s.alpha;
}
s.ismaintermchanged = true;
}
示例12: cqmgeta
/*************************************************************************
This subroutine changes main quadratic term of the model.
INPUT PARAMETERS:
S - model
A - possibly preallocated buffer
OUTPUT PARAMETERS:
A - NxN matrix, full matrix is returned.
Zero matrix is returned if model is empty.
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmgeta(convexquadraticmodel s,
ref double[,] a)
{
int i = 0;
int j = 0;
double v = 0;
int n = 0;
n = s.n;
apserv.rmatrixsetlengthatleast(ref a, n, n);
if( (double)(s.alpha)>(double)(0) )
{
v = s.alpha;
for(i=0; i<=n-1; i++)
{
for(j=0; j<=n-1; j++)
{
a[i,j] = v*s.a[i,j];
}
}
}
else
{
for(i=0; i<=n-1; i++)
{
for(j=0; j<=n-1; j++)
{
a[i,j] = 0.0;
}
}
}
}
示例13: cqmseta
/*************************************************************************
This subroutine changes main quadratic term of the model.
INPUT PARAMETERS:
S - model
A - NxN matrix, only upper or lower triangle is referenced
IsUpper - True, when matrix is stored in upper triangle
Alpha - multiplier; when Alpha=0, A is not referenced at all
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmseta(convexquadraticmodel s,
double[,] a,
bool isupper,
double alpha)
{
int i = 0;
int j = 0;
double v = 0;
alglib.ap.assert(math.isfinite(alpha) && (double)(alpha)>=(double)(0), "CQMSetA: Alpha<0 or is not finite number");
alglib.ap.assert((double)(alpha)==(double)(0) || apserv.isfinitertrmatrix(a, s.n, isupper), "CQMSetA: A is not finite NxN matrix");
s.alpha = alpha;
if( (double)(alpha)>(double)(0) )
{
apserv.rmatrixsetlengthatleast(ref s.a, s.n, s.n);
apserv.rmatrixsetlengthatleast(ref s.ecadense, s.n, s.n);
apserv.rmatrixsetlengthatleast(ref s.tq2dense, s.n, s.n);
for(i=0; i<=s.n-1; i++)
{
for(j=i; j<=s.n-1; j++)
{
if( isupper )
{
v = a[i,j];
}
else
{
v = a[j,i];
}
s.a[i,j] = v;
s.a[j,i] = v;
}
}
}
s.ismaintermchanged = true;
}
示例14: cqmconstrainedoptimum
/*************************************************************************
This subroutine finds optimum of the model. It returns False on failure
(indefinite/semidefinite matrix). Optimum is found subject to active
constraints.
INPUT PARAMETERS
S - model
X - possibly preallocated buffer; automatically resized, if
too small enough.
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static bool cqmconstrainedoptimum(convexquadraticmodel s,
ref double[] x)
{
bool result = new bool();
int n = 0;
int nfree = 0;
int k = 0;
int i = 0;
double v = 0;
int cidx0 = 0;
int itidx = 0;
int i_ = 0;
//
// Rebuild internal structures
//
if( !cqmrebuild(s) )
{
result = false;
return result;
}
n = s.n;
k = s.k;
nfree = s.nfree;
result = true;
//
// Calculate initial point for the iterative refinement:
// * free components are set to zero
// * constrained components are set to their constrained values
//
apserv.rvectorsetlengthatleast(ref x, n);
for(i=0; i<=n-1; i++)
{
if( s.activeset[i] )
{
x[i] = s.xc[i];
}
else
{
x[i] = 0;
}
}
//
// Iterative refinement.
//
// In an ideal world without numerical errors it would be enough
// to make just one Newton step from initial point:
// x_new = -H^(-1)*grad(x=0)
// However, roundoff errors can significantly deteriorate quality
// of the solution. So we have to recalculate gradient and to
// perform Newton steps several times.
//
// Below we perform fixed number of Newton iterations.
//
for(itidx=0; itidx<=newtonrefinementits-1; itidx++)
{
//
// Calculate gradient at the current point.
// Move free components of the gradient in the beginning.
//
cqmgradunconstrained(s, x, ref s.tmpg);
cidx0 = 0;
for(i=0; i<=n-1; i++)
{
if( !s.activeset[i] )
{
s.tmpg[cidx0] = s.tmpg[i];
cidx0 = cidx0+1;
}
}
//
// Free components of the extrema are calculated in the first NFree elements of TXC.
//
// First, we have to calculate original Newton step, without rank-K perturbations
//
for(i_=0; i_<=nfree-1;i_++)
{
s.txc[i_] = -s.tmpg[i_];
}
cqmsolveea(s, ref s.txc, ref s.tmp0);
//
//.........这里部分代码省略.........
示例15: cqmsetq
/*************************************************************************
This subroutine changes linear term of the model
-- ALGLIB --
Copyright 12.06.2012 by Bochkanov Sergey
*************************************************************************/
public static void cqmsetq(convexquadraticmodel s,
double[,] q,
double[] r,
int k,
double theta)
{
int i = 0;
int j = 0;
alglib.ap.assert(k>=0, "CQMSetQ: K<0");
alglib.ap.assert((k==0 || (double)(theta)==(double)(0)) || apserv.apservisfinitematrix(q, k, s.n), "CQMSetQ: Q is not finite matrix");
alglib.ap.assert((k==0 || (double)(theta)==(double)(0)) || apserv.isfinitevector(r, k), "CQMSetQ: R is not finite vector");
alglib.ap.assert(math.isfinite(theta) && (double)(theta)>=(double)(0), "CQMSetQ: Theta<0 or is not finite number");
//
// degenerate case: K=0 or Theta=0
//
if( k==0 || (double)(theta)==(double)(0) )
{
s.k = 0;
s.theta = 0;
s.issecondarytermchanged = true;
return;
}
//
// General case: both Theta>0 and K>0
//
s.k = k;
s.theta = theta;
apserv.rmatrixsetlengthatleast(ref s.q, s.k, s.n);
apserv.rvectorsetlengthatleast(ref s.r, s.k);
apserv.rmatrixsetlengthatleast(ref s.eq, s.k, s.n);
apserv.rmatrixsetlengthatleast(ref s.eccm, s.k, s.k);
apserv.rmatrixsetlengthatleast(ref s.tk2, s.k, s.n);
for(i=0; i<=s.k-1; i++)
{
for(j=0; j<=s.n-1; j++)
{
s.q[i,j] = q[i,j];
}
s.r[i] = r[i];
}
s.issecondarytermchanged = true;
}