コード例 #1
0
ファイル: bsplinebasis.cpp プロジェクト: pmukherj/splinter
// Old implementation of Jacobian
DenseMatrix BSplineBasis::evalBasisJacobianOld(DenseVector &x) const
{
    // Jacobian basis matrix
    DenseMatrix J; J.setZero(getNumBasisFunctions(), numVariables);

    // Calculate partial derivatives
    for (unsigned int i = 0; i < numVariables; i++)
    {
        // One column in basis jacobian
        DenseVector bi; bi.setOnes(1);
        for (unsigned int j = 0; j < numVariables; j++)
        {
            DenseVector temp = bi;
            DenseVector xi;
            if (j == i)
            {
                // Differentiated basis
                xi = bases.at(j).evaluateFirstDerivative(x(j));
            }
            else
            {
                // Normal basis
                xi = bases.at(j).evaluate(x(j));
            }

            bi = kroneckerProduct(temp, xi);
        }

        // Fill out column
        J.block(0,i,bi.rows(),1) = bi.block(0,0,bi.rows(),1);
    }

    return J;
}
コード例 #2
0
double RBFSpline::eval(DenseVector x) const
{
    std::vector<double> y;
    for(int i=0; i<x.rows(); i++)
        y.push_back(x(i));
    return eval(y);
}
コード例 #3
0
DenseVector BilinearRelaxationTest::bilinearFunction(DenseVector x)
{
    assert(x.rows() == 2);
    DenseVector y; y.setZero(1);
    y(0) = x(0)*x(1);
    return y;
}
コード例 #4
0
ファイル: michalewicz.cpp プロジェクト: simudream/censo
DenseVector Michalewicz::michalewiczFunction(DenseVector x)
{
    assert(x.rows() == 2);
    double pi = atan(1)*4;
    DenseVector y; y.setZero(1);
    y(0) = -sin(x(0))*pow(sin(x(0)*x(0)/pi), 20) -sin(x(1))*pow(sin(2*x(1)*x(1)/pi), 20);
    return y;
}
コード例 #5
0
ファイル: bsplinepoly.cpp プロジェクト: simudream/censo
/*
 * Calculate coefficients of B-spline representing a multivariate polynomial
 *
 * The polynomial f(x), with x in R^n, has m terms on the form
 * f(x) = c(0)*x(0)^E(0,0)*x(1)^E(0,1)*...*x(n-1)^E(0,n-1)
 *       +c(1)*x(0)^E(1,0)*x(1)^E(1,1)*...*x(n-1)^E(1,n-1)
 *       +...
 *       +c(m-1)*x(0)^E(m-1,0)*x(1)^E(m-1,1)*...*x(n-1)^E(m-1,n-1)
 * where c in R^m is a vector with coefficients for each of the m terms,
 * and E in N^(mxn) is a matrix with the exponents of each variable in each of the m terms,
 * e.g. the first row of E defines the first term with variable exponents E(0,0) to E(0,n-1).
 *
 * Note: E must be a matrix of nonnegative integers
 */
