Search in sources :

Example 1 with Tensor

use of jcog.math.tensor.Tensor in project narchy by automenta.

the class TensorGlow method main.

public static void main(String[] args) {
    PhyWall p = SpaceGraph.wall(1200, 1000);
    p.W.setGravity(new v2(0, -2.8f));
    staticBox(p.W, -5, -8, +5, 2f, false, true, true, true);
    for (int j = 0; j < 3; j++) {
        BodyDef bodyDef2 = new BodyDef();
        bodyDef2.type = BodyType.DYNAMIC;
        // otocenie
        bodyDef2.angle = -0.6f;
        // smer pohybu
        bodyDef2.linearVelocity = new v2(0.0f, 0.0f);
        // rotacia (rychlost rotacie)
        bodyDef2.angularVelocity = 0.0f;
        Body2D newBody = p.W.addBody(bodyDef2);
        PolygonShape shape2 = new PolygonShape();
        shape2.setAsBox(0.25f, 0.25f);
        Fixture f = newBody.addFixture(shape2, 1.0f);
        // trenie
        f.friction = 0.5f;
        // odrazivost
        f.restitution = 0.0f;
        f.material = new Uniform();
        f.material.m_rigidity = 1.0f;
    }
    // //ceiling rack
    // addBox(p.W, -1, +0.4f, 0, +0.65f, false, true, true, true);
    // new Pacman(p.W);
    {
        p.W.setContactListener(new Explosives.ExplosionContacts());
        TheoJansen t = new TheoJansen(p.W, 0.35f);
        PhyWall.PhyWindow pw = p.put(new Gridding(0.5f, new Port((float[] v) -> {
            // System.out.println(v);
            t.motorJoint.setMotorSpeed(v[0] * 2 - v[1] * 2);
            t.motorJoint.setMaxMotorTorque(v[2]);
            t.motorJoint.enableLimit(true);
            t.motorJoint.setLimits((float) (-v[3] * Math.PI), (float) (+v[4] * Math.PI));
            if (v[5] > 0.5f) {
                t.gun.fire();
            }
            t.turretJoint.setLimits((float) (+Math.PI / 2 + v[6] * Math.PI - 0.1f), (float) (+Math.PI / 2 + v[6] * Math.PI + 0.1f));
        })), 0.8f, 0.4f);
        p.W.addJoint(new RevoluteJoint(p.W, new RevoluteJointDef(pw.body, t.chassis)));
    }
    {
        p.W.setParticleRadius(0.05f);
        p.W.setParticleDamping(0.1f);
        CircleShape shape = new CircleShape();
        shape.center.set(0, 10);
        shape.radius = 2f;
        ParticleGroupDef pd = new ParticleGroupDef();
        pd.flags = ParticleType.b2_waterParticle;
        // b2_viscousParticle;
        // b2_elasticParticle;
        // b2_springParticle;
        // b2_powderParticle;
        pd.color = new ParticleColor(0.7f, 0.1f, 0.1f, 0.8f);
        pd.shape = shape;
        p.W.addParticles(pd);
    }
    HaiQae q = new HaiQae(8, 2);
    float[] in = new float[q.ae.inputs()];
    final Tensor randomVector = Tensor.randomVectorGauss(in.length, 0, 1, rng);
    final FloatRange lerpRate = new FloatRange(0.01f, 0, 1f);
    final TensorLERP lerpVector = new TensorLERP(randomVector, lerpRate);
    PhyWall.PhyWindow w = p.put(new Gridding(0.25f, new AutoUpdateMatrixView(lerpVector.data), new LabeledPane("lerp", new XYSlider().on((x, y) -> {
        lerpRate.set(x);
    })), new LabeledPane("out", new Port((x) -> {
    }) {

        @Override
        public void prePaint(int dtMS) {
            super.prePaint(dtMS);
            out(lerpVector.data);
        }
    })), 0.5f, 0.5f);
    p.put(new TogglePort(), 0.25f, 0.25f);
    PhyWall.PhyWindow qw = p.put(new Gridding(new Label("HaiQ"), new AutoSurface<>(q), new LabeledPane("input", new Port((float[] i) -> {
        System.arraycopy(i, 0, in, 0, i.length);
    })), new Gridding(VERTICAL, new AutoUpdateMatrixView(in), new AutoUpdateMatrixView(q.ae.x), new AutoUpdateMatrixView(q.ae.W), new AutoUpdateMatrixView(q.ae.y)), new Gridding(VERTICAL, new AutoUpdateMatrixView(q.q), new AutoUpdateMatrixView(q.et))), 1, 1);
    Loop.of(() -> {
        lerpVector.update();
        q.act((((float) Math.random()) - 0.5f) * 2, in);
    }).runFPS(25);
}
Also used : Fixture(spacegraph.space2d.phys.dynamics.Fixture) ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) Explosives(spacegraph.space2d.phys.explosive.Explosives) Tensor(jcog.math.tensor.Tensor) HaiQae(jcog.learn.ql.HaiQae) Random(java.util.Random) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) Port(spacegraph.space2d.widget.windo.Port) ParticleColor(spacegraph.space2d.phys.particle.ParticleColor) AutoSurface(spacegraph.space2d.widget.meta.AutoSurface) AutoUpdateMatrixView(spacegraph.space2d.widget.meter.AutoUpdateMatrixView) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) PhyWall(spacegraph.space2d.widget.windo.PhyWall) Gridding(spacegraph.space2d.container.Gridding) BodyType(spacegraph.space2d.phys.dynamics.BodyType) ParticleType(spacegraph.space2d.phys.particle.ParticleType) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef) XoRoShiRo128PlusRandom(jcog.math.random.XoRoShiRo128PlusRandom) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) LabeledPane(spacegraph.space2d.widget.text.LabeledPane) Loop(jcog.exe.Loop) TogglePort(spacegraph.space2d.widget.windo.TogglePort) XYSlider(spacegraph.space2d.widget.slider.XYSlider) Dynamics2D.staticBox(spacegraph.space2d.phys.dynamics.Dynamics2D.staticBox) Uniform(spacegraph.space2d.phys.fracture.materials.Uniform) FloatRange(jcog.math.FloatRange) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) spacegraph.util.math.v2(spacegraph.util.math.v2) TensorLERP(jcog.math.tensor.TensorLERP) VERTICAL(spacegraph.space2d.container.Gridding.VERTICAL) SpaceGraph(spacegraph.SpaceGraph) Label(spacegraph.space2d.widget.text.Label) Body2D(spacegraph.space2d.phys.dynamics.Body2D) PolygonShape(spacegraph.space2d.phys.collision.shapes.PolygonShape) Port(spacegraph.space2d.widget.windo.Port) TogglePort(spacegraph.space2d.widget.windo.TogglePort) Label(spacegraph.space2d.widget.text.Label) Uniform(spacegraph.space2d.phys.fracture.materials.Uniform) CircleShape(spacegraph.space2d.phys.collision.shapes.CircleShape) TogglePort(spacegraph.space2d.widget.windo.TogglePort) FloatRange(jcog.math.FloatRange) AutoSurface(spacegraph.space2d.widget.meta.AutoSurface) LabeledPane(spacegraph.space2d.widget.text.LabeledPane) Fixture(spacegraph.space2d.phys.dynamics.Fixture) BodyDef(spacegraph.space2d.phys.dynamics.BodyDef) ParticleGroupDef(spacegraph.space2d.phys.particle.ParticleGroupDef) HaiQae(jcog.learn.ql.HaiQae) Tensor(jcog.math.tensor.Tensor) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) TensorLERP(jcog.math.tensor.TensorLERP) RevoluteJoint(spacegraph.space2d.phys.dynamics.joints.RevoluteJoint) RevoluteJointDef(spacegraph.space2d.phys.dynamics.joints.RevoluteJointDef) Gridding(spacegraph.space2d.container.Gridding) AutoUpdateMatrixView(spacegraph.space2d.widget.meter.AutoUpdateMatrixView) XYSlider(spacegraph.space2d.widget.slider.XYSlider) PhyWall(spacegraph.space2d.widget.windo.PhyWall) ParticleColor(spacegraph.space2d.phys.particle.ParticleColor) spacegraph.util.math.v2(spacegraph.util.math.v2) Body2D(spacegraph.space2d.phys.dynamics.Body2D)

