Commit 9ad86f45 authored by mrk022's avatar mrk022

progress on tutor.py and added expression tree class

parent 77efe1b5
# Equation Tree class
# AI Final Project
# Takes an equation of the form x + 3 = 5 + x and makes this into an equation tree
# this means = is in root and each expression is a tree on either side
# currently only works for addition and two variables on each side of equation
class EquationTreeNode:
def __init__(self, value, parent=None):
self.value = value
self.left = None
self.right = None
self.parent = parent
def setLeft(self, newNode):
self.left = newNode
def setRight(self, newNode):
self.right = newNode
def addLeftNode(self, newNode):
self.left = newNode
newNode.parent = self
def addRightNode(self, newNode):
self.right = newNode
newNode.parent = self
def splitExpression(self, expression):
expressions = expression.split("+")
leftNode = EquationTreeNode(expressions[0])
rightNode = EquationTreeNode(expressions[1])
self.addLeftNode(leftNode)
self.addRightNode(rightNode)
'''def main():
equation = "3x+5=2+2x"
expressions = equation.split("=")
rootNode = EquationTreeNode("=", None)
leftNode = EquationTreeNode("+")
rootNode.addLeftNode(leftNode)
splitExpression(expressions[0], leftNode)
rightNode = EquationTreeNode("+")
rootNode.addRightNode(rightNode)
splitExpression(expressions[1], rightNode)
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)
main() '''
\ No newline at end of file
import ccm
import re
from ccm.lib.actr import *
from expressionTree import EquationTreeNode
class UserState(ccm.ProductionSystem):
class StepGenerator:
def __init__(self,equation):
self.equation = equation
class UserState(ccm.Model): #ccm.ProductionSystem
#moveConstants = "0" # move constants is 0 when need to move them, 1 when move variables, 2 when neither
ready = False
def init(self, equation):
def __init__(self, equation):
ccm.Model.__init__(self)
self.equation = equation
self.sides = self.getLeftandRightVals()
self.canMoveConstants()
#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"]
#self.addconstants = ["4x=5x-5","4x+5=5x"]
#self.addvariables = ["7=1x+2", "-1x+7=2"]
self.state = "none"
self.end = False
def get_input():
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"
ready = True
def moveConstants():
'''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")'''
def getLeftandRightVals(self):
expressions = self.equation.split("=")
leftVars = re.split("[+-]", expressions[0])
rightVars = re.split("[+-]", expressions[1])
return [leftVars, rightVars]
def canMoveConstants(self):
constantsOnLeft = 0
variablesOnLeft = 0
constantsOnRight = 0
variablesOnRight = 0
for value in self.sides[0]:
if self.isConstant(value):
constantsOnLeft += 1
else:
variablesOnLeft += 1
for value in self.sides[1]:
if self.isConstant(value):
constantsOnRight += 1
else:
variablesOnRight += 1
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")
def isConstant(self, value):
if 'x' in value:
print(value)
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 moveConstants(self):
print("Constants moved to one side of equation")
#self.state = "move_constants"
self.state = "move_constants"
def moveVariables():
def moveVariables(self):
print("Variables moved to one side of equation")
#self.state = "move_variables"
self.state = "move_variables"
# look at how to structure UserState () to not run every function
class IntelligentTutor(ACTR):
goal = Buffer()
user = UserState()
user = UserState("4x+7=5x+2")
def init(self):
def init():
user.get_input()
goal.set(user.state)
def moved_constants(user="ready:True", goal="move_constants"):
user.moveConstants()
def moved_constants(goal="move_constants"): #user="ready:True"
#user.moveConstants()
print("constants")
goal.set("add_constants")
def add_constants(goal= "add_constants"):
goal.set("move_variables")
print("add")
user.get_input()
goal.set(user.state)
def moved_variables(user="ready:True", goal="move_variables"):
user.moveVariables()
def moved_variables(goal="move_variables"):
#user.moveVariables()
print("variables")
goal.set("add_variables")
def add_variables(goal = "add_variables"):
goal.set()
print("add")
goal.set("end_process")
def end_process(goal = "end_process"):
goal.set("Simulation Complete!")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment