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
# 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 = []
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



23
24
25
26
27
28
29
30
31
32
# File 'lib/guruby/model.rb', line 23

def <<(obj)
  case obj
  when Variable
    add_variable obj
  when Constraint
    add_constraint obj
  else
    fail TypeError
  end
end

#compute_IISObject

Compute an irreducible inconsistent subsytem for the model



66
67
68
69
# File 'lib/guruby/model.rb', line 66

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

#objective_valueObject

The value of the objective function



72
73
74
75
76
77
# File 'lib/guruby/model.rb', line 72

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



52
53
54
55
# File 'lib/guruby/model.rb', line 52

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

#set_sense(sense) ⇒ Object

Set the sense of the model



46
47
48
49
# File 'lib/guruby/model.rb', line 46

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



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

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



35
36
37
38
# File 'lib/guruby/model.rb', line 35

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

#write(filename) ⇒ Object

Write the model to a file



41
42
43
# File 'lib/guruby/model.rb', line 41

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