Module: Unodos::Solver
- Defined in:
- lib/unodos/solver.rb
Constant Summary collapse
- TOLERANCE =
1e-12- NAMED_BASES =
[ Unodos::NamedBase.new('1') { |_| 1 }, Unodos::NamedBase.new('n') { |n| n }, Unodos::NamedBase.new('n**2') { |n| n**2 }, Unodos::NamedBase.new('n**3') { |n| n**3 }, Unodos::NamedBase.new('n**4') { |n| n**4 }, Unodos::NamedBase.new('n**5') { |n| n**5 }, Unodos::NamedBase.new('2**n') { |n| 2**n }, ]
Class Method Summary collapse
- .find_solve(vectors, bvector, &block) ⇒ Object
- .least_square_solve(vectors, bvector, &block) ⇒ Object
- .lup_solve(lup, b) ⇒ Object
- .match_vector(vector, bvector) ⇒ Object
- .recursive_solve(vectors, bvector, first_required, &block) ⇒ Object
- .select_solve(vectors, bvector, max_items, first_required, &block) ⇒ Object
- .solve(list, differential_level, min_cost) ⇒ Object
Class Method Details
.find_solve(vectors, bvector, &block) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/unodos/solver.rb', line 73 def self.find_solve(vectors, bvector, &block) (1...vectors.size).each do |i| least_square_solve [vectors[0], vectors[i]], bvector do |vs, pos| block.call vs, pos.map { |c| c == 1 ? i : 0 } end end end |
.least_square_solve(vectors, bvector, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/unodos/solver.rb', line 94 def self.least_square_solve(vectors, bvector, &block) mat = Matrix[*vectors.transpose] tmat = mat.transpose m = tmat * mat b = tmat * Vector[*bvector] vs = lup_solve m.lup, b.to_a return unless vs max_diff = bvector.each_with_index.map do |bv, i| (vectors.zip(vs).sum { |a, v| v * a[i] } - bv).abs end.max block.call vs, (0...vectors.size).to_a if max_diff < TOLERANCE end |
.lup_solve(lup, b) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/unodos/solver.rb', line 14 def self.lup_solve(lup, b) size = b.size m = b.values_at(*lup.pivots) mat_l = lup.l mat_u = lup.u size.times do |k| (k + 1).upto(size - 1) do |i| m[i] -= m[k] * mat_l[i, k] end end (size - 1).downto(0) do |k| next if m[k] == 0 return nil if mat_u[k, k].zero? m[k] = m[k].quo mat_u[k, k] k.times do |i| m[i] -= m[k] * mat_u[i, k] end end m end |
.match_vector(vector, bvector) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/unodos/solver.rb', line 66 def self.match_vector(vector, bvector) v, b = vector.zip(bvector).max_by { |a,| a.abs } a = b.quo v err = vector.zip(bvector).map { |v, b| (v * a - b).abs }.max a if err < TOLERANCE end |
.recursive_solve(vectors, bvector, first_required, &block) ⇒ Object
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 |
# File 'lib/unodos/solver.rb', line 107 def self.recursive_solve(vectors, bvector, first_required, &block) return least_square_solve vectors, bvector, &block if vectors.size < bvector.size size = vectors.size out_size = bvector.size skip_size = size - out_size lup = Matrix[*vectors.transpose].lup mat_l = lup.l mat_u = lup.u.to_a bvector = bvector.values_at(*lup.pivots) out_size.times do |k| (k + 1).upto(out_size - 1) do |i| bvector[i] -= bvector[k] * mat_l[i, k] end end solved = lambda do |u, selected| b = bvector.dup (selected.size - 1).downto 0 do |i| j = selected[i] next if b[i] == 0 return if u[i][j] == 0 b[i] = b[i].quo u[i][j] i.times do |k| b[k] -= b[i] * u[k][j] end end block.call b, selected end solve = lambda do |u, selected, index| return solved.call u, selected if selected.size == out_size j = selected.size restore_index = (j .. [index, out_size - 1].min).max_by do |k| u[k][index].abs end u[j], u[restore_index] = u[restore_index], u[j] bvector[j], bvector[restore_index] = bvector[restore_index], bvector[j] restore = (j + 1 .. [index, out_size - 1].min).map do |k| v = u[j][index] == 0 ? 0 : u[k][index].quo(u[j][index]) (index + 1 ... size).each do |l| u[k][l] -= v * u[j][l] end bvector[k] -= v * bvector[j] [k, v] end selected.push index solve.call u, selected, index + 1 selected.pop restore.reverse_each do |k, v| (index + 1 ... size).each do |l| u[k][l] += v * u[j][l] end bvector[k] += v * bvector[j] end u[j], u[restore_index] = u[restore_index], u[j] bvector[j], bvector[restore_index] = bvector[restore_index], bvector[j] solve.call u, selected, index + 1 if size - index > out_size - selected.size end if first_required solve.call mat_u, [0], 1 else solve.call mat_u, [], 0 end end |
.select_solve(vectors, bvector, max_items, first_required, &block) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/unodos/solver.rb', line 81 def self.select_solve(vectors, bvector, max_items, first_required, &block) if first_required && max_items == 1 a = match_vector vectors[0], bvector block.call [a], [0] if a elsif vectors.size < bvector.size least_square_solve vectors, bvector, &block elsif first_required && max_items == 2 find_solve vectors, bvector, &block elsif recursive_solve vectors, bvector, first_required, &block end end |
.solve(list, differential_level, min_cost) ⇒ Object
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 |
# File 'lib/unodos/solver.rb', line 35 def self.solve(list, differential_level, min_cost) base_cost = differential_level max_items = min_cost - base_cost - 1 return nil if max_items <= 0 result = nil vector_max_bases = NAMED_BASES.map do |base| vector = (differential_level..list.size-1).map do |i| base.proc.call(i) end [vector, vector.map(&:abs).max, base] end if differential_level > 0 (1..differential_level).each do |level| vector = list.take(list.size - level).drop(differential_level - level) vector_max_bases.unshift [vector, vector.map(&:abs).max, Unodos::DifferentialBase.new(level)] end list = list.drop differential_level end select_solve vector_max_bases.map(&:first), list, max_items, differential_level > 0 do |vs, pos| rs = vector_max_bases.values_at(*pos).zip(vs) rs.each { |r| r[1] = 0 if (r[0][1] * r[1]).abs < TOLERANCE } next if differential_level > 0 && rs[0][1] == 0 cost = rs.sum { |(_, _, base), v| v == 0 ? 0 : 1 } + base_cost if cost < min_cost min_cost = cost result = rs end end [min_cost, result] if result end |