Skip to content

Commit

Permalink
Enhances hw checks, corrects bugs in IOPairs modeling
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkar3 committed May 24, 2017
1 parent 1099c9f commit 08f74f8
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 92 deletions.
7 changes: 4 additions & 3 deletions assignment/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def iosource_import_pairs(a):
csv.field_size_limit(sys.maxsize)
try:
if a.url is None:
message = "Assignment %s does not have url for codework" % a
message = "Assignment %s does not have url for codework. " % a
log.error(message)
return message
http = urllib3.PoolManager()
Expand All @@ -26,7 +26,7 @@ def iosource_import_pairs(a):
log.debug("Adding %s: %s" % (row[0], row[1]))
if iopairs.add(a, row[0], row[1]):
count = count + 1
message = "Added %s IOPairs to %s" % (count, a)
message = "Added %s IOPairs to %s. " % (count, a)
return message
except Exception as e:
message = str(e)
Expand All @@ -39,9 +39,10 @@ class AssignmentAdmin(admin.ModelAdmin):
actions = ['import_pairs']

def import_pairs(self, request, queryset):
ret = ""
for query in queryset:
log.info("Importing IOPairs for %s from %s" % (query, query.url))
ret = iosource_import_pairs(query)
ret += iosource_import_pairs(query)
self.message_user(request, "%s" % (ret))
import_pairs.short_description = "Import IO pairs from selected source"

Expand Down
120 changes: 49 additions & 71 deletions assignment/hw_check.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
import math

import json

