Skip to content

Commit

Permalink
Tidying & dead code scrubbing
Browse files Browse the repository at this point in the history
  • Loading branch information
pcantrell committed Apr 8, 2015
1 parent a05b907 commit 3ad204b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
23 changes: 10 additions & 13 deletions src/lib/scheduling/context.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# ActiveModel access is slow enough that we create a stripped-down, in-memory version of the various
# models we need to create a schedule, then run the annealer against this in-memory model.
# This class sucks all the rooms, sessions and timeslots from the DB, and provides them during annealing.
#
# A Context, its Person objects, and their SessionSets do _not_ change during annealing.
# The Schedule class contains all the state we're trying to optimize.
#
module Scheduling

# ActiveModel access is slow enough that we create a stripped-down, in-memory version of the various
# models we need to create a schedule, then run the annealer against this in-memory model.
# This class sucks all the rooms, sessions and timeslots from the DB, and provides them during annealing.
#
# A Context, its Person objects, and their SessionSets do _not_ change during annealing.
# The Schedule class contains all the state we're trying to optimize.
#
class Context

attr_reader :sessions, :timeslots, :rooms
Expand Down Expand Up @@ -34,16 +35,12 @@ def people
@people_by_id.values
end

private

def person(id)
@people_by_id[id]
end

def attendee_count
people.count
end

private

def load_sets(role, association_model)
# This brute force iteration is hardly slick, but I'm too rusty on fancy ActiveRecord querying to care just now. -PPC
size = association_model.where(session_id: @sessions).select(:participant_id, :session_id).each do |assoc|
Expand Down
4 changes: 4 additions & 0 deletions src/lib/scheduling/person.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module Scheduling

# Lightweight model for a person, who has a (possibly empty) set of sessions
# they'd like to attend, and another at which they're presenting.
#
class Person
attr_reader :id, :attending, :presenting

Expand Down
18 changes: 4 additions & 14 deletions src/lib/scheduling/schedule.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
require 'set'
require 'pp'


unless Array.method_defined?(:sample)
class Array
def sample
self[rand(self.size)]
end
end
end


# Represents a particular schedule (i.e. assignment of sessions to rooms) for the purpose of annealing.
# Scores the schedule and returns nearby variations.
#
module Scheduling
# Represents a particular schedule (i.e. assignment of sessions to rooms) for the purpose of annealing.
# Scores the schedule and returns nearby variations.
#
class Schedule
def initialize(event)
@ctx = Scheduling::Context.new(event)
Expand Down Expand Up @@ -57,7 +47,7 @@ def attendance_energy
end

def presenter_energy
overlap_score(:presenting) * ctx.attendee_count # Presenter double-bookings trump attendance preferences
overlap_score(:presenting) * ctx.people.count # Presenter double-bookings trump attendance preferences
end

# Gives lower & upper bounds on the possible range of attendance_energy
Expand Down
12 changes: 5 additions & 7 deletions src/lib/scheduling/session_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Scheduling

# A set of sessions which can be scored against a particular schedule.
#
# This can represent the set of sessions an attendee is interested in seeing,
# This can represent either the set of sessions an attendee is interested in seeing,
# or the set a presenter is presenting. Either way, we want sessions booked in
# nonoverlapping timeslots.
#
Expand All @@ -22,13 +22,13 @@ def add(session_id)
@superset.add(session_id) if @superset
end

# Score if sessions are evenly distributed among all the timeslots.
# The score if sessions are evenly distributed among all available timeslots.
#
def best_possible_score
[@ctx.timeslots.size / size.to_f, 1.0].min
end

# Score if everything is in the same timeslot.
# The score if everything is in the same timeslot.
#
def worst_possible_score
1.0 / size
Expand All @@ -51,10 +51,8 @@ def score(schedule)
overlaps / @sessions.size.to_f
end

%i(size each empty?).each do |forwarded_method|
define_method(forwarded_method) do |*args, &block|
@sessions.send(forwarded_method, *args, &block)
end
def size
@sessions.size
end

end
Expand Down

0 comments on commit 3ad204b

Please sign in to comment.