Sha256: 80a7eb471f8ed352a76ade5a47564e405e1d4c5ff7cfdfefe9485f0d52318dbe

Contents?: true

Size: 1.61 KB

Versions: 13

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

##
# An LP Variable. Used as arguments in LP Expressions.
# The subtypes BV and IV represent Binary and Integer variables.
# These are constructed dynamically by using the special Capitalised variable declaration syntax.
##
class LV
  attr_reader :name, :args
  attr_writer :value
  attr_accessor :lt, :lte, :gt, :gte

  include Rulp::Bounds
  include Rulp::Initializers

  def to_proc
    ->(index){ send(self.meth, index) }
  end

  def meth
    "#{self.name}_#{self.suffix}"
  end

  def suffix
    "f"
  end

  def self.definition(name, *args)
    identifier = "#{name}#{args.join("_")}"
    defined = LV::names_table["#{identifier}"]
    case defined
    when self then defined
    when nil then self.new(name, args)
    else raise StandardError.new("ERROR:\n#{name} was already defined as a variable of type #{defined.class}."+
                              "You are trying to redefine it as a variable of type #{self}")
    end
  end

  def * (numeric)
    self.nocoerce
    Expressions.new([Fragment.new(self, numeric)])
  end

  def -@
    return self * -1
  end

  def -(other)
    self + (-other)
  end

  def + (expressions)
    Expressions[self] + Expressions[expressions]
  end

  def value
    return nil unless @value
    case self
    when BV then @value.round(2) == 1
    when IV then @value
    else @value
    end
  end

  def value?
    value ? value : false
  end

  def inspect
    "#{name}#{args.join("-")}(#{suffix})[#{value.nil? ? 'undefined' : value }]"
  end

  alias_method :selected?, :value?
end

class BV < LV;
  def suffix
    "b"
  end
end
class IV < LV;
  def suffix
    "i"
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rulp-0.0.44 lib/rulp/lv.rb
rulp-0.0.43 lib/rulp/lv.rb
rulp-0.0.42 lib/rulp/lv.rb
rulp-0.0.41 lib/rulp/lv.rb
rulp-0.0.40 lib/rulp/lv.rb
rulp-0.0.39 lib/rulp/lv.rb
rulp-0.0.38 lib/rulp/lv.rb
rulp-0.0.37 lib/rulp/lv.rb
rulp-0.0.36 lib/rulp/lv.rb
rulp-0.0.35 lib/rulp/lv.rb
rulp-0.0.34 lib/rulp/lv.rb
rulp-0.0.33 lib/rulp/lv.rb
rulp-0.0.32 lib/rulp/lv.rb