Class: Longjing::Search::Base

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/longjing/search/base.rb

Direct Known Subclasses

FF, Greedy

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #logger, #logger=

Constructor Details

#initializeBase

Returns a new instance of Base.



11
12
13
14
15
# File 'lib/longjing/search/base.rb', line 11

def initialize
  @t = Time.now
  @statistics = Statistics.new
  reset_best_heuristic
end

Instance Attribute Details

#statisticsObject (readonly)

Returns the value of attribute statistics.



9
10
11
# File 'lib/longjing/search/base.rb', line 9

def statistics
  @statistics
end

Instance Method Details

#log_progress(state) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/longjing/search/base.rb', line 21

def log_progress(state)
  return if @best <= state.cost
  @best = state.cost
  log {
    msg = [
      "#{statistics.generated} generated",
      "#{statistics.evaluated} evaluated",
      "#{statistics.expanded} expanded",
      "h=#{@best}",
      "#{state.path.size} steps",
      "t=#{Time.now - @t}"
    ].join(', ')
  }
end

#log_solution(steps) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/longjing/search/base.rb', line 36

def log_solution(steps)
  log { "Solution found!" }
  log { "Actual search time: #{Time.now - @t}" }
  log { "Plan length: #{steps.size} step(s)." }
  log { "Expanded #{statistics.expanded} state(s)." }
  log { "Evaluated #{statistics.evaluated} state(s)." }
  log { "Generated #{statistics.generated} state(s)." }
  log { "Solution:" }
  steps.each_with_index do |step, i|
    log { "#{i}. #{step}" }
  end
end

#no_solutionObject



53
54
55
56
# File 'lib/longjing/search/base.rb', line 53

def no_solution
  log { "No solution found!" }
  nil
end

#reset_best_heuristicObject



17
18
19
# File 'lib/longjing/search/base.rb', line 17

def reset_best_heuristic
  @best = Float::INFINITY
end

#solution(state) ⇒ Object



49
50
51
# File 'lib/longjing/search/base.rb', line 49

def solution(state)
  state.path.map(&:signature).tap(&method(:log_solution))
end