-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.rb
568 lines (526 loc) · 13.7 KB
/
test.rb
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
class Constraints
@@GOOD_RATIO = true
@@FILLED_SECTIONS = true
@@GENDER = true
attr_accessor :constraints
@@constraints = Hash.new()
def hasKey(a)
return @@constraints.has_key?(a)
end
def set(gender_on,goodratio_on)
@@GENDER = gender_on ? true : false;
@@GOOD_RATIO = goodratio_on ? true : false;
print "#{gender_on},#{goodratio_on}\n"
print "#{@@GENDER},#{@@GOOD_RATIO}\n"
end
def includes(a,b)
return @@constraints[a].include?(b)
end
def printConstraints
print @@constraints
end
def put(key,value)
@@constraints[key]=value
end
def involves(var)
keys = []
@@constraints.each do |key,value|
data = key.split(",")
if data[0].to_i == var or data[1].to_i == var
keys.push(key)
end
end
return keys
end
def conflictingVar(assignment)
@@constraints.each do |key,value|
d1 = key.split(",")
d2 = [d1[0].to_i,d1[1].to_i]
if not @@constraints[key].include?("#{assignment[d2[0]]},#{assignment[d2[1]]}")
return rand(2) == 0 ? d2[0] : d2[1];
end
end
return -1
end
def isSatisfied(assignment,sectionLeaders,students,ratio,min,max)
# print "#{assignment}\n"
# print @@constraints
# @@constraints.each {|key, value| }
@@constraints.each do |key,value|
data = key.split(",")
# print "#{data}\n"
str = "#{assignment[data[0].to_i]},#{assignment[data[1].to_i]}"
if assignment[data[0].to_i] != -1 and assignment[data[1].to_i] != -1
if not @@constraints[key].include? str
return false
end
end
end
# print @@constraints
b2 = @@GOOD_RATIO == true ? isGoodRatio(assignment,sectionLeaders,students,ratio,min,max) : true;
b3 = @@FILLED_SECTIONS == true ? isFilledSections(assignment,sectionLeaders,students) : true;
b4 = @@GENDER == true ? isGenderBalanced(assignment,sectionLeaders,students,ratio) : true;
# puts "#{b2} #{b3} #{b4}"
return (b2 and b3 and b4)
end
def isGoodRatio(assignment,sectionLeaders,students,ratio,min,max)
n = students.size
k = sectionLeaders.size
badRatio = false;
# print "RATIO\n"
for timeSlot in ratio
# print "#{timeSlot}\n"
if timeSlot[1] == 1 and (timeSlot[0] > max) or (timeSlot[0] < min)
badRatio = true
break
end
end
if badRatio and complete(assignment)
# print "-------------\n"
return false
end
# print "-------------\n"
return true
end
def complete(assignment)
for i in assignment
if i == -1
return false
end
end
return true
end
def isGenderBalanced(assignment,sectionLeaders,students,ratio)
# if(timeSlot[2] == 0 && timeSlot[3] > 0 || timeSlot[2] > 0 && timeSlot[3] == 0 ||
# (timeSlot[2] >= (n/(2*k))-1 && timeSlot[3] >= (n/(2*k))-1) ||
# !complete(assignment))
for timeSlot in ratio
# print "#{timeSlot}\n"
n = timeSlot[0]
k = timeSlot[1]
badRatio = true
if (2*k) > 0
if (timeSlot[2] == 0 and timeSlot[3] > 0) or
(timeSlot[2] > 0 and timeSlot[3] == 0) or
(timeSlot[2] >= (n/(2*k))-1 and timeSlot[3] >= (n/(2*k))-1) or
not complete(assignment)
badRatio = false
end
if badRatio
# print "---------\n"
return false
end
end
end
# print "---------\n"
return true
end
def isFilledSections(assignment,sectionLeaders,students)
# print "#{students}\n#{sectionLeaders}\n"
for i in students
misplacedStudent = true
for j in sectionLeaders
# print "#{i},#{j}\n"
if assignment[j] == assignment[i] || assignment[i] == -1
misplacedStudent = false
end
end
if misplacedStudent
return false
end
end
return true;
end
end
class Setup
def parse(filename,filename2,gender_on,good_ratio_on,min,max)
customRestrictions = nil
# print filename2
if filename2
customRestrictions = []
temp = File.open("uploads/#{filename2}","r").read.split()
# print temp
for i in temp
customRestrictions.push(i.split(","))
end
# print "#{customRestrictions}\n"
end
lines = File.open("uploads/#{filename}","r").read.split()
maybeTimes = []
sureTimes = []
vars = []
genders = []
for i in lines
dataSplit = i.split(";")
# print "#{dataSplit}\n"
if dataSplit.size == 2
maybes = dataSplit[1].split(",")
else
maybes = []
end
# print "#{maybes}\n"
temp = []
for j in 0...maybes.size
temp.push(maybes[j].to_i)
end
# print "#{temp}\n"
maybeTimes.push(temp)
data = dataSplit[0].split(",")
vars.push(data[0])
genders.push(data[1])
temp = []
for j in 2...data.size
temp.push(data[j].to_i)
end
sureTimes.push(temp)
# print "#{temp}\n"
end
convert(maybeTimes,sureTimes,vars,genders,customRestrictions,gender_on,good_ratio_on,min,max)
end
def convert(maybeTimes,sureTimes,vars,genders,customRestrictions,gender_on,good_ratio_on,min,max)
print "#{maybeTimes}\n#{sureTimes}\n"
convertedVars = []
sectionLeaders = []
students = []
# print vars
for i in 0...vars.size
convertedVars.push(i)
if vars[i][0] == '*'
sectionLeaders.push(i)
else
students.push(i)
end
end
n = sectionLeaders.size
k = students.size
useMax = (n/k)+1
useMin = (n/k)-1
if min.to_i > 0 and min.to_i < vars.size
useMin = min.to_i
end
if max.to_i > 0 and max.to_i < var.size
useMax = max.to_i
end
intToTime = []
timeToInt = {}
index = 0
for i in maybeTimes
for j in i
if not intToTime.include? j
intToTime.push(j)
timeToInt[j] = index
index += 1
end
end
end
for i in sureTimes
for j in i
if not intToTime.include? j
intToTime.push(j)
timeToInt[j] = index
index += 1
end
end
end
# print "#{vars}\n#{timeToInt}\n"
convertedMaybeTimes = []
convertedSureTimes = []
convertedTotalDomains = []
for i in maybeTimes
temp = []
temp2 = []
for j in i
temp.push(timeToInt[j])
temp2.push(timeToInt[j])
end
convertedMaybeTimes.push(temp)
convertedTotalDomains.push(temp2)
end
totalIndex = 0
for i in sureTimes
temp = []
for j in i
temp.push(timeToInt[j])
convertedTotalDomains[totalIndex].push(timeToInt[j])
end
totalIndex+=1
convertedSureTimes.push(temp)
end
# print "#{convertedMaybeTimes}\n"
# print "#{convertedSureTimes}\n"
index = 0
# print "#{convertedTotalDomains}\n"
constraints = Constraints.new
constraints.set(gender_on,good_ratio_on)
for i in 0...sectionLeaders.size
for j in i+1...sectionLeaders.size
# print "BOOP #{convertedTotalDomains[i]},#{convertedTotalDomains[j]}\n"
nonMatchingDomains = []
for k in 0...convertedTotalDomains[i].size
for l in 0...convertedTotalDomains[j].size
if convertedTotalDomains[i][k] != convertedTotalDomains[j][l] and not nonMatchingDomains.include? "#{convertedTotalDomains[i][k]},#{convertedTotalDomains[j][l]}"
nonMatchingDomains.push("#{convertedTotalDomains[i][k]},#{convertedTotalDomains[j][l]}")
end
end
end
# print "#{nonMatchingDomains}\n"
# for sl1 in 0...sectionLeaders.size
# for sl2 in sl1+1...sectionLeaders.size
constraints.put("#{sectionLeaders[i]},#{sectionLeaders[j]}", nonMatchingDomains)
# print "pushing #{sectionLeaders[i]},#{sectionLeaders[j]}\n#{nonMatchingDomains}\n"
# end
# end
end
end
# print "#{vars}\n"
# print "CONSTRAINTS\n"
if customRestrictions
for i in customRestrictions.size-1..0
for name in customRestrictions[i]
if not vars.include?(name)
customRestrictions.delete_at(i)
break
end
end
end
for additionalConstraint in customRestrictions
index = 0
for i in index...additionalConstraint.size
for j in i+1...additionalConstraint.size
nonConflictingTimes = []
i1 = vars.index(additionalConstraint[i])
i2 = vars.index(additionalConstraint[j])
# print "#{i1},additionalConstraint#{i2}\n"
for k in convertedTotalDomains[i1]
for l in convertedTotalDomains[i2]
if k != l
nonConflictingTimes.push("#{k},#{l}")
end
end
end
constraints.put("#{i1},#{i2}",nonConflictingTimes)
# print "#{i1},#{i2},#{nonConflictingTimes}\n"
end
end
index += 1
end
end
finishedAssignments=[]
finishedAssignments = backtrackingSearch(convertedVars,convertedTotalDomains,constraints,sectionLeaders,students,genders,intToTime,maybeTimes,min,max)
# print "hi\n"
# print "#{finishedAssignments}\n"
# finishedAssignments = minConflicts(1000,nil,vars,constraints,sectionLeaders,students,intToTime,convertedTotalDomains)
stuffs = []
if not finishedAssignments[0]
print "no solutions\n"
else
for assignment in finishedAssignments
stuff = []
for i in 0...intToTime.size
stuff.push([])
stuff[i] = []
end
for i in 0...assignment.size
time = intToTime[assignment[i]]
if not stuff[assignment[i]].include? time
stuff[assignment[i]].push(time)
end
end
for i in 0...assignment.size
time = intToTime[assignment[i]]
stuff[assignment[i]].push(vars[i])
end
for i in (stuff.size-1).downto(0)
if stuff[i].size == 0
stuff.delete_at(i)
end
end
stuffs.push(stuff)
end
end
print "SOLUTION\n"
# print stuffs
# print finishedAssignments
for stuff in stuffs
print "#{stuff}\n"
end
# assignment = [1,2,4,7,3,3]
# print "#{sectionLeaders}\n"
# print "#{students}\n"
# constraints.isSatisfied(assignment,sectionLeaders,students,[])
if stuffs.size == 0
return nil
end
return stuffs
end
def backtrackingSearch(vars,domains,constraints,sectionLeaders,students,genders,intToTime,maybeTimes,min,max)
assignment = []
for i in 0...vars.size
assignment.push(-1)
end
finishedAssignments = []
ratio = []
for i in 0...intToTime.size
ratio.push([0,0,0,0])
end
recursiveBacktrackingSearch(vars,assignment,finishedAssignments,domains,constraints,sectionLeaders,students,ratio,genders,Time.now,min,max)
maxScore = 0;
maxAssignment = 0;
for i in 0...finishedAssignments.size
index = 0
tempScore = 0
for j in finishedAssignments[i]
if maybeTimes[index].include? intToTime[j]
tempScore += 1
else
tempScore += 2
end
index += 1
end
if tempScore > maxScore
maxScore = tempScore
maxAssignment = i
end
end
# puts "final"
# print "#{intToTime}\n"
# print "#{finishedAssignments}\n"
# print "#{finishedAssignments[maxAssignment]}\n"
temp=finishedAssignments[maxAssignment]
finishedAssignments = []
finishedAssignments.push(temp)
# return finishedAssignments
return [finishedAssignments[maxAssignment]]
end
def recursiveBacktrackingSearch(vars,assignment,finishedAssignments,domains,constraints,sectionLeaders,students,ratio,genders,time,min,max)
# print "#{Time.now},#{time}\n"
if Time.now-time > 10
return nil
end
if(goalTest(assignment))
# print "GOAL #{assignment}\n"
finishedAssignments.push(assignment.clone)
return finishedAssignments
else
var = unassignedVariable(vars,assignment)
# print "#{orderDomainValues(var,domains)}\n"
for value in orderDomainValues(var,domains)
assignment[var] = value
# print "#{assignment}\n"
if students.include? var
ratio[value][0] += 1
else
ratio[value][1] += 1
end
if genders[var] == 'm'
ratio[value][2] += 1
else
ratio[value][3] += 1
end
if constraints.isSatisfied(assignment,sectionLeaders,students,ratio,min,max)
# print "satisfied\n"
result = recursiveBacktrackingSearch(vars,assignment,finishedAssignments,domains,constraints,sectionLeaders,students,ratio,genders,time,min,max)
# print result
if result
return result
end
end
assignment[var] = -1
if students.include? var
ratio[value][0] -= 1
else
ratio[value][1] -= 1
end
if genders[var] == 'm'
ratio[value][2] -= 1
else
ratio[value][3] -= 1
end
end
end
# puts "nil"
return nil
end
def goalTest(assignment)
for i in 0...assignment.size
if assignment[i] == -1
return false
end
end
return true
end
def unassignedVariable(vars,assignment)
for i in 0...assignment.size
if assignment[i] == -1
return vars[i]
end
end
end
def orderDomainValues(var,domains)
return domains[var];
end
def minConflicts(maxSteps,newAssignment,vars,constraints,sectionLeaders,students,intToTime,domains)
# print 'hi'
ratio = []
for i in 0...intToTime.size
ratio.push([0,0,0,0])
end
assignment = newAssignment
if not assignment
assignment = []
for i in 0...vars.size
assignment.push(0)
end
end
print "#{vars}\n"
print "#{intToTime}\n"
for i in 0...maxSteps
# puts i
if goalTest(assignment) and constraints.isSatisfied(assignment,sectionLeaders,students,ratio)
return [assignment]
end
var = constraints.conflictingVar(assignment)
minValue = 99999
minDomain = 99999
for j in domains[var]
value = conflicts(var,j,assignment,constraints)
if value < minValue
minValue = value
minDomain = j
end
end
if assignment[var] == -1
if students.include?(var)
ratio[value][0] += 1
else
ratio[value][1] += 1
end
if genders[temp] == 'm'
ratio[value][2] += 1
else
ratio[value][3] += 1
end
end
assignment[var] = minDomain
end
end
def conflicts(var,value,assignment,constraints)
numConflicts = 0
involved = constraints.involves(var)
for s in involved
data = s.split(",")
other = data[1].to_i == var ? data[0].to_i : data[1].to_i
if constraints.hasKey("#{var},#{other}")
if not constraints.includes("#{var},#{other}","#{value},#{assignment[other]}")
numConflicts += 1
end
elsif constraints.hasKey("#{other},#{var}")
if not constraints.includes("#{other},#{var}","#{assignment[other]},#{other}")
numConflicts += 1
end
end
end
return numConflicts
end
# Setup.new.parse("case2.txt",nil,false,false,0,0)
end