Search in sources :

Example 6 with Op

use of nars.Op in project narchy by automenta.

the class Revision method intermpolate.

/*@NotNull*/
static Term intermpolate(/*@NotNull*/
Term a, long bOffset, /*@NotNull*/
Term b, float aProp, float curDepth, NAR nar) {
    if (a.equals(b) && bOffset == 0) {
        return a;
    }
    Op ao = a.op();
    Op bo = b.op();
    if (ao != bo)
        // fail, why
        return Null;
    // assert (ao == bo) : a + " and " + b + " have different op";
    // 
    // if (ao == NEG) {
    // return intermpolate(a.unneg(), 0, b.unneg(),
    // aProp, curDepth, rng, mergeOrChoose).neg();
    // }
    int len = a.subs();
    if (len > 0) {
        if (ao.temporal) {
            boolean mergeOrChoose = nar.dtMergeOrChoose.get();
            if (ao == CONJ) {
                return dtMergeConjEvents(a, bOffset, b, aProp, curDepth, mergeOrChoose, nar.random(), nar.dtDitherCycles());
            } else if (ao == IMPL) {
                return dtMergeDirect(a, b, aProp, curDepth, nar, mergeOrChoose);
            } else
                throw new UnsupportedOperationException();
        } else {
            if (a.equals(b)) {
                return a;
            }
            Term[] ab = new Term[len];
            boolean change = false;
            Subterms aa = a.subterms();
            Subterms bb = b.subterms();
            for (int i = 0; i < len; i++) {
                Term ai = aa.sub(i);
                Term bi = bb.sub(i);
                if (!ai.equals(bi)) {
                    Term y = intermpolate(ai, 0, bi, aProp, curDepth / 2f, nar);
                    if (y instanceof Bool && (!(ai instanceof Bool)))
                        // failure
                        return Null;
                    if (!ai.equals(y)) {
                        change = true;
                        ai = y;
                    }
                }
                ab[i] = ai;
            }
            return !change ? a : ao.the(choose(a, b, aProp, nar.random()).dt(), /**
             * this effectively chooses between && and &| in a size >2 case
             */
            ab);
        }
    }
    return choose(a, b, aProp, nar.random());
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op) Bool(nars.term.atom.Bool) Term(nars.term.Term)

Example 7 with Op

use of nars.Op in project narchy by automenta.

the class Revision method dtDiff.

static float dtDiff(Term a, Term b, int depth) {
    if (a.equals(b))
        return 0f;
    // if (!a.isTemporal() || !b.isTemporal()) {
    // return 0f;
    // }
    Op ao = a.op();
    Op bo = b.op();
    if (ao != bo)
        // why?
        return Float.POSITIVE_INFINITY;
    Subterms aa = a.subterms();
    int len = aa.subs();
    Subterms bb = b.subterms();
    float d = 0;
    // if (len!=blen) {
    // if (!aa.equals(bb)) {
    // return (a.dtRange() + b.dtRange()) / depth; //estimate
    // }
    // int blen = bb.subs();
    boolean aSubsEqualsBSubs = aa.equals(bb);
    if (a.op() == CONJ && !aSubsEqualsBSubs) {
        // HACK :)
        Conj c = new Conj();
        String as = Conj.sequenceString(a, c).toString();
        String bs = Conj.sequenceString(b, c).toString();
        int levDist = Texts.levenshteinDistance(as, bs);
        float seqDiff = (((float) levDist) / (Math.min(as.length(), bs.length())));
        // HACK estimate
        float rangeDiff = Math.max(1f, Math.abs(a.dtRange() - b.dtRange()));
        d += (1f + rangeDiff) * (1f + seqDiff);
    } else {
        if (!aSubsEqualsBSubs) {
            if (aa.subs() != bb.subs())
                return Float.POSITIVE_INFINITY;
            for (int i = 0; i < len; i++) d += dtDiff(aa.sub(i), bb.sub(i), depth + 1);
        }
        int adt = a.dt();
        int bdt = b.dt();
        // ockham temoral razor - prefer temporally shorter explanations
        if (adt == DTERNAL)
            adt = // 0; //dternal prefer match with immediate dt=0
            bdt;
        if (bdt == DTERNAL)
            bdt = // bdt = 0; //dternal prefer match with immediate dt=0
            adt;
        if (adt == XTERNAL)
            adt = bdt;
        if (bdt == XTERNAL)
            bdt = adt;
        if (adt != bdt) /* && adt != DTERNAL && bdt != DTERNAL*/
        {
            // if (adt == DTERNAL) {
            // adt = 0;
            // dLocal += 0.5f;
            // }
            // if (bdt == DTERNAL) {
            // bdt = 0;
            // dLocal += 0.5f;
            // }
            d += Math.abs(adt - bdt);
        }
    }
    return d / depth;
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op) Conj(nars.term.compound.util.Conj)

