Class: MIPPeR::LPSolveModel

Inherits:
Model
  • Object
show all
Defined in:
lib/mipper/lp_solve/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, #write_mps

Constructor Details

#initializeLPSolveModel

Returns a new instance of LPSolveModel.



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

def initialize
  super

  @var_count = 0
  @constr_count = 0

  # Construct a new model
  @ptr = FFI::AutoPointer.new LPSolve.make_lp(0, 0),
                              LPSolve.method(:delete_lp)

  # Disable output
  ret = LPSolve.set_outputfile @ptr, ''
  fail if ret != 1
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



3
4
5
# File 'lib/mipper/lp_solve/model.rb', line 3

def ptr
  @ptr
end

Instance Method Details

#objective_valueObject

The value of the objective function



49
50
51
# File 'lib/mipper/lp_solve/model.rb', line 49

def objective_value
  LPSolve.get_objective @ptr
end

#optimizeObject

Optimize the model



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

def optimize
  # Ensure pending variables and constraints are added
  update

  # Run the solver and save the status for later
  @status = LPSolve.solve @ptr
end

#sense=(sense) ⇒ Object

Set the sense of the model



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

def sense=(sense)
  @sense = sense
  sense = sense == :min ? 0 : 1
  LPSolve.set_sense @ptr, sense
end

#set_variable_bounds(var_index, lb, ub) ⇒ Object



64
65
66
67
# File 'lib/mipper/lp_solve/model.rb', line 64

def set_variable_bounds(var_index, lb, ub)
  ret = LPSolve.set_bounds @ptr, var_index, lb, ub
  fail if ret != 1
end

#statusObject

Get the status of the model



37
38
39
40
41
42
43
44
45
46
# File 'lib/mipper/lp_solve/model.rb', line 37

def status
  case @status
  when LPSolve::OPTIMAL
    :optimized
  when LPSolve::INFEASIBLE, LPSolve::UNBOUNDED, LPSolve::DEGENERATE
    :invalid
  else
    :unknown
  end
end

#variable_value(var) ⇒ Object

Get the value of a variable from the model



54
55
56
57
58
59
60
61
62
# File 'lib/mipper/lp_solve/model.rb', line 54

def variable_value(var)
  # To access the value of a result variable we need an index into the
  # solution which setarts with the objective function, followed by all
  # of the constraints, and finally the variables
  # We explicitly ask for the number of rows since extra constraints
  # may have been added
  rows = LPSolve.get_Nrows(@ptr)
  LPSolve.get_var_primalresult @ptr, rows + var.index
end