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.



12
13
14
15
16
17
# File 'lib/ruby-cbc/model.rb', line 12

def initialize(name: "ILP Problem")
  @vars = Set.new
  @constraints = Set.new
  @objective = nil
  @name = name
end

Instance Attribute Details

#constraintsObject

Returns the value of attribute constraints.



10
11
12
# File 'lib/ruby-cbc/model.rb', line 10

def constraints
  @constraints
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/ruby-cbc/model.rb', line 10

def name
  @name
end

#objectiveObject

Returns the value of attribute objective.



10
11
12
# File 'lib/ruby-cbc/model.rb', line 10

def objective
  @objective
end

#varsObject

Returns the value of attribute vars.



10
11
12
# File 'lib/ruby-cbc/model.rb', line 10

def vars
  @vars
end

Instance Method Details

#bin_var(name: nil) ⇒ Object



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

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

#bin_var_array(length, names: nil) ⇒ Object



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

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

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



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

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

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



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

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

#enforce(*constraints) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby-cbc/model.rb', line 43

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

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



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

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

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



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

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

#maximize(expression) ⇒ Object



65
66
67
68
# File 'lib/ruby-cbc/model.rb', line 65

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

#minimize(expression) ⇒ Object



60
61
62
63
# File 'lib/ruby-cbc/model.rb', line 60

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

#to_problemObject



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

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

#to_sObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ruby-cbc/model.rb', line 74

def to_s
  str = ""
  if objective
  str << objective.to_s << "\n"
  else
    str << "Maximize\n  0 #{vars.first.to_s}\n"
  end
  str << "\nSubject To\n"
  constraints.each do |cons|
    str << "  " << cons.to_s << "\n"
  end
  bounded_vars = vars.select{ |v| v.kind != Ilp::Var::BINARY_KIND }
  if bounded_vars.any?
    str << "\nBounds\n"
    bounded_vars.each { |v| str << "  #{lb_to_s(v.lower_bound)} <= #{v} <= #{ub_to_s(v.upper_bound)}\n" }
  end

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

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

  str
end