Example 8 with Op

use of nars.Op in project narchy by automenta.

the class Compound method dtRange.

@Override
default int dtRange() {
    Op o = op();
    switch(o) {
        // 
        case CONJ:
            Subterms tt = subterms();
            int l = tt.subs();
            if (l == 2) {
                int dt = dt();
                switch(dt) {
                    case DTERNAL:
                    case XTERNAL:
                    case 0:
                        dt = 0;
                        break;
                    default:
                        dt = Math.abs(dt);
                        break;
                }
                return tt.sub(0).dtRange() + (dt) + tt.sub(1).dtRange();
            } else {
                int s = 0;
                for (int i = 0; i < l; i++) {
                    s = Math.max(s, tt.sub(i).dtRange());
                }
                return s;
            }
        default:
            return 0;
    }
}
Also used : Subterms(nars.subterm.Subterms) Op(nars.Op)

Example 9 with Op

use of nars.Op in project narchy by automenta.

the class SerialCompound method dt.

@Override
public int dt() {
    // last two bytes
    Op o = op();
    if (o.temporal) {
        int p = this.len;
        final byte[] b = bytes;
        return Ints.fromBytes(b[p - 3], b[p - 2], b[p - 1], b[p]);
    } else {
        return DTERNAL;
    }
}
Also used : Op(nars.Op)

Example 10 with Op

use of nars.Op in project narchy by automenta.

the class Conj method add.

/**
 * returns false if contradiction occurred, in which case this
 * ConjEvents instance is
 * now corrupt and its result via .term() should be considered final
 */
public boolean add(Term t, long at) {
    if (term != null)
        throw new RuntimeException("already terminated");
    if (t == True)
        // ignore
        return true;
    else if (t == False) {
        this.term = False;
        return false;
    } else if (t == Null) {
        this.term = Null;
        return false;
    }
    Op x = t.op();
    boolean polarity;
    if (x == NEG) {
        t = t.unneg();
        polarity = false;
    } else {
        polarity = true;
    }
    int dt;
    boolean atEternal = at == ETERNAL;
    if (x == CONJ && (dt = t.dt()) != XTERNAL && (dt != DTERNAL || atEternal) && (dt != 0 || !atEternal) && (!atEternal || (dt == DTERNAL))) {
        // try {
        return t.eventsWhile((w, e) -> add(e, w), at, !atEternal, // only decompose DTERNAL if in the ETERNAL context, otherwise they are embedded as events
        atEternal, false, 0);
    // } catch (StackOverflowError e) {
    // System.err.println(t + " " + at + " " + dt);
    // throw new RuntimeException(t + " should not have recursed");
    // }
    } else {
        int id = add(t);
        if (!polarity)
            id = -id;
        if (!addIfValid(at, id)) {
            // contradiction
            term = False;
            return false;
        }
        return true;
    }
}
Also used : Op(nars.Op)

Aggregations

Op (nars.Op)36 Term (nars.term.Term)13 Subterms (nars.subterm.Subterms)11 Nullable (org.jetbrains.annotations.Nullable)8 Atomic (nars.term.atom.Atomic)4 Test (org.junit.jupiter.api.Test)4 FasterList (jcog.list.FasterList)3 NAR (nars.NAR)3 Termed (nars.term.Termed)3 Atom (nars.term.atom.Atom)3 Bool (nars.term.atom.Bool)3 Pair (org.eclipse.collections.api.tuple.Pair)3 NotNull (org.jetbrains.annotations.NotNull)3 java.io (java.io)2 java.util (java.util)2 List (java.util.List)2 Map (java.util.Map)2 Stream (java.util.stream.Stream)2 nars.$ (nars.$)2 Concept (nars.concept.Concept)2