def old_test_compile_addition(): engine = AdditionEngine() engine.reset() f1 = Fact(id='JCommTable.R0C0', value='1', contentEditable=False) f2 = Fact(id='JCommTable.R1C0', value='2', contentEditable=False) f3 = Fact(id='JCommTable.R1C1', contentEditable=True, value='') engine.declare(f1) engine.declare(f2) engine.declare(f3) engine.run(10) ex = Explanation(engine.sais[0]) nr = ex.new_rule new_wm = ExpertaWorkingMemory(EmptyAdditionEngine()) new_wm.add_rule(nr) f1b = Fact(id='JCommTable.R0C0', value='1', contentEditable=False) f2b = Fact(id='JCommTable.R1C0', value='2', contentEditable=False) f3b = Fact(id='JCommTable.R1C1', contentEditable=True, value='') new_wm.ke.declare(f1b) new_wm.ke.declare(f2b) new_wm.ke.declare(f3b) # test that the new rule fires correctly new_wm.ke.run(10) s = new_wm.ke.sais[0] assert s == Sai(selection='JCommTable.R1C1', action='UpdateTextField', inputs={'value': '3'})
def test_explain(): cf = KillEngine() cf.reset() cf.run(10) kill_fact = cf.facts[7] new_wm = ExpertaWorkingMemory(KillEngineEmpty()) # generate a new rule and assign it to the blank working memory x = Explanation(kill_fact) r = x.new_rule new_wm.add_rule(r) # test that the new rule fires correctly new_wm.ke.run(10) assert new_wm.ke.fired == cf.fired
def __init__(self, wm: WorkingMemory = None, when: WhenLearner = None, request_epsilon: float = 0.01, train_epsilon: float = 0.3, action_penalty: float = -0.05, gamma: float = 0.7, negative_actions: bool = False, skill_map: Dict[str, Skill] = fraction_skill_set, prior_skills=None, **kwargs): # Just track the state as a set of Facts? # initialize to None, so gets replaced on first state. super().__init__() self.last_activation = None self.last_sai = None if wm is None: wm = ExpertaWorkingMemory(ke=KnowledgeEngine()) if when is None: when = DQNLearner(gamma=gamma) if prior_skills is None: prior_skills = { "click_done": True, "check": True, "equal": False, "update_answer": True, "update_convert": True, "add": True, "multiply": True, } # Need a working memory class if isinstance(wm, WorkingMemory): self.working_memory = wm if isinstance(wm, ABCMeta): self.working_memory = wm() log.debug(prior_skills) log.debug('GAMMA = %0.2f' % gamma) prior_skills = [ skill_map[s] for s, active in prior_skills.items() if active and s in skill_map ] self.working_memory.add_skills(prior_skills) # will take a activation and facts and return reward if when is not None: self.when_learning = when else: self.when_learning = None self.request_epsilon = request_epsilon self.train_epsilon = train_epsilon self.action_penalty = action_penalty log.debug("action_penalty" + str(action_penalty)) self.negative_actions = negative_actions
def test_update_simple(): class Fruit(Fact): pass wm = ExpertaWorkingMemory() facts = { 1: { 'color': 'red', 'name': 'apple_1', '__class__': Fruit }, 2: { 'color': 'yellow', 'name': 'banana', '__class__': Fruit } } wm.update(facts) fl = wm.facts assert len(fl.keys()) == 3
def test_update_diff(): class Fruit(Fact): pass wm = ExpertaWorkingMemory() facts1 = { 1: { 'color': 'red', 'name': 'apple', '__class__': Fruit }, 2: { 'color': 'yellow', 'name': 'banana', '__class__': Fruit } } wm.update(facts1) fl = wm.facts assert len(fl.keys()) == 3 facts2 = { 1: { 'color': 'orange', 'name': 'orange', '__class__': Fruit }, 2: { 'color': 'yellow', 'name': 'banana', '__class__': Fruit }, 3: { 'color': 'purple', 'name': 'plum', '__class__': Fruit } } diff = jsondiff.diff(facts1, facts2) wm.update(diff) assert len(wm.facts.keys()) == 4
def test_update_1(): initial = { replace: { '?ele-JCommTable5.R0C0': { 'contentEditable': True, 'id': 'JCommTable5.R0C0', 'value': '' }, '?ele-JCommTable6.R0C0': { 'contentEditable': True, 'id': 'JCommTable6.R0C0', 'value': '' }, '?ele-JCommTable8.R0C0': { 'contentEditable': True, 'id': 'JCommTable8.R0C0', 'value': '' }, '?ele-ctatdiv68': { 'id': 'ctatdiv68' }, } } dd = {'?ele-JCommTable6.R0C0': {'contentEditable': False, 'value': '2'}} wm = ExpertaWorkingMemory() wm.update(initial) f = wm.lookup['?ele-JCommTable6.R0C0'] assert f['value'] == '' for f2 in wm.facts.values(): if 'id' in f2: if f2['id'] == f['id']: wmf = f2 assert f.as_dict() == wmf wm.update(dd) assert wm.lookup['?ele-JCommTable6.R0C0'].as_dict()['value'] == '2'
"(e.g., 1,2):") loc = loc.split(',') row = int(loc[0]) col = int(loc[1]) player = game.current_player break except Exception: print("error with input, try again.") return Sai(None, "move", {"row": row, "col": col, "player": player}) if __name__ == "__main__": # with experta knowledge engine wm1 = ExpertaWorkingMemory(ke=ttt_engine()) a1 = SoarTechAgent( # wm=wm1, when=q_learner.QLearner(func=q_learner.Cobweb, q_init=0.0) feature_set=[], function_set=[], wm=wm1, epsilon=0.5, # when=q_learner.QLearner(func=q_learner.LinearFunc, q_init=0.0), negative_actions=True, action_penalty=0.0) new_game = True while new_game: game = ttt_oracle() winner = False last_state = None
from experta import KnowledgeEngine from apprentice.agents import SoarTechAgent from apprentice.working_memory import ExpertaWorkingMemory from ttt_simple import ttt_oracle from examples.ttt_simple import ttt_skill_map if __name__ == "__main__": # with experta knowledge engine wm2 = ExpertaWorkingMemory(ke=KnowledgeEngine()) a1 = SoarTechAgent( wm=wm2, # when=q_learner.QLearner(func=q_learner.Cobweb, q_init=0.0), feature_set=[], function_set=[], negative_actions=True, action_penalty=0.0, epsilon=0.3, skill_map=ttt_skill_map, prior_skills=['make_move']) max_training_games = 100 consecutive_wins = 0 prev_win_board = None i = 0 while consecutive_wins < 5 and i < max_training_games: o = ttt_oracle() winner = False print("#############################") print("Training game {}".format(i))
class KillEngineEmpty(KnowledgeEngine): @DefFacts() def first(self): yield Depressed("JOHN") yield Buy("JOHN", "OBJ1") yield Gun("OBJ1") from apprentice.explain.util import rename_rule_unique if __name__ == "__main__": from apprentice.explain.explanation import Explanation from apprentice.working_memory import ExpertaWorkingMemory new_wm = ExpertaWorkingMemory(KillEngineEmpty()) cf = KillEngine() cf.reset() cf.run(10) facts = cf.facts kill_fact = cf.facts[7] x = Explanation(kill_fact) #print(x.general) #print(x.conditions) c = x.conditions[-1] print("===") r = x.new_rule new_wm.add_rule(r) new_wm.ke.reset()
prior_skills = None # wm = ExpertaWorkingMemory(ke=KnowledgeEngine()) # wm.add_skills(prior_skills) # import collections.OrderedDict if prior_skills is None: prior_skills = { "click_done": False, # True, "check": False, # True, "equal": False, "update_answer": True, "update_convert": False, # , True, "add": False, # True, "multiply": False # , True, } wm = ExpertaWorkingMemory(ke=KnowledgeEngine()) skill_map = experta_skill_map prior_skills = [ skill_map[s] for s, active in prior_skills.items() if active and s in skill_map ] wm.add_skills(prior_skills) temp = wm.ke.matcher # wm.ke.matcher = None c = copy.deepcopy(wm) wm.ke.matcher = temp # self.ke.matcher.__init__(self.ke)