Class: Cgl::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/cgl/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(steps, max_cost, grid_height, grid_width = grid_height) ⇒ Runner

Returns a new instance of Runner.



11
12
13
14
15
16
17
# File 'lib/cgl/runner.rb', line 11

def initialize( steps, max_cost, grid_height, grid_width = grid_height )
  @steps = steps
  @max_cost = max_cost
  @grid_height = grid_height
  @grid_width = grid_width
  @n_agents = grid_height * grid_width
end

Instance Attribute Details

#grid_heightObject (readonly)

Returns the value of attribute grid_height.



8
9
10
# File 'lib/cgl/runner.rb', line 8

def grid_height
  @grid_height
end

#grid_widthObject (readonly)

Returns the value of attribute grid_width.



9
10
11
# File 'lib/cgl/runner.rb', line 9

def grid_width
  @grid_width
end

#max_costObject (readonly)

Returns the value of attribute max_cost.



7
8
9
# File 'lib/cgl/runner.rb', line 7

def max_cost
  @max_cost
end

#n_agentsObject (readonly)

Returns the value of attribute n_agents.



10
11
12
# File 'lib/cgl/runner.rb', line 10

def n_agents
  @n_agents
end

#stepsObject (readonly)

Returns the value of attribute steps.



6
7
8
# File 'lib/cgl/runner.rb', line 6

def steps
  @steps
end

Instance Method Details

#runObject



19
20
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
# File 'lib/cgl/runner.rb', line 19

def run
  cost_vector = Array.new(@steps+2) # cost_vector[i][state]cost of getting from state to admissible state in
      # @steps+2-i time@steps 

  # control[j][i] has a sequence for applying control to state i
  control = Array.new(@steps+2) # need an extra vector of empty strings, so we
    #can later concatenate without worrying about being at the last entry
  for i in 0..@steps+1 do 
    cost_vector[i] = Array.new(2**@n_agents)
    control[i] = Array.new(2**@n_agents, "")
  end

  for i in 0..2**@n_agents-1 do 
    cost_vector[@steps+1][i] = assign_final_cost(i)
  end

  for j in 0..@steps do 
    puts @steps - j
    for i in 0..2**@n_agents-1 do 
      i_as_array = ("%0#{@n_agents}d" % i.to_s(2)).split(//)

       # calculate cost without control
      output = iterate i_as_array
      state = output.to_s.to_i(2)
      cost_without_control = cost_vector[@steps-j+1][state]

       # calculate cost with control and take min
      # TODO pass agent to kill as arg
      # control: kill agent 2 
      i_as_array[1] = "0"
      output = iterate i_as_array
      state_with_control = output.to_s.to_i(2)
      cost_for_control = 1
      cost_with_control = cost_vector[@steps-j+1][state_with_control] + cost_for_control

      # take min and record if control was applied 
      if (cost_without_control < cost_with_control)
        cost_vector[@steps-j][i] = cost_without_control
        control[@steps-j][i] = control[@steps-j+1][state]
      else 
        cost_vector[@steps-j][i] = cost_with_control
        control[@steps-j][i] = "#{@steps-j+1}" + control[@steps-j+1][state_with_control]
      end
    end
  end
   
  [cost_vector[0], control[0]]
end