Class: MIPPeR::CbcModel

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

Instance Attribute Summary collapse

Attributes inherited from Model

#constraints, #variables

Instance Method Summary collapse

Methods inherited from Model

#<<, #compute_IIS, #update, #write_lp

Constructor Details

#initializeCbcModel



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

def initialize
  super

  @var_count = 0
  @constr_count = 0

  # Construct a new model
  @ptr = FFI::AutoPointer.new Cbc.Cbc_newModel,
                              Cbc.method(:Cbc_deleteModel)
  Cbc.Cbc_setParameter @ptr, 'logLevel', '0'
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



5
6
7
# File 'lib/mipper/cbc/model.rb', line 5

def ptr
  @ptr
end

Instance Method Details

#objective_valueObject

The value of the objective function



61
62
63
# File 'lib/mipper/cbc/model.rb', line 61

def objective_value
  Cbc.Cbc_getObjValue @ptr
end

#optimizeObject

Optimize the model



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mipper/cbc/model.rb', line 36

def optimize
  # Ensure pending variables and constraints are added
  update

  # Run the solver and save the status for later
  Cbc.Cbc_solve @ptr
  fail if Cbc.Cbc_status(@ptr) != 0

  # Check and store the model status
  if Cbc.Cbc_isProvenOptimal(@ptr) == 1
    @status = :optimized
  elsif Cbc.Cbc_isProvenInfeasible(@ptr) == 1 or
        Cbc.Cbc_isContinuousUnbounded(@ptr) == 1
    @status = :invalid
  else
    @status = :unknown
  end
end

#sense=(sense) ⇒ Object

Set the sense of the model



29
30
31
32
33
# File 'lib/mipper/cbc/model.rb', line 29

def sense=(sense)
  @sense = sense
  sense = sense == :min ? 1 : -1
  Cbc.Cbc_setObjSense @ptr, sense
end

#set_variable_bounds(var_index, lb, ub) ⇒ Object



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

def set_variable_bounds(var_index, lb, ub)
  Cbc.Cbc_setColLower @ptr, var_index, lb
  Cbc.Cbc_setColUpper @ptr, var_index, ub
end

#statusObject

Get the status of the model



56
57
58
# File 'lib/mipper/cbc/model.rb', line 56

def status
  @status
end

#variable_value(var) ⇒ Object

Get the value of a variable from the model



66
67
68
69
# File 'lib/mipper/cbc/model.rb', line 66

def variable_value(var)
  dblptr = Cbc.Cbc_getColSolution @ptr
  dblptr.read_array_of_double(@variables.length)[var.index]
end

#write_mps(filename) ⇒ Object

Write the model to a file in MPS format



20
21
22
23
24
25
26
# File 'lib/mipper/cbc/model.rb', line 20

def write_mps(filename)
  Cbc.Cbc_writeMps @ptr, File.join(File.dirname(filename),
                                   File.basename(filename, '.mps'))
  contents = Zlib::GzipReader.open(filename + '.gz').read
  File.delete(filename + '.gz')
  File.open(filename, 'w').write contents
end