Search in sources :

Example 31 with VectorNd

use of maspack.matrix.VectorNd in project artisynth_core by artisynth.

the class DynamicRegularizationTerm method applyMapping.

/*
    * Apply a monotonic decreasing function that maps any real value to [0,1]
    * Returns a set of weights that penalizes low activations 
    */
// TODO multiple maps: exponential (softmax), squared, higher powers?
private VectorNd applyMapping(VectorNd x) {
    double[] w = new double[x.size()];
    double sum = 0;
    int n = w.length;
    switch(mapping) {
        case EXPONENTIAL:
            // System.out.println("Applying exponential mapping");
            double min = x.minElement();
            for (int i = 0; i < n; i++) {
                // w(i) > 0 for numeric precision of exponential
                w[i] = x.get(i) - min;
                w[i] = Math.exp(-w[i] * param * 1e-6);
                sum += w[i];
            }
            break;
        case MONOMIAL:
            // System.out.println("Applying monomial mapping");
            double max = x.maxElement();
            for (int i = 0; i < n; i++) {
                w[i] = Math.pow(-x.get(i) + max, param);
                sum += w[i];
            }
            break;
        case SIGMOID:
            // System.out.println("Applying sigmoid mapping");
            VectorNd sorted = new VectorNd();
            sorted.sort(x);
            /*
             * This part is a little hacky.. trying to position
             * the transition point of the sigmoid function at
             * some sort of median location. 
             */
            double l = sorted.get((int) Math.floor(sigmoidMean * (n - 1)));
            double h = sorted.get((int) Math.ceil(sigmoidMean * (n - 1)));
            double frac = sigmoidMean * (n - 1) - Math.floor(sigmoidMean * (n - 1));
            double mean = l + frac * (h - l);
            for (int i = 0; i < n; i++) {
                w[i] = 1 / (1 + Math.exp(param * (x.get(i) - mean)));
                sum += w[i];
            }
    }
    if (isNormalized) {
        for (int i = 0; i < n; i++) {
            w[i] *= n / sum;
        }
    }
    return new VectorNd(w);
}
Also used : VectorNd(maspack.matrix.VectorNd)

Example 32 with VectorNd

use of maspack.matrix.VectorNd in project artisynth_core by artisynth.

the class MotionTargetTerm method getTerm.

/**
 * Fills <code>H</code> and <code>b</code> with this motion term
 * @param H LHS matrix to fill
 * @param b RHS vector to fill
 * @param rowoff row offset to start filling term
 * @param t0 starting time of time step
 * @param t1 ending time of time step
 * @return next row offset
 */
public int getTerm(MatrixNd H, VectorNd b, int rowoff, double t0, double t1) {
    double h = TimeBase.round(t1 - t0);
    // set myTargetVel
    updateTarget(t0, t1);
    // set myCurrentVel
    updateModelVelocity();
    // fixTargetPositions();   // XXX not sure why this is needed
    VectorNd vbar = new VectorNd(myTargetVelSize);
    // XXX do useTimestepScaling, useNormalizeH on targetVel
    vbar.sub(myTargetVel, myController.getData().getV0());
    if (myController.getDebug()) {
        System.out.println("(MotionTargetTerm)");
        System.out.println("\tmyTargetVel: " + myTargetVel.toString("%.3f"));
        System.out.println("\tV0: " + myController.getData().getV0().toString("%.3f"));
        System.out.println("\tvbar: " + vbar.toString("%.3f"));
    }
    MatrixNd Hv = new MatrixNd(myTargetVelSize, myController.numExcitations());
    Hv.set(myController.getData().getHv());
    if (myController.getData().normalizeH) {
        double fn = 1.0 / Hv.frobeniusNorm();
        Hv.scale(fn);
        vbar.scale(fn);
    }
    if (myController.getData().useTimestepScaling) {
        // makes it independent of the time step
        Hv.scale(1 / h);
        vbar.scale(1 / h);
    }
    // apply weights
    if (myTargetWgts != null) {
        MotionForceInverseData.diagMul(myTargetWgts, Hv, Hv);
        MotionForceInverseData.pointMul(myTargetWgts, vbar, vbar);
    }
    if (myWeight >= 0) {
        Hv.scale(myWeight);
        vbar.scale(myWeight);
    }
    H.setSubMatrix(rowoff, 0, Hv);
    b.setSubVector(rowoff, vbar);
    return rowoff + Hv.rowSize();
// return myMotionTerm.getTerm(H, b, rowoff, t0, t1,
// myTargetVel, myCurrentVel, myTargetWgts, getVelocityJacobian());
}
Also used : MatrixNd(maspack.matrix.MatrixNd) VectorNd(maspack.matrix.VectorNd)

