From 6180fd722660bacbc80cb7b78cebd37ff000fcaa Mon Sep 17 00:00:00 2001 From: mrk022 Date: Mon, 2 Dec 2019 15:37:53 -0500 Subject: [PATCH] worked on tutor.py --- tree.py | 20 +++++++++------- tutor.py | 73 +++++++++++++++++++++++++++++++------------------------- 2 files changed, 53 insertions(+), 40 deletions(-) diff --git a/tree.py b/tree.py index ee6add1..a7c020b 100644 --- a/tree.py +++ b/tree.py @@ -17,8 +17,11 @@ class TimesNode: return eval(evalStr) else: return self.left.eval() * self.right.eval()''' - #if isinstance(self.left, VariableNode): - # return self.left.getMultiplier() * self.left.getMultiplier() + #if isinstance(self.left, VariableNode) and isinstance(self.right,VariableNode): + # return self.left.eval()*self.right.eval() + #elif isinstance(self.left,VariableNode) or isinstance(self.right,VariableNode): + # print("can't combine variables of differing types") + #need some sort of combined node here i think #else: return self.left.eval() * self.right.eval() @@ -106,6 +109,7 @@ OPERATORS = set(['+', '-', '*', '/', '(', ')']) PRIORITY = {'+':1, '-':1, '*':2, '/':2} def infix_to_prefix(formula): + # this method was taken from open source https://github.com/lilianweng/LeetcodePython/blob/master/expression.py op_stack = [] exp_stack = [] for ch in formula.split(): @@ -139,14 +143,14 @@ def infix_to_prefix(formula): def eval_tree(expression): - x = NumNode(5) + '''x = NumNode(5) y = NumNode(4) p = PlusNode(x,y) t = TimesNode(p, NumNode(6)) root = PlusNode(t, NumNode(3)) print(root.eval()) - print(root.inorder()) + print(root.inorder())''' #x = input("Please enter a prefix expression: ") x = infix_to_prefix(expression) @@ -159,9 +163,10 @@ def eval_tree(expression): root = E(q) + result = root.eval() print(root.eval()) print(root.inorder()) - + return result #print(infix_to_prefix("3 + 4 - 6")) def split_equation(equation): @@ -169,9 +174,8 @@ def split_equation(equation): return expressions def main(): - exp = split_equation("4x * 3x") - - eval_tree(exp[0]) + exp = split_equation("4x * 3x + 2") + x = eval_tree(exp[0]) if __name__ == "__main__": diff --git a/tutor.py b/tutor.py index 24488d4..79a770a 100644 --- a/tutor.py +++ b/tutor.py @@ -1,7 +1,8 @@ import ccm import re from ccm.lib.actr import * -from expressionTree import EquationTreeNode +#from expressionTree import EquationTreeNode +from tree import * class StepGenerator: def __init__(self,equation): @@ -15,7 +16,7 @@ class UserState(ccm.Model): #ccm.ProductionSystem ccm.Model.__init__(self) self.equation = equation self.sides = self.getLeftandRightVals() - self.canMoveConstants() + self.constantCount = self.getConstantCount() #self.generate_tree() #self.moveconstants = ["4x=5x+2-7","4x+7-2=5x", "7-2=1x", "-1x+7-2=0"] #self.movevariables = ["7=5x-4x+2", "4x-5x+7=2"] @@ -27,26 +28,12 @@ class UserState(ccm.Model): #ccm.ProductionSystem def get_input(self): user_step = input("Please enter your next step: ") #figure out what user step means # call some get_state() function comparing user step to what state should be - self.state = "move_constants" + oldEquation = self.equation + self.equation = user_step + newSides = self.getLeftandRightVals() + self.state = self.get_state(newSides) ready = True - - '''def generate_tree(self): - #equation = "3x+5=2+2x" - expressions = self.equation.split("=") - rootNode = EquationTreeNode("=", None) - - leftNode = EquationTreeNode("+") - rootNode.addLeftNode(leftNode) - leftNode.splitExpression(expressions[0]) - - rightNode = EquationTreeNode("+") - rootNode.addRightNode(rightNode) - rightNode.splitExpression(expressions[1]) - - print("\nEquation Tree is:") - print(" ",rootNode.value) - print(" ", rootNode.left.value, " ", rootNode.right.value) - print(rootNode.left.left.value, rootNode.left.right.value, rootNode.right.left.value, rootNode.right.right.value, "\n")''' + return user_step def getLeftandRightVals(self): expressions = self.equation.split("=") @@ -54,7 +41,7 @@ class UserState(ccm.Model): #ccm.ProductionSystem rightVars = re.split("[+-]", expressions[1]) return [leftVars, rightVars] - def canMoveConstants(self): + def getConstantCount(self): constantsOnLeft = 0 variablesOnLeft = 0 constantsOnRight = 0 @@ -69,6 +56,7 @@ class UserState(ccm.Model): #ccm.ProductionSystem constantsOnRight += 1 else: variablesOnRight += 1 + return [variablesOnLeft, constantsOnLeft, variablesOnRight, constantsOnRight] print("There are ", variablesOnLeft, " variables on the left and ", constantsOnLeft, " constants on the left") print("There are ", variablesOnRight, " variables on the right and ", constantsOnRight, " constants on the right") @@ -79,12 +67,18 @@ class UserState(ccm.Model): #ccm.ProductionSystem return False return True - def get_state(self, user_input): - if user_input in self.moveconstants: - return "move_constants" - elif user_input in self.movevariables: - return "move_variables" - + def get_state(self, newSides): + # based on number of variables constants on each side of the equation, the agent guesses which state the user is moving to + if newSides[0] != self.sides[0] and newSides[2] != newSides[2]: + # if not same number of variables as before for both sides then moved variables + self.state = "move_variables" + elif newSides[0] != self.sides[0] or newSides[2] != newSides[2]: + self.state = "add_variables" + elif newSides[1] != self.sides[1] and newSides[3] != newSides[3]: + self.state = "move_constants" + elif newSides[1] != self.sides[1] or newSides[3] != newSides[3]: + self.state = "add_constants" + # need to do this for division and simplifying fraction def moveConstants(self): print("Constants moved to one side of equation") @@ -106,14 +100,23 @@ class IntelligentTutor(ACTR): goal.set(user.state) def moved_constants(goal="move_constants"): #user="ready:True" - #user.moveConstants() - print("constants") - goal.set("add_constants") + #execute move constants on current equation + #check if user input is the same as what we expect + #if so then + user.get_input() + goal.set(user.state) + #if it is invalid then move to invalid state, and return correct state we found in that function + #print("constants") + #goal.set("add_constants") def add_constants(goal= "add_constants"): - print("add") + # use tree functionality to add constants, pass in expression with two constants to expression tree and solve + # generate new correct equation based on this result + # check if user input is the same as what we caluclated + # if it is correct then user.get_input() goal.set(user.state) + # if it is invalid then move to the invalid state def moved_variables(goal="move_variables"): #user.moveVariables() @@ -124,6 +127,12 @@ class IntelligentTutor(ACTR): print("add") goal.set("end_process") + def incorrect_state(goal = "invalid state"): + print("You have entered an invalid state. We anticipated the correct step to be: \n Please continue solving the problem with the corrected equation above.") + # need to print what correct state would have been + user.get_input() + goal.set(user.state) + def end_process(goal = "end_process"): goal.set("Simulation Complete!") self.stop() -- 2.24.1