Class: Cbc::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-cbc/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: "ILP Problem") ⇒ Model

Returns a new instance of Model.



21
22
23
24
25
26
# File 'lib/ruby-cbc/model.rb', line 21

def initialize(name: "ILP Problem")
  @vars = []
  @constraints = []
  @objective = nil
  @name = name
end

Instance Attribute Details

#constraintsObject

Returns the value of attribute constraints.



19
20
21
# File 'lib/ruby-cbc/model.rb', line 19

def constraints
  @constraints
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/ruby-cbc/model.rb', line 19

def name
  @name
end

#objectiveObject

Returns the value of attribute objective.



19
20
21
# File 'lib/ruby-cbc/model.rb', line 19

def objective
  @objective
end

#varsObject

Returns the value of attribute vars.



19
20
21
# File 'lib/ruby-cbc/model.rb', line 19

def vars
  @vars
end

Instance Method Details

#bin_var(name: nil) ⇒ Object



36
37
38
# File 'lib/ruby-cbc/model.rb', line 36

def bin_var(name: nil)
  var(Ilp::Var::BINARY_KIND, 0..1, name)
end

#bin_var_array(length, names: nil) ⇒ Object



40
41
42
# File 'lib/ruby-cbc/model.rb', line 40

def bin_var_array(length, names: nil)
  array_var(length, Ilp::Var::BINARY_KIND, 0..1, names)
end

#cont_var(range = nil, name: nil) ⇒ Object



44
45
46
# File 'lib/ruby-cbc/model.rb', line 44

def cont_var(range = nil, name: nil)
  var(Ilp::Var::CONTINUOUS_KIND, range, name)
end

#cont_var_array(length, range = nil, names: nil) ⇒ Object



48
49
50
# File 'lib/ruby-cbc/model.rb', line 48

def cont_var_array(length, range = nil, names: nil)
  array_var(length, Ilp::Var::CONTINUOUS_KIND, range, names)
end

#enforce(*constraints) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby-cbc/model.rb', line 52

def enforce(*constraints)
  constraints.each do |constraint|
    if constraint.instance_of? Ilp::Constraint
      self.constraints << constraint
    elsif constraint.instance_of? Array
      self.constraints.concat constraint
    elsif constraint.instance_of? Hash
      to_add = constraint.map do |name, cons|
        cons.tap { |c| c.function_name = name.to_s }
      end
      self.constraints.concat to_add
    else
      puts "Not a constraint: #{constraint}"
    end
  end
end

#int_var(range = nil, name: nil) ⇒ Object



28
29
30
# File 'lib/ruby-cbc/model.rb', line 28

def int_var(range = nil, name: nil)
  var(Ilp::Var::INTEGER_KIND, range, name)
end

#int_var_array(length, range = nil, names: nil) ⇒ Object



32
33
34
# File 'lib/ruby-cbc/model.rb', line 32

def int_var_array(length, range = nil, names: nil)
  array_var(length, Ilp::Var::INTEGER_KIND, range, names)
end

#maximize(expression) ⇒ Object



74
75
76
77
# File 'lib/ruby-cbc/model.rb', line 74

def maximize(expression)
  @objective = Ilp::Objective.new(expression, Ilp::Objective::MAXIMIZE) if expression
  self
end

#minimize(expression) ⇒ Object



69
70
71
72
# File 'lib/ruby-cbc/model.rb', line 69

def minimize(expression)
  @objective = Ilp::Objective.new(expression, Ilp::Objective::MINIMIZE) if expression
  self
end

#to_problemObject



79
80
81
# File 'lib/ruby-cbc/model.rb', line 79

def to_problem
  Cbc::Problem.from_model(self)
end

#to_sObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ruby-cbc/model.rb', line 83

def to_s
  str = if objective
          "#{objective}\n"
        else
          "Maximize\n  0 #{vars.first}\n"
        end

  str << "\nSubject To\n"
  constraints.each do |cons|
    str << "  #{cons}\n"
  end
  bounded_vars = vars.select { |v| v.kind != Ilp::Var::BINARY_KIND }
  unless bounded_vars.empty?
    str << "\nBounds\n"
    bounded_vars.each do |v|
      str << "  #{lb_to_s(v.lower_bound)} <= #{v} <= #{ub_to_s(v.upper_bound)}\n"
    end
  end

  int_vars = vars.select { |v| v.kind == Ilp::Var::INTEGER_KIND }
  unless int_vars.empty?
    str << "\nGenerals\n"
    int_vars.each { |v| str << "  #{v}\n" }
  end

  bin_vars = vars.select { |v| v.kind == Ilp::Var::BINARY_KIND }
  unless bin_vars.empty?
    str << "\nBinaries\n"
    bin_vars.each { |v| str << "  #{v}\n" }
  end
  str << "\nEnd\n"

  str
end