def main(): sets=[] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. for file in os.listdir("Problems"): # One problem set per folder in /Problems/ newSet = ProblemSet(file) # Each problem set is named after the folder in /Problems/ sets.append(newSet) for problem in os.listdir("Problems" + os.sep + file): # Each file in the problem set folder becomes a problem in that set. f = open("Problems" + os.sep + file + os.sep + problem) # Make sure to add only problem files to subfolders of /Problems/ newSet.addProblem(f) # Initializing problem-solving agent from Agent.java agent=Agent() # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against each problem set results=open("Results.txt","w") # Results will be written to Results.txt. # Note that each run of the program will overwrite the previous results. # Do not write anything else to Results.txt during execution of the program. for set in sets: results.write("%s\n" % set.getName()) # Your agent will solve one problem set at a time. results.write("%s\n" % "-----------") # Problem sets will be individually categorized in the results file. for problem in set.getProblems(): # Your agent will solve one problem at a time. problem.setAnswerReceived(agent.Solve(problem)) # The problem will be passed to your agent as a RavensProblem object as a parameter to the Solve method # Your agent should return its answer at the conclusion of the execution of Solve. # Note that if your agent makes use of RavensProblem.check to check its answer, the answer passed to check() will be used. # Your agent cannot change its answer once it has checked its answer. result=problem.getName() + ": " + problem.getGivenAnswer() + " " + problem.getCorrect() + " (" + problem.correctAnswer + ")" results.write("%s\n" % result) results.write("\n")
def main(): sets=[] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. for file in os.listdir("Problems"): # One problem set per folder in /Problems/ newSet = ProblemSet(file) # Each problem set is named after the folder in /Problems/ sets.append(newSet) for problem in os.listdir("Problems" + os.sep + file): # Each file in the problem set folder becomes a problem in that set. f = open("Problems" + os.sep + file + os.sep + problem) # Make sure to add only problem files to subfolders of /Problems/ newSet.addProblem(f) # Initializing problem-solving agent from Agent.java agent=Agent() # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against each problem set results=open("Results.txt","w") # Results will be written to Results.txt. # Note that each run of the program will overwrite the previous results. # Do not write anything else to Results.txt during execution of the program. for set in sets: results.write("%s\n" % set.getName()) # Your agent will solve one problem set at a time. results.write("%s\n" % "-----------") # Problem sets will be individually categorized in the results file. for problem in set.getProblems(): # Your agent will solve one problem at a time. problem.setAnswerReceived(agent.Solve(problem)) # The problem will be passed to your agent as a RavensProblem object as a parameter to the Solve method # Your agent should return its answer at the conclusion of the execution of Solve. # Note that if your agent makes use of RavensProblem.check to check its answer, the answer passed to check() will be used. # Your agent cannot change its answer once it has checked its answer. result=problem.getName() + ": " + problem.getGivenAnswer() + " " + problem.getCorrect() + " (Correct Answer: " + problem.checkAnswer("") + ")" results.write("%s\n" % result) results.write("\n")
def load_problems(self): """Load the ProblemSets defined in the filename. Yields: The next ProblemSet defined in the file. """ with open(self.filename + '.txt', 'r') as fin: lines = [a.strip() for a in fin.readlines()] key = lines[0] lines = lines[1:] for line in lines: problem_set = ProblemSet() problem_set.init_line(line, key) yield problem_set
def solve(): sets=[] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. r = open(os.path.join("Problems","ProblemSetList.txt")) # ProblemSetList.txt lists the sets to solve. line = getNextLine(r) # Sets will be solved in the order they appear in the file. while not line=="": # You may modify ProblemSetList.txt for design and debugging. sets.append(ProblemSet(line)) # We will use a fresh copy of all problem sets when grading. line=getNextLine(r) # We will also use some problem sets not given in advance. # Initializing problem-solving agent from Agent.java agent=Agent() # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against each problem set with open("AgentAnswers.csv","w") as results: # Results will be written to ProblemResults.csv. # Note that each run of the program will overwrite the previous results. # Do not write anything else to ProblemResults.txt during execution of the program. results.write("ProblemSet,RavensProblem,Agent's Answer\n") for set in sets: for problem in set.problems: # Your agent will solve one problem at a time. #try: answer = agent.Solve(problem) # The problem will be passed to your agent as a RavensProblem object as a parameter to the Solve method # Your agent should return its answer at the conclusion of the execution of Solve. results.write("%s,%s,%d\n" % (set.name, problem.name, answer)) r.close()
def solve(): sets = [] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. r = open(os.path.join( "Problems", "ProblemSetList.txt")) # ProblemSetList.txt lists the sets to solve. line = getNextLine( r) # Sets will be solved in the order they appear in the file. while not line == "": # You may modify ProblemSetList.txt for design and debugging. sets.append( ProblemSet(line) ) # We will use a fresh copy of all problem sets when grading. line = getNextLine( r) # We will also use some problem sets not given in advance. # Initializing problem-solving agent from Agent.java agent = Agent( ) # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against selected problem for set in sets: if set.name == "Basic Problems D": for problem in set.problems: # Your agent will solve one problem at a time. if problem.name == "Basic Problem D-06": print('\n', problem.name, problem.problemType, problem.problemSetName, \ problem.hasVisual, problem.hasVerbal) answer = agent.Solve(problem) print('Final answer:', str(answer)) r.close()
def main(): sets = [] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. r = open( "Problems" + os.sep + "ProblemSetList.txt") # ProblemSetList.txt lists the sets to solve. line = getNextLine( r) # Sets will be solved in the order they appear in the file. while not line == "": # You may modify ProblemSetList.txt for design and debugging. sets.append( ProblemSet(line) ) # We will use a fresh copy of all problem sets when grading. line = getNextLine( r) # We will also use some problem sets not given in advance. # Initializing problem-solving agent from Agent.java agent = Agent( ) # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against each problem set results = open("ProblemResults.csv", "w") # Results will be written to ProblemResults.csv. # Note that each run of the program will overwrite the previous results. # Do not write anything else to ProblemResults.txt during execution of the program. setResults = open( "SetResults.csv", "w") # Set-level summaries will be written to SetResults.csv. results.write("Problem,Agent's Answer,Correct?,Correct Answer\n") setResults.write("Set,Correct,Incorrect,Skipped\n") for set in sets: for problem in set.problems: # Your agent will solve one problem at a time. #try: problem.setAnswerReceived( agent.Solve(problem) ) # The problem will be passed to your agent as a RavensProblem object as a parameter to the Solve method # Your agent should return its answer at the conclusion of the execution of Solve. # Note that if your agent makes use of RavensProblem.check to check its answer, the answer passed to check() will be used. # Your agent cannot change its answer once it has checked its answer. result = problem.name + "," + str( problem.givenAnswer) + "," + problem.getCorrect() + "," + str( problem.correctAnswer) results.write("%s\n" % result) #except: # print("Error encountered in " + problem.name + ":") # print(sys.exc_info()[0]) # result=problem.name + "," + str(problem.givenAnswer) + ",Error," + str(problem.correctAnswer) # results.write("%s\n" % result) setResult = set.name + "," + str(set.getTotal("Correct")) + "," + str( set.getTotal("Incorrect")) + "," + str(set.getTotal("Skipped")) setResults.write("%s\n" % setResult) results.close() setResults.close()
def solve(): sets = [] r = open(os.path.join("Problems", "ProblemSetList.txt")) line = getNextLine(r) while not line == "": sets.append(ProblemSet(line)) line = getNextLine(r) agent = Agent() with open("AgentAnswers.csv", "w") as results: results.write("ProblemSet,RavensProblem,Agent's Answer\n") for set in sets: for problem in set.problems: answer = agent.Solve(problem) results.write("%s,%s,%d\n" % (set.name, problem.name, answer)) r.close()
def main(): problem_sets = ['Basic Problems B', 'Challenge Problems B'] extractor = RavensShapeExtractor() classifier = RavensShapeTemplateClassifier() templates_to_save = [] print 'Running template recorder for problem sets: {}'.format(problem_sets) for problem_set in problem_sets: problems = ProblemSet(problem_set).problems for problem in problems: skip = raw_input('\nSkip problem {}? '.format(problem.name)).upper() if skip == 'Y': continue problem = RavensVisualProblemFactory().create(problem.problemType, problem.figures) for answer in problem.answers: shapes = extractor.apply(answer) for shape in shapes: _show(shape) correct = 'N' name = None while correct != 'Y': name = raw_input('\nWhat shape is this? ').upper() if name == _SKIP: break correct = raw_input('Is {} correct? '.format(name)).upper() if name == _SKIP: continue templates_to_save.append(RavensShapeTemplateClassifier.Template(name=name, points=shape.contour)) print '\nSaving {} templates'.format(len(templates_to_save)) classifier.save_templates(templates_to_save) print '[DONE]'
def setUp(self): # taken from RavensProject.py self.sets = [] r = open(os.path.join("Problems", "ProblemSetList.txt") ) # ProblemSetList.txt lists the sets to solve. line = getNextLine( r) # Sets will be solved in the order they appear in the file. while not line == "": # You may modify ProblemSetList.txt for design and debugging. self.sets.append( ProblemSet(line) ) # We will use a fresh copy of all problem sets when grading. line = getNextLine(r) r.close() self.agent = Agent() self.problemDict = {} for set in self.sets: for problem in set.problems: self.problemDict[problem.name] = problem
def main(): sets=[] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. r = open("Problems" + os.sep + "ProblemSetList.txt") # ProblemSetList.txt lists the sets to solve. line = getNextLine(r) # Sets will be solved in the order they appear in the file. while not line=="": sets.append(ProblemSet(line)) line=getNextLine(r) # Initializing problem-solving agent from Agent.java agent=Agent() # Running agent against each problem set results=open("ProblemResults.csv","w") setResults=open("SetResults.csv","w") # Set-level summaries will be written to SetResults.csv. results.write("Problem,Agent's Answer,Correct?,Correct Answer\n") setResults.write("Set,Correct,Incorrect,Skipped\n") for set in sets: for problem in set.problems: # Your agent will solve one problem at a time. #try: problem.setAnswerReceived(agent.Solve(problem)) result=problem.name + "," + str(problem.givenAnswer) + "," + problem.getCorrect() + "," + str(problem.correctAnswer) results.write("%s\n" % result) #except: # print("Error encountered in " + problem.name + ":") # print(sys.exc_info()[0]) # result=problem.name + "," + str(problem.givenAnswer) + ",Error," + str(problem.correctAnswer) # results.write("%s\n" % result) setResult=set.name + "," + str(set.getTotal("Correct")) + "," + str(set.getTotal("Incorrect")) + "," + str(set.getTotal("Skipped")) setResults.write("%s\n" % setResult) results.close() setResults.close()
def main(): sets = [] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. r = open( "Problems" + os.sep + "ProblemSetList.txt") # ProblemSetList.txt lists the sets to solve. line = getNextLine( r) # Sets will be solved in the order they appear in the file. while not line == "": # You may modify ProblemSetList.txt for design and debugging. sets.append( ProblemSet(line) ) # We will use a fresh copy of all problem sets when grading. line = getNextLine( r) # We will also use some problem sets not given in advance. # Initializing problem-solving agent from Agent.java agent = Agent( ) # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against each problem set results = open("ProblemResults.csv", "w") # Results will be written to ProblemResults.csv. # Note that each run of the program will overwrite the previous results. # Do not write anything else to ProblemResults.txt during execution of the program. setResults = open( "SetResults.csv", "w") # Set-level summaries will be written to SetResults.csv. results.write("Problem,Correct Confidence,Score,Time\n") setResults.write("Set,Sum Correct Confidence,Sum Score\n") for set in sets: sum_correct_confidence = 0 sum_score = 0 for problem in set.problems: # Your agent will solve one problem at a time. try: start = time.time() problem.setAnswerReceived( agent.Solve(problem) ) # The problem will be passed to your agent as a RavensProblem object as a parameter to the Solve method # Your agent should return its answer at the conclusion of the execution of Solve. # Note that if your agent makes use of RavensProblem.check to check its answer, the answer passed to check() will be used. # Your agent cannot change its answer once it has checked its answer. end = time.time() timeUsed = (end - start) * 1000 correct_confidence = 0 score = 0 if type(problem.givenAnswer) is list: answer = problem.givenAnswer if len(answer) >= problem.correctAnswer: answer = [max(i, 0) for i in answer] if sum(answer) > 0.01: sum_answer = float(sum(answer)) answer = [i / sum_answer for i in answer] correct_confidence = answer[problem.correctAnswer - 1] score = 1 - (1 - correct_confidence)**2 sum_correct_confidence += correct_confidence sum_score += score result = problem.name + "," + str( correct_confidence) + "," + str(score) + "," + str( timeUsed) results.write("%s\n" % result) except: print("Error encountered in " + problem.name + ":") print(traceback.format_exc()) result = problem.name + ",Error,Error, " results.write("%s\n" % result) setResult = set.name + "," + str(sum_correct_confidence) + "," + str( sum_score) setResults.write("%s\n" % setResult) results.close() setResults.close()
from Agent import Agent from ProblemSet import ProblemSet import logging import problem_utils logging.basicConfig() LOGGER = logging.getLogger(__name__) LOGGER.setLevel(logging.DEBUG) agent = Agent() n_correct = 0 n_total = len(ProblemSet("Basic Problems B").problems) for p in ProblemSet("Basic Problems B").problems: LOGGER.info('=================================') LOGGER.info('Solving problem {}'.format(p.name)) if problem_utils.is_problem2x2(p): source = p.figures['A'] destination = p.figures['B'] guess = agent.Solve(p) answer = p.checkAnswer(guess) if guess == answer: LOGGER.info('{}++++++++++++Correct+++++++++++++'.format(p.name)) n_correct += 1 else: LOGGER.error('Wrong') else: print 'Not 2x2 problem' print('Total correct answers {} out of {}'.format(n_correct, n_total))
#from __future__ import absolute_import import unittest import pprint from Agent import Agent from ProblemSet import ProblemSet agent = Agent() problemSet = ProblemSet('Basic Problems B') class AgentTest(unittest.TestCase): def testB1(self): # objects.attributes # {'fill': 'yes', 'shape': 'square', 'size': 'very large'} pprint.pprint(problemSet.problems[0].problemType) self.assertEqual(agent.Solve(problemSet.problems[5]), 2) def testB2(self): pprint.pprint(problemSet.problems[1].problemType) self.assertEqual(agent.Solve(problemSet.problems[1]), 5) def testB3(self): pprint.pprint(problemSet.problems[2].problemType) self.assertEqual(agent.Solve(problemSet.problems[2]), 1) def testB4(self): pprint.pprint(problemSet.problems[3].problemType) self.assertEqual(agent.Solve(problemSet.problems[3]), 3)
def main(): parser = argparse.ArgumentParser( description='Filters for what problems to solve') parser.add_argument( '--problem_sets', type=str, choices=['Basic', 'Challenge', 'Classmates', 'Unordered', 'Test'], nargs='*') parser.add_argument('--problems', type=int, nargs='*') parser.add_argument('--interactive', type=bool, default=False) args = parser.parse_args() sets = [] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. for file in os.listdir( "Problems"): # One problem set per folder in /Problems/ if file.startswith('.'): continue newSet = ProblemSet( file) # Each problem set is named after the folder in /Problems/ sets.append(newSet) for problem in os.listdir( "Problems" + os.sep + file ): # Each file in the problem set folder becomes a problem in that set. if problem.startswith('.'): continue f = open( "Problems" + os.sep + file + os.sep + problem ) # Make sure to add only problem files to subfolders of /Problems/ newSet.addProblem(f) # Initializing problem-solving agent from Agent.java agent = Agent( ) # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against each problem set results = open("Results.txt", "w") # Results will be written to Results.txt. # Note that each run of the program will overwrite the previous results. # Do not write anything else to Results.txt during execution of the program. for set in sets: if args.problem_sets and set.getName().split( )[1] not in args.problem_sets: continue results.write( "%s\n" % set.getName()) # Your agent will solve one problem set at a time. results.write( "%s\n" % "-----------" ) # Problem sets will be individually categorized in the results file. print set.getName() correct = 0 for problem in set.getProblems( ): # Your agent will solve one problem at a time. if args.problems and int( problem.getName().split()[3]) not in args.problems: continue problem.setAnswerReceived( agent.Solve(problem) ) # The problem will be passed to your agent as a RavensProblem object as a parameter to the Solve method # Your agent should return its answer at the conclusion of the execution of Solve. # Note that if your agent makes use of RavensProblem.check to check its answer, the answer passed to check() will be used. # Your agent cannot change its answer once it has checked its answer. result = problem.getName() + ": " + problem.getGivenAnswer( ) + " " + problem.getCorrect() + " (" + problem.correctAnswer + ")" if problem.getCorrect() == 'Correct': correct += 1 results.write("%s\n" % result) results.write("\n") print "%d correct" % correct
return score def solve_problem(problem): """ Given problem definition provide the best solution :param problem: problem definition :return: solution which has highest score """ A = problem.figures['A'] B = problem.figures['B'] C = problem.figures['C'] choices = problem_utils.problem_choices(problem) ab_rules = transformation_rules(A, B) cx_rules = { c: transformation_rules(C, figure) for c, figure in choices.iteritems() } scores = { choice: similarity_score(ab_rules, rules) for choice, rules in cx_rules.iteritems() } LOGGER.debug('Scores for problem {}: {}'.format(problem.name, scores)) guess = max(scores.iteritems(), key=operator.itemgetter(1))[0] LOGGER.debug('Best guess: {}'.format(guess)) return int(guess) if __name__ == '__main__': solve_problem(ProblemSet('Basic Problems B').problems[5])
def main(): sets = [] # The variable 'sets' stores multiple problem sets. # Each problem set comes from a different folder in /Problems/ # Additional sets of problems will be used when grading projects. # You may also write your own problems. r = open("Problems" + os.sep + "ProblemSetListTemp.txt" ) # ProblemSetList.txt lists the sets to solve. line = getNextLine( r) # Sets will be solved in the order they appear in the file. while not line == "": # You may modify ProblemSetList.txt for design and debugging. sets.append( ProblemSet(line) ) # We will use a fresh copy of all problem sets when grading. line = getNextLine( r) # We will also use some problem sets not given in advance. # Initializing problem-solving agent from Agent.java agent = Agent( ) # Your agent will be initialized with its default constructor. # You may modify the default constructor in Agent.java # Running agent against each problem set results = open("ProblemResults.csv", "w") # Results will be written to ProblemResults.csv. # Note that each run of the program will overwrite the previous results. # Do not write anything else to ProblemResults.txt during execution of the program. setResults = open( "SetResults.csv", "w") # Set-level summaries will be written to SetResults.csv. results.write("Problem,Correct Confidence\n") setResults.write("Set,Sum Correct Confidence\n") for set in sets: sum_correct_comfidence = 0 for problem in set.problems: # Your agent will solve one problem at a time. try: problem.setAnswerReceived( agent.Solve(problem) ) # The problem will be passed to your agent as a RavensProblem object as a parameter to the Solve method # Your agent should return its answer at the conclusion of the execution of Solve. # Note that if your agent makes use of RavensProblem.check to check its answer, the answer passed to check() will be used. # Your agent cannot change its answer once it has checked its answer. correct_comfidence = 0 if type(problem.givenAnswer) is list: answer = problem.givenAnswer if len(answer) >= problem.correctAnswer: if sum(answer) > 1: sum_answer = float(sum(answer)) answer = [i / sum_answer for i in answer] correct_comfidence = answer[problem.correctAnswer - 1] sum_correct_comfidence += correct_comfidence result = problem.name + "," + str(correct_comfidence) results.write("%s\n" % result) except Exception as e: exceptionType, exceptionValue, exceptionTraceback = sys.exc_info( ) print("*** print_tb:") traceback.print_tb(exceptionTraceback, limit=1, file=sys.stdout) print("*** print_exception:") traceback.print_exception(exceptionType, exceptionValue, exceptionTraceback, limit=2, file=sys.stdout) print("*** print_exc:") traceback.print_exc() print("*** format_exc, first and last line:") formatted_lines = traceback.format_exc().splitlines() print(formatted_lines[0]) print(formatted_lines[-1]) print("*** format_exception:") print( repr( traceback.format_exception(exceptionType, exceptionValue, exceptionTraceback))) print("*** extract_tb:") print(repr(traceback.extract_tb(exceptionTraceback))) print("*** format_tb:") print(repr(traceback.format_tb(exceptionTraceback))) #print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback))''' print("Error encountered in " + problem.name + ":") print(sys.exc_info()[0]) #result=problem.name + "," + str(problem.givenAnswer) + ",Error," #results.write("%s\n" % result) setResult = set.name + "," + str(sum_correct_comfidence) setResults.write("%s\n" % setResult) results.close() setResults.close()