Class: Cbc::Problem
- Inherits:
-
Object
- Object
- Cbc::Problem
- Defined in:
- lib/ruby-cbc/problem.rb
Defined Under Namespace
Classes: CCS
Instance Attribute Summary collapse
-
#crs ⇒ Object
Returns the value of attribute crs.
-
#model ⇒ Object
Returns the value of attribute model.
-
#variable_index ⇒ Object
Returns the value of attribute variable_index.
Class Method Summary collapse
- .crs_to_ccs(crs) ⇒ Object
- .finalizer(cbc_model, int_arrays, double_arrays) ⇒ Object
- .from_compressed_row_storage(crs, continuous: false) ⇒ Object
- .from_model(model, continuous: false) ⇒ Object
Instance Method Summary collapse
-
#best_bound ⇒ Object
Returns the best know bound so far.
- #create_cbc_problem(continuous: false) ⇒ Object
- #find_conflict ⇒ Object
- #find_conflict_vars ⇒ Object
- #objective_value ⇒ Object
- #proven_infeasible? ⇒ Boolean
- #proven_optimal? ⇒ Boolean
- #set_constraint_bounds(c, idx) ⇒ Object
- #set_time_limit(seconds) ⇒ Object
- #solution_limit_reached? ⇒ Boolean
- #solve(params = {}) ⇒ Object
- #time_limit_reached? ⇒ Boolean
- #value_of(var) ⇒ Object
- #write ⇒ Object
Instance Attribute Details
#crs ⇒ Object
Returns the value of attribute crs.
3 4 5 |
# File 'lib/ruby-cbc/problem.rb', line 3 def crs @crs end |
#model ⇒ Object
Returns the value of attribute model.
3 4 5 |
# File 'lib/ruby-cbc/problem.rb', line 3 def model @model end |
#variable_index ⇒ Object
Returns the value of attribute variable_index.
3 4 5 |
# File 'lib/ruby-cbc/problem.rb', line 3 def variable_index @variable_index end |
Class Method Details
.crs_to_ccs(crs) ⇒ Object
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 |
# File 'lib/ruby-cbc/problem.rb', line 25 def self.crs_to_ccs(crs) nb_per_column = Array.new(crs.col_idx.max.to_i + 1, 0) nb_values = crs.values.size crs.col_idx.each { |col_idx| nb_per_column[col_idx] += 1 } ccs = CCS.new(Array.new(nb_per_column.size + 1), Array.new(nb_values), Array.new(nb_values)) ccs.col_ptr[0] = 0 idx = 0 while idx < nb_per_column.size ccs.col_ptr[idx + 1] = ccs.col_ptr[idx] + nb_per_column[idx] idx += 1 end cols_idx = ccs.col_ptr.clone row_idx = 0 end_row_idx = crs.row_ptr.size - 1 while row_idx < end_row_idx current_idx = crs.row_ptr[row_idx] last_idx = crs.row_ptr[row_idx + 1] - 1 while current_idx <= last_idx col_idx = crs.col_idx[current_idx] ccs_col_idx = cols_idx[col_idx] cols_idx[col_idx] += 1 ccs.row_idx[ccs_col_idx] = row_idx ccs.values[ccs_col_idx] = crs.values[current_idx] current_idx += 1 end row_idx += 1 end ccs end |
.finalizer(cbc_model, int_arrays, double_arrays) ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/ruby-cbc/problem.rb', line 185 def self.finalizer(cbc_model, int_arrays, double_arrays) proc do Cbc_wrapper.Cbc_deleteModel(cbc_model) int_arrays.each { |ar| Cbc_wrapper.delete_intArray(ar) } double_arrays.each { |ar| Cbc_wrapper.delete_doubleArray(ar) } end end |
.from_compressed_row_storage(crs, continuous: false) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/ruby-cbc/problem.rb', line 10 def self.from_compressed_row_storage(crs, continuous: false) new.tap do |p| p.model = crs.model p.variable_index = crs.variable_index p.crs = crs p.create_cbc_problem(continuous: continuous) end end |
.from_model(model, continuous: false) ⇒ Object
5 6 7 8 |
# File 'lib/ruby-cbc/problem.rb', line 5 def self.from_model(model, continuous: false) crs = Util::CompressedRowStorage.from_model(model) from_compressed_row_storage(crs, continuous: continuous) end |
Instance Method Details
#best_bound ⇒ Object
Returns the best know bound so far
173 174 175 |
# File 'lib/ruby-cbc/problem.rb', line 173 def best_bound Cbc_wrapper.Cbc_getBestPossibleObjValue(@cbc_model) end |
#create_cbc_problem(continuous: false) ⇒ Object
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ruby-cbc/problem.rb', line 58 def create_cbc_problem(continuous: false) @int_arrays = [] @double_arrays = [] ccs = self.class.crs_to_ccs(@crs) objective = Array.new(ccs.nb_vars, 0) if model.objective model.objective.terms.each do |term| objective[@variable_index[term.var]] = term.mult end end @cbc_model = Cbc_wrapper.Cbc_newModel Cbc_wrapper.Cbc_loadProblem( @cbc_model, ccs.nb_vars, @crs.nb_constraints, to_int_array(ccs.col_ptr), to_int_array(ccs.row_idx), to_double_array(ccs.values), nil, nil, to_double_array(objective), nil, nil) # Segmentation errors when setting name # Cbc_wrapper.Cbc_setProblemName(@cbc_model, model.name) if model.name if model.objective obj_sense = model.objective.objective_function == Ilp::Objective::MINIMIZE ? 1 : -1 Cbc_wrapper.Cbc_setObjSense(@cbc_model, obj_sense) end idx = 0 while idx < @crs.nb_constraints c = @crs.model.constraints[idx] set_constraint_bounds(c, idx) idx += 1 end idx = 0 while idx < ccs.nb_vars v = @crs.model.vars[idx] if continuous Cbc_wrapper.Cbc_setContinuous(@cbc_model, idx) else case v.kind when Ilp::Var::INTEGER_KIND, Ilp::Var::BINARY_KIND Cbc_wrapper.Cbc_setInteger(@cbc_model, idx) when Ilp::Var::CONTINUOUS_KIND Cbc_wrapper.Cbc_setContinuous(@cbc_model, idx) end end Cbc_wrapper.Cbc_setColLower(@cbc_model, idx, v.lower_bound) unless v.lower_bound.nil? Cbc_wrapper.Cbc_setColUpper(@cbc_model, idx, v.upper_bound) unless v.upper_bound.nil? idx += 1 end ObjectSpace.define_finalizer(self, self.class.finalizer(@cbc_model, @int_arrays, @double_arrays)) @default_solve_params = { log: 0 } end |
#find_conflict ⇒ Object
177 178 179 |
# File 'lib/ruby-cbc/problem.rb', line 177 def find_conflict @conflict_set ||= ConflictSolver.new(self).find_conflict end |
#find_conflict_vars ⇒ Object
181 182 183 |
# File 'lib/ruby-cbc/problem.rb', line 181 def find_conflict_vars @conflict_vars ||= find_conflict.map(&:vars).flatten.uniq end |
#objective_value ⇒ Object
168 169 170 |
# File 'lib/ruby-cbc/problem.rb', line 168 def objective_value Cbc_wrapper.Cbc_getObjValue(@cbc_model) end |
#proven_infeasible? ⇒ Boolean
156 157 158 |
# File 'lib/ruby-cbc/problem.rb', line 156 def proven_infeasible? Cbc_wrapper.Cbc_isProvenInfeasible(@cbc_model) == 1 end |
#proven_optimal? ⇒ Boolean
152 153 154 |
# File 'lib/ruby-cbc/problem.rb', line 152 def proven_optimal? Cbc_wrapper.Cbc_isProvenOptimal(@cbc_model) == 1 end |
#set_constraint_bounds(c, idx) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby-cbc/problem.rb', line 116 def set_constraint_bounds(c, idx) case c.type when Ilp::Constraint::LESS_OR_EQ Cbc_wrapper.Cbc_setRowUpper(@cbc_model, idx, c.bound) when Ilp::Constraint::GREATER_OR_EQ Cbc_wrapper.Cbc_setRowLower(@cbc_model, idx, c.bound) when Ilp::Constraint::EQUALS Cbc_wrapper.Cbc_setRowUpper(@cbc_model, idx, c.bound) Cbc_wrapper.Cbc_setRowLower(@cbc_model, idx, c.bound) end end |
#set_time_limit(seconds) ⇒ Object
148 149 150 |
# File 'lib/ruby-cbc/problem.rb', line 148 def set_time_limit(seconds) @default_solve_params[:sec] = seconds end |
#solution_limit_reached? ⇒ Boolean
164 165 166 |
# File 'lib/ruby-cbc/problem.rb', line 164 def solution_limit_reached? Cbc_wrapper.Cbc_isSolutionLimitReached(@cbc_model) == 1 end |
#solve(params = {}) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/ruby-cbc/problem.rb', line 128 def solve(params = {}) @default_solve_params.merge(params).each do |name, value| Cbc_wrapper.Cbc_setParameter(@cbc_model, name.to_s, value.to_s) end Cbc_wrapper.Cbc_solve(@cbc_model) @solution = Cbc_wrapper::DoubleArray.frompointer(Cbc_wrapper.Cbc_getColSolution(@cbc_model)) @double_arrays << @solution @solution end |
#time_limit_reached? ⇒ Boolean
160 161 162 |
# File 'lib/ruby-cbc/problem.rb', line 160 def time_limit_reached? Cbc_wrapper.Cbc_isSecondsLimitReached(@cbc_model) == 1 end |
#value_of(var) ⇒ Object
138 139 140 141 142 143 144 145 146 |
# File 'lib/ruby-cbc/problem.rb', line 138 def value_of(var) idx = @variable_index[var] return nil if idx.nil? if var.kind == Ilp::Var::CONTINUOUS_KIND @solution[idx] else @solution[idx].round end end |
#write ⇒ Object
193 194 195 |
# File 'lib/ruby-cbc/problem.rb', line 193 def write Cbc_wrapper.Cbc_writeMps(@cbc_model, "test") end |