Class: MIPPeR::GLPKModel

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

Instance Attribute Summary collapse

Attributes inherited from Model

#constraints, #variables

Instance Method Summary collapse

Methods inherited from Model

#<<, #compute_iis, #objective_value, #status, #update, #variable_value

Constructor Details

#initializeGLPKModel

Returns a new instance of GLPKModel.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mipper/glpk/model.rb', line 5

def initialize
  fail unless MIPPeR.const_defined?(:GLPK)

  super

  @var_count = 0
  @constr_count = 0

  # Constraint matrix initialization (see #add_constraints)
  @ia = [0]
  @ja = [0]
  @ar = [0]

  # Disable terminal output
  GLPK.glp_term_out GLPK::GLP_OFF

  # Construct a new model
  @ptr = FFI::AutoPointer.new GLPK.glp_create_prob,
                              GLPK.method(:glp_delete_prob)
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



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

def ptr
  @ptr
end

Instance Method Details

#optimizeObject

Optimize the model



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mipper/glpk/model.rb', line 46

def optimize
  # Ensure pending variables and constraints are added
  update

  # Run the solver and save the status for later
  iocp = GLPK::IOCP.new
  GLPK.glp_init_iocp iocp
  iocp[:presolve] = GLPK::GLP_ON
  status = GLPK.glp_intopt(@ptr, iocp)

  save_solution status
end

#sense=(sense) ⇒ Object

Set the sense of the model



39
40
41
42
43
# File 'lib/mipper/glpk/model.rb', line 39

def sense=(sense)
  @sense = sense
  sense = sense == :min ? GLPK::GLP_MIN : GLPK::GLP_MAX
  GLPK.glp_set_obj_dir @ptr, sense
end

#set_variable_bounds(var_index, lb, ub) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mipper/glpk/model.rb', line 59

def set_variable_bounds(var_index, lb, ub)
  # Determine the type of the variable bounds
  if lb == ub
    type = GLPK::GLP_FX
  elsif lb.finite?
    type = ub.finite? ? GLPK::GLP_DB : GLPK::GLP_LO
  else
    type = ub.finite? ? GLPK::GLP_UP : GLPK::GLP_FR
  end

  # Set the bounds on the variable
  GLPK.glp_set_col_bnds(@ptr, var_index, type, lb, ub)
end

#write_lp(filename) ⇒ Object

Write the model to a file in CPLEX LP format



27
28
29
30
# File 'lib/mipper/glpk/model.rb', line 27

def write_lp(filename)
  ret = GLPK.glp_write_lp @ptr, 0, filename
  fail if ret != 0
end

#write_mps(filename) ⇒ Object

Write the model to a file in MPS format



33
34
35
36
# File 'lib/mipper/glpk/model.rb', line 33

def write_mps(filename)
  ret = GLPK.glp_write_mps @ptr, GLPK::GLP_MPS_FILE, nil, filename
  fail if ret != 0
end