Class: Longjing::FF::RelaxedGraphPlan

Inherits:
Object
  • Object
show all
Defined in:
lib/longjing/ff/relaxed_graph_plan.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cg) ⇒ RelaxedGraphPlan

Returns a new instance of RelaxedGraphPlan.



14
15
16
17
18
19
# File 'lib/longjing/ff/relaxed_graph_plan.rb', line 14

def initialize(cg)
  @actions = cg.actions
  @add2actions = cg.add2actions
  @pre2actions = cg.pre2actions
  @literals = cg.literals
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



12
13
14
# File 'lib/longjing/ff/relaxed_graph_plan.rb', line 12

def actions
  @actions
end

#literalsObject (readonly)

Returns the value of attribute literals.



12
13
14
# File 'lib/longjing/ff/relaxed_graph_plan.rb', line 12

def literals
  @literals
end

Instance Method Details

#extract(goal, state, added_goals = []) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/longjing/ff/relaxed_graph_plan.rb', line 77

def extract(goal, state, added_goals=[])
  layers(goal, state)
  goal_layers = goal.map(&:ff_layer)
  return nil if goal_layers.any?(&:nil?)
  # m = first layer contains all goals
  m = goal_layers.max

  marks = Hash.new{|h,k| h[k]={}}
  layer2facts = Array.new(m + 1) { [] }

  goal.each do |lit|
    layer2facts[lit.ff_layer] << lit
  end

  plan = []
  (1..m).to_a.reverse.each do |i|
    subplan = []
    layer2facts[i].each do |g|
      next if marks[g].include?(i)
      next unless actions = @add2actions[g]
      action = actions.select do |a|
        a.layer == i - 1
      end.min_by(&:difficulty)

      action.pre.each do |lit|
        if lit.ff_layer != 0 && !marks[lit].include?(i - 1)
          layer2facts[lit.ff_layer] << lit
        end
      end
      action.add.each do |lit|
        marks[lit][i] = true
        marks[lit][i-1] = true
      end
      unless added_goals.empty?
        action.del.each do |lit|
          if added_goals.include?(lit)
            return nil
          end
        end
      end

      subplan << action
    end
    unless subplan.empty?
      plan << subplan
    end
  end
  if plan.empty?
    [plan]
  else
    helpful_actions = {}
    layer2facts[1].each do |lit|
      @add2actions[lit].each do |action|
        if action.layer == 0
          helpful_actions[action.action] = true
        end
      end
    end
    [plan, helpful_actions.empty? ? nil : helpful_actions.keys]
  end
end

#layers(goal, state) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/longjing/ff/relaxed_graph_plan.rb', line 21

def layers(goal, state)
  step = 0
  scheduled_facts = state.raw.to_a
  scheduled_actions = []
  @literals.each do |lit|
    lit.ff_layer = nil
    lit.ff_goal = false
  end
  goal.each do |lit|
    lit.ff_goal = true
  end
  @actions.each do |action|
    action.counter = 0
    action.layer = Float::INFINITY
    if action.pre.empty?
      action.difficulty = 0
      scheduled_actions << action
    else
      action.difficulty = Float::INFINITY
    end
  end
  goal_count = goal.size
  loop do
    scheduled_facts.each do |lit|
      next unless lit.ff_layer.nil?
      lit.ff_layer = step
      if lit.ff_goal
        goal_count -= 1
      end
      if actions = @pre2actions[lit]
        actions.each do |action|
          next if action.counter == action.count_target
          action.counter += 1
          if action.counter == action.count_target
            action.difficulty = step
            scheduled_actions << action
          end
        end
      end
    end
    break if goal_count == 0
    scheduled_facts = []
    scheduled_actions.each do |action|
      action.layer = step
      action.add.each do |lit|
        if lit.ff_layer.nil?
          scheduled_facts << lit
        end
      end
    end
    scheduled_actions = []
    break if scheduled_facts.empty?
    step += 1
  end
end