Module: Weight

Includes:
Helpers
Included in:
Algorithm
Defined in:
lib/gthc/olson/weight.rb

Class Method Summary collapse

Methods included from Helpers

calculatePeopleNeeded

Class Method Details

.gridLimits(row, rowLength) ⇒ Object



48
49
50
51
# File 'lib/gthc/olson/weight.rb', line 48

def self.gridLimits(row, rowLength)
  return row - 1 < 0,
         row + 1 > rowLength - 1
end

.weightBalance(people, slots) ⇒ Object

Weight Balance - prioritize people with fewer scheduled shifts



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
# File 'lib/gthc/olson/weight.rb', line 15

def self.weightBalance(people, slots)

  slots.each do | currentSlot |

    # Establish variables.
    currentPersonID = currentSlot.col
    dayScheduled = people[currentPersonID].dayScheduled
    nightScheduled = people[currentPersonID].nightScheduled
    night = currentSlot.isNight;

    nightMulti = 1;
    dayMulti = 1;

    # Set multipliers.
    if nightScheduled != 0
      nightMulti = 1.0 / (nightScheduled + 1)
    end

    if dayScheduled != 0
      dayMulti = 1.0 / (dayScheduled + 1)
    end

    #Adjust weights with multipliers.
    if night
      currentSlot.weight = currentSlot.weight * nightMulti
    else
      currentSlot.weight = currentSlot.weight * dayMulti
    end

  end
  return people, slots
end

.weightContiguous(slots, scheduleGrid) ⇒ Object

Weight Contiguous - prioritize people to stay in the tent more time at once.



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
# File 'lib/gthc/olson/weight.rb', line 54

def self.weightContiguous(slots, scheduleGrid)

  i = 0
  while i < slots.length
    # Establish Variables
    currentRow = slots[i].row
    currentCol = slots[i].col

    aboveRow = currentRow-1
    belowRow = currentRow+1

    # grab all slots under the same col as the inspected slot in order
    # to get the slots above and below
    allSlots = scheduleGrid[currentCol]
    slotsLength = allSlots.length

    # find what to skip
    skipAboveRow, skipBelowRow = gridLimits(currentRow, slotsLength)

    currentIsNight = slots[i].isNight
    aboveIsNight = !skipAboveRow && allSlots[aboveRow].isNight
    belowIsNight = !skipBelowRow && allSlots[belowRow].isNight

    aboveTent = !skipAboveRow && allSlots[aboveRow].status == "Scheduled"
    belowTent = !skipBelowRow && allSlots[belowRow].status == "Scheduled"
    aboveSome = !skipAboveRow && allSlots[aboveRow].status == "Somewhat"
    belowSome = !skipBelowRow && allSlots[belowRow].status == "Somewhat"
    aboveFree = !skipAboveRow && allSlots[aboveRow].status == "Available"
    belowFree = !skipBelowRow && allSlots[belowRow].status == "Available"

    multi = 1

    # Both are scheduled.
    if aboveTent && belowTent
      multi = 100
    end

    # Both are not free
    if !belowTent && !belowFree && !aboveSome && !belowSome && !aboveTent && !aboveFree
      multi *= 0.25
    end

    # Above is scheduled, below is free.
    if aboveTent && !belowTent && belowFree
      multi = 3.25
    end

    # Below is scheduled, above is free.
    if belowTent && !aboveTent && aboveFree
      multi = 3.25
    end

    # Above is scheduled, below is not free.
    if aboveTent && !belowTent && !belowFree
      multi = 3
    end

    # Below is scheduled, above is not free.
    if belowTent && !aboveTent && !aboveFree
      multi = 3
    end

    # Both are free
    if belowFree && aboveFree
      multi = 2.75
    end

    # Above is free, below is not free
    if aboveFree && !belowTent && !belowFree
      multi = 1
    end

    # Below is free, above is not free
    if(belowFree && !aboveTent && !aboveFree)
      multi = 1
    end

    # Night Multi
    if aboveIsNight || belowIsNight || currentIsNight
      multi *= 1.25
    end

    # Occurance of Somewhat Available
    if slots[i].status == "Somewhat"
      multi *= 0.5
    end

    slots[i].weight = slots[i].weight*multi
    i += 1

  end

  return slots, scheduleGrid
end

.weightPick(people, slots, graveyard, scheduleGrid) ⇒ Object

Update people, spreadsheet, and remove slots.



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
# File 'lib/gthc/olson/weight.rb', line 175

def self.weightPick(people, slots, graveyard, scheduleGrid)

  # Remove winner from list.
  winner = slots.shift;

  # Update person information.
  currentPersonID = winner.col;
  currentTime = winner.isNight;

  if currentTime
    people[currentPersonID].nightScheduled += 1
    people[currentPersonID].nightFree -= 1
   else
    people[currentPersonID].dayScheduled += 1
    people[currentPersonID].dayFree -= 1
  end

  # Establish Variables
  currentRow = winner.row
  currentCol = winner.col
  tentCounter = 0

  # Update Data
  scheduleGrid[currentCol][currentRow].status = "Scheduled";

  # Count number of scheduled tenters during winner slot.
  i = 0
  while i < scheduleGrid.length
    if scheduleGrid[i][currentRow].status == "Scheduled"
      tentCounter = tentCounter + 1
    end
    i += 1
  end

  # Determine how many people are needed.
  peopleNeeded = Helpers.calculatePeopleNeeded(currentTime, winner.phase)

  # Update Slots and Graveyard
  if tentCounter >= peopleNeeded
    graveyard[currentRow] = 1
    j = 0
    tempSlots = []
    while j < slots.length
      tempRow = slots[j].row
      if tempRow != currentRow
        tempSlots.push slots[j]
      end
      j += 1
    end
    slots = tempSlots
  end

  return people, slots, graveyard, scheduleGrid
end

.weightReset(slots) ⇒ Object

Weight Reset - set all weights to 1.



7
8
9
10
11
12
# File 'lib/gthc/olson/weight.rb', line 7

def self.weightReset(slots)
  slots.each do | currentSlot |
    currentSlot.weight = 1;
  end
  slots
end

.weightToughTime(slots, scheduleLength) ⇒ Object

Weight Tough Time - prioritize time slots with few people available. */



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/gthc/olson/weight.rb', line 150

def self.weightToughTime(slots, scheduleLength)

  # Set up counterArray (Rows that are filled).
  counterArray = Array.new(scheduleLength + 1, 0)

  # Fill counterArray.
  slots.each do | currentSlot |
    currentRow = currentSlot.row
    counterArray[currentRow] = counterArray[currentRow] + 1
  end

  # Update Weights.
  slots.each do | currentSlot |
    currentRow = currentSlot.row
    currentPhase = currentSlot.phase
    nightBoolean = currentSlot.isNight
    peopleNeeded = Helpers.calculatePeopleNeeded(nightBoolean, currentPhase)
    numFreePeople = counterArray[currentRow]
    currentSlot.weight = currentSlot.weight*(12/numFreePeople)*peopleNeeded
  end

  return slots
end