-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestionsOptimization.py
More file actions
536 lines (438 loc) · 17.3 KB
/
QuestionsOptimization.py
File metadata and controls
536 lines (438 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
"""
Module for Questions Optimization
A program that simulates a scenario where you are given
a quiz with n multiple-choice questions with m possible choices
on each question. After choosing answers the first time, you are
given the total number of questions answered correctly, and you
provide the certainty that your answer is correct for each question.
The program should optimize the minimum number of attempts afterward
to produce a perfect-score quiz given that each sequential attempt
enables the computer to change answers how they see fit, and their
new score is recorded. Logic is still a work in progress.
"""
import math
import random
class Runner(object):
"""
The simulator class for the program
The runner is responsible for tracking progress through Generation objects
and making decisions on which questions to switch answers to
Optimization:
Load in the best Generation (generation with highest score)
Recalculate certainties based on most recent Generation
Assuming n switches to questions...
Determine the number of correct/incorrect switches
If score increases
By how many questions?
N switches, score increases by k Qs, then at least n-k incorrect Qs in cluster
Loop through each question to determine P(their switch correct)?
Find new certainties?
If score stays same
ceil(n/2) incorrect questions
Either all correct, or pairs of Qs containing opposing score-changing results
eg. Score-changing: switching from I to C or C to I
If score decreases
By how many questions? Say m
Then there are at least n-m correct questions
Attribute _quiz: the quiz to be completed
Invariant: _quiz is a Quiz object and inaccessible
Attribute _gens: a list of all generation completed
Invariant: _gens is an inaccesible list of Generation objects
"""
def __init__(self, numQs):
"""
Initializes a new Quiz and provides the number of questions
Parameter numQs: the number of questions in the Quiz
Precondition: numQs is an int greater than zero
"""
assert type(numQs) == int and numQs > 0
self._quiz = Quiz(numQs)
self.printQuiz()
self._gens = [Generation(1, self._quiz)]
print('\nRunning initial second generation...\n')
if self._quiz.getScore() == 1.0:
print('Quiz Completed. 1 generation needed')
else:
incorrect = int(round((1.0 - self._quiz.getScore())*numQs,0))
lowestcerts = []
for n in range(incorrect):
lowcert = 1.0
lowcertIndex = 0
for q in range(len(self._quiz.getQuestions())):
if lowcert > self._quiz.getQuestions()[q].getCertainty() \
and not (self._quiz.getQuestions()[q] in lowestcerts):
lowcert = self._quiz.getQuestions()[q].getCertainty()
lowcertIndex = q
print(str(lowcertIndex))
lowestcerts.append(self._quiz.getQuestions()[lowcertIndex])
for q in lowestcerts:
q.switchSelection()
self.printQuiz()
self._gens.append(Generation(2,self._quiz))
def nextGen(self):
"""
UNFINISHED!
"""
latGenScore = self._gens[-1].getScore()
prevGenScore = self._gens[-2].getScore()
differential = int(round((latGenScore-prevGenScore)*numQs,0))
#If score increases
if differential > 0:
pass
#5 questions switched, +2 differential
#Revert, switch 3
def printQuiz(self):
"""
Prints all relevant quiz information to console
"""
try:
print('Generation: ' + str(self.gens[-1].getGenNum()))
print('Quiz:\n'+str(self._quiz)+'Score: '+str(self._quiz.getScore()))
except AttributeError:
print('Quiz:\n'+str(self._quiz)+'Score: '+str(self._quiz.getScore()))
def selectN(self, elist, n):
"""
Returns a list of every combination of size n of elements in the list
Containing the first element in the list
Parameter list: the list of elements to create combinations
Precondition: list is a list
Parameter n: the number of elements for each combination
Precondition: n is an int greater than 0
"""
assert isinstance(elist, list)
assert type(n) == int
if n == 0:
return []
if elist == []:
return []
elif len(elist) == 1:
return elist[:]
gen = self.genIndices(elist)
return self.selectN
# first = [elist[index]]
# els = elist[:index] + elist[index+1:]
# return self.selectN(els,n-1)
# first = [elist[len(list)-1]]
# els = elist[:index]
# return self.selectN(els,n-1)
def genIndices(self, input, n):
"""
Generates indices from 0 to len(input) and the recursive process
Parameter input: the list of elements to generate indices
Precondition: input is a list
Parameter n: the number of elements for each combination
Precondition: n is an int greater than 0
"""
assert type(input) == list
assert type(n) == int
if n == 0:
try:
for x in input:
yield [x]
except StopIteration:
pass
new_gen = self.genIndices(input[1:],n-1)
try:
for x in input:
for y in range(len(input[1:])):
yield [x]+next(new_gen)
except StopIteration:
pass
class Generation(object):
"""
An object that stores information about which generation this information
pertains to, what the results of the quiz were, and
what the scope of question certainties were like at the time
"""
# HIDDEN ATTRIBUTES:
#
# Attribute _genNum: Stores what generation number this info pertains to
# Invariant: _genNum is an int > 0
#
# Attribute _score: the results of the quiz in the generation
# Invariant: _score is a float between 0.0 and 1.0 inclusive
#
# Attribute _certs: the list of certainties for each question
# Invariant: _certs is a list of floats
def getGenNum(self):
"""
Returns the generation number for the information
"""
return self._genNum
def setGenNum(self, num):
"""
Sets the generation number related to this information
Parameter num: the generation number
Precondition: num is an int greater than zero
"""
assert type(num) == int and num > 0
self._genNum = num
def getScore(self):
"""
Returns the score acquired on the quiz
"""
return self._score
def setScore(self, score):
"""
Sets the score earned on the quiz
Parameter score: the results of the quiz in the generation
Precondition: is a float between 0.0 and 1.0 inclusive
"""
assert type(score) == float
assert score >= 0.0 and score <= 1.0
self._score = score
def getCerts(self):
"""
Returns a list of the certainties for the questions (ordered by each Q)
"""
return self._certs
def setCerts(self, quiz):
"""
Creates and sets the certainty list for the question
Parameter quiz: a Quiz object with the current quiz results
Precondition: quiz is a Quiz object
"""
assert isinstance(quiz, Quiz)
self._certs = []
for q in quiz.getQuestions():
self._certs.append(q.getCertainty())
def __init__(self, genNum, quiz):
"""
Initializes a generation object by specifying the generation number,
the score on the quiz, and providing the quiz object for accessing
question certainties
Parameter num: the generation number
Precondition: num is an int greater than zero
Parameter quiz: a Quiz object with the current quiz results
Precondition: quiz is a Quiz object
"""
self.setGenNum(genNum)
self.setScore(quiz.getScore())
self.setCerts(quiz)
class Quiz(object):
"""
The class for the quiz – essentially a list of Question Objects
"""
# HIDDEN ATTRIBUTES:
#
# Attribute _questions: all of the questions for the quiz
# Invariant: _questions is a list of Question objects of length numQ
def getQuestions(self):
"""
Returns the questions list
"""
return self._questions
def __init__(self, numQ=0, numChoices=4, quiz=None):
"""
Initializes a quiz object, which creates numQ list of question objects
"""
assert type(numQ) == int and numQ > 0
assert type(numChoices) == int and numChoices > 0
assert quiz is None or isinstance(quiz, Quiz)
self._questions = []
if quiz is None:
for n in range(numQ):
self._questions.append(self._newQuestion(numChoices))
else:
self._questions = quiz
def _newQuestion(self, numChoices):
"""
Creates a new question with randomly assigned attributes
Returns the created question object
Parameter numChoices: the number of choices in the question
"""
#randomly generate certainty and assign answer
c = math.sqrt(random.random())
answer = random.randint(1,numChoices)
#collect all the incorrect answer selections
incAnsws = []
for x in range(numChoices):
if x != answer:
incAnsws.append(x)
#certainty represents probability user selection is correct
#Establishes user selection given the probabiility that
#they respond correctly is c
if random.random() < c:
select = answer
else:
incAns = random.randint(0,len(incAnsws)-1)
select = incAnsws[incAns]+1
return Question(numChoices, answer, c, select)
def addQuestion(self, choice, answer, certainty):
"""
Adds a new question to the quiz
Parameter choice: the number of possible choices for a question
Precondition: choice is an int and greater than zero
Parameter answer: the correct answer choice (a number)
Precondition: answer is an int between 0 and numChoices-1 inclusive
Parameter certainty: A number representing how certain the computer/person
is that they have answered this question correctly
Precondition: certainty is a float between 0.0 and 1.0 inclusive
Preconditions handled in Question class
"""
self._questions.append(Question(choice, answer, certainty))
def getScore(self):
"""
Returns the portion of questions answered correctly in this quiz
as a decimal of the total number of questions
"""
correct = 0
for question in self._questions:
if question.isCorrect():
correct += 1
return float(correct/len(self._questions))
def __str__(self):
"""
Print version of a Quiz object (prints each question)
"""
result = ''
for q in self._questions:
result += q.__str__() + '\n'
return result
class Question(object):
"""
A class for the question object, containing information
about the question, its answer, and the certainty of the computer
that this question is answered correctly
NOTE: Knowing the question is not important, as certainty for each question
will be inputted by the user for the first generation
"""
# HIDDEN ATTRIBUTES:
#
# Attribute _numChoices: the number of possible choices for a question
# Invariant: _numChoices is an int and greater than zero
#
# Atribute _answer: the correct answer choice (a number)
# Invariant: _answer is an int between 0 and numChoices inclusive
#
# Attribute _certainty: A number representing how certain the computer/person
# is that they have answered this question correctly
# Invariant: _certainty is a float between 0.0 and 1.0 inclusive
#
# Atribute _selection: the user-selected choice (a number)
# Invariant: _selection is an int between 0 and numChoices inclusive
#
# Attribute _answersChosen: an immutable attribute storing all selections
# that the user has already made on the question (to elimintae repeat selections)
# Invariant: _answersChosen is a list of ints
def setChoices(self, choice):
"""
Sets the number of choices for the question
Parameter numChoices: the number of possible choices for a question
Precondition: numChoices is an int and greater than zero
"""
assert type(choice) == int and choice > 0
self._numChoices = choice
def getChoices(self):
"""
Returns the number of choices for the question
"""
return self._numChoices
def setAnswer(self, answer):
"""
Sets the correct answer for the question. When choices are ordered,
answer is the index+1 choice for the question
Parameter answer: the correct answer choice (a number)
Precondition: answer is an int between 0 and numChoices inclusive
"""
assert type(answer) == int
assert answer > 0 and answer <= self._numChoices
self._answer = answer
def getAnswer(self):
"""
Returns the answer choice as an int
"""
return self._answer
def setCertainty(self, certainty):
"""
Sets the certainty of the question to a decimal probability
Parameter certainty: A number representing how certain the computer/person
is that they have answered this question correctly
Precondition: certainty is a float between 0.0 and 1.0 inclusive
"""
assert type(certainty) == float
assert certainty > 0.0 and certainty < 1.0
self._certainty = certainty
def getCertainty(self):
"""
Returns the certainty of the computer/person for the question
"""
return self._certainty
def setSelection(self, select):
"""
Sets the response the user thinks is the correct answer
Parameter select: the user-selected choice (a number)
Precondition: select is an int between 0 and numChoices inclusive
"""
assert type(select) == int
assert select > 0 and select <= self._numChoices
self._selection = select
def getSelection(self):
"""
Returns what the user responds as the correct answer
"""
return self._selection
def getAnswersChosen(self):
"""
Returns the list of selections the user has already made including
the current selection
"""
return self._answersChosen
def getAnswersAvailable(self):
"""
Returns a list of all the selections still to be chosen
"""
answsAvail = []
canAdd = True
for select in range(1,self._numChoices+1):
for choice in self._answersChosen:
if select == choice:
canAdd = False
if(canAdd):
answsAvail.append(select)
canAdd = True
return answsAvail
def __init__(self, choice, answer, certainty, select = None):
"""
Initializes a question object with given number of choices,
the correct answer choice, and certainty for the question
Parameter choice: the number of possible choices for a question
Precondition: choice is an int and greater than zero
Parameter answer: the correct answer choice (a number)
Precondition: answer is an int between 0 and numChoices-1 inclusive
Parameter certainty: A number representing how certain the computer/person
is that they have answered this question correctly
Precondition: certainty is a float between 0.0 and 1.0 inclusive
"""
self.setChoices(choice)
self.setAnswer(answer)
self.setCertainty(certainty)
self.setSelection(select)
self._answersChosen = [select]
def __str__(self):
"""
Print version of a question object
"""
return 'Question(Choices: ' + str(self._numChoices) + ' Answer: ' + \
str(self._answer) + ' Certainty: ' + str(self._certainty) + \
' Selection: ' + str(self._selection) + ')'
def isCorrect(self):
"""
Returns True if answer matches the user selection, False otherwise
"""
return self._answer == self._selection
def switchSelection(self):
"""
Switches the selection of the user to a new, unused selection
"""
answsAvail = self.getAnswersAvailable()
if answsAvail == []:
print('No more available answers to pick!')
else:
if len(answsAvail) == 1:
self.setSelection(answsAvail[0])
else:
rand = random.randint(0,len(answsAvail)-1)
self.setSelection(answsAvail[rand])
self._answersChosen.append(self._selection)
r = Runner(numQs=10)