Filling individual cells in a grid

  • Thread starter Joop Van den tillaart
  • Start date
M

Morton Goldberg

<code>
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
</code>

As is not unusual for me, I've had second thoughts. Here is a better
version.

<code>
def each_with_neighbor
each do |cell|
next if cell.locked
neighbors(cell).each { |other| yield cell, other }
end
end

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

Regards, Morton
 
M

Morton Goldberg

As is not unusual for me, I've had second thoughts. Here is a
better version.

<code>
def each_with_neighbor
each do |cell|
next if cell.locked
neighbors(cell).each { |other| yield cell, other }
end
end

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

Third thoughts. I solved the wrong problem. On further consideration,
I think this is want you are looking for:

<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_remaining
each do |cell|
next if cell.locked
x, y = cell.x, cell.y
(x+1...width).each do |col|
_cell = cell.grid[col, y]
yield cell, _cell unless _cell.locked
end
(y+1...height).each do |row|
(0...width).each do |col|
_cell = cell.grid[col, row]
yield cell, _cell unless _cell.locked
end
end
end
end

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.distance_road = 0
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.cell_type = 2 }
(k...n).each { |i| zero_cells.cell_type = 3 }
grid.print_field_values

puts

# Example showing how to use each_with_remaining. Shows where your code
# would go. Also prints outs what cell pairs get processed.
grid.each_with_remaining 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>

Regards, Morton
 
J

Joop Van den tillaart

Thanks Morton,

you're really a big help for me...and I know you are right about the
stuff I need to do by myself but for me, not having any real experience
in coding/programming and being also new to ruby it's hard to see how
problems should be solved by code...

It's hard to explain but I don't always 'see' what code should be used
to make it do what I want...It's really slow in progress sometimes when
you don't have that experience :(

For the word file, I understand what you're saying, the only reason for
me to make it in a word file was because I had some figures of my grid
and the steps that should be followed throughout the algorithm...not
sure if my story will be clear when I try to type it in here in a reply
but I may have a go later today.

FOR NOW: thank you very much again, I'm going to plough through your new
solution and see if I can follow/understand...

Thanks!
 
J

Joop Van den tillaart

So the code can find al the available cell pairs...that can be useful
for me in a next step I have to take but I still have a question...

On the second thought you had you did something with delta and
neigbours..that is what I need for now but I can't seem to get it to
work...

How can I find for example:

All cells that are in a distance of 1 cell from [1, 1] (or directly
'touch' this cell) ?

The result then has to be something like:

[0,0], [1,0], [2,0], [0,1], [2,1], [0,2], [1,2], [2,2]

I now your code should be able to do this but I'm not sure how...(jeezz
i feel so noobish)

If anyone else can point this out please do so...

Thanks
 
M

Morton Goldberg

Thanks Morton,

you're really a big help for me...and I know you are right about the
stuff I need to do by myself but for me, not having any real
experience
in coding/programming and being also new to ruby it's hard to see how
problems should be solved by code...

The problem you are trying to code is rather advanced for someone who
is new to both programming and Ruby. I don't want to discourage you
because my first "real" program was on similar level of difficulty.
It took me six months to get it to work! This was a long time ago
when programmers were mainly self-taught and there was no internet to
ask questions on. It didn't help that I was writing the program in
assembly language.

I admire both your ambition and tenacity.
It's hard to explain but I don't always 'see' what code should be used
to make it do what I want...It's really slow in progress sometimes
when
you don't have that experience :(

Your right about this, but it's important to do as much as possible
for yourself. You learn more from correcting your own mistakes than
you do from other people. At least that's my belief.

Good judgment comes from bad experience, and a lot of that comes
from bad judgment.

Experience is something you often don't get until just after you
need it.
For the word file, I understand what you're saying, the only reason
for
me to make it in a word file was because I had some figures of my grid
and the steps that should be followed throughout the algorithm...not
sure if my story will be clear when I try to type it in here in a
reply
but I may have a go later today.

