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
26 27 28 29 30 31 |
# File 'lib/ruby-cbc/model.rb', line 26 def initialize(name: "ILP Problem") @vars = Set.new @constraints = Set.new @objective = nil @name = name end |
Instance Attribute Details
#constraints ⇒ Object
Returns the value of attribute constraints.
24 25 26 |
# File 'lib/ruby-cbc/model.rb', line 24 def constraints @constraints end |
#name ⇒ Object
Returns the value of attribute name.
24 25 26 |
# File 'lib/ruby-cbc/model.rb', line 24 def name @name end |
#objective ⇒ Object
Returns the value of attribute objective.
24 25 26 |
# File 'lib/ruby-cbc/model.rb', line 24 def objective @objective end |
#vars ⇒ Object
Returns the value of attribute vars.
24 25 26 |
# File 'lib/ruby-cbc/model.rb', line 24 def vars @vars end |
Instance Method Details
#bin_var(name: nil) ⇒ Object
41 42 43 |
# File 'lib/ruby-cbc/model.rb', line 41 def bin_var(name: nil) var(Ilp::Var::BINARY_KIND, 0..1, name) end |
#bin_var_array(length, names: nil) ⇒ Object
45 46 47 |
# File 'lib/ruby-cbc/model.rb', line 45 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
49 50 51 |
# File 'lib/ruby-cbc/model.rb', line 49 def cont_var(range = nil, name: nil) var(Ilp::Var::CONTINUOUS_KIND, range, name) end |
#cont_var_array(length, range = nil, name: nil) ⇒ Object
53 54 55 |
# File 'lib/ruby-cbc/model.rb', line 53 def cont_var_array(length, range = nil, name: nil) array_var(length, Ilp::Var::CONTINUOUS_KIND, range, names) end |
#enforce(*constraints) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/ruby-cbc/model.rb', line 57 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
33 34 35 |
# File 'lib/ruby-cbc/model.rb', line 33 def int_var(range = nil, name: nil) var(Ilp::Var::INTEGER_KIND, range, name) end |
#int_var_array(length, range = nil, names: nil) ⇒ Object
37 38 39 |
# File 'lib/ruby-cbc/model.rb', line 37 def int_var_array(length, range = nil, names: nil) array_var(length, Ilp::Var::INTEGER_KIND, range, names) end |
#maximize(expression) ⇒ Object
79 80 81 82 |
# File 'lib/ruby-cbc/model.rb', line 79 def maximize(expression) @objective = Ilp::Objective.new(expression, Ilp::Objective::MAXIMIZE) if expression self end |
#minimize(expression) ⇒ Object
74 75 76 77 |
# File 'lib/ruby-cbc/model.rb', line 74 def minimize(expression) @objective = Ilp::Objective.new(expression, Ilp::Objective::MINIMIZE) if expression self end |
#to_problem ⇒ Object
84 85 86 |
# File 'lib/ruby-cbc/model.rb', line 84 def to_problem Cbc::Problem.new(self) end |
#to_s ⇒ Object
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 118 119 |
# File 'lib/ruby-cbc/model.rb', line 88 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 |