Class: Util::CompressedRowStorage
- Inherits:
-
Object
- Object
- Util::CompressedRowStorage
- Defined in:
- lib/ruby-cbc/utils/compressed_row_storage.rb
Instance Attribute Summary collapse
-
#col_idx ⇒ Object
Returns the value of attribute col_idx.
-
#model ⇒ Object
Returns the value of attribute model.
-
#row_ptr ⇒ Object
Returns the value of attribute row_ptr.
-
#values ⇒ Object
Returns the value of attribute values.
-
#variable_index ⇒ Object
Returns the value of attribute variable_index.
Class Method Summary collapse
Instance Method Summary collapse
- #delete_missing_vars ⇒ Object
- #fill_matrix ⇒ Object
- #move_block_to_start(array, block_start_idx, nb_values) ⇒ Object
- #move_constraint_to_start(range_idxs) ⇒ Object
- #nb_constraints ⇒ Object
- #present_var_indices ⇒ Object
- #restrict_to_n_constraints(nb_constraints) ⇒ Object
Instance Attribute Details
#col_idx ⇒ Object
Returns the value of attribute col_idx.
4 5 6 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 4 def col_idx @col_idx end |
#model ⇒ Object
Returns the value of attribute model.
4 5 6 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 4 def model @model end |
#row_ptr ⇒ Object
Returns the value of attribute row_ptr.
4 5 6 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 4 def row_ptr @row_ptr end |
#values ⇒ Object
Returns the value of attribute values.
4 5 6 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 4 def values @values end |
#variable_index ⇒ Object
Returns the value of attribute variable_index.
4 5 6 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 4 def variable_index @variable_index end |
Class Method Details
.from_model(model) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 6 def self.from_model(model) new.tap do |crs| crs.model = model crs.variable_index = {} idx = 0 while idx < model.vars.size do v = model.vars[idx] crs.variable_index[v] = idx idx += 1 end crs.fill_matrix end end |
Instance Method Details
#delete_missing_vars ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 64 def delete_missing_vars at_least_one_missing = false here = present_var_indices new_idx = Array.new(@variable_index.count, -1) idx = 0 current_index = 0 while idx < new_idx.size do if here[idx] new_idx[idx] = current_index current_index += 1 else at_least_one_missing = true end idx += 1 end return unless at_least_one_missing new_variable_index = {} @variable_index.each do |v, i| new_variable_index[v] = new_idx[i] if new_idx[i] != -1 end @variable_index = new_variable_index @col_idx.map! { |idx| new_idx[idx] } @model.vars = Array.new(@variable_index.size) @variable_index.each do |var, idx| @model.vars[idx] = var end end |
#fill_matrix ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 24 def fill_matrix nb_values = model.constraints.map { |c| c.terms.count }.inject(:+) || 0 @row_ptr = Array.new(model.constraints.count) @col_idx = Array.new(nb_values) @values = Array.new(nb_values) nb_cols = 0 c_idx = 0 while c_idx < @model.constraints.size do constraint = @model.constraints[c_idx] @row_ptr[c_idx] = nb_cols nb_insert = constraint.terms.count @col_idx[nb_cols, nb_insert] = constraint.terms.map { |term| variable_index[term.var] } @values[nb_cols, nb_insert] = constraint.terms.map { |term| term.mult } nb_cols += nb_insert c_idx += 1 end @row_ptr << @col_idx.count end |
#move_block_to_start(array, block_start_idx, nb_values) ⇒ Object
114 115 116 117 118 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 114 def move_block_to_start(array, block_start_idx, nb_values) to_move = array[block_start_idx, nb_values] array[nb_values, block_start_idx] = array[0, block_start_idx] array[0, nb_values] = to_move end |
#move_constraint_to_start(range_idxs) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 94 def move_constraint_to_start(range_idxs) # Move in the model constraints = model.constraints[range_idxs] @model.constraints = @model.constraints.clone @model.constraints[constraints.count, range_idxs.max] = model.constraints[0, range_idxs.min] @model.constraints[0, constraints.count] = constraints # Move in the matrix constraint_start_idx = @row_ptr[range_idxs.min] nb_vars = @row_ptr[range_idxs.max + 1] - constraint_start_idx offset= @row_ptr[range_idxs.min] new_begin = @row_ptr[range_idxs].map! { |idx| idx - offset } ((range_idxs.count)..(range_idxs.max)).reverse_each do |idx| @row_ptr[idx] = @row_ptr[idx - range_idxs.count] + nb_vars end @row_ptr[0, range_idxs.count] = new_begin move_block_to_start(@col_idx, constraint_start_idx, nb_vars) move_block_to_start(@values, constraint_start_idx, nb_vars) end |
#nb_constraints ⇒ Object
20 21 22 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 20 def nb_constraints row_ptr.count - 1 end |
#present_var_indices ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 56 def present_var_indices is_present = Array.new(@variable_index.count, false) @col_idx.each do |col_idx| is_present[col_idx] = true end is_present end |
#restrict_to_n_constraints(nb_constraints) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 44 def restrict_to_n_constraints(nb_constraints) length_of_values = @row_ptr[nb_constraints] CompressedRowStorage.new.tap do |crs| crs.model = @model.clone crs.variable_index = @variable_index crs.row_ptr = @row_ptr[0, nb_constraints + 1] crs.col_idx = @col_idx[0, length_of_values] crs.values = @values[0, length_of_values] crs.delete_missing_vars end end |