Class: Clp::Model

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

Instance Method Summary collapse

Constructor Details

#initializeModel

Returns a new instance of Model.



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

def initialize
  @model = FFI.Clp_newModel
  @model.free = FFI["Clp_deleteModel"]

  FFI.Clp_setLogLevel(model, 0)
end

Instance Method Details

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/clp/model.rb', line 10

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

  FFI.Clp_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.Clp_setObjSense(model, FFI::OBJ_SENSE.fetch(sense))
  FFI.Clp_setObjectiveOffset(model, offset)
end

#read_mps(filename) ⇒ Object



26
27
28
# File 'lib/clp/model.rb', line 26

def read_mps(filename)
  check_status FFI.Clp_readMps(model, filename, 0, 0)
end

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



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

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

  num_rows = FFI.Clp_numberRows(model)
  num_cols = FFI.Clp_numberColumns(model)

  {
    status: FFI::STATUS[FFI.Clp_status(model)],
    objective: FFI.Clp_objectiveValue(model),
    primal_row: read_double_array(FFI.Clp_primalRowSolution(model), num_rows),
    primal_col: read_double_array(FFI.Clp_primalColumnSolution(model), num_cols),
    dual_row: read_double_array(FFI.Clp_dualRowSolution(model), num_rows),
    dual_col: read_double_array(FFI.Clp_dualColumnSolution(model), num_cols)
  }
end

#write_mps(filename) ⇒ Object



30
31
32
33
34
35
# File 'lib/clp/model.rb', line 30

def write_mps(filename)
  unless FFI.respond_to?(:Clp_writeMps)
    raise Error, "This feature requires Clp 1.17.2+"
  end
  check_status FFI.Clp_writeMps(model, filename, 0, 1, 0)
end