Class: Cbc::Model
- Inherits:
-
Object
- Object
- Cbc::Model
- Defined in:
- lib/ruby-cbc/model.rb
Instance Attribute Summary collapse
-
#constraints ⇒ Object
Returns the value of attribute constraints.
-
#name ⇒ Object
Returns the value of attribute name.
-
#objective ⇒ Object
Returns the value of attribute objective.
-
#vars ⇒ Object
Returns the value of attribute vars.
Instance Method Summary collapse
- #bin_var(name: nil) ⇒ Object
- #bin_var_array(length, names: nil) ⇒ Object
- #cont_var(range = nil, name: nil) ⇒ Object
- #cont_var_array(length, range = nil, name: nil) ⇒ Object
- #enforce(*constraints) ⇒ Object
-
#initialize(name: "ILP Problem") ⇒ Model
constructor
A new instance of Model.
- #int_var(range = nil, name: nil) ⇒ Object
- #int_var_array(length, range = nil, names: nil) ⇒ Object
- #maximize(expression) ⇒ Object
- #minimize(expression) ⇒ Object
- #to_problem ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name: "ILP Problem") ⇒ Model
Returns a new instance of Model.
24 25 26 27 28 29 |
# File 'lib/ruby-cbc/model.rb', line 24 def initialize(name: "ILP Problem") @vars = [] @constraints = [] @objective = nil @name = name end |
Instance Attribute Details
#constraints ⇒ Object
Returns the value of attribute constraints.
22 23 24 |
# File 'lib/ruby-cbc/model.rb', line 22 def constraints @constraints end |
#name ⇒ Object
Returns the value of attribute name.
22 23 24 |
# File 'lib/ruby-cbc/model.rb', line 22 def name @name end |
#objective ⇒ Object
Returns the value of attribute objective.
22 23 24 |
# File 'lib/ruby-cbc/model.rb', line 22 def objective @objective end |
#vars ⇒ Object
Returns the value of attribute vars.
22 23 24 |
# File 'lib/ruby-cbc/model.rb', line 22 def vars @vars end |
Instance Method Details
#bin_var(name: nil) ⇒ Object
39 40 41 |
# File 'lib/ruby-cbc/model.rb', line 39 def bin_var(name: nil) var(Ilp::Var::BINARY_KIND, 0..1, name) end |
#bin_var_array(length, names: nil) ⇒ Object
43 44 45 |
# File 'lib/ruby-cbc/model.rb', line 43 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
47 48 49 |
# File 'lib/ruby-cbc/model.rb', line 47 def cont_var(range = nil, name: nil) var(Ilp::Var::CONTINUOUS_KIND, range, name) end |
#cont_var_array(length, range = nil, name: nil) ⇒ Object
51 52 53 |
# File 'lib/ruby-cbc/model.rb', line 51 def cont_var_array(length, range = nil, name: nil) array_var(length, Ilp::Var::CONTINUOUS_KIND, range, names) end |
#enforce(*constraints) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby-cbc/model.rb', line 55 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
31 32 33 |
# File 'lib/ruby-cbc/model.rb', line 31 def int_var(range = nil, name: nil) var(Ilp::Var::INTEGER_KIND, range, name) end |
#int_var_array(length, range = nil, names: nil) ⇒ Object
35 36 37 |
# File 'lib/ruby-cbc/model.rb', line 35 def int_var_array(length, range = nil, names: nil) array_var(length, Ilp::Var::INTEGER_KIND, range, names) end |
#maximize(expression) ⇒ Object
77 78 79 80 |
# File 'lib/ruby-cbc/model.rb', line 77 def maximize(expression) @objective = Ilp::Objective.new(expression, Ilp::Objective::MAXIMIZE) if expression self end |
#minimize(expression) ⇒ Object
72 73 74 75 |
# File 'lib/ruby-cbc/model.rb', line 72 def minimize(expression) @objective = Ilp::Objective.new(expression, Ilp::Objective::MINIMIZE) if expression self end |
#to_problem ⇒ Object
82 83 84 |
# File 'lib/ruby-cbc/model.rb', line 82 def to_problem Cbc::Problem.from_model(self) end |
#to_s ⇒ Object
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 117 |
# File 'lib/ruby-cbc/model.rb', line 86 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 |