Class: Highs::Model

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

Instance Method Summary collapse

Constructor Details

#initializeModel



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

def initialize
  @ptr = FFI.Highs_create
  @ptr.free = FFI["Highs_destroy"]

  check_status FFI.Highs_setBoolOptionValue(@ptr, +"output_flag", 0)
end

Instance Method Details

#solve(verbose: false, time_limit: nil) ⇒ Object



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

def solve(verbose: false, time_limit: nil)
  num_col = FFI.Highs_getNumCol(@ptr)
  num_row = FFI.Highs_getNumRow(@ptr)

  col_value = DoubleArray.new(num_col)
  col_dual = DoubleArray.new(num_col)
  row_value = DoubleArray.new(num_row)
  row_dual = DoubleArray.new(num_row)
  col_basis = IntArray.new(num_col)
  row_basis = IntArray.new(num_row)

  with_options(verbose: verbose, time_limit: time_limit) do
    check_status FFI.Highs_run(@ptr)
  end
  check_status FFI.Highs_getSolution(@ptr, col_value, col_dual, row_value, row_dual)
  check_status FFI.Highs_getBasis(@ptr, col_basis, row_basis)
  model_status = FFI.Highs_getModelStatus(@ptr)

  {
    status: FFI::MODEL_STATUS[model_status],
    obj_value: FFI.Highs_getObjectiveValue(@ptr),
    col_value: col_value.to_a,
    col_dual: col_dual.to_a,
    row_value: row_value.to_a,
    row_dual: row_dual.to_a,
    col_basis: col_basis.to_a.map { |v| FFI::BASIS_STATUS[v] },
    row_basis: row_basis.to_a.map { |v| FFI::BASIS_STATUS[v] }
  }
end

#to_ptrObject



44
45
46
# File 'lib/highs/model.rb', line 44

def to_ptr
  @ptr
end

#write(filename) ⇒ Object



40
41
42
# File 'lib/highs/model.rb', line 40

def write(filename)
  check_status FFI.Highs_writeModel(@ptr, +filename)
end