Class: Cbc::Util::CompressedRowStorage
- Inherits:
-
Object
- Object
- Cbc::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
- #change_indexes(new_idx) ⇒ Object
- #delete_missing_vars ⇒ Object
- #fill_matrix ⇒ Object
- #init_matrix ⇒ Object
- #make_variable_index ⇒ Object
- #move_block_to_start(array, block_start_idx, nb_values) ⇒ Object
- #move_constraint_to_start(range_idxs) ⇒ Object
- #nb_constraints ⇒ Object
- #new_indexes ⇒ Object
- #present_var_indexes ⇒ 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 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 6 def self.from_model(model) new.tap do |crs| crs.model = model crs.make_variable_index crs.fill_matrix end end |
Instance Method Details
#change_indexes(new_idx) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 77 def change_indexes(new_idx) 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 end |
#delete_missing_vars ⇒ Object
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 85 def delete_missing_vars new_idx = new_indexes return if new_idx.nil? change_indexes(new_idx) @col_idx.map! { |i| new_idx[i] } @model.vars = Array.new(@variable_index.size) @variable_index.each { |var, i| @model.vars[i] = var } end |
#fill_matrix ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 30 def fill_matrix init_matrix nb_cols = 0 c_idx = 0 while c_idx < @model.constraints.size constraint = @model.constraints[c_idx] @row_ptr[c_idx] = nb_cols nb_insert = constraint.terms.size @col_idx[nb_cols, nb_insert] = constraint.terms.map { |term| variable_index[term.var] } @values[nb_cols, nb_insert] = constraint.terms.map(&:mult) nb_cols += nb_insert c_idx += 1 end @row_ptr << @col_idx.size end |
#init_matrix ⇒ Object
23 24 25 26 27 28 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 23 def init_matrix nb_values = model.constraints.map { |c| c.terms.size }.inject(:+) || 0 @row_ptr = Array.new(model.constraints.size) @col_idx = Array.new(nb_values) @values = Array.new(nb_values) end |
#make_variable_index ⇒ Object
18 19 20 21 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 18 def make_variable_index indexes = @model.vars.size.times.to_a @variable_index = model.vars.zip(indexes).to_h end |
#move_block_to_start(array, block_start_idx, nb_values) ⇒ Object
116 117 118 119 120 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 116 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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 96 def move_constraint_to_start(range_idxs) # Move in the model constraints = model.constraints[range_idxs] @model.constraints = @model.constraints.clone @model.constraints[constraints.size, range_idxs.max] = model.constraints[0, range_idxs.min] @model.constraints[0, constraints.size] = 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.size)..(range_idxs.max)).reverse_each do |idx| @row_ptr[idx] = @row_ptr[idx - range_idxs.size] + nb_vars end @row_ptr[0, range_idxs.size] = 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
14 15 16 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 14 def nb_constraints row_ptr.size - 1 end |
#new_indexes ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 64 def new_indexes present = present_var_indexes return nil if present.all? new_idx = Array.new(@variable_index.size, -1) current_index = 0 new_idx.size.times.each do |idx| next unless present[idx] new_idx[idx] = current_index current_index += 1 end new_idx end |
#present_var_indexes ⇒ Object
58 59 60 61 62 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 58 def present_var_indexes present = Array.new(@variable_index.size, false) @col_idx.each { |col_idx| present[col_idx] = true } present end |
#restrict_to_n_constraints(nb_constraints) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ruby-cbc/utils/compressed_row_storage.rb', line 46 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 |