Guest User

Untitled

a guest
Jul 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. class InitialSolutionSolver
  2. include Evaluator
  3.  
  4. def solve(instance, c)
  5. return [] if instance.empty?
  6. return [find_first_config(instance, c)] + solve(instance[1..-1],c)
  7. end
  8.  
  9. def find_first_config(instance, c)
  10. first_config = instance.first.clone
  11. instance[1..-1].each do |config|
  12. puts config
  13. config.fixed.each do|tool|
  14. first_config.add(tool)
  15. return first_config if first_config.size == c
  16. end
  17. end
  18. first_config
  19. end
  20. end
  21.  
  22.  
  23.  
  24.  
  25.  
  26. require "enumerator"
  27. class Config
  28.  
  29. include Enumerable
  30. attr_accessor :name, :fixed, :variable
  31.  
  32. def each(&block)
  33. @fixed.each{|i|yield i}
  34. end
  35.  
  36. def initialize(name,numslots)
  37. @name = name
  38. @fixed = Array.new
  39. @variable = Array.new
  40. @maxsize = numslots
  41. end
  42.  
  43. def full?
  44. size == @maxsize
  45. end
  46.  
  47. def already_in_slot?(tool)
  48. fixed.include?(tool) || variable.include?(tool)
  49. end
  50.  
  51. def add(tool)
  52. unless full? or already_in_slot?(tool)
  53. variable << tool
  54. end
  55. end
  56.  
  57. def to_s
  58. "#{@name}: [#{(@fixed.collect{|e| e.to_s+'*'}.to_a + @variable.to_a).join(",").to_s}]"
  59. end
  60.  
  61. alias :inspect :to_s
  62.  
  63. def size
  64. @fixed.size + @variable.size
  65. end
  66.  
  67. end
Add Comment
Please, Sign In to add comment