I think it will be a good exercise. If you can write a good plain
text description, you will be more than half-way toward knowing how
to code it.
FOR NOW: thank you very much again, I'm going to plough through
your new
solution and see if I can follow/understand...

Thanks!

You're welcome.

Regards, Morton
 
M

Morton Goldberg

So the code can find al the available cell pairs...that can be useful
for me in a next step I have to take but I still have a question...

On the second thought you had you did something with delta and
neigbours..that is what I need for now but I can't seem to get it to
work...

How can I find for example:

All cells that are in a distance of 1 cell from [1, 1] (or directly
'touch' this cell) ?

The result then has to be something like:

[0,0], [1,0], [2,0], [0,1], [2,1], [0,2], [1,2], [2,2]

I now your code should be able to do this but I'm not sure how...
(jeezz
i feel so noobish)

I am not sure I understand what you are asking. If you want to look
in a neighborhood around a cell that is two cells deep, shouldn't the
deltas be given by

<code>
tmp =[]
(-2..2).each { |dx| (-2..2).each { |dy| tmp << [dx, dy]} }
DELTAS = tmp - [[0, 0]]
DELTAS # => [[-2, -2], [-2, -1], [-2, 0], [-2, 1], [-2, 2], [-1, -2],
[-1, -1], [-1, 0], [-1, 1], [-1, 2], [0, -2], [0, -1], [0, 1], [0,
2], [1, -2], [1, -1], [1, 0], [1, 1], [1, 2], [2, -2], [2, -1], [2,
0], [2, 1], [2, 2]]
</code>

I mean, don't want to look left, right, over, and under?

Regards, Morton
 
J

Joop Van den tillaart

Morton said:
I am not sure I understand what you are asking. If you want to look
in a neighborhood around a cell that is two cells deep, shouldn't the
deltas be given by

Well i'll try to explain very very short what i need...