Example 33 with VectorNd

use of maspack.matrix.VectorNd in project artisynth_core by artisynth.

the class MotionTargetTerm method updateModelVelocity.

private void updateModelVelocity() {
    int n = myMech.getActiveVelStateSize();
    if (myCurrentVel == null || myCurrentVel.size() != n)
        myCurrentVel = new VectorNd(n);
    myMech.getActiveVelState(myCurrentVel);
}
Also used : VectorNd(maspack.matrix.VectorNd) Point(artisynth.core.mechmodels.Point)

Example 34 with VectorNd

use of maspack.matrix.VectorNd in project artisynth_core by artisynth.

the class MotionTargetTerm method interpolateTargetVelocity.

/*
    * calculate target velocity by differencing current and last target positions
    */
private void interpolateTargetVelocity(double h) {
    // paranoid
    assert myTargets.size() == mySources.size();
    updateTargetPos(h);
    if (prevTargetPos == null || prevTargetPos.size() != myTargetPosSize) {
        prevTargetPos = new VectorNd(myTargetPos);
    }
    double[] prevPosBuf = prevTargetPos.getBuffer();
    int idx = 0;
    for (int i = 0; i < myTargets.size(); i++) {
        MotionTargetComponent target = myTargets.get(i);
        if (target instanceof Point) {
            idx = tmpPoint.setPosState(prevPosBuf, idx);
            interpolateTargetVelocityFromPositions(tmpPoint, (Point) target, h);
        } else if (target instanceof Frame) {
            idx = tmpFrame.setPosState(prevPosBuf, idx);
            interpolateTargetVelocityFromPositions(tmpFrame, (Frame) target, h);
        }
    }
    prevTargetPos.set(myTargetPos);
}
Also used : Frame(artisynth.core.mechmodels.Frame) VectorNd(maspack.matrix.VectorNd) Point(artisynth.core.mechmodels.Point) MotionTargetComponent(artisynth.core.mechmodels.MotionTargetComponent) Point(artisynth.core.mechmodels.Point)

Example 35 with VectorNd

use of maspack.matrix.VectorNd in project artisynth_core by artisynth.

the class MotionTargetTerm method updateTargetVel.

private void updateTargetVel(double h) {
    if (myTargetVel == null || myTargetVel.size() != myTargetVelSize)
        myTargetVel = new VectorNd(myTargetVelSize);
    double[] velBuf = myTargetVel.getBuffer();
    int idx = 0;
    for (int i = 0; i < myTargets.size(); i++) {
        idx = myTargets.get(i).getVelState(velBuf, idx);
    }
}
Also used : VectorNd(maspack.matrix.VectorNd) Point(artisynth.core.mechmodels.Point)

Aggregations

VectorNd (maspack.matrix.VectorNd)136 Point (artisynth.core.mechmodels.Point)29 Point3d (maspack.matrix.Point3d)16 Vector3d (maspack.matrix.Vector3d)15 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)11 ArrayList (java.util.ArrayList)11 ContactPoint (artisynth.core.mechmodels.ContactPoint)9 MatrixNd (maspack.matrix.MatrixNd)9 Vertex3d (maspack.geometry.Vertex3d)8 SparseMatrixNd (maspack.matrix.SparseMatrixNd)8 IntegrationPoint3d (artisynth.core.femmodels.IntegrationPoint3d)7 PointAttachment (artisynth.core.mechmodels.PointAttachment)7 PointFem3dAttachment (artisynth.core.femmodels.PointFem3dAttachment)6 FemNode (artisynth.core.femmodels.FemNode)5 PolygonalMesh (maspack.geometry.PolygonalMesh)5 ReaderTokenizer (maspack.util.ReaderTokenizer)5 FemNode3d (artisynth.core.femmodels.FemNode3d)4 IntegrationData3d (artisynth.core.femmodels.IntegrationData3d)4 StringReader (java.io.StringReader)4 HashMap (java.util.HashMap)4