Example 2 with Tensor

use of jcog.math.tensor.Tensor in project narchy by automenta.

the class BagTest method testBagSamplingDistribution.

static void testBagSamplingDistribution(Bag<PLink<String>, PLink<String>> bag, float batchSizeProp) {
    // bag.forEach(System.out::println);
    int cap = bag.capacity();
    int batchSize = (int) Math.ceil(batchSizeProp * cap);
    int batches = cap * 1000 / batchSize;
    Tensor f1 = samplingPriDist(bag, batches, batchSize, Math.min(10, Math.max(2, cap / 2)));
    String h = "cap=" + cap + " total=" + (batches * batchSize);
    System.out.println(h + ":\n\t" + f1.tsv2());
    System.out.println();
    float[] ff = f1.get();
    // monotonically increasing
    // TODO minimize this
    float orderThresh = 0.1f;
    for (int j = 0; j < ff.length; j++) {
        for (int i = j + 1; i < ff.length; i++) {
            float diff = ff[j] - ff[i];
            boolean unordered = diff > orderThresh;
            if (unordered) {
                fail("sampling distribution not ordered. contents=" + bag.toString());
            }
        }
    }
    // should be higher
    final float MIN_RATIO = 1.5f;
    for (int lows : ff.length > 4 ? new int[] { 0, 1 } : new int[] { 0 }) {
        for (int highs : ff.length > 4 ? new int[] { ff.length - 1, ff.length - 2 } : new int[] { ff.length - 1 }) {
            float maxMinRatio = ff[highs] / ff[lows];
            assertTrue(maxMinRatio > MIN_RATIO, maxMinRatio + " ratio between max and min");
        }
    }
// TODO verify the histogram resulting from the above execution is relatively flat:
// ex: [0.21649484536082475, 0.2268041237113402, 0.28865979381443296, 0.26804123711340205]
// the tests below assume that it begins with a relatively flat distribution
// System.out.println(Arrays.toString(bag.priHistogram(4)));
// System.out.println(Arrays.toString(bag.priHistogram(8)));
// System.out.print("Sampling: " );
// printDist(samplingPriDistribution((CurveBag) n.core.concepts, 1000));
// System.out.print("Priority: " );
// EmpiricalDistribution pri;
// printDist(pri = getSamplingPriorityDistribution(n.core.concepts, 1000));
// 
// List<SummaryStatistics> l = pri.getBinStats();
// assertTrue(l.get(0).getN() < l.get(l.size() - 1).getN());
}
Also used : Tensor(jcog.math.tensor.Tensor) ArrayTensor(jcog.math.tensor.ArrayTensor)

