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

> Hi guys,
>
> i'm back with a new problem with my grid:
>
> I have three celltypes:
>
> 2 = housing
> 3 = green
>
> Each type has a table where preference scores are stored regarding the
> adjacency of other celltypes...these are stored in the hash ATable...
>
> Now i have to code an algorithm that iterates over all cells and which
> try to
> explain what the algorithm needs to do. Me and a teacher have coded
> two
> things for this:
>
> ----------------------------
> def scoreA(cell)
>   # here the algorithm has to be placed
> end
> ----------------------------
>
> The adjacency score of one cell needs to be calculated within this
> method...
>
> ----------------------------
> for cell1 in grid
>   for cell2 in grid
> #    # compare cell1 with cell2
>     next if cell1 == cell2
>     next if cell1.locked || cell2.locked
>
>     # calculate score for two cells together
>     a_before = scoreA(cell1) + scoreA(cell2)
>
>     # swap cells
>     cell1.cell_type, cell2.cell_type = cell2.cell_type,
> cell1.cell_type
>
>     # calculate score for two cells together
>     a_after = scoreA(cell1) + scoreA(cell2)
>
>     # if last score is lower or even as the score before swap, then
> swap
> cells back to first situation
>     if a_after <= a_before
>     cell1.cell_type, cell2.cell_type = cell2.cell_type,
> cell1.cell_type
>     end
>   end
> 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

```