Class: MIPPeR::CbcModel

Inherits:
Model
  • Object
show all
Defined in:
lib/mipper/cbc/model.rb

Overview

A linear programming model using the COIN-OR solver

Instance Attribute Summary collapse

Attributes inherited from Model

#constraints, #variables

Instance Method Summary collapse

Methods inherited from Model

#<<, #compute_iis, #objective_value, #status, #variable_value, #write_lp

Constructor Details

#initializeCbcModel

Returns a new instance of CbcModel.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mipper/cbc/model.rb', line 8

def initialize
  fail unless MIPPeR.const_defined?(:Cbc)

  super

  @var_count = 0
  @constr_count = 0

  # Construct a new model
  @ptr = new_model
end

Instance Attribute Details

#ptrObject (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

#optimizeObject

Optimize the model



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mipper/cbc/model.rb', line 52

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_updateObject



37
# File 'lib/mipper/cbc/model.rb', line 37

alias_method :parent_update, :update

#sense=(sense) ⇒ Object

Set the sense of the model



45
46
47
48
49
# File 'lib/mipper/cbc/model.rb', line 45

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



67
68
69
70
71
72
73
74
# File 'lib/mipper/cbc/model.rb', line 67

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

#updateObject

Avoid doing anything here. Updating multiple times will break the model so we defer to #solve.



41
42
# File 'lib/mipper/cbc/model.rb', line 41

def update
end

#write_mps(filename) ⇒ Object

Write the model to a file in MPS format



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mipper/cbc/model.rb', line 21

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