See my representation of a grid (now don't laugh ;) )

All the 1's stand for cell with celltype road...

x x x 1 o o o 1

x 2 x 1 o o o 1

x x x 1 o o o 1

1 1 1 1 1 1 1 1

o o o 1 o o o 1

o o o 1 o o o 1

o o o 1 o o o 1

Let's concentrate on the number 2 in the top left 'segment' of the grid
and it's surrounding x's. Let the 2 stand for a cell with celltype
housing...the x's are also given a celltype of 2 ór 3, but i haven't
displayed them here...in the code this is done by the 60 and 40 percent
thingy...

For the celltypes 2 and 3 I have preference scores like this:

Table for celltype 2: housing
---------------------------------------
regarding type: preference score:
celltype 1 12
celltype 2 12
celltype 3 8

Table for celltype 3: green environment
---------------------------------------
regarding type: preference score:
celltype 1 3
celltype 2 8
celltype 3 12

Now i need something that reads the celltypes of the x's, or so to say
of the cells which touch the '2 cell'. Then when it has read all the
celltypes it needs to add all the preference scores of those celltypes
(gathered out of the table of the central cell, in this case Table for
celltype 2: housing.

Example: there are 8 x'es or 8 cells surrounding that 2 cell. Let five
of them be of celltype 2 and the remaining three of them are celltype 3.
Then I, or better the program ;), need(s) to calculate a score of:

5 x (score of celltype 2 in table 2) = 5 x 12 = 60
3 x (score of celltype 3 in table 2) = 5 x 8 = 40
---------------------------------------------------
Total adjacency score of this cell = 100

So I need to really only have the cells which are in a distance range of
1 of some cell.

The above is an explanation of the step I need to find out, below
follows a small explanation of how the total system should work. So ONLY
read the below if you're interested, its just a further explanation of
what my end goal is.
------------------------------------------------------------------------------


The above should be processed as follows.

For the first and second cell ([0,0] and [1,0] OR [0,0] and [0,1], my
teacher and I dont think it should matter in the end, because all cells
are going to be switched, troughout the algorithm) the above scores
should be calculated.

surrounding cells for cell 1 and cell 2:
-----------------------------------------
2 x x x

x x 3 x

x x

STEP 1:
It needs to calculate a total adjacency score for cell 1 (3 x's), from
table for celltype 2, and a total adjacency score of cell 2 (5 x's),
from table for celltype 3. Then the total of these two subtotals is
calculated (this first random filled situation represents just a random
filling of space with housing and green).

STEP 2: Now the celltypes of cell 1 and cell 2 are switched and the new
subtotals are calculated (this switch represents a new situation):

3 x x x

x x 2 x

x x

If the total of the two subtotals in this switched situation is higher
than the total of step 1, then the cells stay switched, if it's lower
then we have to switch back.
So to say we look if the suitability of the switched situation is higher
as the old situation, if so let the cells keep the higher suitability,
if not switch back to the old situation with higher suitability.

After this decision (wether the cells are switched or not, that doesnt
matter) cell 1 and cell 3 are looked at and the two steps are taken
again. Again look if switching has a higher suitability, if so keep the
switch between cell 1 and 3, otherwise switch back.

Then cell 1 and 4
Then cell 1 and 5
etc. until the last cell is looked at.

-------------------------------------------------------------------------------
** When going through the cells all cells with celltype = 1 have to be
skipped (hence the locked status) because these stand for roads which
are predefined in the planning area and should not be switched. They are
in fact regarded when they act as surrounding cells because we want for
example to get the housing cells located near the road cells (houses in
Holland are usually located at a street ;-) )
-------------------------------------------------------------------------------

After that, we start with cell 2 and cell 3, cell 2 and cell 4, cell 2
and cell 5 etc. After that cell 3 and cell 4, cell 3 and cell 4, cell 3
and cell 5 etc.

The main idea is that this algorithm should calculate the ultimate
suitability of a plan area which has to be developed with housing and
green at the hand of preference scores. As you see in the text, it is a
real pain if you would have to do this by hand so that's why we try to
develop some system...Furthermore if I get this to work, then we can
expand the different celltypes, tables, preference scores etc to a more
sophisticated system.

Anyways I hope this all makes a bit sense...I had it in a word file
explained earlier but now with the help of this text maybe you guys get
a better view at the problem...

Sorry for the size of my reply, but when I start typing I have the
tendacy to get a bit abundant :D:D...

Greets, Joop
 
M

Morton Goldberg

Well i'll try to explain very very short what i need...

Sorry for the size of my reply, but when I start typing I have the
tendacy to get a bit abundant :D:D...

Your reply is really quite concise and very clear. Now I think I
fully understand what you are trying to accomplish. And as you will
see, you were already very close to a solution.

With the information you supplied above, I took a shot at completing
your code. There was really very little I had to add. Here is what I
coded:

<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)
begin
@grid[y][x]
rescue
raise IndexError
end
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_remaining
each do |cell|
next if cell.locked
x, y = cell.x, cell.y
(x+1...width).each do |col|
_cell = cell.grid[col, y]
yield cell, _cell unless _cell.locked
end
(y+1...height).each do |row|
(0...width).each do |col|
_cell = cell.grid[col, row]
yield cell, _cell unless _cell.locked
end
end
end
end

end

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

HOUSING_TABLE = {1=>12, 2=>12, 3=>8}
GREEN_TABLE = {1=>3, 2=>8, 3=>12}
def score(type, neighbors)
score = 0
case type
when 2
neighbors.each { |cell| score += HOUSING_TABLE[cell.cell_type] }
when 3
neighbors.each { |cell| score += GREEN_TABLE[cell.cell_type] }
end
score
end

grid = Grid.new(20, 15)
grid.each { |cell| cell.cell_type = 0 }
grid.height.times { |y| grid[19, y].cell_type = 1 }
grid.height.times { |y| grid[9, y].cell_type = 1 }
grid.width.times { |x| grid[x, 9].cell_type = 1 }
grid.each do |cell|
if cell.cell_type == 1
cell.distance_road = 0
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.cell_type = 2 }
(k...n).each { |i| zero_cells.cell_type = 3 }
grid.print_field_values

