Last active
September 10, 2022 09:52
-
-
Save patham9/6e6cb56ae9cea4f622d5083e500a3a1b to your computer and use it in GitHub Desktop.
Perception-NAR / Nalifier
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| #identify instance as existing instance if matching better than this value, else create new one: | |
| SUFFICIENT_MATCH_EXP = sys.argv[1] if len(sys.argv) > 1 else 0.5 | |
| TRUTH_EVIDENTAL_HORIZON = 1 | |
| def Truth_c2w(c): | |
| return TRUTH_EVIDENTAL_HORIZON * c / (1 - c); | |
| def Truth_w2c(w): | |
| return w/(w + TRUTH_EVIDENTAL_HORIZON) if w > 0 else 0 | |
| def Truth_w2f(w_plus, w_minus): | |
| return w_plus / (w_plus + w_minus) if w_plus + w_minus > 0 else 0.5 | |
| def TruthValue(w_plus, w_minus): | |
| return (Truth_w2f(w_plus, w_minus), Truth_w2c(w_plus + w_minus)) | |
| def Truth_Abduction(v1, v2): | |
| ((f1,c1), (f2,c2)) = (v1, v2) | |
| return (f2, Truth_w2c(f1 * c1 * c2)) | |
| def Truth_Induction(v1, v2): | |
| return Truth_Abduction(v2, v1) | |
| def Truth_Revision(v1, v2): | |
| MAX_CONFIDENCE = 0.99 | |
| ((f1,c1), (f2,c2)) = (v1, v2) | |
| w1 = Truth_c2w(c1) | |
| w2 = Truth_c2w(c2) | |
| w = w1 + w2 | |
| if w == 0.0: | |
| return (0.5, 0.0) | |
| return ( min(1.0, (w1 * f1 + w2 * f2) / w), min(MAX_CONFIDENCE, max(max(Truth_w2c(w), c1), c2))) | |
| #Positive evidence collection: | |
| #Case 1: A and B share a common property C | |
| #A --> C | |
| #B --> C | |
| #derive: | |
| #w+ for A --> B | |
| #Case 2: A and B share a common instance | |
| #A --> C | |
| #B --> C | |
| #derive: | |
| #w+ for A --> B | |
| #Case 3: B has a property which B does not have | |
| #B --> C | |
| #not A --> C | |
| #derive: | |
| #w- for A --> B | |
| #Case 4: A has an instance which B does not have | |
| #C --> A | |
| #not C --> B | |
| #derive: | |
| #w- for A --> B | |
| def inheritances(terms, isTuple=False): | |
| Inheritances = dict([]) | |
| for i, term1 in enumerate(terms): | |
| (extension1, intension1) = terms[term1] | |
| for j, term2 in enumerate(terms): | |
| if i != j: | |
| (extension2, intension2) = terms[term2] | |
| w_plus = 0 | |
| w_minus = 0 | |
| truth2 = (0.5, 0.0) | |
| truth3 = (0.5, 0.0) | |
| for prop1, T1 in intension1: | |
| for prop2, T2 in intension2: | |
| if prop1 == prop2: | |
| w_plus+=1 | |
| truthPlus = (1.0, 0.5) | |
| truth2 = Truth_Revision(truth2, truthPlus) | |
| truth3 = Truth_Revision(truth3, Truth_Induction(T1, T2)) | |
| for prop1, T1 in extension1: | |
| for prop2, T2 in extension2: | |
| if prop1 == prop2: | |
| w_plus+=1 | |
| truthPlus = (1.0, 0.5) | |
| truth2 = Truth_Revision(truth2, truthPlus) | |
| truth3 = Truth_Revision(truth3, Truth_Abduction(T1, T2)) | |
| for prop2, T2 in intension2: | |
| AHasProperty = False | |
| for prop1, T1 in intension1: | |
| if prop1 == prop2: | |
| AHasProperty = True | |
| if not AHasProperty: | |
| w_minus+=1 | |
| truthMinus = (0.0, 0.5) | |
| truth2 = Truth_Revision(truth2, truthMinus) | |
| T1 = (0.0, 1.0) #fabricated truth since the property does not appear in T1 | |
| truth3 = Truth_Revision(truth3, Truth_Induction(T1, T2)) | |
| for inst1, T1 in extension1: | |
| BHasInstance = False | |
| for inst2, T2 in extension2: | |
| if inst1 == inst2: | |
| BHasInstance = True | |
| if not BHasInstance: | |
| w_minus+=1 | |
| truthMinus = (0.0, 0.5) | |
| truth2 = Truth_Revision(truth2, truthMinus) | |
| T2 = (0.0, 1.0) #fabricated truth since the property does not appear in T1 | |
| truth3 = Truth_Revision(truth3, Truth_Abduction(T1, T2)) | |
| statement = "<{" + term2 + "} --> [see]>. :|:" | |
| Inheritances[statement] = truth3 | |
| #see: all ways to calculate the truth are equal, unless terms contains c < 1 entries which is only captured by the NAL way, truth3: | |
| #print(statement, w_plus, w_minus, TruthValue(w_plus, w_minus), truth2, truth3) | |
| if isTuple: break | |
| return Inheritances | |
| def Truth_Expectation(T): | |
| (f,c) = T | |
| return (c * (f - 0.5) + 0.5) | |
| prototypes = {} | |
| current_prototypes = {} | |
| last_instance = None | |
| last_winner = None | |
| last_winner_truth_exp = 0.0 | |
| def NAL_AddInput(inp): #<{instance} --> [property]>. :|: %frequency% | |
| global current_prototypes, prototypes, last_instance, last_winner, last_winner_truth_exp | |
| if inp != "1": | |
| instance = inp.split("{")[1].split("}")[0] | |
| property = inp.split("[")[1].split("]")[0] | |
| frequency = float(inp.split("%")[1].split("%")[0].split(";")[0]) if "%" in inp else 1.0 | |
| if inp == "1" or (instance != last_instance and last_instance is not None): | |
| #DETERMINE BEST MATCH IN CURRENT PROTOTYPES: | |
| if last_winner is not None and last_winner_truth_exp > SUFFICIENT_MATCH_EXP: | |
| for k,v in last_winner.items(): #just 1 | |
| print(f"{k} %{v[0]};{v[1]}%" ) | |
| else: | |
| print("<{" + last_instance + "} --> [see]>. :|:") | |
| prototypes.update(current_prototypes) | |
| current_prototypes = {} | |
| if inp == "1": | |
| return | |
| last_instance = instance | |
| print("//" + inp) | |
| if instance not in current_prototypes: | |
| current_prototypes[instance] = (set(),set()) | |
| current_prototypes[instance][1].add((property, (frequency, 0.9))) | |
| winner = None | |
| winner_truth_exp = 0.0 | |
| for (key, value) in prototypes.items(): | |
| terms = {instance: current_prototypes[instance], key: value} | |
| candidate = inheritances(terms, True) | |
| candidate_truth_exp = Truth_Expectation(list(candidate.values())[0]) | |
| if candidate_truth_exp > winner_truth_exp: | |
| winner = candidate | |
| winner_truth_exp = candidate_truth_exp | |
| last_winner_truth_exp = winner_truth_exp | |
| last_winner = winner | |
| print("*volume=0") | |
| NAL_AddInput("<{scoobydoo} --> [furry]>. :|:") | |
| NAL_AddInput("<{scoobydoo} --> [brown]>. :|:") | |
| NAL_AddInput("<{scoobydoo} --> [barks]>. :|:") | |
| NAL_AddInput("<{scoobydoo} --> [loud]>. :|:") | |
| NAL_AddInput("<{garfield} --> [furry]>. :|:") | |
| NAL_AddInput("<{garfield} --> [yellow]>. :|:") | |
| NAL_AddInput("<{garfield} --> [meows]>. :|:") | |
| NAL_AddInput("<{garfield} --> [loud]>. :|: %0.0%") | |
| NAL_AddInput("<{duck} --> [feathered]>. :|:") | |
| NAL_AddInput("<{duck} --> [yellow]>. :|:") | |
| NAL_AddInput("<{duck} --> [quacks]>. :|:") | |
| NAL_AddInput("<{duck} --> [loud]>. :|:") | |
| NAL_AddInput("<{anon} --> [feathered]>. :|: %0.6%") | |
| NAL_AddInput("<{anon} --> [barks]>. :|:") | |
| NAL_AddInput("<{anon} --> [loud]>. :|:") | |
| NAL_AddInput("<{anon} --> [yellow]>. :|:") | |
| NAL_AddInput("1") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment