Class: SwitchGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/whatnot/switch_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nums = [], min_on: 1, max_on: 1) ⇒ SwitchGroup

Returns a new instance of SwitchGroup.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/whatnot/switch_group.rb', line 4

def initialize(nums=[], min_on: 1, max_on: 1)
  @min_on = min_on
  @max_on = max_on
  @switches = []
  @possibilities = []

  if block_given?
    Switch.each do |switch|
      @switches << switch if yield(switch.payload)
    end
  else
    @switches = Switch.all.values_at(*nums)
  end

  find_all_possibilities!
end

Instance Attribute Details

#possibilitiesObject

Returns the value of attribute possibilities.



2
3
4
# File 'lib/whatnot/switch_group.rb', line 2

def possibilities
  @possibilities
end

#switchesObject

Returns the value of attribute switches.



2
3
4
# File 'lib/whatnot/switch_group.rb', line 2

def switches
  @switches
end

Instance Method Details

#dimacsObject



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
# File 'lib/whatnot/switch_group.rb', line 124

def dimacs
  r = ""

  r << "c Switches:\n"
  @switches.each do |switch|
    switch_pp = switch.payload.pretty_inspect.lines
    r << "c   #{switch.number}: #{switch_pp[0]}"
    switch_pp[1..-1].each do |line|
      r << "c     #{line}"
    end
  end

  if @min_on > 0
    @switches.combination(@switches.size - (@min_on-1)).each do |combo|
      r << combo.map(&:number).map(&:to_s).join(" ") + " 0\n"
    end
  end

  if @max_on < @switches.size
    @switches.combination(@max_on+1).each do |combo|
      r << combo.map(&:number).map { |s| "-#{s}" }.join(" ") + " 0\n"
    end
  end

  r << "\n\n"
  r
end

#find_all_possibilities!Object



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
47
48
49
50
51
52
53
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
# File 'lib/whatnot/switch_group.rb', line 21

def find_all_possibilities!
  switchmap = Hash[@switches.each_with_index.map { |s, ix| [s.number, ix+1] }]

  groupdimacs = dimacs.lines.map do |line|
    next if line.start_with?("c") || line.lstrip == ""

    nums = line.split(" ").map(&:to_i)

    newnums = []
    nums.each do |n|
      if n > 0
        newnums << switchmap[n]
      elsif n < 0
        newnums << switchmap[n*-1] * -1
      else
        newnums << n
      end
    end

    newnums.map(&:to_s).join(" ")
  end

  groupdimacs = groupdimacs.compact.join("\n")

  groupinterpreter = -> (str) do
    nums = str.split(" ").map(&:to_i)

    newnums = []
    nums.each do |n|
      if n > 0
        newnums << @switches[n-1].number
      elsif n == 0
        newnums << n
      else
        newnums << @switches[(-1*n)-1].number * -1
      end
    end

    solution = newnums.map(&:to_s).join(" ")

    [group_interpret(solution), solution]
  end

  if groupdimacs.empty?
    # all solutions are possible, so iterate all.
    # can't use minisat here (it won't know how many vars there are).
    possibilities = {}

    switch_combos = switches.map do |switch|
      n = switch.number
      [n, -1 * n]
    end

    switch_combos.inject(&:product).each do |product|
      product = product.is_a?(Array) ? product.flatten : [product]
      solution = product.map(&:to_s).join(" ") + " 0"
      possibilities[group_interpret(solution)] = solution
    end

    @possibilities = possibilities
  else
    @possibilities = Hash[SolutionEnumerator.new(groupinterpreter, groupdimacs).entries]
  end
end

#group_interpret(solution) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/whatnot/switch_group.rb', line 86

def group_interpret(solution)
  out = {}

  solution.split(" ").each do |num|
    num = num.to_i

    if num > 0
      merge_payload!(for_num: num, to_hash: out)
    end
  end

  out
end

#merge_payload!(for_num: nil, to_hash: nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/whatnot/switch_group.rb', line 100

def merge_payload!(for_num: nil, to_hash: nil)
  if for_num.nil? || to_hash.nil?
    raise "left off required kwargs #{"for_num, " unless for_num}#{"to_hash" unless to_hash}" 
  end

  payload = Switch.find(for_num).payload
  payload_key, payload_val = *payload.to_a[0]

  # manage sets vs. slots
  if @max_on > 1
    payload_val = [payload_val] unless payload_val.is_a?(Array)

    if to_hash[payload_key]
      to_hash[payload_key] += payload_val
    else
      to_hash[payload_key] = payload_val
    end

    return to_hash
  end

  to_hash.merge!(payload)
end