Skip to content

Commit

Permalink
Merge pull request #3 from platanus/fix-kernel-method-missing-slowing…
Browse files Browse the repository at this point in the history
…-apps

Fix kernel method missing slowing apps
  • Loading branch information
difernandez authored Dec 11, 2023
2 parents 7bd76bc + e7826eb commit 978c51d
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 95 deletions.
25 changes: 12 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
GEM
remote: https://rubygems.org/
specs:
byebug (4.0.5)
columnize (= 0.9.0)
coderay (1.1.0)
columnize (0.9.0)
method_source (0.8.2)
byebug (11.1.3)
coderay (1.1.3)
method_source (1.0.0)
minitest (5.5.1)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-byebug (3.1.0)
byebug (~> 4.0)
pry (~> 0.10)
pry (0.13.1)
coderay (~> 1.1)
method_source (~> 1.0)
pry-byebug (3.9.0)
byebug (~> 11.0)
pry (~> 0.13.0)
rake (10.4.2)
slop (3.6.0)

PLATFORMS
ruby
Expand All @@ -25,3 +21,6 @@ DEPENDENCIES
pry
pry-byebug
rake

BUNDLED WITH
2.3.10
1 change: 0 additions & 1 deletion lib/extensions/extensions.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require_relative './kernel_extensions'
require_relative './object_extensions'
require_relative './file_extensions'
require_relative './os_extensions'
17 changes: 0 additions & 17 deletions lib/extensions/kernel_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@
# ...
##
module Kernel
alias_method :old_method_missing, :method_missing
def method_missing(value, *args)
method_name = "#{value}" rescue ""
if ("A".."Z").cover?(method_name[0])
if method_name.end_with?(BV.suffix)
method_name = method_name.chomp(BV.suffix).chomp("_")
return BV.definition(method_name, args)
elsif method_name.end_with?(IV.suffix)
method_name = method_name.chomp(IV.suffix).chomp("_")
return IV.definition(method_name, args)
elsif method_name.end_with?(LV.suffix)
method_name = method_name.chomp(LV.suffix).chomp("_")
return LV.definition(method_name, args)
end
end
old_method_missing(value, *args)
end

def _profile
start = Time.now
Expand Down
28 changes: 0 additions & 28 deletions lib/extensions/object_extensions.rb

This file was deleted.

9 changes: 9 additions & 0 deletions lib/rulp/lv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def meth
"#{self.name}_#{self.suffix}"
end

alias_method :old_method_missing, :method_missing
def method_missing(value, *args)
if value.to_s.end_with?(self.suffix)
return self.class.definition(value.to_s.chomp(self.suffix).chomp("_"), *args)
end

old_method_missing(value, *args)
end

def self.suffix
ENV['RULP_LV_SUFFIX'] || "f"
end
Expand Down
34 changes: 34 additions & 0 deletions lib/rulp/rulp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ module Rulp
GUROBI => Gurobi,
}

class <<self
alias_method :old_method_missing, :method_missing
def method_missing(value, *args)
method_name = "#{value}" rescue ""
if ("A".."Z").cover?(method_name[0])
if method_name.end_with?(BV.suffix)
method_name = method_name.chomp(BV.suffix).chomp("_")
return BV.definition(method_name, args)
elsif method_name.end_with?(IV.suffix)
method_name = method_name.chomp(IV.suffix).chomp("_")
return IV.definition(method_name, args)
elsif method_name.end_with?(LV.suffix)
method_name = method_name.chomp(LV.suffix).chomp("_")
return LV.definition(method_name, args)
end
end
old_method_missing(value, *args)
end

alias_method :old_const_missing, :const_missing
def const_missing(value)
method_name = "#{value}".split("::")[-1] rescue ""
if ("A".."Z").cover?(method_name[0])
if method_name.end_with?(BV.suffix)
return BV.definition(method_name.chomp(BV.suffix).chomp("_"))
elsif method_name.end_with?(IV.suffix)
return IV.definition(method_name.chomp(IV.suffix).chomp("_"))
elsif method_name.end_with?(LV.suffix)
return LV.definition(method_name.chomp(LV.suffix).chomp("_"))
end
end
old_const_missing(value)
end
end

def self.Glpk(lp, opts={})
lp.solve_with(GLPK, opts)
Expand Down
1 change: 1 addition & 0 deletions rulp.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'rulp/version'
require 'date'

Gem::Specification.new do |s|
s.name = 'rulp'
Expand Down
42 changes: 21 additions & 21 deletions test/test_basic_suite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,55 @@ class BasicSuite < Minitest::Test

def test_single_binary_var
each_solver do |solver|
assert_equal X_b.value, nil
assert_equal Rulp::X_b.value, nil

# The minimal value for a single binary variable is 0
Rulp::Min(X_b).(solver)
assert_equal X_b.value, false
Rulp::Min(Rulp::X_b).(solver)
assert_equal Rulp::X_b.value, false

# The maximal value for a single binary variable is 1
Rulp::Max(X_b).(solver)
assert_equal X_b.value, true
Rulp::Max(Rulp::X_b).(solver)
assert_equal Rulp::X_b.value, true

# If we set an upper bound this is respected by the solver
Rulp::Max(X_b)[1 * X_b <= 0].(solver)
assert_equal X_b.value, false
Rulp::Max(Rulp::X_b)[1 * Rulp::X_b <= 0].(solver)
assert_equal Rulp::X_b.value, false

