Class: OpenShip::Shipment

Inherits:
Object
  • Object
show all
Defined in:
lib/open-ship/sortr.rb

Defined Under Namespace

Classes: CartonDispenser

Constant Summary collapse

@@fitness_values =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Shipment

Returns a new instance of Shipment.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/open-ship/sortr.rb', line 154

def initialize(opts = {})
  if opts[:logger]
    @logger = opts[:logger]
  else
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::WARN
  end
  if opts[:carton_dispenser]
    @carton_dispenser = opts[:carton_dispenser]
  else
    @carton_dispenser = CartonDispenser.new
  end
  @cartons_to_stores = nil
  @boxes_to_stores = {}
end

Instance Attribute Details

#boxes_to_storesObject

Returns the value of attribute boxes_to_stores.



140
141
142
# File 'lib/open-ship/sortr.rb', line 140

def boxes_to_stores
  @boxes_to_stores
end

#carton_dispenserObject

Returns the value of attribute carton_dispenser.



140
141
142
# File 'lib/open-ship/sortr.rb', line 140

def carton_dispenser
  @carton_dispenser
end

#cartons_to_storesObject

Returns the value of attribute cartons_to_stores.



140
141
142
# File 'lib/open-ship/sortr.rb', line 140

def cartons_to_stores
  @cartons_to_stores
end

#loggerObject

Returns the value of attribute logger.



140
141
142
# File 'lib/open-ship/sortr.rb', line 140

def logger
  @logger
end

Instance Method Details

#calculate_cartonsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/open-ship/sortr.rb', line 170

def calculate_cartons
  @cartons_to_stores = {}
  self.boxes_to_stores.each { |k, v|
    @cartons_to_stores[k] ||= []
    cart = @carton_dispenser.get_carton
    @cartons_to_stores[k] << cart
    v.each { |box|
      pos = cart.add_box(box)
      if pos.nil?
        cart = @carton_dispenser.get_carton
        @cartons_to_stores[k] << cart
        pos = cart.add_box(box)
        if pos.nil?
          raise "Box is too big for carton."
        end
      end
    }
  }
  @cartons_to_stores
end

#fitnessObject



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/open-ship/sortr.rb', line 191

def fitness

  if @cartons_to_stores.nil?
    self.calculate_cartons
  end

  free_space = self.cartons_to_stores.sum { |k, v| v.sum { |c| c.free_space } }
  box_count = self.boxes_to_stores.sum { |k, v| v.count }
  carton_count = self.cartons_to_stores.sum { |k, v| v.count }
  @logger.debug "Box Count: " + box_count.to_s
  @logger.debug "Carton Free Space: " + free_space.to_s
  @logger.debug "Carton Count: " + carton_count.to_s
  fitness = (box_count.to_f / carton_count.to_f)
  @@fitness_values << fitness
  @logger.debug "Fitness Mean: " + fitness_mean.to_s
  @logger.debug "Fitness: " + fitness.to_s
  fitness
end

#fitness_meanObject



232
233
234
# File 'lib/open-ship/sortr.rb', line 232

def fitness_mean
  (@@fitness_values.sum { |f| f } / @@fitness_values.length)
end

#mutateObject



236
237
238
239
240
241
242
243
244
# File 'lib/open-ship/sortr.rb', line 236

def mutate
  #if (self.fitness < self.fitness_mean)
    @logger.debug "################ Making a mutation!"
    self.boxes_to_stores.each { |k, v|
      self.boxes_to_stores[k] = v.shuffle
    }
  #end
  self
end

#recombine(c2) ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/open-ship/sortr.rb', line 215

def recombine(c2)
  return1 = self.clone
  return2 = c2.clone
  self.boxes_to_stores.each { |k, v|
    bxs = c2.boxes_to_stores[k]

    cross_point = (rand * bxs.size).to_i
    c1_a, c1_b = v.separate(cross_point)
    c2_a, c2_b = bxs.separate(cross_point)
    return1.boxes_to_stores[k] = c1_a + c2_b
    return2.boxes_to_stores[k] = c2_a + c1_b
  }
  return1.cartons_to_stores = nil
  return2.cartons_to_stores = nil
  return1
end

#run_ga(generations = 10, population_size = 10) ⇒ Object



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
# File 'lib/open-ship/sortr.rb', line 246

def run_ga(generations = 10, population_size = 10)
  @@fitness_values = []

  population = []
  population_size.times {
    #ship = OpenShip::Shipment.new(:logger => @logger, :carton_dispenser => @carton_dispenser)
    ship = self.clone
    ship.boxes_to_stores = self.boxes_to_stores.clone

    ship.boxes_to_stores.each { |k, v|
      ship.boxes_to_stores[k] = v.shuffle
    }
    population << ship
  }
  ga = GeneticAlgorithm.new(population, {:logger => @logger})
  generations.times { ga.evolve }
  best_fit = ga.best_fit[0]
  best_fit.cartons_to_stores.each { |k, v|
    puts k
    v.each { |cart|
      @logger.debug "Carton"
      cart.box_positions.each { |bp|
        @logger.debug "length: " + bp.box.length.to_s + " width: " + bp.box.width.to_s + " height: " + bp.box.height.to_s
        @logger.debug "x: " + bp.position.x.to_s + " y: " + bp.position.y.to_s + " z: " + bp.position.z.to_s
      }
    }
  }
  best_fit.fitness
  best_fit
end

#statsObject



210
211
212
# File 'lib/open-ship/sortr.rb', line 210

def stats
  ["Total Volume :" + self.boxes_to_stores.sum { |k, v| v.sum { |b| b.volume } }.to_s]
end