-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestions.py
303 lines (235 loc) · 9.8 KB
/
questions.py
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
import random
from graph_nodes import Graph
from lines import lines as l
import numpy as np
class Graphing_question():
def __init__(self, access_mode, nec_info):
#access_modes - y-intercept,x-intercept,area to shade, click
#nec_info is majorly a line
"""
Parameters
----------
access_mode : STRING
SUB AREA OF GRAPHING LINEAR PROGRAMMING PROBLEMS
nec_info : ANY
INFO NEEDED TO MAKE QUESTION
part:int
Phase of question- 1 x or y intercept ,2 - drawing and shading
Returns
-------
None.
"""
self.access_mode = access_mode
self.nec_info = nec_info
if self.access_mode =="area to shade":
self.part = 2
elif self.access_mode == "y-intercept" :
self.part =1
elif self.access_mode == "x-intercept":
self.part = 1
if self.part:
self.style = random.randint(0,1)
self.question_stems = {"c":[f"What is the {access_mode} of the line {nec_info}?",
f"is the {access_mode} of the line {nec_info}"]}
self.question_stem = self.question_stems["c"][self.style]
def get_question(self, question_type, axes = None, lenaxis = None):
self.question_type = question_type
if question_type == "fill_blank":
if self.access_mode == "y-intercept" :
self.answer = self.nec_info.c
elif self.access_mode == "x-intercept":
self.answer = self.nec_info.xc
elif self.access_mode == "area to shade":
places, letters, correct = put_letters(self.nec_info, lenaxis, axes)
self.answer = letters[places.index(correct)]
#print(self.answer)
return self,self.question_stem
elif question_type == "MCQ":
if self.part ==1:
a = f"({self.nec_info.xc},0)"
b = f"({self.nec_info.c},0)"
c = f"(0,{self.nec_info.xc})"
d = f"(0,{self.nec_info.c})"
e = f"(0,0)"
self.options = [a,b,c,d,e]
if self.access_mode == "y-intercept":
self.answer = d
elif self.access_mode == "x-intercept":
self.answer = a
return self,self.question_stem,self.options
elif self.part == 2:
places, letters, correct = put_letters(self.nec_info, lenaxis, axes)
a,b = letters
self.options = [a,b]
self.answer = self.options[places.index(correct)]
return self, self.question_stem,self.options
elif question_type == "touch":
self.question_stem = "Click on the part of graph that should be shaded out"
return self, self.question_stem
def validate(equation,event):
"""function to validate where user clicks"""
if equation.status == ">=":
if (event.xdata * equation.x + event.ydata * equation.y) > equation.rhs:
return "Wrong"
else:
return "CorrecT"
elif equation.status == "<=":
if event.xdata * equation.x + event.ydata * equation.y < equation.rhs:
return "Wrong"
else:
return "CorrecT"
def validates(equations,event):
"""Validates user click in feasible region"""
for equation in equations:
lhs = equation.x *event.xdata + equation.y * event.ydata
if equation.status == "<=":
if lhs < equation.rhs:
continue
else:
return False
elif equation.status == ">=":
if lhs > equation.rhs:
continue
else:
return False
return True
def suit_y(ob_func,entry, lenaxis):
"""Testing for a suitable yintercept for objective function"""
#creating a new line and testing to see if c will be suitable
equation = l(x = ob_func.x,y = ob_func.y,rhs =ob_func.test)
if equation.m >0 :
new_eq = l(equation.x,equation.y,entry)
if lenaxis>new_eq.c > 0 or 0<new_eq.xc < lenaxis:
hold = True
else:
hold = False
elif equation.m < 0:
new_eq = l(equation.x,equation.y, entry)
if new_eq.c > 0 and new_eq.output(lenaxis) < lenaxis:
hold = True
else:
hold = False
elif equation.m == 0:
if entry > 0 and entry < lenaxis:
hold = True
else:
hold = False
elif equation.m == np.inf:
if entry == "NA":
hold = True
else:
hold = False
new_eq = l(equation.x,equation.y,entry)
return hold, new_eq
from lines_for_lp import get_lineLP
from linprog import max_obj
import statistics as stats
def filter_cycles(cycles):
"""filters the cycles returning cycle with greatest number of nodes"""
lengths = [len(cycle) for cycle in cycles]
for cycle in cycles:
if len(cycle) == max(lengths):
return cycle
def lp_question(lines, cycles):
"""
Parameters
----------
lines : List
has lines that form LP problem
nodes : List
has nodes that form cycle on graph
Returns
-------
lines : list
lines have been given inequality symbols
cop_nodes : list containing vertices of feasible region
"""
nodes = filter_cycles(cycles)
xs = [node.x for node in nodes] #xcoordinate of all nodes
ys = [node.y for node in nodes] #ycoordinate of all nodes
xbar = stats.mean(xs) #mean xcoordinate
ybar = stats.mean(ys) #mean ycoordinate
ytests = [line.output(xbar) for line in lines] #y value of lines at xbar
for ytest ,line in zip(ytests,lines):
if ytest == "NA": #for lines such as x = blah
if xbar < line.rhs:
line.status = "<="
elif xbar > line.rhs:
line.status = ">="
#set inequality of line based on comaprison with ybar
elif ybar < ytest:
line.status = "<="
elif ybar > ytest:
line.status = ">="
#also want to get the nodes that form feasible region need to filter nodes that form cycle to those in feasible region
cop_nodes = nodes[:]
for node in nodes:
for line in lines:
if line.status == "<=":
#another computer issue gave -59.6200000000000000005 when i wanted -59.62
if round(node.x * line.x + node.y * line.y,10) > line.rhs:
cop_nodes.remove(node)
elif line.status == ">=":
if round(node.x * line.x + node.y* line.y,10) < line.rhs:
cop_nodes.remove(node)
return lines, cop_nodes
class LPquestion():
"""Linear programming Ineqaulties"""
def __init__(self,num_of_lines,lenaxis):
#getting the lines
cycles = None
#getting the nodes
#finding the cycles
while not cycles:
lines, x = get_lineLP(num_of_lines ,lenaxis)
nodes = max_obj(lines, "get_nodes",(lenaxis,lenaxis))
graph = Graph(nodes)
A = graph.get_node((0,0))
cycles = graph.find_cycles(A,[A])
#equations that make the linear programming question
self.equations, self.nodes = lp_question(lines, cycles)
self.minimax = random.choice(("high","low"))
def put_letters(line, lenaxis, axes):
"""function to return points at which i can put letters i want """
line = line
lenaxis_tup = (lenaxis,lenaxis)
if line.rhs == lenaxis:
return None
else:
lines_add = [l(1,0,100),l(0,1,100), l(1,0,0),l(0,1,0)]
#putting in the boundaries
lines_add.append(line)
#getting nodes for graph
nodes = max_obj(lines_add, "get_nodes", lenaxis = lenaxis_tup)
graph = Graph(nodes)
b = graph.get_node((0,100))
new_cycles = []
cycles = graph.find_cycles(b,[b]) # found cycles
###if the cycle contains node whose borders are the boundaries of the square remove it
for cycle in cycles:
if len(cycle) != len(nodes):
new_cycles.append(cycle)
#print(new_cycles)
places = [] #places to put letters with correspoding letters
#we've gotten new cycles
if len(new_cycles) == 2:
#want to recieve an axis and put letters at the centre of the cycles
for cycle in new_cycles:#
xs = [node.x for node in cycle] #xcoordinate of all nodes
ys = [node.y for node in cycle] #ycoordinate of all nodes
xbar = stats.mean(xs) #mean xcoordinate
ybar = stats.mean(ys) #mean ycoordinate
places.append((xbar,ybar))#places
correct = None
for place in places:
if line.status == ">=":
if place[0] * line.x + place[1] * line.y < line.rhs:
correct = place
elif line.status == "<=":
if place[0] * line.x + place[1] * line.y > line.rhs:
correct = place
letters = ["A","B","C","D","E"]
letters = letters[:len(places)]
for letter, place in zip(letters, places):
axes.text(place[0],place[1],letter)
return places,letters, correct