Search in sources :

Example 1 with IntObjectPair

use of org.eclipse.collections.api.tuple.primitive.IntObjectPair in project eclipse-collections by eclipse.

the class IntInterval method zip.

@Override
public <T> ImmutableList<IntObjectPair<T>> zip(Iterable<T> iterable) {
    int size = this.size();
    int othersize = Iterate.sizeOf(iterable);
    MutableList<IntObjectPair<T>> target = Lists.mutable.withInitialCapacity(Math.min(size, othersize));
    IntIterator iterator = this.intIterator();
    Iterator<T> otherIterator = iterable.iterator();
    for (int i = 0; i < size && otherIterator.hasNext(); i++) {
        target.add(PrimitiveTuples.pair(iterator.next(), otherIterator.next()));
    }
    return target.toImmutable();
}
Also used : IntObjectPair(org.eclipse.collections.api.tuple.primitive.IntObjectPair) IntIterator(org.eclipse.collections.api.iterator.IntIterator)

Example 2 with IntObjectPair

use of org.eclipse.collections.api.tuple.primitive.IntObjectPair 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)

Example 3 with IntObjectPair

use of org.eclipse.collections.api.tuple.primitive.IntObjectPair in project narchy by automenta.

the class ArithmeticIntroduction method apply.

public static Term apply(Term x, @Nullable Anon anon, Random rng) {
    if ((anon == null && !x.hasAny(INT)) || x.complexity() < 3)
        return x;
    // find all unique integer subterms
    IntHashSet ints = new IntHashSet();
    x.recurseTerms((t) -> {
        Int it = null;
        if (t instanceof Anom) {
            Anom aa = ((Anom) t);
            Term ta = anon.get(aa);
            if (ta.op() == INT)
                it = ((Int) ta);
        } else if (t instanceof Int) {
            it = (Int) t;
        }
        if (it == null)
            return;
        ints.add((it.id));
    });
    // Set<Term> ints = ((Compound) x).recurseTermsToSet(INT);
    int ui = ints.size();
    if (ui <= 1)
        // nothing to do
        return x;
    // increasing so that relational comparisons can assume that 'a' < 'b'
    int[] ii = ints.toSortedArray();
    // potential mods to select from
    // FasterList<Supplier<Term[]>> mods = new FasterList(1);
    IntObjectHashMap<List<Supplier<Term[]>>> mods = new IntObjectHashMap(ii.length);
    Variable v = $.varDep("x");
    // test arithmetic relationships
    for (int a = 0; a < ui; a++) {
        int ia = ii[a];
        for (int b = a + 1; b < ui; b++) {
            int ib = ii[b];
            assert (ib > ia);
            if (ib - ia < ia && (ia != 0)) {
                mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("add", v, $.the(ib - ia)) });
                mods.getIfAbsentPut(ib, FasterList::new).add(() -> new Term[] { Int.the(ia), $.func("add", v, $.the(ia - ib)) });
            } else if ((ia != 0 && ia != 1) && (ib != 0 && ib != 1) && Util.equals(ib / ia, (((float) ib) / ia), Float.MIN_NORMAL)) {
                mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("mul", v, $.the(ib / ia)) });
            } else if (ia == -ib) {
                // negation (x * -1)
                mods.getIfAbsentPut(ia, FasterList::new).add(() -> new Term[] { Int.the(ib), $.func("mul", v, $.the(-1)) });
                mods.getIfAbsentPut(ib, FasterList::new).add(() -> new Term[] { Int.the(ia), $.func("mul", v, $.the(-1)) });
            }
        }
    }
    if (mods.isEmpty())
        return x;
    // TODO fair select randomly if multiple of the same length
    RichIterable<IntObjectPair<List<Supplier<Term[]>>>> mkv = mods.keyValuesView();
    int ms = mkv.maxBy(e -> e.getTwo().size()).getTwo().size();
    mkv.reject(e -> e.getTwo().size() < ms);
    // convention: choose lowest base
    MutableList<IntObjectPair<List<Supplier<Term[]>>>> mmm = mkv.toSortedListBy(IntObjectPair::getOne);
    IntObjectPair<List<Supplier<Term[]>>> m = mmm.get(0);
    int base = m.getOne();
    Term baseTerm = Int.the(base);
    if (anon != null)
        baseTerm = anon.put(baseTerm);
    Term yy = x.replace(baseTerm, v);
    for (Supplier<Term[]> s : m.getTwo()) {
        Term[] mm = s.get();
        if (anon != null)
            mm[0] = anon.put(mm[0]);
        yy = yy.replace(mm[0], mm[1]);
    }
    Term y = CONJ.the(yy, SIM.the(baseTerm, v));
    if (y.op() != CONJ) {
        // something happened
        return null;
    }
    if (x.isNormalized()) {
        y = y.normalize();
    }
    return y;
}
Also used : Variable(nars.term.var.Variable) IntObjectHashMap(org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap) FasterList(jcog.list.FasterList) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet) Term(nars.term.Term) Int(nars.term.atom.Int) IntObjectPair(org.eclipse.collections.api.tuple.primitive.IntObjectPair) Anom(nars.term.anon.Anom) FasterList(jcog.list.FasterList) MutableList(org.eclipse.collections.api.list.MutableList) List(java.util.List) Supplier(java.util.function.Supplier)

Aggregations

IntObjectPair (org.eclipse.collections.api.tuple.primitive.IntObjectPair)3 List (java.util.List)2 Serializable (java.io.Serializable)1 Consumer (java.util.function.Consumer)1 IntConsumer (java.util.function.IntConsumer)1 Supplier (java.util.function.Supplier)1 Agent (jcog.learn.Agent)1 FasterList (jcog.list.FasterList)1 FloatSupplier (jcog.math.FloatSupplier)1 IntIntToObjectFunc (jcog.math.IntIntToObjectFunc)1 ScalarTensor (jcog.math.tensor.ScalarTensor)1 Tensor (jcog.math.tensor.Tensor)1 nars.$ (nars.$)1 Term (nars.term.Term)1 Anom (nars.term.anon.Anom)1 Int (nars.term.atom.Int)1 Variable (nars.term.var.Variable)1 IntIterator (org.eclipse.collections.api.iterator.IntIterator)1 MutableList (org.eclipse.collections.api.list.MutableList)1 IntObjectHashMap (org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap)1