Class: MIPPeR::GurobiModel

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

Overview

A linear programming model using the Gurobi solver

Instance Attribute Summary collapse

Attributes inherited from Model

#constraints, #variables

Instance Method Summary collapse

Methods inherited from Model

#<<, #objective_value, #status, #variable_value

Constructor Details

#initializeGurobiModel

Returns a new instance of GurobiModel.



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

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

  super

  @var_count = 0

  @environment = Gurobi::Environment.new

  @ptr = FFI::MemoryPointer.new :pointer
  Gurobi.GRBnewmodel @environment.ptr, @ptr, 'model',
                     0, nil, nil, nil, nil, nil
  @ptr = @ptr.read_pointer

  # Ensure the model is freed
  ObjectSpace.define_finalizer self, self.class.finalize(@ptr)
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



4
5
6
# File 'lib/mipper/gurobi/model.rb', line 4

def environment
  @environment
end

#ptrObject (readonly)

Returns the value of attribute ptr.



4
5
6
# File 'lib/mipper/gurobi/model.rb', line 4

def ptr
  @ptr
end

Instance Method Details

#compute_iisObject

Compute an irreducible inconsistent subsytem for the model



62
63
64
65
# File 'lib/mipper/gurobi/model.rb', line 62

def compute_iis
  ret = Gurobi.GRBcomputeIIS @ptr
  fail if ret != 0
end

#optimizeObject

Optimize the model



51
52
53
54
55
56
57
58
59
# File 'lib/mipper/gurobi/model.rb', line 51

def optimize
  # Ensure pending variables and constraints are added
  update

  ret = Gurobi.GRBoptimize @ptr
  fail if ret != 0

  save_solution
end

#sense=(sense) ⇒ Object

Set the sense of the model



43
44
45
46
47
48
# File 'lib/mipper/gurobi/model.rb', line 43

def sense=(sense)
  @sense = sense
  sense = sense == :min ? Gurobi::GRB_MINIMIZE : Gurobi::GRB_MAXIMIZE
  ret = Gurobi.GRBsetintattr @ptr, Gurobi::GRB_INT_ATTR_MODELSENSE, sense
  fail if ret != 0
end

#set_variable_bounds(var_index, lb, ub) ⇒ Object



67
68
69
70
# File 'lib/mipper/gurobi/model.rb', line 67

def set_variable_bounds(var_index, lb, ub)
  set_double_attribute Gurobi::GRB_DBL_ATTR_LB, var_index, lb
  set_double_attribute Gurobi::GRB_DBL_ATTR_UB, var_index, ub
end

#updateObject

Update the model



25
26
27
28
29
30
# File 'lib/mipper/gurobi/model.rb', line 25

def update
  super

  ret = Gurobi.GRBupdatemodel @ptr
  fail if ret != 0
end

#write_lp(filename) ⇒ Object

Write the model to a file in CPLEX LP format



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

def write_lp(filename)
  Gurobi.GRBwrite @ptr, filename
end

#write_mps(filename) ⇒ Object

Write the model to a file in MPS format



38
39
40
# File 'lib/mipper/gurobi/model.rb', line 38

def write_mps(filename)
  Gurobi.GRBwrite @ptr, filename
end