Search in sources :

Example 1 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class Polygon method selectNextEdge.

/**
 * find the next edge, for monotone polygon searching purpose;
 * for any given edge, find the next edge we should choose when searching for
 * monotone polygon pieces;
 * auxiliary function to find monotone polygon pieces
 * C++ code: return unsigned int, eid also unsigned, same for nexte_ccw, nexte_cw
 */
private int selectNextEdge(Linebase edge) {
    int eid = edge.endPoint(1).id;
    IntHashSet edges = getSetFromStartAdjEdgeMap(eid);
    int numEdges = edges.size();
    assert (numEdges != 0);
    int nexte = 0;
    if (numEdges == 1)
        nexte = (edges.intIterator().next());
    else {
        // int[] edgesKeys = edges.toSortedArray();
        int nexte_ccw = 0, nexte_cw = 0;
        // max min of cos(alfa)
        double max = -2.0, min = 2.0;
        Linebase iEdge;
        IntIterator iter = edges.toSortedList().intIterator();
        int it;
        while (iter.hasNext()) {
            it = iter.next();
            if (it == edge.id())
                continue;
            iEdge = getEdge(it);
            double[] A = { 0, 0 }, B = { 0, 0 }, C = { 0, 0 };
            A[0] = edge.endPoint(0).x;
            A[1] = edge.endPoint(0).y;
            B[0] = edge.endPoint(1).x;
            B[1] = edge.endPoint(1).y;
            if (!edge.endPoint(1).equals(iEdge.endPoint(0)))
                iEdge.reverse();
            C[0] = iEdge.endPoint(1).x;
            C[1] = iEdge.endPoint(1).y;
            double area = Poly2TriUtils.orient2d(A, B, C);
            double cosb = angleCosb(A, B, C);
            if (area > 0 && max < cosb) {
                nexte_ccw = it;
                max = cosb;
            } else if (min > cosb) {
                nexte_cw = it;
                min = cosb;
            }
        }
        nexte = (nexte_ccw != 0) ? nexte_ccw : nexte_cw;
    }
    return nexte;
}
Also used : MutableIntIterator(org.eclipse.collections.api.iterator.MutableIntIterator) IntIterator(org.eclipse.collections.api.iterator.IntIterator) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 2 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class ConstUndirGraph method neighbors.

// ---------------------------------------------------------------
/**
 * Uses sets as collection so does not support multiple edges now, even if
 * the underlying directed graph does.
 */
@Override
public IntHashSet neighbors(int i) {
    IntHashSet result = new IntHashSet();
    result.addAll(g.neighbors(i));
    if (in != null)
        result.addAll(in[i]);
    return result;
}
Also used : IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 3 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class PrefixSubGraph method neighbors.

// ---------------------------------------------------------------
@Override
public IntHashSet neighbors(int i) {
    if (i < 0 || i >= prefSize)
        throw new IndexOutOfBoundsException();
    IntHashSet result = new IntHashSet();
    g.neighbors(i).forEach(j -> {
        if (j < prefSize)
            result.add(j);
    });
    return result;
}
Also used : IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Example 4 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class BitMatrixGraph method neighbors.

// ---------------------------------------------------------------
@Override
public IntHashSet neighbors(int i) {
    IntHashSet result = new IntHashSet();
    BitSet neighb = sets.get(i);
    final int max = size();
    for (int j = 0; j < max; ++j) {
        if (neighb.get(j))
            result.add(j);
    }
    return result;
}
Also used : IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet) BitSet(java.util.BitSet)

Example 5 with IntHashSet

use of org.eclipse.collections.impl.set.mutable.primitive.IntHashSet in project narchy by automenta.

the class Predict_NARS_Core method main.