puts

n = 3
n.times do
grid.each_with_remaining do |cell, other|
cn, on = neighbors(cell), neighbors(other)
before_score = score(cell.cell_type, cn) +
score(other.cell_type, on)
after_score = score(other.cell_type, cn) +
score(cell.cell_type, on)
if after_score <= before_score
cell.cell_type, other.cell_type = other.cell_type,
cell.cell_type
end
end
grid.print_field_values
puts
end
</code>

I ran the script for three iterations. I'm not sure how to interpret
the results. I'm not even sure it the loop is converging. I leave it
to you to work that out. It may be working as you expect it to, it
may be that your scoring function needs adjustment, or may be that
there's a bug in my code. One thought that occurs to me: would it be
better to start out with a random distribution of housing and green
space?

<results>
# 1st iteration
03 03 03 03 03 03 03 03 03 01 03 03 03 03 03 03 03 03 03 01
03 03 03 03 03 03 03 03 03 01 03 03 03 03 03 03 03 03 03 01
03 03 03 03 03 03 03 03 03 01 03 03 03 03 03 03 03 03 03 01
03 03 03 03 03 03 03 03 03 01 03 03 03 03 03 03 03 03 03 01
03 02 03 02 03 02 03 02 03 01 03 02 03 02 03 02 03 02 02 01
03 02 03 02 03 02 03 02 02 01 02 02 03 02 03 02 03 02 02 01
02 02 03 02 03 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 03 03 02 02 01 02 03 02 03 02 03 02 03 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 03 02 03 02 03 02 03 02 01 02 03 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01

# 2nd iteration
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 03 03 02 01 02 03 02 03 02 03 03 03 02 01
02 03 03 02 03 02 03 02 03 01 02 03 02 03 02 03 02 03 02 01
02 03 03 02 03 03 03 03 03 01 03 03 02 03 02 03 02 03 03 01
03 02 03 02 03 02 03 02 03 01 02 03 02 03 02 03 02 03 02 01
03 02 03 02 03 02 03 02 03 01 03 03 02 03 02 03 02 03 02 01
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
03 03 02 03 02 03 02 02 03 01 03 03 02 02 03 02 03 02 03 01
02 03 03 03 03 03 03 03 03 01 02 02 03 03 03 02 03 02 03 01
03 02 02 02 02 02 02 02 02 01 03 03 03 02 02 02 03 02 03 01
03 02 03 03 03 03 03 03 03 01 02 02 02 03 03 03 03 02 03 01
03 02 03 02 03 02 03 02 03 01 03 03 03 03 02 02 03 02 03 01

# 3rd iteration
03 03 03 03 03 03 03 03 03 01 03 03 03 03 03 03 03 03 03 01
03 03 03 03 03 03 03 03 03 01 03 03 02 03 03 02 03 02 03 01
03 02 03 02 03 02 02 02 02 01 02 03 02 03 02 03 03 02 03 01
03 02 03 02 03 02 03 03 03 01 02 03 02 02 02 02 02 02 02 01
03 02 03 02 03 02 03 02 02 01 03 02 02 03 03 03 02 03 02 01
03 02 03 02 03 02 02 02 03 01 03 02 03 02 02 02 02 03 02 01
02 02 03 02 03 02 03 03 02 01 02 02 02 02 03 03 02 03 02 01
03 02 03 02 03 02 02 03 02 01 03 02 03 02 02 02 02 03 02 01
02 02 02 02 02 02 02 02 02 01 02 02 02 02 02 03 02 02 02 01
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
02 02 02 02 02 02 02 02 02 01 02 02 03 02 02 02 02 02 02 01
02 03 02 03 02 03 02 03 02 01 02 02 02 02 03 03 03 03 02 01
02 03 02 02 02 02 02 02 02 01 02 03 03 02 02 02 02 02 02 01
02 03 02 03 02 03 03 03 02 01 02 02 02 02 03 03 03 03 02 01
02 02 02 02 02 02 02 02 02 01 02 02 03 02 02 02 02 02 02 01
</results>