def is_number(s):
'''
Expand All @@ -13,16 +13,17 @@ def is_number(s):
except ValueError:
return False

def check_hw1(s):
def check_floating_point_answer(s):
max_score = 100./s.assignment.num_codeproblems
output = s.pair.output
if s.output_submitted:
if is_number(s.output_submitted):
if math.fabs(float(s.output_submitted) - float(output)) < 0.01:
s.comments = "Answer is correct."
if s.updated < s.assignment.due_date:
s.score = "10.0"
s.score = max_score
else:
s.score = "5.0"
s.score = max_score/2.0
else:
s.comments = "Answer is wrong. ("+ s.output_submitted +")"
else:
Expand All @@ -31,68 +32,69 @@ def check_hw1(s):
s.comments = "No solution yet."
s.save()

def check_hw2(s):

# Two Armed Bandit
def check_two_armed_bandit(s):
max_score = 100./s.assignment.num_codeproblems
output = s.pair.output
if s.output_submitted:
if is_number(s.output_submitted):
if math.fabs(float(s.output_submitted) - float(output)) < 0.01:
s.comments = "Answer is correct."
try:
if s.output_submitted:
output = output.strip('{}()[]')
output_submitted = s.output_submitted.strip('{}()[]')
output = output.lower()
output = output.replace("value=", "")
output_submitted = output_submitted.lower()
output_submitted = output_submitted.replace("value=", "")
if math.fabs(float(output) - float(output_submitted)) > 1.00:
s.score = 0
s.comments = "Solution is wrong."
else:
s.comments = "Solution is correct."
if s.updated < s.assignment.due_date:
s.score = "20.0"
s.score = max_score
else:
s.score = "10.0"
else:
s.comments = "Answer is wrong. ("+ s.output_submitted +")"
s.score = max_score/2.0
else:
s.comments = "Answer is not a number."
else:
s.comments = "No solution yet."
s.save()
s.comments = "No solution yet."
except Exception as e:
s.comments = '%s (%s)' % (e.message, type(e))
finally:
s.save()

# Old homeworks
def check_hw3(s):
max_score = 100./s.assignment.num_codeproblems
output = s.pair.output
if s.output_submitted:
nums = re.compile(r"[+-]?\d+(?:\.\d+)?")
o = re.findall(nums, output)
o_s = re.findall(nums, s.output_submitted)
if len(o) != 3:
s.comments = "Input is wrong. Send the input to TA."
s.score = "0.0"
s.score = 0.0
s.save()
return
if len(o_s) != 3:
s.comments = "You need to give atleast 3 numbers: bestX=1,bestY=2,LInfinityDistance=3."
s.score = "0.0"
s.score = 0.0
s.save()
return

if int(o[2]) == int(o_s[2]):
s.comments = "LInfinityDistance Value is correct."
if s.updated < s.assignment.due_date:
s.score = "20.0"
s.score = max_score
else:
s.score = "10.0"
s.score = max_score/2.0
else:
s.comments = "LInfinityDistance of " + str(o_s[2]) + " is wrong. Try again."
else:
s.comments = "No solution yet."
s.save()

def check_hw4(s):
if s.output_submitted:
try:
json_object = json.loads(s.output_submitted)
s.comments = "String is a valid JSON. We will validate the answer soon."
except ValueError:
s.comments = "String is not a valid JSON."
finally:
s.save()
else:
s.comments = "No solution yet."
s.save()

# HW4 of 2017
def check_hw5(s):
max_score = 100./s.assignment.num_codeproblems
max_attempts = 15
output = s.pair.output
if s.output_submitted:
Expand All @@ -102,9 +104,9 @@ def check_hw5(s):
if math.fabs(float(s.output_submitted) - float(output)) < 0.01:
s.comments = "Answer is correct."
if s.updated < s.assignment.due_date:
s.score = "10.0"
s.score = max_score
else:
s.score = "5.0"
s.score = max_score/2.0
else:
s.comments = "Answer is wrong. ("+ s.output_submitted +")."
else:
Expand All @@ -120,8 +122,10 @@ def check_hw5(s):
s.comments += " " + str(s.count) + " attempts. Maximum number of attempts excedded."
s.save()


# Messing with Rewards
def check_hw7(s):
def check_messing_with_rewards(s):
max_score = 100./s.assignment.num_codeproblems
output = s.pair.output
try:
if s.output_submitted:
Expand All @@ -139,9 +143,9 @@ def check_hw7(s):
if len(err) == 0:
s.comments = "Solution is correct."
if s.updated < s.assignment.due_date:
s.score = "30.0"
s.score = max_score
else:
s.score = "15.0"
s.score = max_score/2.0
else:
if len(err) == 1:
s.comments = "Value at state " + str(err[-1]) + " is wrong. You are close!"
Expand Down Expand Up @@ -182,12 +186,12 @@ def check_hw6(s):
if len(err) == 0:
s.comments = "Solution is correct."
if s.updated < s.assignment.due_date:
s.score = "100.0"
s.score = 100.0
else:
s.score = "50.0"
s.score = 100.0/2.0
else:
if len(err) > 100:
s.score = "10.0"
s.score = 10.0
else:
s.score = hw6_score(len(err))
s.comments = str(len(err)) + " values are wrong."
Expand All @@ -206,6 +210,7 @@ def check_hw6(s):

# Continuous MDP problem
def check_hw7_old(s):
max_score = 100./s.assignment.num_codeproblems
output = s.pair.output
try:
if s.output_submitted:
Expand All @@ -227,9 +232,9 @@ def check_hw7_old(s):
if len(err) == 0:
s.comments = "Solution is correct."
if s.updated < s.assignment.due_date:
s.score = "20.0"
s.score = max_score
else:
s.score = "10.0"
s.score = max_score/2.0
else:
s.score = "0"
s.comments = "Solution is wrong."
Expand All @@ -241,30 +246,3 @@ def check_hw7_old(s):
s.comments = '%s (%s)' % (e.message, type(e))
finally:
s.save()

# Two Armed Bandit
def check_hw8(s):
output = s.pair.output
try:
if s.output_submitted:
output = output.strip('{}()[]')
output_submitted = s.output_submitted.strip('{}()[]')
output = output.lower()
output = output.replace("value=", "")
output_submitted = output_submitted.lower()
output_submitted = output_submitted.replace("value=", "")
if math.fabs(float(output) - float(output_submitted)) > 1.00:
s.score = "0"
s.comments = "Solution is wrong."
else:
s.comments = "Solution is correct."
if s.updated < s.assignment.due_date:
s.score = "20.0"
else:
s.score = "10.0"
else:
s.comments = "No solution yet."
except Exception as e:
s.comments = '%s (%s)' % (e.message, type(e))
finally:
s.save()
21 changes: 5 additions & 16 deletions assignment/iosolutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def update(solution, output=None, submit_late="false"):
if len(output) > 100000:
return "Solution length should be less than 100,000 chars."
solution.output_submitted = output
solution.count = solution.count + 1
solution.save()
return "Solution submitted."
else:
Expand All @@ -94,22 +95,10 @@ def update(solution, output=None, submit_late="false"):
def check(solutions):
for s in solutions:
a_name = s.assignment.short_name
if a_name == "hw1":
hw_check.check_hw1(s)
elif a_name == "hw2":
hw_check.check_hw2(s)
elif a_name == "hw3":
hw_check.check_hw4(s)
elif a_name == "hw4":
hw_check.check_hw4(s) # Needs to change later
elif a_name == "hw5":
hw_check.check_hw7(s)
elif a_name == "hw6":
hw_check.check_hw6(s)
elif a_name == "hw7":
hw_check.check_hw7(s)
elif a_name == "hw8":
hw_check.check_hw8(s)
if a_name == "bandit":
hw_check.check_two_armed_bandit(s)
else:
hw_check.check_floating_point_answer(s)

def get_stats(solutions):
stats = {}
Expand Down
4 changes: 3 additions & 1 deletion assignment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Assignment(models.Model):
end_date = models.DateTimeField(null=True)
released = models.BooleanField(default=False)
enable_codework = models.BooleanField(default=False)
num_codeproblems = models.PositiveIntegerField(default=10)
url = models.CharField(max_length=200, null=True)

def __str__(self):
Expand Down Expand Up @@ -65,11 +66,12 @@ class IOSolution(models.Model):
''' Contains Input/Output pairs submited by students '''
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
count = models.PositiveIntegerField(default=0)
student = models.ForeignKey(Student)
assignment = models.ForeignKey(Assignment)
pair = models.ForeignKey(IOPair)
output_submitted = models.CharField(max_length=100000, null=True, blank=True)
score = models.CharField(max_length=10, default="0.0")
score = models.FloatField(default=0.0)
comments = models.CharField(max_length=2000, null=True, blank=True)

def __str__(self):
Expand Down
2 changes: 1 addition & 1 deletion assignment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def code(request, a_name):
else:
time_left = "(Deadline Passed)"

solutions = iosolutions.get(s, a, 10)
solutions = iosolutions.get(s, a, a.num_codeproblems)
stats = iosolutions.get_stats(solutions)

return render(request, 'codework_work.html', {
Expand Down

0 comments on commit 08f74f8

Please sign in to comment.