```On Sep 11, 2007, at 7:57 AM, Joop Van den tillaart wrote:

> Morton Goldberg wrote:
>> On Sep 11, 2007, at 4:57 AM, Joop Van den tillaart wrote:
>>
>>> Each type has a table where preference scores are stored
>>> regarding the
>>> def scoreA(cell)
>>> #    # compare cell1 with cell2
>>>     # calculate score for two cells together
>>> end
>>> ----------------------------
>>
>> I see one problem with the above code -- it walks the entire
>> adjacency matrix. It would be better to redesign it to walk only on
>> either the upper or the lower triangular sub-matrix. Then you only
>> need to deal with each distinct cell pairing once.
>>
>>> Attachments:
>>
>> Sorry. There is no way that I'm going to open a Word document of
>> unknown content on my computer.
>>
>> Regards, Morton
>
> I noticed that too...it doesnt look to the surrounding cells on a
> distance of 1 cell...it walks through all the cells...how do i change
> the code so it will only take into account surrounding cells on
> distance
> 1?

<code>
require 'ostruct'

class Grid
include Enumerable

def initialize(width, height)
@grid = Array.new(height) do |row|
Array.new(width) do |col|
OpenStruct.new(:x => col, :y => row, :grid => self)
end
end
end

def width
@grid.first.size
end

def height
@grid.size
end

def each
@grid.each do |row|
row.each do |cell|
yield cell
end
end
end

def [](x, y)
@grid[y][x] if @grid[y]
end

def print_field_values(field_name = :cell_type)
each_with_index do |cell, i|
print "%02d " % cell.send(field_name)
puts if i % width == width - 1
end
end

def each_with_neighbor
each { |cell| cell.visited = false }
each do |cell|
next if cell.locked
neighbors(cell).each { |other| yield cell, other }
cell.visited = true
end
end

def neighbors(cell)
result = []
x, y = cell.x, cell.y
(-1..1).each do |dx|
(-1..1).each do |dy|
begin
next if dx == 0 && dy == 0
_cell = cell.grid[x+dx, y+dy]
result << _cell unless _cell.locked || _cell.visited
rescue IndexError
next
end
end
end
result
end

end

# I'm setting up a small grid here to reduce the amount of output.
grid = Grid.new(5, 4)
grid.each { |cell| cell.cell_type = 0 }
grid.height.times { |y| grid[4, y].cell_type = 1 }
grid.height.times { |y| grid[2, y].cell_type = 1 }
grid.width.times { |x| grid[x, 3].cell_type = 1 }
grid.each do |cell|
if cell.cell_type == 1
cell.locked = true
end
end

zero_cells = grid.select { |cell| cell.cell_type == 0 }
n = zero_cells.size
k = (0.6 * n).round
(0...k).each { |i| zero_cells[i].cell_type = 2 }
(k...n).each { |i| zero_cells[i].cell_type = 3 }
grid.print_field_values

puts

# Example showing how to use each_with_neighbor. Shows where your code
# would go. Also prints outs what cell pairs get processed.
grid.each_with_neighbor do |cell, other|
# # calculate score for two cells together
# a_before = scoreA(cell) + scoreA(other)
# # swap cells
# cell.cell_type, other.cell_type = other.cell_type, cell.cell_type
# # calculate score for two cells together
# a_after = scoreA(cell) + scoreA(other)
# # if last score is lower or even as the score before swap,
# # then swap  cells back to first situation
# if a_after <= a_before
#    cell.cell_type, other.cell_type = other.cell_type,
cell.cell_type
# end
p [[cell.x, cell.y, cell.cell_type],
[other.x, other.y, other.cell_type]]
end
</code>

> And euhm...why do you worry about the word document? It's just a brief
> explanation of what the algorithm should do...I'm sure you have your
> reasons for not opening it (but I don't see them :D)...anyways thanks
> for your help so far...

There are viruses that attach themselves to Word documents and which