class RAMS::Formatters::LP

LP formatter

Public Class Methods

bounds(var) click to toggle source
# File lib/rams/formatters/lp.rb, line 47
def self.bounds(var)
  return bounds_binary(var) if var.type == :binary
  "#{var.low.nil? ? '-inf' : var.low} <= #{var.name} <= #{var.high.nil? ? '+inf' : var.high}"
end
bounds_binary(var) click to toggle source
# File lib/rams/formatters/lp.rb, line 52
def self.bounds_binary(var)
  low_s = var.low.nil? ? 0.0 : [0.0, var.low].max
  high_s = var.high.nil? ? 1.0 : [1.0, var.high].min
  "#{low_s} <= #{var.name} <= #{high_s}"
end
constant(const) click to toggle source
# File lib/rams/formatters/lp.rb, line 43
def self.constant(const)
  "#{const >= 0 ? '+' : '-'} #{const.abs}"
end
constraint(cons) click to toggle source
# File lib/rams/formatters/lp.rb, line 29
def self.constraint(cons)
  sense_s = cons.sense == :== ? '=' : cons.sense.to_s
  "#{cons.name}: #{terms(cons.lhs).join(' ').sub(/^\+\s*/, '')} #{sense_s} " +
    constant(cons.rhs).sub(/^\+\s*/, '')
end
expression(expr) click to toggle source

rubocop:enable AbcSize

# File lib/rams/formatters/lp.rb, line 23
def self.expression(expr)
  expr_terms = terms(expr.coefficients)
  expr_terms << constant(expr.constant) unless expr.constant.zero?
  expr_terms.join(' ').sub(/^\+\s*/, '')
end
format(model) click to toggle source

rubocop:disable AbcSize

# File lib/rams/formatters/lp.rb, line 6
      def self.format(model)
        <<-LP
#{model.sense}
  obj: #{expression model.objective}
st
  #{model.constraints.values.map { |c| constraint c }.join "\n  "}
bounds
  #{model.variables.values.map { |v| bounds v }.join "\n  "}
general
  #{model.variables.values.select { |v| v.type == :integer }.map(&:name).join "\n  "}
binary
  #{model.variables.values.select { |v| v.type == :binary }.map(&:name).join "\n  "}
end
        LP
      end
terms(coef) click to toggle source
# File lib/rams/formatters/lp.rb, line 35
def self.terms(coef)
  coef.map { |v, c| variable(v, c) }
end
variable(var, coeff) click to toggle source
# File lib/rams/formatters/lp.rb, line 39
def self.variable(var, coeff)
  "#{constant(coeff)} #{var.name}"
end