Class: Guruby::Model

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Model

Returns a new instance of Model.



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

def initialize(env)
  @environment = env

  @ptr = FFI::MemoryPointer.new :pointer
  Gurobi.GRBnewmodel env.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)

  @var_count = 0
  @variables = []
  @constraints = []

  @pending_variables = []
  @pending_constraints = []
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



5
6
7
# File 'lib/guruby/model.rb', line 5

def constraints
  @constraints
end

#environmentObject (readonly)

Returns the value of attribute environment.



5
6
7
# File 'lib/guruby/model.rb', line 5

def environment
  @environment
end

#ptrObject (readonly)

Returns the value of attribute ptr.



5
6
7
# File 'lib/guruby/model.rb', line 5

def ptr
  @ptr
end

#variablesObject (readonly)

Returns the value of attribute variables.



5
6
7
# File 'lib/guruby/model.rb', line 5

def variables
  @variables
end

Instance Method Details

#<<(obj) ⇒ Object

Add new objects (variables and constraints) to the model



26
27
28
29
30
31
32
33
34
35
# File 'lib/guruby/model.rb', line 26

def <<(obj)
  case obj
  when Variable
    @pending_variables << obj
  when Constraint
    @pending_constraints << obj
  else
    fail TypeError
  end
end

#compute_IISObject

Compute an irreducible inconsistent subsytem for the model



86
87
88
89
# File 'lib/guruby/model.rb', line 86

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

#objective_valueObject

The value of the objective function



92
93
94
95
96
97
# File 'lib/guruby/model.rb', line 92

def objective_value
  dblptr = FFI::MemoryPointer.new :pointer
  ret = Gurobi.GRBgetdblattr @ptr, GRB_DBL_ATTR_OBJVAL, dblptr
  fail if ret != 0
  dblptr.read_double
end

#optimizeObject

Optimize the model



69
70
71
72
73
74
75
# File 'lib/guruby/model.rb', line 69

def optimize
  # Ensure pending variables and constraints are added
  update

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

#set_sense(sense) ⇒ Object

Set the sense of the model



63
64
65
66
# File 'lib/guruby/model.rb', line 63

def set_sense(sense)
  ret = Gurobi.GRBsetintattr @ptr, GRB_INT_ATTR_MODELSENSE, sense
  fail if ret != 0
end

#statusObject

Get the status of the model



78
79
80
81
82
83
# File 'lib/guruby/model.rb', line 78

def status
  intptr = FFI::MemoryPointer.new :pointer
  ret = Gurobi.GRBgetintattr @ptr, GRB_INT_ATTR_STATUS, intptr
  fail if ret != 0
  intptr.read_int
end

#updateObject

Update the model



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

def update
  if @pending_variables.length == 1
    add_variable @pending_variables.first
  elsif @pending_variables.length > 0
    add_variables @pending_variables
  end
  @pending_variables = []

  if @pending_constraints.length == 1
    add_constraint @pending_constraints.first
  elsif @pending_constraints.length > 0
    add_constraints @pending_constraints
  end
  @pending_constraints = []

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

#write(filename) ⇒ Object

Write the model to a file



58
59
60
# File 'lib/guruby/model.rb', line 58

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