Class: Cbc::Model

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

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



3
4
5
6
7
8
9
# File 'lib/cbc/model.rb', line 3

def initialize
  @model = FFI.Cbc_newModel
  @model.free = FFI["Cbc_deleteModel"]

  @below210 = Gem::Version.new(Cbc.lib_version) < Gem::Version.new("2.10.0")
  FFI.Cbc_setLogLevel(model, 0) unless @below210
end

Instance Method Details

#load_problem(sense:, start:, index:, value:, col_lower:, col_upper:, obj:, row_lower:, row_upper:, col_type:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cbc/model.rb', line 11

def load_problem(sense:, start:, index:, value:, col_lower:, col_upper:, obj:, row_lower:, row_upper:, col_type:)
  start_size = start.size
  index_size = index.size
  num_cols = col_lower.size
  num_rows = row_lower.size

  FFI.Cbc_loadProblem(
    model, num_cols, num_rows,
    big_index_array(start, start_size), int_array(index, index_size), double_array(value, index_size),
    double_array(col_lower, num_cols), double_array(col_upper, num_cols), double_array(obj, num_cols),
    double_array(row_lower, num_rows), double_array(row_upper, num_rows)
  )
  FFI.Cbc_setObjSense(model, FFI::OBJ_SENSE.fetch(sense))

  if col_type.size != num_cols
    raise ArgumentError, "wrong size (given #{value.size}, expected #{size})"
  end

  col_type.each_with_index do |v, i|
    case v
    when :integer
      FFI.Cbc_setInteger(model, i)
    when :continuous
      FFI.Cbc_setContinuous(model, i)
    else
      raise ArgumentError, "Unknown col_type"
    end
  end
end

#read_lp(filename) ⇒ Object



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

def read_lp(filename)
  check_version
  check_status FFI.Cbc_readLp(model, filename)
end

#read_mps(filename) ⇒ Object



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

def read_mps(filename)
  check_status FFI.Cbc_readMps(model, filename)
end

#solve(log_level: nil, time_limit: nil) ⇒ Object



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
# File 'lib/cbc/model.rb', line 59

def solve(log_level: nil, time_limit: nil)
  with_options(log_level: log_level, time_limit: time_limit) do
    # do not check status
    FFI.Cbc_solve(model)
  end

  num_cols = FFI.Cbc_getNumCols(model)

  status = FFI::STATUS[FFI.Cbc_status(model)]
  secondary_status = FFI::SECONDARY_STATUS[FFI.Cbc_secondaryStatus(model)]

  ret_status =
    case status
    when :not_started
      if FFI.Cbc_isInitialSolveProvenOptimal(model) != 0
        :optimal
      elsif FFI.Cbc_isInitialSolveProvenPrimalInfeasible(model) != 0
        :primal_infeasible
      else
        secondary_status
      end
    when :finished
      if FFI.Cbc_isProvenOptimal(model) != 0
        :optimal
      elsif FFI.Cbc_isProvenInfeasible(model) != 0
        :infeasible
      else
        secondary_status
      end
    else
      secondary_status
    end

  {
    status: ret_status,
    objective: FFI.Cbc_getObjValue(model),
    primal_col: read_double_array(FFI.Cbc_getColSolution(model), num_cols)
  }
end

#write_lp(filename) ⇒ Object



50
51
52
53
# File 'lib/cbc/model.rb', line 50

def write_lp(filename)
  check_version
  FFI.Cbc_writeLp(model, filename)
end

#write_mps(filename) ⇒ Object



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

def write_mps(filename)
  FFI.Cbc_writeMps(model, filename)
end