Regards, Morton
 
M

Morton Goldberg

<code>
n = 3
n.times do
grid.each_with_remaining do |cell, other|
cn, on = neighbors(cell), neighbors(other)
before_score = score(cell.cell_type, cn) +
score(other.cell_type, on)
after_score = score(other.cell_type, cn) +
score(cell.cell_type, on)
if after_score <= before_score
cell.cell_type, other.cell_type = other.cell_type,
cell.cell_type
end
end
grid.print_field_values
puts
end
</code>

I realized I was doing something silly in the above code -- I was
scoring two cells even if they were of the same type. It runs
noticeably faster if I don't do that.

<code>
n = 3
n.times do
grid.each_with_remaining do |cell, other|
next if cell.cell_type == other.cell_type # <-- added this line
cn, on = neighbors(cell), neighbors(other)
before_score = score(cell.cell_type, cn) + score
(other.cell_type, on)
after_score = score(other.cell_type, cn) + score
(cell.cell_type, on)
if after_score <= before_score
cell.cell_type, other.cell_type = other.cell_type,
cell.cell_type
end
end
grid.print_field_values
puts
end
</code>

Regards, Morton
 
M

Morton Goldberg

I ran the script for three iterations. I'm not sure how to
interpret the results. I'm not even sure it the loop is converging.
I leave it to you to work that out. It may be working as you expect
it to, it may be that your scoring function needs adjustment, or
may be that there's a bug in my code. One thought that occurs to
me: would it be better to start out with a random distribution of
housing and green space?

Well, I made some minor changes so I test for convergence, and I
found that it does converge and fairly quickly at that. I reach a
fixed point in on the 7th iteration. Here is the modified code and
the results. Hope it's what you expected.

<code>
limit = 10
limit.times do |n|
swaps = 0
grid.each_with_remaining do |cell, other|
next if cell.cell_type == other.cell_type
cn, on = neighbors(cell), neighbors(other)
before_score = score(cell.cell_type, cn) +
score(other.cell_type, on)
after_score = score(other.cell_type, cn) +
score(cell.cell_type, on)
if after_score < before_score # <-- note change from <= to <
cell.cell_type, other.cell_type =
other.cell_type, cell.cell_type
swaps += 1
end
end
puts "Iteration no. #{n+1}"
grid.print_field_values
puts
break if swaps == 0
end
</code>

<results>
Iteration no. 7
02 02 02 02 02 02 02 03 02 01 02 03 02 03 02 03 02 03 02 01
02 03 03 03 03 03 02 03 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 03 02 01 03 03 03 03 03 03 03 03 03 01
03 03 03 03 03 03 02 03 02 01 02 02 02 02 02 02 02 02 02 01
02 02 02 02 02 02 02 03 02 01 03 02 03 02 03 02 03 03 03 01
03 03 03 03 03 03 02 03 02 01 03 02 03 02 03 02 02 02 02 01
02 02 02 02 02 02 02 03 02 01 02 02 03 02 03 02 03 02 03 01
03 03 03 03 03 03 02 03 02 01 03 02 03 02 03 02 03 02 02 01
02 02 02 02 02 02 02 03 02 01 02 02 03 02 03 02 02 02 02 01
01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01
02 02 02 02 02 02 03 02 02 01 02 03 02 03 02 03 02 03 02 01
03 02 03 02 03 02 03 02 03 01 02 03 02 03 02 03 02 03 02 01
02 02 03 02 03 02 03 02 02 01 02 03 02 03 02 03 02 03 02 01
03 02 03 02 03 02 03 02 03 01 02 03 02 03 02 03 02 03 02 01
02 02 03 02 03 02 03 02 02 01 02 03 02 03 02 03 02 03 02 01
<results>

