use of jcog.pri.PLink in project narchy by automenta.
the class BagTest method populate.
public static void populate(Bag<String, PriReference<String>> a, Random rng, int count, int dimensionality, float minPri, float maxPri, float minQua, float maxQua) {
float dPri = maxPri - minPri;
for (int i = 0; i < count; i++) {
a.put(new PLink("x" + rng.nextInt(dimensionality), rng.nextFloat() * dPri + minPri));
}
a.commit(null);
if (a instanceof ArrayBag)
assertSorted((ArrayBag) a);
}
use of jcog.pri.PLink in project narchy by automenta.
the class Sweeper method main.
public static void main(String[] args) {
final Sweeper sweeper = new Sweeper();
sweeper.registerShutdownHook();
final AtomicBoolean stop = new AtomicBoolean(false);
Consumer<Object> handler = (v) -> {
System.out.println("onGC action run! " + v);
stop.set(true);
};
Object key = new Object();
Object val = new PLink("xyz", 0.5f);
ConsumingWeakReference<Object> x = sweeper.onWeakGC(val, key, handler);
for (int i = 0; !stop.get(); i++) {
key = new ArrayList<Integer>(i);
for (int k = 0; k < i; k++) {
((Collection) key).add(k);
}
Thread.yield();
}
System.out.println(((Collection) key).size());
System.out.println(x + " " + x.get());
sweeper.shutdown();
}
use of jcog.pri.PLink in project narchy by automenta.
the class ArrayBagTest method testSort.
@Test
public void testSort() {
PLinkArrayBag a = new PLinkArrayBag(4, plus, new HashMap<>(4));
a.put(new PLink("x", 0.1f));
a.put(new PLink("y", 0.2f));
a.commit(null);
Iterator<PriReference<String>> ii = a.iterator();
assertEquals("y", ii.next().get());
assertEquals("x", ii.next().get());
assertEquals("[$0.2000 y, $0.1000 x]", a.listCopy().toString());
System.out.println(a.listCopy());
a.put(new PLink("x", 0.2f));
System.out.println(a.listCopy());
a.commit();
// x should now be ahead
// x first
assertTrue(a.listCopy().toString().contains("x,"));
// y second
assertTrue(a.listCopy().toString().contains("y]"));
ii = a.iterator();
assertEquals("x", ii.next().get());
assertEquals("y", ii.next().get());
}
use of jcog.pri.PLink in project narchy by automenta.
the class TermlinkTemplates method activate.
public void activate(Concept src, float budgeted, NAR nar) {
int n = this.size();
if (n == 0)
return;
float budgetedToEach = budgeted / n;
// if (budgetedToEach < Pri.EPSILON)
// return;
MutableFloat refund = new MutableFloat(0);
// int nextTarget = nar.random().nextInt(n);
Term srcTerm = src.term();
Bag<Term, PriReference<Term>> srcTermLinks = src.termlinks();
float balance = nar.termlinkBalance.floatValue();
for (int i = 0; i < n; i++) {
Term tgtTerm = get(i);
boolean reverseLinked = false;
boolean conceptualizable = i < concepts;
if (conceptualizable) {
@Nullable Concept tgt = nar.conceptualize(tgtTerm);
float budgetedForward = budgetedToEach * (1f - balance);
float budgetedReverse = budgetedToEach * balance;
if (tgt != null) {
// insert termlink
tgt.termlinks().put(new PLink<>(srcTerm, budgetedForward), refund);
nar.activate(tgt, // budgetedToEach
budgetedForward);
reverseLinked = true;
// use the concept's id
tgtTerm = tgt.term();
}
if (!reverseLinked)
refund.add(budgetedForward);
((Bag) srcTermLinks).put(new PLink(tgtTerm, budgetedReverse), refund);
}
// float r = refund.floatValue();
// float cost = budgeted - r;
// return cost;
}
}
use of jcog.pri.PLink in project narchy by automenta.
the class ActivateTest method testConceptFireLinkSelection.
@Test
public void testConceptFireLinkSelection() throws Narsese.NarseseException {
int count = 8;
NAR nar = new NARS().tmp();
// low priority so it doesnt affect links
nar.input("$0.01 a:b.");
nar.run(1);
System.out.println("inputs:\n");
Concept c = nar.conceptualize("a:b");
for (int n = 0; n < count; n++) {
PLink<Term> inserted = new PLink<>($("x" + n + ":a"), ((1 + n) / ((float) count)));
System.out.println(inserted);
c.termlinks().put(inserted);
}
System.out.println();
HashBag<String> termlinkHits = new HashBag();
HashBag<String> taskHits = new HashBag();
// HashBag<String> premiseHits = new HashBag();
Activate cf = new Activate(c, 1f);
Term A = $.the("a");
// BatchActivation ba = new BatchActivation();
for (int i = 0; i < 100; i++) {
final int[] remain = { 9 };
cf.premises(nar, (task, term) -> {
Task ptask = task;
Term pterm = term.get();
System.out.println("tasklink=" + ptask + " termlink=" + pterm);
if (pterm instanceof Atom || !A.equals(pterm.sub(0)))
// ignore
return true;
String tls = pterm.toString();
// premiseHits.addOccurrences(p.toString(), 1);
termlinkHits.addOccurrences(/*tasklink.get() + " " +*/
tls, 1);
taskHits.addOccurrences(/*tasklink.get() + " " +*/
(ptask + " " + pterm), 1);
return --remain[0] > 0;
}, 1, 3);
// ba.commit(nar);
}
System.out.println("termlinks pri (after):\n");
c.termlinks().print();
System.out.println("\ntermlink hits:\n");
termlinkHits.topOccurrences(termlinkHits.size()).forEach(System.out::println);
// System.out.println("\ntask hits:\n");
// taskHits.topOccurrences(taskHits.size()).forEach(System.out::println);
// System.out.println("\npremise hits:\n");
// premiseHits.topOccurrences(premiseHits.size()).forEach(System.out::println);
// System.out.println();
// c.print();
// System.out.println();
ObjectIntPair<String> top = termlinkHits.topOccurrences(1).get(0);
ObjectIntPair<String> bottom = termlinkHits.bottomOccurrences(1).get(0);
String min = bottom.getOne();
// allow either 0 or 1
assertTrue("(a-->x0)".equals(min) || "(a-->x1)".equals(min));
assertEquals("(a-->x" + (count - 1) + ")", top.getOne());
}
Aggregations