Class: Schedsolver2::School

Inherits:
Object
  • Object
show all
Includes:
Schedsolver2
Defined in:
lib/schedsolver2/school.rb

Constant Summary

Constants included from Schedsolver2

LIBPATH, PATH, VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Schedsolver2

libpath, #log, log, #log=, log=, path, require_all_libs_relative_to

Constructor Details

#initialize(si, ts, cs) ⇒ School

Returns a new instance of School.



8
9
10
11
12
13
14
# File 'lib/schedsolver2/school.rb', line 8

def initialize si, ts, cs
  @si, @ts, @cs = si, ts, cs
  @consts = generate_constants
  @scheds = []
  verify_initialize_args
  parse_part_time_constraints
end

Instance Attribute Details

#constsObject (readonly)

Returns the value of attribute consts.



6
7
8
# File 'lib/schedsolver2/school.rb', line 6

def consts
  @consts
end

#csObject

Returns the value of attribute cs.



5
6
7
# File 'lib/schedsolver2/school.rb', line 5

def cs
  @cs
end

#schedsObject (readonly)

Returns the value of attribute scheds.



6
7
8
# File 'lib/schedsolver2/school.rb', line 6

def scheds
  @scheds
end

#siObject (readonly)

Returns the value of attribute si.



6
7
8
# File 'lib/schedsolver2/school.rb', line 6

def si
  @si
end

#tsObject (readonly)

Returns the value of attribute ts.



6
7
8
# File 'lib/schedsolver2/school.rb', line 6

def ts
  @ts
end

Instance Method Details

#add(type, *args) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/schedsolver2/school.rb', line 94

def add type, *args
  case type
  when :schedules
    raise ArgumentError unless (args.size == 1 and args[0] > 0)
    args[0].times { @scheds << Schedule.new(@consts) }
  when :hard_constraint
    @cs[:h] << args[0]
  end
end

#daysObject



78
79
80
# File 'lib/schedsolver2/school.rb', line 78

def days 
  @consts[:days]
end

#dbObject



171
172
173
174
# File 'lib/schedsolver2/school.rb', line 171

def db
  @scheds[0][:mon, 8, :Art] = 'TEST' 
  @scheds[1][:wed, 12, :Music] = 'TEST' 
end

#et_idsObject



86
87
88
# File 'lib/schedsolver2/school.rb', line 86

def et_ids
  @consts[:et_ids]
end

#generate_constantsObject



60
61
62
63
64
65
66
67
# File 'lib/schedsolver2/school.rb', line 60

def generate_constants
  h = Hash.new
  h[:days] = [:mon, :tue, :wed, :thu, :fri]
  h[:times] = generate_times
  h[:et_ids] = @ts[:e].map {|t| t.id}
  h[:ht_ids] = @ts[:h].map {|t| t.id}
  return h
end

#generate_schedule(index) ⇒ Object



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
# File 'lib/schedsolver2/school.rb', line 104

def generate_schedule(index)
  #log.debug("GENERATE SCHEDULE")
  i = index
  @ts[:h].shuffle.each do |ht|
    ht_id = ht.id
    #log.debug("Assigning for ht #{ht_id}")
    count = nil
    count = ClassCounter.new(@consts[:et_ids])

    @ts[:e].shuffle.each do |et|
      et_id = et.id
      w_remaining = @cs[:ecs_per_week] - count.by_et_id(et_id) 
      #log.debug("\tLOOKING at et #{et_id} with #{w_remaining} remaining this week")
      if w_remaining == 0
        #log.debug("\tBREAK because remaining is 0 (top of days loop) ")
        break
      end

      @consts[:times].shuffle.each do |time|
        #log.debug("\tTIME: #{time}")
        w_remaining = @cs[:ecs_per_week] - count.by_et_id(et_id) 

        if w_remaining == 0 
          #log.debug("\tBREAK because w_remaining is 0 (top of #{time} loop) ")
          break
        end
         
        @consts[:days].shuffle.each do |day|

          d_remaining = @cs[:max_ecs_per_day] - count.by_day(day) 
          w_remaining = @cs[:ecs_per_week] - count.by_et_id(et_id) 

          if d_remaining == 0 
            #log.debug("\tBREAK because d_remaining is 0 (top of #{day} loop) ")
            next
          elsif w_remaining == 0 
            #log.debug("\tBREAK because w_remaining is 0 (top of #{day} loop) ")
            break
          elsif @scheds[i][day, time, et_id] != nil
            #log.debug("\tNEXT because slot (#{day}, #{time}) is full")
            next
          elsif count[day, et_id] > 0
            #log.debug("\tNEXT because this EC is already assigned to #{day}")
            next
          elsif not et.working?(day)
            #log.debug("\tNEXT because et #{et_id} doesn't work on #{day}")
            next
          end

          begin
            @scheds[i][day, time, et_id] = ht_id
          rescue InsanityError
            #log.debug("\tNEXT because of insanity")
            next
          else
            count[day, et_id] += 1
            d_remaining = @cs[:max_ecs_per_day] - count.by_day(day) 
            w_remaining = @cs[:ecs_per_week] - count.by_et_id(et_id) 
            #log.debug("\tASSIGNED to (#{day},#{time}) with #{d_remaining} remaining today and #{w_remaining} for week")
          end

        end
      end
    end
  end
end

#generate_timesObject



69
70
71
72
73
74
75
76
# File 'lib/schedsolver2/school.rb', line 69

def generate_times
  ts, t = [], @si[:start_time]
  @si[:num_blocks].times do 
    ts << t
    t += @si[:block_size]
  end
  return ts
end

#ht_idsObject



90
91
92
# File 'lib/schedsolver2/school.rb', line 90

def ht_ids
  @consts[:ht_ids]
end

#parse_part_time_constraintsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/schedsolver2/school.rb', line 31

def parse_part_time_constraints
  @cs[:part_time] ||= {}
  @ts[:e].each do |et|
  not_work_days = (days - et.work_days)
    if not_work_days == [] then next end
    s_d = Constraint.days_descriptor(not_work_days, times, [et.id])
    #log.debug("School#parse_part_time constraints\nS_D = #{s_d}\n")
    new_pt_c = Constraint.new(:type  => :part_time,
                              :et_ids => [et.id],
                              :s_ds  => [s_d],
                              :name => "et#{et.id} part time")
    @cs[:part_time][et.id] = new_pt_c
  end
end


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/schedsolver2/school.rb', line 16

def print attr
  case attr
  when :schedules
    @scheds.each {|s| log.info("\n" + s.to_s) }
  when :school_info
    log.info("SCHOOL INFO #{@si}")
  when :consts
    log.info("CONSTS #{@consts}")
  when :teachers
    log.info("TEACHERS #{@ts}")
  when :constraints
    log.info("CONSTRAINTS #{@cs}")
  end
end

#timesObject



82
83
84
# File 'lib/schedsolver2/school.rb', line 82

def times
  @consts[:times]
end

#verify_initialize_argsObject

Raises:

  • (ArgumentError)


46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/schedsolver2/school.rb', line 46

def verify_initialize_args
  check = ((@si[:end_time] - @si[:start_time]) == (@si[:num_blocks] * @si[:block_size]))
  raise ArgumentError, "si: end-start != num*size" unless check == true

  @ts[:e].each do |t|
    raise ArgumentError, "ts:e: has non-teacher" unless t.class == Teacher
  end

  @ts[:h].each do |t|
    raise ArgumentError, "ts:h: has non-teacher" unless t.class == Teacher
  end
  #add check for cs?
end