use of nars.entity.TruthValue in project opennars by opennars.
the class PerceptionAccel method perceive.
public void perceive(DerivationContext nal) {
// implement Peis idea here now
// we start with length 2 compounds, and search for patterns which are one longer than the longest observed one
boolean longest_result_derived_already = false;
for (int Len = cur_maxlen + 1; Len >= 2; Len--) {
// ok, this is the length we have to collect, measured from the end of event buffer
// there is a interval term for every event
Term[] relterms = new Term[2 * Len - 1];
// measuring its distance to the next event, but for the last event this is obsolete
// thus it are 2*Len-1] terms
Task newEvent = eventbuffer.get(eventbuffer.size() - 1);
TruthValue truth = newEvent.sentence.truth;
Stamp st = new Stamp(nal.memory);
ArrayList<Long> evBase = new ArrayList<Long>();
int k = 0;
for (int i = 0; i < Len; i++) {
// we go till to the end of the event buffer
int j = eventbuffer.size() - 1 - (Len - 1) + i;
if (j < 0) {
// but the mechanism already looks for length 2 patterns on the occurence of the first event
break;
}
Task current = eventbuffer.get(j);
for (long l : current.sentence.stamp.evidentialBase) {
evBase.add(l);
}
relterms[k] = current.sentence.term;
if (i != Len - 1) {
// if its not the last one, then there is a next one for which we have to put an interval
truth = TruthFunctions.deduction(truth, current.sentence.truth);
Task next = eventbuffer.get(j + 1);
relterms[k + 1] = new Interval(next.sentence.getOccurenceTime() - current.sentence.getOccurenceTime());
}
k += 2;
}
long[] evB = new long[evBase.size()];
int u = 0;
for (long l : evBase) {
evB[u] = l;
u++;
}
st.baseLength = evB.length;
st.evidentialBase = evB;
boolean eventBufferDidNotHaveSoMuchEvents = false;
for (int i = 0; i < relterms.length; i++) {
if (relterms[i] == null) {
eventBufferDidNotHaveSoMuchEvents = true;
}
}
if (eventBufferDidNotHaveSoMuchEvents) {
continue;
}
// decide on the tense of &/ by looking if the first event happens parallel with the last one
// Todo refine in 1.6.3 if we want to allow input of difference occurence time
boolean after = newEvent.sentence.stamp.after(eventbuffer.get(eventbuffer.size() - 1 - (Len - 1)).sentence.stamp, Parameters.DURATION);
// critical part: (not checked for correctness yet):
// we now have to look at if the first half + the second half already exists as concept, before we add it
Term[] firstHalf;
Term[] secondHalf;
if (relterms[Len - 1] instanceof Interval) {
// the middle can be a interval, for example in case of a,+1,b , in which case we dont use it
// so we skip the middle here
firstHalf = new Term[Len - 1];
// as well as here
secondHalf = new Term[Len - 1];
// make index mapping easier by counting
int h = 0;
for (int i = 0; i < Len - 1; i++) {
firstHalf[i] = relterms[h];
h++;
}
// we have to overjump the middle element this is why
h += 1;
for (int i = 0; i < Len - 1; i++) {
secondHalf[i] = relterms[h];
h++;
}
} else {
// it is a event so its fine
// 2*Len-1 in total
firstHalf = new Term[Len];
// but the middle is also used in the second one
secondHalf = new Term[Len];
// make index mapping easier by counting
int h = 0;
for (int i = 0; i < Len; i++) {
firstHalf[i] = relterms[h];
h++;
}
// we have to use the middle twice this is why
h--;
for (int i = 0; i < Len; i++) {
secondHalf[i] = relterms[h];
h++;
}
}
Term firstC = Conjunction.make(firstHalf, after ? ORDER_FORWARD : ORDER_CONCURRENT);
Term secondC = Conjunction.make(secondHalf, after ? ORDER_FORWARD : ORDER_CONCURRENT);
Concept C1 = nal.memory.concept(firstC);
Concept C2 = nal.memory.concept(secondC);
if (C1 == null || C2 == null) {
if (debugMechanism) {
System.out.println("one didn't exist: " + firstC.toString() + " or " + secondC.toString());
}
// the components were not observed, so don't allow creating this compound
continue;
}
if (C1.getPriority() < partConceptsPrioThreshold || C2.getPriority() < partConceptsPrioThreshold) {
// too less priority
continue;
}
Conjunction C = (Conjunction) Conjunction.make(relterms, after ? ORDER_FORWARD : ORDER_CONCURRENT);
// importance "summation"
Sentence S = new Sentence(C, Symbols.JUDGMENT_MARK, truth, st);
Task T = new Task(S, new BudgetValue(BudgetFunctions.or(C1.getPriority(), C2.getPriority()), Parameters.DEFAULT_JUDGMENT_DURABILITY, truth), true);
if (debugMechanism) {
System.out.println("success: " + T.toString());
}
if (longest_result_derived_already) {
T.setElemOfSequenceBuffer(false);
}
longest_result_derived_already = true;
// lets make the new event the parent task, and derive it
nal.derivedTask(T, false, false, false);
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class GlobalAnticipation method temporalPredictionsAdapt.
// check all predictive statements, match them with last events
public void temporalPredictionsAdapt(DerivationContext nal) {
if (TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV == 0.0f) {
return;
}
ArrayList<Task> lastEvents = new ArrayList<Task>();
for (Task stmLast : stm) {
lastEvents.add(stmLast);
}
if (lastEvents.isEmpty()) {
return;
}
final long duration = Parameters.DURATION;
ArrayList<Task> derivetasks = new ArrayList<Task>();
for (final Task c : current_tasks) {
// a =/> b or (&/ a1...an) =/> b
boolean concurrent_conjunction = false;
Term[] args = new Term[1];
Implication imp = (Implication) c.sentence.term.clone();
boolean concurrent_implication = imp.getTemporalOrder() == TemporalRules.ORDER_CONCURRENT;
args[0] = imp.getSubject();
if (imp.getSubject() instanceof Conjunction) {
Conjunction conj = (Conjunction) imp.getSubject();
if (!conj.isSpatial) {
if (conj.temporalOrder == TemporalRules.ORDER_FORWARD || conj.temporalOrder == TemporalRules.ORDER_CONCURRENT) {
concurrent_conjunction = conj.temporalOrder == TemporalRules.ORDER_CONCURRENT;
// in case of &/ this are the terms
args = conj.term;
}
}
}
int i = 0;
boolean matched = true;
int off = 0;
long expected_time = lastEvents.get(0).sentence.getOccurenceTime();
for (i = 0; i < args.length; i++) {
// handling of intervals:
if (args[i] instanceof Interval) {
if (!concurrent_conjunction) {
expected_time += ((Interval) args[i]).time;
}
off++;
continue;
}
if (i - off >= lastEvents.size()) {
break;
}
if (!Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, args[i], lastEvents.get(i - off).sentence.term)) {
// it didnt match, instead sth different unexpected happened
// whether intermediate events should be tolerated or not was a important question when considering this,
matched = false;
// if it should be allowed, the sequential match does not matter only if the events come like predicted.
break;
} else {
if (lastEvents.get(i - off).sentence.truth.getExpectation() <= 0.5f) {
// it matched according to sequence, but is its expectation bigger than 0.5? todo: decide how truth values of the expected events
// it didn't happen
matched = false;
break;
}
long occurence = lastEvents.get(i - off).sentence.getOccurenceTime();
boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
if (!right_in_time) {
// it matched so far, but is the timing right or did it happen when not relevant anymore?
matched = false;
break;
}
}
if (!concurrent_conjunction) {
expected_time += duration;
}
}
if (concurrent_conjunction && !concurrent_implication) {
// implication is not concurrent
// so here we have to add duration
expected_time += duration;
} else if (!concurrent_conjunction && concurrent_implication) {
expected_time -= duration;
}
// ok it matched, is the consequence also right?
if (matched && lastEvents.size() > args.length - off) {
long occurence = lastEvents.get(args.length - off).sentence.getOccurenceTime();
boolean right_in_time = Math.abs(occurence - expected_time) < ((double) duration) / TEMPORAL_PREDICTION_FEEDBACK_ACCURACY_DIV;
if (right_in_time && Variables.hasSubstitute(Symbols.VAR_INDEPENDENT, imp.getPredicate(), lastEvents.get(args.length - off).sentence.term)) {
// it matched and same consequence, so positive evidence
// c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(1.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(1.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
derivetasks.add(t);
} else {
// it matched and other consequence, so negative evidence
// c.sentence.truth=TruthFunctions.revision(c.sentence.truth, new TruthValue(0.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE));
Sentence s2 = new Sentence(c.sentence.term.clone(), Symbols.JUDGMENT_MARK, new TruthValue(0.0f, Parameters.DEFAULT_JUDGMENT_CONFIDENCE), new Stamp(nal.memory));
Task t = new Task(s2, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, s2.truth), true);
derivetasks.add(t);
}
// todo use derived task with revision instead
}
}
for (Task t : derivetasks) {
if (nal.derivedTask(t, false, false, false)) {
boolean debug = true;
}
}
ArrayList<Task> toDelete = new ArrayList<Task>();
for (Task t : current_tasks) {
Concept w = nal.memory.concept(t.sentence.term);
if (w == null) {
// concept does not exist anymore, delete
toDelete.add(t);
}
}
for (Task t : toDelete) {
current_tasks.remove(t);
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class Want method execute.
/**
* To create a goal with a given statement
* @param args Arguments, a Statement followed by an optional tense
* @param memory The memory in which the operation is executed
* @return Immediate results as Tasks
*/
@Override
protected ArrayList<Task> execute(Operation operation, Term[] args, Memory memory) {
Term content = args[1];
TruthValue truth = new TruthValue(1, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Sentence sentence = new Sentence(content, Symbols.GOAL_MARK, truth, new Stamp(memory));
BudgetValue budget = new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY, Parameters.DEFAULT_GOAL_DURABILITY, truth);
return Lists.newArrayList(new Task(sentence, budget, true));
}
use of nars.entity.TruthValue in project opennars by opennars.
the class Emotions method adjustBusy.
public void adjustBusy(float newValue, float weight, DerivationContext nal) {
busy += newValue * weight;
busy /= (1.0f + weight);
if (!enabled) {
return;
}
float frequency = -1;
if (Math.abs(busy - lastbusy) > CHANGE_THRESHOLD && nal.memory.time() - last_busy_time > change_steps_demanded) {
if (busy > Parameters.BUSY_EVENT_HIGHER_THRESHOLD && lastbusy <= Parameters.BUSY_EVENT_HIGHER_THRESHOLD) {
frequency = 1.0f;
}
if (busy < Parameters.BUSY_EVENT_LOWER_THRESHOLD && lastbusy >= Parameters.BUSY_EVENT_LOWER_THRESHOLD) {
frequency = 0.0f;
}
lastbusy = busy;
last_busy_time = nal.memory.time();
}
if (frequency != -1) {
// ok lets add an event now
Term predicate = SetInt.make(new Term("busy"));
Term subject = new Term("SELF");
Inheritance inh = Inheritance.make(subject, predicate);
TruthValue truth = new TruthValue(busy, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Sentence s = new Sentence(inh, Symbols.JUDGMENT_MARK, truth, new Stamp(nal.memory));
s.stamp.setOccurrenceTime(nal.memory.time());
Task t = new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, BudgetFunctions.truthToQuality(truth)), true);
nal.addTask(t, "emotion");
}
}
use of nars.entity.TruthValue in project opennars by opennars.
the class Emotions method adjustSatisfaction.
public void adjustSatisfaction(float newValue, float weight, DerivationContext nal) {
// float oldV = happyValue;
happy += newValue * weight;
happy /= 1.0f + weight;
if (!enabled) {
return;
}
float frequency = -1;
if (Math.abs(happy - lasthappy) > CHANGE_THRESHOLD && nal.memory.time() - last_happy_time > change_steps_demanded) {
if (happy > Parameters.HAPPY_EVENT_HIGHER_THRESHOLD && lasthappy <= Parameters.HAPPY_EVENT_HIGHER_THRESHOLD) {
frequency = 1.0f;
}
if (happy < Parameters.HAPPY_EVENT_LOWER_THRESHOLD && lasthappy >= Parameters.HAPPY_EVENT_LOWER_THRESHOLD) {
frequency = 0.0f;
}
lasthappy = happy;
last_happy_time = nal.memory.time();
}
if (frequency != -1) {
// ok lets add an event now
Term predicate = SetInt.make(new Term("satisfied"));
Term subject = Term.SELF;
Inheritance inh = Inheritance.make(subject, predicate);
TruthValue truth = new TruthValue(happy, Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Sentence s = new Sentence(inh, Symbols.JUDGMENT_MARK, truth, new Stamp(nal.memory));
s.stamp.setOccurrenceTime(nal.memory.time());
Task t = new Task(s, new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY, Parameters.DEFAULT_JUDGMENT_DURABILITY, BudgetFunctions.truthToQuality(truth)), true);
nal.addTask(t, "emotion");
/*if(Parameters.REFLECT_META_HAPPY_GOAL) { //remind on the goal whenever happyness changes, should suffice for now
TruthValue truth2=new TruthValue(1.0f,Parameters.DEFAULT_GOAL_CONFIDENCE);
Sentence s2=new Sentence(inh,Symbols.GOAL_MARK,truth2,new Stamp(nal.memory));
s2.stamp.setOccurrenceTime(nal.memory.time());
Task t2=new Task(s2,new BudgetValue(Parameters.DEFAULT_GOAL_PRIORITY,Parameters.DEFAULT_GOAL_DURABILITY,BudgetFunctions.truthToQuality(truth2)));
nal.addTask(t2, "metagoal");
//this is a good candidate for innate belief for consider and remind:
Operator consider=nal.memory.getOperator("^consider");
Operator remind=nal.memory.getOperator("^remind");
Term[] arg=new Term[1];
arg[0]=inh;
if(InternalExperience.enabled && Parameters.CONSIDER_REMIND) {
Operation op_consider=Operation.make(consider, arg, true);
Operation op_remind=Operation.make(remind, arg, true);
Operation[] op=new Operation[2];
op[0]=op_remind; //order important because usually reminding something
op[1]=op_consider; //means it has good chance to be considered after
for(Operation o : op) {
TruthValue truth3=new TruthValue(1.0f,Parameters.DEFAULT_JUDGMENT_CONFIDENCE);
Sentence s3=new Sentence(o,Symbols.JUDGMENT_MARK,truth3,new Stamp(nal.memory));
s3.stamp.setOccurrenceTime(nal.memory.time());
//INTERNAL_EXPERIENCE_DURABILITY_MUL
BudgetValue budget=new BudgetValue(Parameters.DEFAULT_JUDGMENT_PRIORITY,Parameters.DEFAULT_JUDGMENT_DURABILITY,BudgetFunctions.truthToQuality(truth3));
budget.setPriority(budget.getPriority()*InternalExperience.INTERNAL_EXPERIENCE_PRIORITY_MUL);
budget.setDurability(budget.getPriority()*InternalExperience.INTERNAL_EXPERIENCE_DURABILITY_MUL);
Task t3=new Task(s3,budget);
nal.addTask(t3, "internal experience for consider and remind");
}
}
}*/
}
// if (Math.abs(oldV - happyValue) > 0.1) {
// Record.append("HAPPY: " + (int) (oldV*10.0) + " to " + (int) (happyValue*10.0) + "\n");
}
Aggregations