Class: MIPPeR::CbcModel
Overview
A linear programming model using the COIN-OR solver
Instance Attribute Summary collapse
-
#ptr ⇒ Object
readonly
Returns the value of attribute ptr.
Attributes inherited from Model
Instance Method Summary collapse
-
#initialize ⇒ CbcModel
constructor
A new instance of CbcModel.
-
#optimize ⇒ Object
Optimize the model.
- #parent_update ⇒ Object
-
#sense=(sense) ⇒ Object
Set the sense of the model.
-
#set_variable_bounds(var_index, lb, ub, force = false) ⇒ Object
Set the bounds of a variable in the model.
-
#update ⇒ Object
Avoid doing anything here.
-
#write_mps(filename) ⇒ Object
Write the model to a file in MPS format.
Methods inherited from Model
#<<, #compute_iis, #objective_value, #status, #variable_value, #write_lp
Constructor Details
#initialize ⇒ CbcModel
Returns a new instance of CbcModel.
8 9 10 11 12 13 14 15 16 |
# File 'lib/mipper/cbc/model.rb', line 8 def initialize super @var_count = 0 @constr_count = 0 # Construct a new model @ptr = new_model end |
Instance Attribute Details
#ptr ⇒ Object (readonly)
Returns the value of attribute ptr.
6 7 8 |
# File 'lib/mipper/cbc/model.rb', line 6 def ptr @ptr end |
Instance Method Details
#optimize ⇒ Object
Optimize the model
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/mipper/cbc/model.rb', line 50 def optimize # Ensure pending variables and constraints are added parent_update # Run the solver and save the status for later Cbc.Cbc_solve @ptr fail if Cbc.Cbc_status(@ptr) != 0 save_solution @ptr = new_model reset_model end |
#parent_update ⇒ Object
35 |
# File 'lib/mipper/cbc/model.rb', line 35 alias_method :parent_update, :update |
#sense=(sense) ⇒ Object
Set the sense of the model
43 44 45 46 47 |
# File 'lib/mipper/cbc/model.rb', line 43 def sense=(sense) @sense = sense sense = sense == :min ? 1 : -1 Cbc.Cbc_setObjSense @ptr, sense end |
#set_variable_bounds(var_index, lb, ub, force = false) ⇒ Object
Set the bounds of a variable in the model
65 66 67 68 69 70 71 72 |
# File 'lib/mipper/cbc/model.rb', line 65 def set_variable_bounds(var_index, lb, ub, force = false) # This is a bit of a hack so that we don't try to set # the variable bounds before they get added to the model return unless force Cbc.Cbc_setColLower @ptr, var_index, lb Cbc.Cbc_setColUpper @ptr, var_index, ub end |
#update ⇒ Object
Avoid doing anything here. Updating multiple times will break the model so we defer to #solve.
39 40 |
# File 'lib/mipper/cbc/model.rb', line 39 def update end |
#write_mps(filename) ⇒ Object
Write the model to a file in MPS format
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mipper/cbc/model.rb', line 19 def write_mps(filename) # Make a new model and ensure everything is added old_ptr = @ptr @ptr = new_model parent_update Cbc.Cbc_writeMps @ptr, filename.chomp('.mps') contents = Zlib::GzipReader.open(filename + '.gz').read File.delete(filename + '.gz') File.open(filename, 'w').write contents # Reset to the original model @ptr = old_ptr reset_model end |