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
- #fill_matrix ⇒ Object
- #move_block_to_start(array, block_start_idx, nb_values) ⇒ Object
- #move_constraint_to_start(range_idxs) ⇒ Object
- #nb_constraints ⇒ 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
#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
75 76 77 78 79 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 75 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
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 55 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 |
#restrict_to_n_constraints(nb_constraints) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# 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 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] end end |