Sha256: da33e6451f35bdf673e03e3b4aaa2614b04085954fe9baf2b1981aa2503772a5

Contents?: true

Size: 1.91 KB

Versions: 9

Compression:

Stored size: 1.91 KB

Contents

##
# 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_accessor :lt, :lte, :gt, :gte, :value
  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.method_missing(name, *args)
    return self.definition(name, args)
  end

  def self.const_missing(name)
    return self.definition(name)
  end

  def self.definition(name, args)
    identifier = "#{name}#{args.join("_")}"
    self.class.send(:define_method, identifier){|index=nil|
      if index
        self.definition(name, index)
      else
        defined = LV::names_table["#{identifier}"]
        if defined && defined.class != self
          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}")
        elsif(!defined)
          self.new(name, args)
        else
          defined
        end
      end
    }
    return self.send(identifier) || self.new(name, args)
  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
    if self.class == BV
      return @value.round(2) == 1
    elsif self.class == IV
      return @value
    else
      @value
    end
  end

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

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rulp-0.0.14 lib/rulp/lv.rb
rulp-0.0.13 lib/rulp/lv.rb
rulp-0.0.12 lib/rulp/lv.rb
rulp-0.0.11 lib/rulp/lv.rb
rulp-0.0.10 lib/rulp/lv.rb
rulp-0.0.9 lib/rulp/lv.rb
rulp-0.0.8 lib/rulp/lv.rb
rulp-0.0.7 lib/rulp/lv.rb
rulp-0.0.6 lib/rulp/lv.rb