Regards, Morton
 
J

Joop Van den tillaart

Morton said:
On Sep 12, 2007, at 5:36 PM, Morton Goldberg wrote:
Well, I made some minor changes so I test for convergence, and I
found that it does converge and fairly quickly at that. I reach a
fixed point in on the 7th iteration. Here is the modified code and
the results. Hope it's what you expected.

First of all, many thanks for your help to this point...

It's a bit hard to explain but when I have to invent the code all by
myself I sometimes cannot come up with the working code. In my head I
know what needs to be done but to actually get the code for it...is just
hard. I'm not too familiar with all the right ways to write code because
of my fair lack of programming experience...as I said it's hard to
explain but as you said yourself it took a while for you to get where
you are now so maybe you have a bit of an idea of what I'm talking
about.

Then for helping me out all this time, I just hope you have, maybe only
a little bit, a feeling of reward when you are able to help someone out,
by making use of knowledge you have over the knowledge of others (in
this case me ^_^ )...

I have been pretty active in the 3d modelling scene and on that field
there are also many forums where I've been walking around (and still do
actually) and I have made some contribution in these forums to help
others out, and in my beginning stages being helped. For me helping out
others always gave a good feeling, and in many cases I learned something
for myself too.

The internet is great for offering and receiving these kinds of help in
times when one is stuck.

Long story short: many thanks untill this point and you won't be
forgotten ;)

Greets, Joop
 
M

Morton Goldberg

First of all, many thanks for your help to this point...

Once again, you're welcome.
It's a bit hard to explain but when I have to invent the code all by
myself I sometimes cannot come up with the working code. In my head I
know what needs to be done but to actually get the code for it...is
just
hard. I'm not too familiar with all the right ways to write code
because
of my fair lack of programming experience...as I said it's hard to
explain but as you said yourself it took a while for you to get where
you are now so maybe you have a bit of an idea of what I'm talking
about.

Learning a programming language has much in common with learning a
spoken language. You must have extensive practice with it before you
learn to think in it. In your case, I infer programming is a side
issue -- your main professional interests lie elsewhere. That may
mean you will never get the time to master Ruby to the point where
think about a problem and some code for expressing it immediately
pops into your conscious mind. Don't let that stop you, you can still
write useful programs although more slowly than someone who thinks in
the language. But you must keep practicing Ruby or you will loose it,
just as one loses a spoken language if one stops using it.
Then for helping me out all this time, I just hope you have, maybe
only
a little bit, a feeling of reward when you are able to help someone
out,
by making use of knowledge you have over the knowledge of others (in
this case me ^_^ )...

I have been pretty active in the 3d modelling scene and on that field
there are also many forums where I've been walking around (and
still do
actually) and I have made some contribution in these forums to help
others out, and in my beginning stages being helped. For me helping
out
others always gave a good feeling, and in many cases I learned
something
for myself too.

The person who helps another with a technical problem always learns.
That is one of the main rewards of helping.
The internet is great for offering and receiving these kinds of
help in
times when one is stuck.

Long story short: many thanks untill this point and you won't be
forgotten ;)

It sounds to me that you think my most recent code actually solves
your layout problem. But I don't. By now you know I always have
second thoughts. My second thought on the latest code is that it
gives an artificially low score to cells on the edge of the grid,
which probably biases the result in some undesirable way. I couldn't
deal with that without knowing how such low scores should be
adjusted, and that's a not a coding issue -- it's a problem
specification issue. So you will have deal with it. You may decide
that my concern about edge cells is unwarranted. But if you decide to
adjust the edge scores, I will be happy to help you integrate your
adjusted scoring system into the Ruby code should you feel you need
such help.

Regards, Morton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top