DenseMatrix getBSplineBasisCoefficients(DenseVector c, DenseMatrix E, std::vector<double> lb, std::vector<double> ub)
{
    unsigned int dim = E.cols();
    unsigned int terms = E.rows();
    assert(dim >= 1); // At least one variable
    assert(terms >= 1); // At least one term (assumes that c is a column vector)
    assert(terms == c.rows());
    assert(dim == lb.size());
    assert(dim == ub.size());

    // Get highest power of each variable
    DenseVector powers = E.colwise().maxCoeff();

    // Store in std vector
    std::vector<unsigned int> powers2;
    for (unsigned int i = 0; i < powers.size(); ++i)
        powers2.push_back(powers(i));

    // Calculate tensor product transformation matrix T
    DenseMatrix T = getTransformationMatrix(powers2, lb, ub);

    // Compute power basis coefficients (lambda vector)
    SparseMatrix L(T.cols(),1);
    L.setZero();

    for (unsigned int i = 0; i < terms; i++)
    {
        SparseMatrix Li(1,1);
        Li.insert(0,0) = 1;

        for (unsigned int j = 0; j < dim; j++)
        {
            int e = E(i,j);
            SparseVector li(powers(j)+1);
            li.reserve(1);
            li.insert(e) = 1;

            SparseMatrix temp = Li;
            Li = kroneckerProduct(temp, li);
        }

        L += c(i)*Li;
    }

    // Compute B-spline coefficients
    DenseMatrix C = T*L;

    return C;
}
コード例 #6
0
ConstraintPtr ConstraintQuadratic::getConvexRelaxation()
{
    if (!constraintConvex)
    {
        // Temp fix
        bool isQuadConvex = false;
        assert(isQuadConvex);
        return this->clone();
        // Temp fix end

        // TODO: this code has not been used or tested in a long while!
        Eigen::EigenSolver<DenseMatrix> es(H);
        DenseVector eigs = es.eigenvalues().real();

        double max_alpha = 0;
        for (int i = 0; i < eigs.rows(); i++)
        {
            cout <<"eig nr " << i << " = " << eigs(i) << endl;
            max_alpha = std::max(max_alpha, -0.5 * eigs(i));
        }

        if (max_alpha != 0)
        {
            cout << "Max alpha = " << max_alpha <<endl;

            std::vector<double> alpha;
            for (unsigned int i = 0; i < variables.size(); i++)
            {
                alpha.push_back(max_alpha);
            }

            cout << "Sour about that negative hessian." <<endl;

            //ConstraintPtr quad_org(this);
            //ConstraintPtr quad_org(this->clone());
            //return new ConstraintDecoratorQuadraticConvexRelaxation(quad_org,alpha);
        }
    }

    // Constraint is convex
    return this->clone(false);
}
コード例 #7
0
ファイル: michalewicz.cpp プロジェクト: bgrimstad/censo
double Michalewicz::michalewiczFunction(DenseVector x, unsigned int m = 10)
{
    assert(x.rows() == 2);
    double pi = atan(1)*4;
    return -sin(x(0))*pow(sin(x(0)*x(0)/pi), 2*m) -sin(x(1))*pow(sin(2*x(1)*x(1)/pi), 2*m);
}
コード例 #8
0
ConstraintQuadratic::ConstraintQuadratic(std::vector<VariablePtr> variables, DenseMatrix A, DenseMatrix b, double c, double lb, double ub)
    : Constraint(variables), A(A), b(b), c(c)
{
    assert(A.cols() == (int)variables.size());
    assert(A.rows() == b.rows());
    assert(b.cols() == 1);

    numConstraints = 1;

    this->lb.push_back(lb);
    this->ub.push_back(ub);

    jacobianCalculated = true;
    hessianCalculated = true;
    constraintLinear = false;
    constraintConvex = false;
    convexRelaxationAvailable = true;

    nnzJacobian = A.rows();
    nnzHessian = 0;

    constraintName = "Constraint Quadratic";

    //    // Check for parameters for NaN
    //    for (int i = 0; i < A.rows(); i++)
    //    {
    //        for (int j = 0; j < A.cols(); j++)
    //        {
    //            bool nanA = false;
    //            if (A(i,j) != A(i,j)) nanA = true;
    //            assert(nanA == false);
    //        }
    //        bool nanb = false;
    //        if (b(i) != b(i)) nanb = true;
    //        assert(nanb == false);
    //    }

    // Calculate and store Hessian
    H = A.transpose() + A;

    // H is symmetric so fill out lower left triangle only
    for (int row = 0; row < H.rows(); row++)
    {
        for (int col = 0; col <= row; col++)
        {
            if (H(row,col) != 0)
            {
                nnzHessian++;
            }
        }
    }

    // Check convexity using Hessian
    Eigen::EigenSolver<DenseMatrix> es(H);
    DenseVector eigs = es.eigenvalues().real();
    double minEigVal = 0;
    for (int i = 0; i < eigs.rows(); i++)
    {
        if (eigs(i) < minEigVal) minEigVal = eigs(i);
    }

    if (minEigVal >= 0 && lb <= -INF)
    {
        constraintConvex = true;
    }
    // Note could also check that max. eigen value <= 0 and ub = INF

    checkConstraintSanity();
}
コード例 #9
0
double sixHumpCamelBack(DenseVector x)
{
    assert(x.rows() == 2);
    return (4 - 2.1*x(0)*x(0) + (1/3.)*x(0)*x(0)*x(0)*x(0))*x(0)*x(0) + x(0)*x(1) + (-4 + 4*x(1)*x(1))*x(1)*x(1);
}