class Cbc::Model
Attributes
constraints[RW]
name[RW]
objective[RW]
vars[RW]
Public Class Methods
new(name: "ILP Problem")
click to toggle source
# File lib/ruby-cbc/model.rb, line 21 def initialize(name: "ILP Problem") @vars = [] @constraints = [] @objective = nil @name = name end
Public Instance Methods
bin_var(name: nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/ruby-cbc/model.rb, line 69 def minimize(expression) @objective = Ilp::Objective.new(expression, Ilp::Objective::MINIMIZE) if expression self end
to_problem()
click to toggle source
# File lib/ruby-cbc/model.rb, line 79 def to_problem Cbc::Problem.from_model(self) end
to_s()
click to toggle source
# 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
Private Instance Methods
array_var(length, kind, range, names)
click to toggle source
# File lib/ruby-cbc/model.rb, line 120 def array_var(length, kind, range, names) ar = Array.new(length) { var(kind, range, nil) } ar.zip(names).each { |var, name| var.name = name } unless names.nil? ar end
lb_to_s(lb)
click to toggle source
# File lib/ruby-cbc/model.rb, line 136 def lb_to_s(lb) return "-inf" if lb.nil? || lb == -Cbc::INF return "+inf" if lb == Cbc::INF lb.to_s end
ub_to_s(ub)
click to toggle source
# File lib/ruby-cbc/model.rb, line 142 def ub_to_s(ub) return "+inf" if ub.nil? || ub == Cbc::INF return "-inf" if ub == -Cbc::INF ub.to_s end
var(kind, range, name)
click to toggle source
# File lib/ruby-cbc/model.rb, line 126 def var(kind, range, name) v = if range.nil? Ilp::Var.new(kind: kind, name: name) else Ilp::Var.new(kind: kind, name: name, lower_bound: range.min, upper_bound: range.max) end @vars << v v end