public static void main(String[] args) throws Narsese.NarseseException {
    Param.DEBUG = false;
    int duration = 8;
    float freq = 1.0f / duration * 0.1f;
    int thinkInterval = 24;
    float discretization = 7f;
    // this.activeTasks = activeTasks;
    NAR n = new NARS().get();
    // n.shortTermMemoryHistory.set(3);
    // n.param.duration.set(duration);
    // n.param.noiseLevel.set(0);
    // n.param.conceptForgetDurations.set(16);
    n.eventTask.on(t -> {
        if (!t.isDeleted() && t.isBelief() && t.op() == Op.PROD && t.term().volume() == 2 && !t.isEternal() && t.start() > n.time() && t.expectation() > 0.5) {
            long time = (int) t.start();
            String ts = t.term().toString();
            if (ts.startsWith("(y")) {
                predict(time, t);
            }
        }
    });
    // IntervalTree<Long,Float> observed = new //new TreeMLData("value", Color.WHITE).setRange(0, 1f);
    // predictions = new TreeMLData[(int)discretization];
    // TreeMLData[] reflections = new TreeMLData[(int)discretization];
    // 
    // for (int i = 0; i < predictions.length; i++) {
    // predictions[i] = new TreeMLData("Pred" + i,
    // Color.getHSBColor(0.25f + i / 4f, 0.85f, 0.85f));
    // 
    // reflections[i] = new TreeMLData("Refl" + i,
    // Color.getHSBColor(0.25f + i / 4f, 0.85f, 0.85f));
    // reflections[i].setDefaultValue(0.0);
    // }
    // TimelineVis tc = new TimelineVis(
    // new LineChart(0,1,observed).thickness(16f).height(128),
    // new LineChart(predictions[0]).thickness(16f).height(128)
    // //new BarChart(reflections).thickness(16f).height(128)
    // /*new LineChart(predictions[1]).thickness(16f).height(128),
    // new LineChart(predictions[2]).thickness(16f).height(128),*/
    // );
    // new NWindow("_", new PCanvas(tc)).show(800, 800, true);
    n.log();
    n.run((int) discretization * 4);
    ChangedTextInput chg = new ChangedTextInput(n);
    double lastsignal = 0;
    double lasttime = 0;
    while (true) {
        try {
            n.run(thinkInterval);
        } catch (Exception e) {
            System.err.println(e);
            n.stop();
        // e.printStackTrace();
        }
        // Util.pause(30);
        // signal  = (float)Math.max(0, Math.min(1.0, Math.tan(freq * n.time()) * 0.5f + 0.5f));
        signal = (float) Math.sin(freq * n.time()) * 0.5f + 0.5f;
        // signal = ((float) Math.sin(freq * n.time()) > 0 ? 1f : -1f) * 0.5f + 0.5f;
        // signal *= 1.0 + (Math.random()-0.5f)* 2f * noiseRate;
        int cols = 40;
        int colActual = Math.round(signal * cols);
        int val = (int) (((int) ((signal * discretization)) * (10.0 / discretization)));
        long windowStart = n.time();
        long windowEnd = n.time() + 2;
        predictions.removeContainedBy(0L, windowStart);
        List<Task> pp = predictions.searchContainedBy(windowStart, windowEnd);
        IntHashSet pi = new IntHashSet();
        for (Task tf : pp) {
            if (tf.isDeleted())
                continue;
            char cc = tf.term().toString().charAt("(y".length());
            int f = cc - '0';
            // if(time>=curmax) {
            // curmax=time;
            // }
            // maxval=Math.max(maxval, (value)/10.0f);
            pi.add(Math.round((f / discretization) * cols));
            if (Math.abs(f - val) > 1) {
                System.err.println(f + " vs actual " + val);
                System.err.println(tf.proof());
            } else {
                System.out.println("OK: " + tf);
                System.out.println(tf.proof());
            }
        }
        for (int i = 0; i <= cols; i++) {
            char c;
            if (pi.contains(i))
                c = 'X';
            else if (i == colActual)
                c = '#';
            else
                c = '.';
            out.print(c);
        }
        out.println();
        // observed.removeData((int) (lasttime+1));  //this
        // observed.removeData((int) (lasttime+2));  //is not good practice
        // observed.add((int) n.time(), signal);
        // observed.add((int) n.time()+1, -1); //but is fine
        // observed.add((int) n.time()+2, 1); //for now (just wanted a line at the end)
        lastsignal = signal;
        lasttime = n.time();
        // predictions[0].setData(0, maxval);
        // if(cnt<1000) { //switch to see what NARS does when observations end :)
        int dt = 1;
        // }
        if (chg.set("(y" + val + "). :|:")) {
            if (last != -1) {
                n.input("(y" + last + "). :|: %0.00;0.90%");
            }
            last = val;
        }
        // System.out.println(val);
        /*} else if (cnt==1000){
                System.out.println("observation phase end, residual predictions follow");
            }*/
        // n.ask(n.term("(?X)"), '?', n.time() + thinkInterval);
        n.que($.$("(?X)"), QUESTION, n.time() + thinkInterval / 2);
    // n.ask(n.term("(?X)"), '?', n.time() + thinkInterval / 4);
    }
}
Also used : ChangedTextInput(nars.op.in.ChangedTextInput) IntHashSet(org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)

Aggregations

IntHashSet (org.eclipse.collections.impl.set.mutable.primitive.IntHashSet)22 MutableIntSet (org.eclipse.collections.api.set.primitive.MutableIntSet)8 Random (java.util.Random)4 FasterList (jcog.list.FasterList)2 BitSet (java.util.BitSet)1 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Supplier (java.util.function.Supplier)1 ChangedTextInput (nars.op.in.ChangedTextInput)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 MutableIntIterator (org.eclipse.collections.api.iterator.MutableIntIterator)1 MutableList (org.eclipse.collections.api.list.MutableList)1 IntObjectPair (org.eclipse.collections.api.tuple.primitive.IntObjectPair)1 IntIntHashMap (org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap)1 IntObjectHashMap (org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap)1 Test (org.junit.Test)1