Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/tsp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'alns'
require 'alns/state'
require 'alns/accept/hill_climbing'
require 'alns/accept/great_deluge'
require 'alns/select/roulette_wheel'
require 'alns/stop/max_iterations'
require 'alns/stop/max_runtime'
Expand Down Expand Up @@ -81,6 +82,7 @@ def self.solve

select = ALNS::Select::RouletteWheel.new([3, 2, 1, 0.5], 0.8, num_destroy, num_repair)
accept = ALNS::Accept::HillClimbing.new
# accept = ALNS::Accept::GreatDeluge.new(1.05, 0.01)
stop = ALNS::Stop::MaxIterations.new(max_iterations)
# stop = ALNS::Stop::MaxRuntime.new(2)

Expand Down
29 changes: 29 additions & 0 deletions lib/alns/accept/great_deluge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'alns/accept/base'

module ALNS
module Accept
class GreatDeluge < Base
def initialize(alpha, beta)
if alpha <= 1 || !(0 < beta && beta < 1)
raise ArgumentError, 'alpha must be > 1 and beta must be in (0, 1).'
end

@alpha = alpha
@beta = beta
@threshold = nil
end

def accept?(_rnd, best, _current, candidate)
@threshold = @alpha * best.objective if @threshold.nil?

diff = @threshold - candidate.objective

@threshold -= @beta * diff

diff.positive?
end
end
end
end
28 changes: 28 additions & 0 deletions lib/alns/accept/threshold.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module ALNS
module Accept
module Threshold
LINEAR = 1
EXPONENTIAL = 2

def self.to_s(value)
case value
when LINEAR then 'LINEAR'
when EXPONENTIAL then 'EXPONENTIAL'
else
raise ArgumentError, "Invalid method: #{value}"
end
end

def self.update(current, step, method)
case method
when LINEAR then current - step
when EXPONENTIAL then current * step
else
raise ArgumentError, "Invalid method: #{value}"
end
end
end
end
end
18 changes: 18 additions & 0 deletions test/accept/great_deluge_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require_relative '../test_helper'
require 'minitest/autorun'
require 'alns/accept/great_deluge'
require_relative '../fake_state'

class GreatDelugeTest < Minitest::Test
def test_accept?
accept = ALNS::Accept::GreatDeluge.new(2, 0.01)

assert !accept.accept?(nil, Zero, Zero, One)

assert accept.accept?(nil, Zero, Zero, Zero)

assert accept.accept?(nil, Zero, Zero, Zero)
end
end
2 changes: 1 addition & 1 deletion test/accept/hill_climbing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative '../test_helper'
require 'minitest/autorun'
require 'alns/accept/hill_climbing'
require_relative '../models'
require_relative '../fake_state'

class HillClimbingTest < Minitest::Test
def test_accept?
Expand Down
6 changes: 5 additions & 1 deletion test/models.rb → test/fake_state.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# frozen_string_literal: true

require 'minitest/autorun'
require 'alns/state'

class FakeState < ALNS::State
Expand All @@ -11,3 +10,8 @@ def initialize(val)
@objective = val
end
end

Sentinel = FakeState.new(0)
Zero = FakeState.new(0)
One = FakeState.new(1)
Two = FakeState.new(2)
2 changes: 1 addition & 1 deletion test/select/roulette_wheel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require_relative '../test_helper'
require 'minitest/autorun'
require 'alns/select/roulette_wheel'
require_relative '../models'
require_relative '../fake_state'

class RouletteWheelTest < Minitest::Test
def setup
Expand Down
2 changes: 1 addition & 1 deletion test/solver_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
require 'alns/accept/hill_climbing'
require 'alns/select/roulette_wheel'
require 'alns/stop/max_iterations'
require_relative 'models'
require_relative 'fake_state'

class SolverTest < Minitest::Test
def setup
Expand Down
2 changes: 1 addition & 1 deletion test/stop/no_improvement_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../models'
require_relative '../fake_state'
require 'minitest/autorun'
require 'alns/stop/no_improvement'

Expand Down