use of nars.concept.Concept in project narchy by automenta.
the class Premise method match.
/**
* resolve the most relevant belief of a given term/concept
* <p>
* patham9 project-eternalize
* patham9 depending on 4 cases
* patham9 https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj
* sseehh__ ok ill add that in a bit
* patham9 you need project-eternalize-to
* sseehh__ btw i disabled immediate eternalization entirely
* patham9 so https://github.com/opennars/opennars2/blob/a143162a559e55c456381a95530d00fee57037c4/src/nal/deriver/projection_eternalization.clj#L31
* patham9 especially try to understand the "temporal temporal" case
* patham9 its using the result of higher confidence
* <p>
* returns ttl used, -1 if failed before starting
*
* @param matchTime - temporal focus control: determines when a matching belief or answer should be projected to
*/
@Nullable
public Derivation match(Derivation d, long[] focus, int matchTTL) {
NAR n = d.nar;
if (task == null || task.isDeleted()) {
// }
return null;
}
// n.conceptualize(task.term(), (c)->{});
int dur = d.dur;
Term beliefTerm = term();
Term taskTerm = task.term();
Term taskConcept = task.term().concept();
final boolean[] beliefConceptCanAnswerTaskConcept = { false };
boolean unifiedBelief = false;
Op to = taskTerm.op();
Op bo = beliefTerm.op();
if (to == bo) {
if (taskConcept.equals(beliefTerm.concept())) {
beliefConceptCanAnswerTaskConcept[0] = true;
} else {
if ((bo.conceptualizable) && (beliefTerm.hasAny(var) || taskTerm.hasAny(var))) {
Term _beliefTerm = beliefTerm;
final Term[] unifiedBeliefTerm = new Term[] { null };
UnifySubst u = new UnifySubst(/*null*/
VAR_QUERY, n, (y) -> {
if (y.op().conceptualizable) {
y = y.normalize();
beliefConceptCanAnswerTaskConcept[0] = true;
if (!y.equals(_beliefTerm)) {
unifiedBeliefTerm[0] = y;
// stop
return false;
}
}
// keep going
return true;
}, matchTTL);
u.varSymmetric = true;
u.varCommonalize = true;
u.unify(taskTerm, beliefTerm, true);
if (unifiedBeliefTerm[0] != null) {
beliefTerm = unifiedBeliefTerm[0];
unifiedBelief = true;
}
}
}
}
// HACK ?? assert(beliefTerm.op()!=NEG);
beliefTerm = beliefTerm.unneg();
// QUESTION ANSWERING and TERMLINK -> TEMPORALIZED BELIEF TERM projection
Task belief = null;
// float timeFocus = n.timeFocus.floatValue();
// int fRad = Math.round(Math.max(1,dur * timeFocus));
final Concept beliefConcept = beliefTerm.op().conceptualizable ? // conceptualize in case of dynamic concepts
n.conceptualize(beliefTerm) : null;
if (beliefConcept != null) {
if (!beliefTerm.hasVarQuery()) {
// doesnt make sense to look for a belief in a term with query var, it will have none
final BeliefTable bb = beliefConcept.beliefs();
if (task.isQuestOrQuestion()) {
if (beliefConceptCanAnswerTaskConcept[0]) {
final BeliefTable answerTable = (task.isGoal() || task.isQuest()) ? beliefConcept.goals() : bb;
// //see if belief unifies with task (in reverse of previous unify)
// if (questionTerm.varQuery() == 0 || (unify((Compound)beliefConcept.term(), questionTerm, nar) == null)) {
//
// } else {
//
// }
Task match = answerTable.answer(task.start(), task.end(), dur, task, beliefTerm, n, d::add);
if (match != null) {
assert (task.isQuest() || match.punc() == BELIEF) : "quest answered with a belief but should be a goal";
@Nullable Task answered = task.onAnswered(match, n);
if (answered != null) {
n.emotion.onAnswer(task, answered);
}
if (match.isBelief()) {
belief = match;
}
}
}
}
if ((belief == null) && !bb.isEmpty()) {
belief = bb.match(focus[0], focus[1], beliefTerm, n, taskConcept.equals(beliefConcept.term()) ? x -> !x.equals(task) : null);
}
}
if (unifiedBelief) {
Concept originalBeliefConcept = n.conceptualize(term());
if (originalBeliefConcept != null)
linkVariable(originalBeliefConcept, beliefConcept);
}
}
if (belief != null) {
// use the belief's actual possibly-temporalized term
beliefTerm = belief.term().unneg();
// if (belief.equals(task)) { //do not repeat the same task for belief
// belief = null; //force structural transform; also prevents potential inductive feedback loop
// }
}
if (beliefTerm instanceof Bool) {
// logger.warn("{} produced Bool beliefTerm", this);
return null;
}
if (!d.reset().proto(task, belief, beliefTerm))
return null;
return d;
}
use of nars.concept.Concept in project narchy by automenta.
the class AutoConceptualizer method update.
protected void update(NAR n) {
long now = n.time();
float[] x = this.x;
int inputs = in.size();
for (int i = 0, inSize = inputs; i < inSize; i++) {
Concept xx = in.get(i);
Truth t = ((BeliefTable) xx.table(beliefOrGoal ? BELIEF : GOAL)).truth(now, n);
float f;
if (t == null) {
f = 0.5f;
// 0.5f + (learningRate * 2) * n.random().nextFloat() - learningRate;
// n.random()
} else {
f = t.freq();
}
x[i] = f;
}
// System.out.println(n4(x));
float err = ae.put(x, learningRate, noiseLevel, 0, true);
// System.out.println("err=" + n4(err/inputs) + ": \t" + n4(ae.y));
// decompile/unfactor the outputs
// if err < thresh
int outputs = ae.outputs();
float[] b = new float[outputs];
float thresh = n.freqResolution.floatValue();
int[] order = new int[inputs];
for (int i = 0; i < outputs; i++) {
// basis vector for each output
b[i] = 1;
float[] a = ae.decode(b, true);
// System.out.println("\tfeature " + i + "=" + n4(a));
Term feature = conj(order, a, /* threshold, etc */
5, /*a.length/2*/
thresh);
if (feature != null) {
// System.out.println("\t " + feature);
onFeature(feature);
}
// clear
b[i] = 0;
}
}
use of nars.concept.Concept in project narchy by automenta.
the class ConceptIndex method conceptAsync.
public final void conceptAsync(Term x, boolean createIfMissing, Consumer<Concept> with) {
Term y;
if (x instanceof Concept) {
Concept ct = (Concept) x;
if (!ct.isDeleted()) {
with.accept(ct);
// assumes an existing Concept index isnt a different copy than what is being passed as an argument
return;
}
// otherwise if it is deleted, continue
y = ct.term();
} else {
y = x.term().concept();
if (!y.op().conceptualizable)
// TODO error?
return;
}
getAsync(y, createIfMissing).handle((t, e) -> {
if (e != null) {
e.printStackTrace();
} else {
with.accept((Concept) t);
}
return this;
});
}
use of nars.concept.Concept in project narchy by automenta.
the class BooleanTest method testSAT2Individual.
void testSAT2Individual(int i, int j) throws Narsese.NarseseException {
final float confThresh = 0.7f;
Param.DEBUG = true;
// for (int i = 0; i < 2; i++) {
// for (int j = 0; j < 2; j++) {
NAR d = NARS.tmp();
d.freqResolution.set(0.05f);
d.termVolumeMax.set(24);
String[] outcomes = { // "(x-->(0,0))",
"x:{0}", // "(x-->(0,1))",
"x:{1}", // "(x-->(1,0))",
"x:{2}", // "(x-->(1,1))"};
"x:{3}" };
// String expected = "(x --> (" + i + "," + j + "))";
d.believe("( (--(x-->i) && --(x-->j)) ==> " + outcomes[0] + ")");
d.believe("( (--(x-->i) && (x-->j)) ==> " + outcomes[1] + ")");
d.believe("( ((x-->i) && --(x-->j)) ==> " + outcomes[2] + ")");
d.believe("( ((x-->i) && (x-->j)) ==> " + outcomes[3] + ")");
Term I = $.$("(x-->i)").negIf(i == 0);
Term J = $.$("(x-->j)").negIf(j == 0);
// d.believe(I);
// d.believe(J);
d.believe(CONJ.the(I, J));
// for (String s : outcomes) {
// d.ask(s);
// }
d.run(1024);
System.out.println(i + " " + j + " ");
for (int k = 0, outcomesLength = outcomes.length; k < outcomesLength; k++) {
String s = outcomes[k];
Concept dc = d.conceptualize(s);
assertNotNull(dc);
@Nullable Task t = d.belief(dc.term(), d.time());
Truth b = t != null ? t.truth() : null;
System.out.println("\t" + s + "\t" + b + "\t" + outcomes[k]);
int ex = -1, ey = -1;
switch(k) {
case 0:
ex = 0;
ey = 0;
break;
case 1:
ex = 0;
ey = 1;
break;
case 2:
ex = 1;
ey = 0;
break;
case 3:
ex = 1;
ey = 1;
break;
}
boolean thisone = ((ex == i) && (ey == j));
if (thisone && b == null)
fail("unrecognized true case");
if (thisone && b.isNegative() && b.conf() > confThresh)
fail("wrong true case:\n" + t.proof());
if (!thisone && b != null && b.isPositive() && b.conf() > confThresh)
fail("wrong false case:\n" + t.proof());
}
// System.out.println();
// return;
// }
// }
}
use of nars.concept.Concept in project narchy by automenta.
the class LinkageTest method Variable_Normalization_1.
// @Test
// public void Advanced_Concept_Formation_Test4() throws Exception {
// testConceptFormed("(&&,<#1 --> (/,open,#2,_)>,<#1 --> lock>,<#2 --> key>)");
// }
@Test
public void Variable_Normalization_1() throws Exception {
// this.activeTasks = activeTasks;
NAR tester = NARS.tmp();
test.requireConditions = false;
String nonsense = "<(&&,<#1 --> M>,<#2 --> M>) ==> <#1 --> nonsense>>";
// .en("If robin is a type of bird then robin can fly.");
tester.believe(nonsense);
tester.run(1);
Concept c = tester.conceptualize(nonsense);
assertNotNull(c);
}
Aggregations