Example 3 with Tensor

use of jcog.math.tensor.Tensor in project narchy by automenta.

the class TensorTest method test1DTensorChain.

@Test
public void test1DTensorChain() {
    ArrayTensor a = new ArrayTensor(4);
    a.set(1, 2);
    ArrayTensor b = new ArrayTensor(2);
    b.set(2, 0);
    Tensor ab = TensorChain.get(a, b);
    assertEquals(1, ab.shape().length);
    assertEquals(6, ab.shape()[0]);
    final String[] s = { "" };
    ab.forEach((i, v) -> s[0] += v + " ");
    assertEquals("[0.0 0.0 1.0 0.0 2.0 0.0 ]", Arrays.toString(s));
}
Also used : ArrayTensor(jcog.math.tensor.ArrayTensor) Tensor(jcog.math.tensor.Tensor) ArrayTensor(jcog.math.tensor.ArrayTensor) Test(org.junit.jupiter.api.Test)

Example 4 with Tensor

use of jcog.math.tensor.Tensor in project narchy by automenta.

the class AgentBuilder method get.

public WiredAgent get() {
    final int inputs = sensors.stream().mapToInt(Tensor::volume).sum();
    final int outputs = actions.stream().mapToInt(IntObjectPair::getOne).sum() + (NOP_ACTION ? 1 : 0);
    Consumer<float[]> inputter = (f) -> {
        int s = sensors.size();
        int j = 0;
        for (int i = 0; i < s; i++) {
            Tensor x = sensors.get(i);
            x.writeTo(f, j);
            j += x.volume();
        }
        assert (j == f.length);
    };
    IntConsumer act = (c) -> {
        int s = actions.size();
        for (int i = 0; i < s; i++) {
            IntObjectPair<? extends IntConsumer> aa = actions.get(i);
            int bb = aa.getOne();
            if (c >= bb) {
                c -= bb;
            } else {
                aa.getTwo().accept(c);
                return;
            }
        }
    };
    return new WiredAgent(a, inputs, inputter, reward, outputs, act);
}
Also used : IntObjectPair(org.eclipse.collections.api.tuple.primitive.IntObjectPair) Consumer(java.util.function.Consumer) List(java.util.List) Tensor(jcog.math.tensor.Tensor) FloatSupplier(jcog.math.FloatSupplier) IntIntToObjectFunc(jcog.math.IntIntToObjectFunc) IntConsumer(java.util.function.IntConsumer) Agent(jcog.learn.Agent) PrimitiveTuples(org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples) nars.$(nars.$) ScalarTensor(jcog.math.tensor.ScalarTensor) Serializable(java.io.Serializable) IntObjectPair(org.eclipse.collections.api.tuple.primitive.IntObjectPair) Tensor(jcog.math.tensor.Tensor) ScalarTensor(jcog.math.tensor.ScalarTensor) IntConsumer(java.util.function.IntConsumer)

Aggregations

Tensor (jcog.math.tensor.Tensor)4 ArrayTensor (jcog.math.tensor.ArrayTensor)2 Serializable (java.io.Serializable)1 List (java.util.List)1 Random (java.util.Random)1 Consumer (java.util.function.Consumer)1 IntConsumer (java.util.function.IntConsumer)1 Loop (jcog.exe.Loop)1 Agent (jcog.learn.Agent)1 HaiQae (jcog.learn.ql.HaiQae)1 FloatRange (jcog.math.FloatRange)1 FloatSupplier (jcog.math.FloatSupplier)1 IntIntToObjectFunc (jcog.math.IntIntToObjectFunc)1 XoRoShiRo128PlusRandom (jcog.math.random.XoRoShiRo128PlusRandom)1 ScalarTensor (jcog.math.tensor.ScalarTensor)1 TensorLERP (jcog.math.tensor.TensorLERP)1 nars.$ (nars.$)1 IntObjectPair (org.eclipse.collections.api.tuple.primitive.IntObjectPair)1 PrimitiveTuples (org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples)1 Test (org.junit.jupiter.api.Test)1