# If we set a lower bound this is respected by the solver
Rulp::Min(X_b)[1 * X_b >= 1].(solver)
assert_equal X_b.value, true
Rulp::Min(Rulp::X_b)[1 * Rulp::X_b >= 1].(solver)
assert_equal Rulp::X_b.value, true
end
end

def test_single_integer_var
each_solver do |solver|
assert_equal X_i.value, nil
assert_equal Rulp::X_i.value, nil

given[ -35 <= X_i <= 35 ]
given[ -35 <= Rulp::X_i <= 35 ]

# Integer variables respect integer bounds
Rulp::Min(X_i).(solver)
assert_equal X_i.value, -35
Rulp::Min(Rulp::X_i).(solver)
assert_equal Rulp::X_i.value, -35

# Integer variables respect integer bounds
Rulp::Max(X_i).(solver)
assert_equal X_i.value, 35
Rulp::Max(Rulp::X_i).(solver)
assert_equal Rulp::X_i.value, 35
end
end

def test_single_general_var
each_solver do |solver|
assert_equal X_f.value, nil
assert_equal Rulp::X_f.value, nil

given[ -345.4321 <= X_f <= 345.4321 ]
given[ -345.4321 <= Rulp::X_f <= 345.4321 ]

# Integer variables respect integer bounds
Rulp::Min(X_f).(solver)
assert_in_delta X_f.value, -345.4321, 0.001
Rulp::Min(Rulp::X_f).(solver)
assert_in_delta Rulp::X_f.value, -345.4321, 0.001

# Integer variables respect integer bounds
Rulp::Max(X_f).(solver)
assert_in_delta X_f.value, 345.4321, 0.001
Rulp::Max(Rulp::X_f).(solver)
assert_in_delta Rulp::X_f.value, 345.4321, 0.001
end
end
end
2 changes: 1 addition & 1 deletion test/test_boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
##
class BooleanTest < Minitest::Test
def setup
@items = 30.times.map(&Shop_Item_b)
@items = 30.times.map(&Rulp::Shop_Item_b)
items_count = @items.inject(:+)
@items_costs = @items.map{|item| item * Random.rand(1.0...5.0)}.inject(:+)

Expand Down
2 changes: 1 addition & 1 deletion test/test_infeasible.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
##
class InfeasibleTest < Minitest::Test
def setup
@items = 30.times.map(&Shop_Item_b)
@items = 30.times.map(&Rulp::Shop_Item_b)
items_count = @items.inject(:+)
@items_costs = @items.map{|item| item * Random.rand(1.0...5.0)}.inject(:+)

Expand Down
10 changes: 5 additions & 5 deletions test/test_save_to_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
class SaveToFile < Minitest::Test
def setup
LV::clear
given[ X_i >= 0, Y_i >= 0, Z_i >= 0 ]
@objective = 10 * X_i + 6 * Y_i + 4 * Z_i
given[ Rulp::X_i >= 0, Rulp::Y_i >= 0, Rulp::Z_i >= 0 ]
@objective = 10 * Rulp::X_i + 6 * Rulp::Y_i + 4 * Rulp::Z_i
@problem = Rulp::Max( @objective ) [
X_i + Y_i + Z_i <= 100,
10 * X_i + 4 * Y_i + 5 * Z_i <= 600,
2 * X_i + 2 * Y_i + 6 * Z_i <= 300
Rulp::X_i + Rulp::Y_i + Rulp::Z_i <= 100,
10 * Rulp::X_i + 4 * Rulp::Y_i + 5 * Rulp::Z_i <= 600,
2 * Rulp::X_i + 2 * Rulp::Y_i + 6 * Rulp::Z_i <= 300
]
end

Expand Down
16 changes: 8 additions & 8 deletions test/test_simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@

class SimpleTest < Minitest::Test
def setup
given[ X_i >= 0, Y_i >= 0, Z_i >= 0 ]
@objective = 10 * X_i + 6 * Y_i + 4 * Z_i
given[ Rulp::X_i >= 0, Rulp::Y_i >= 0, Rulp::Z_i >= 0 ]
@objective = 10 * Rulp::X_i + 6 * Rulp::Y_i + 4 * Rulp::Z_i
@problem = Rulp::Max( @objective ) [
X_i + Y_i + Z_i <= 100,
10 * X_i + 4 * Y_i + 5 * Z_i <= 600,
2 * X_i + 2 * Y_i + 6 * Z_i <= 300
Rulp::X_i + Rulp::Y_i + Rulp::Z_i <= 100,
10 * Rulp::X_i + 4 * Rulp::Y_i + 5 * Rulp::Z_i <= 600,
2 * Rulp::X_i + 2 * Rulp::Y_i + 6 * Rulp::Z_i <= 300
]
end

def test_simple
each_solver do |solver|
setup
@problem.send(solver)
assert_equal X_i.value, 33
assert_equal Y_i.value, 67
assert_equal Z_i.value, 0
assert_equal Rulp::X_i.value, 33
assert_equal Rulp::Y_i.value, 67
assert_equal Rulp::Z_i.value, 0
assert_equal @objective.evaluate , 732
end
end
Expand Down

0 comments on commit 978c51d

Please sign in to comment.