Esempio n. 1
0
ConvexObjectivePtr CostFromErrFunc::convex(const vector<double>& xin, Model* model) {
    VectorXd x = getVec(xin, vars_);
    MatrixXd jac = (dfdx_) ? dfdx_->call(x) : calcForwardNumJac(*f_, x, epsilon_);
    ConvexObjectivePtr out(new ConvexObjective(model));
    VectorXd y = f_->call(x);
    for (int i=0; i < jac.rows(); ++i) {
        AffExpr aff = affFromValGrad(y[i], x, jac.row(i), vars_);
        if (coeffs_.size()>0) {
            exprScale(aff, coeffs_[i]);
            if (coeffs_[i] == 0) continue;
        }
        switch (pen_type_) {
        case SQUARED:
            out->addQuadExpr(exprSquare(aff));
            break;
        case ABS:
            out->addAbs(aff, 1);
            break;
        case HINGE:
            out->addHinge(aff, 1);
            break;
        default:
            assert(0 && "unreachable");
        }
    }
    return out;
}
Esempio n. 2
0
 RotationContinuityQuadraticCost::RotationContinuityQuadraticCost(const VarVector& vars, double coeff, NeedleProblemHelperPtr helper) : Cost("RotationContinuity"), vars(vars), coeff(coeff), helper(helper)
 {
   for (int i = 1; i < vars.size(); ++i)
   {
     exprInc(expr, exprMult(exprSquare(exprSub(AffExpr(vars[i]), AffExpr(vars[i-1]))), coeff));
   }
 }
Esempio n. 3
0
JointVelCost::JointVelCost(const VarArray& vars, const VectorXd& coeffs) :
    Cost("JointVel"), vars_(vars), coeffs_(coeffs) {
  for (int i=0; i < vars.rows()-1; ++i) {
    for (int j=0; j < vars.cols(); ++j) {
      AffExpr vel;
      exprInc(vel, exprMult(vars(i,j), -1));
      exprInc(vel, exprMult(vars(i+1,j), 1));
      exprInc(expr_, exprMult(exprSquare(vel),coeffs_[j]));
    }
  }
}
Esempio n. 4
0
JointAccCost::JointAccCost(const VarArray& vars, const VectorXd& coeffs) :
    Cost("JointAcc"), vars_(vars), coeffs_(coeffs) {
  for (int i=0; i < vars.rows()-2; ++i) {
    for (int j=0; j < vars.cols(); ++j) {
      AffExpr acc;
      exprInc(acc, exprMult(vars(i,j), -1));
      exprInc(acc, exprMult(vars(i+1,j), 2));
      exprInc(acc, exprMult(vars(i+2,j), -1));
      exprInc(expr_, exprMult(exprSquare(acc), coeffs_[j]));
    }
  }
}
Esempio n. 5
0
ConvexObjectivePtr CostFromNumDiffErr::convex(const vector<double>& xin, Model* model) {
    VectorXd x = getVec(xin, vars_);
    MatrixXd jac = calcForwardNumJac(*f_, x, epsilon_);
    ConvexObjectivePtr out(new ConvexObjective(model));
    VectorXd y = f_->call(x);
    for (int i=0; i < jac.rows(); ++i) {
        if (coeffs_[i] > 0) {
            AffExpr aff;
            aff.constant = y[i] - jac.row(i).dot(x);
            aff.coeffs = toDblVec(jac.row(i));
            aff.vars = vars_;
            aff = cleanupAff(aff);
            if (pen_type_ == SQUARED) {
                out->addQuadExpr(exprMult(exprSquare(aff), coeffs_[i]));
            }
            else {
                out->addAbs(aff, coeffs_[i]);
            }
        }
    }
    return out;
}
Esempio n. 6
0
 RotationQuadraticCost::RotationQuadraticCost(const VarVector& vars, double coeff, NeedleProblemHelperPtr helper) : Cost("Rotation"), vars(vars), coeff(coeff), helper(helper) {
   for (int i = 0; i < vars.size(); ++i) {
     exprInc(expr, exprMult(exprSquare(vars[i]), coeff));
   }
 }