For reasons not clear even to myself, I’m trying to predict the growth
and
production of a city in CivIV (c.f. CivFanatics
http://www.civfanatics.com
).
Each city has an integer population and a number of tiles (actually 20
but
reduced here to 10 to speed-up testing). Each population point (P) can
work
one tile in a given turn. Tiles have three attributes excitingly named
F, G and H. I am attempting to enumerate all interesting combinations
of worked tiles for any given city size. Output is in gnuplot format.
Here’s the code (fore!).
puts “Starting #{Time.now}”
numArray = (1…10).to_a
Generate tiles
tiles = numArray.inject([]) { |acc, node| acc + [[rand(4),rand(4),
rand(4)]]
}
Generate unique combinations for given size
File::open(“output.txt”, “w”) { |file|
file << “#P F G H\n”
numArray.inject([[]]) { |acc, node|
acc.inject([]) { |acc, injectedNodes| acc + [injectedNodes,
injectedNodes + [node]] }
}.collect { |c|
c.inject([0,0,0,0]) { |acc, node| [acc[0].succ, acc[1] +
tiles[node-1][0], acc[2] + tiles[node-1][1], acc[3] + tiles[node-1][2]]
}
}.uniq.sort.inject(1) { |acc, node|
file << “\n” if node[0] == acc
file << " " << node.join(" ") << “\n”
node[0] == acc ? acc.succ : acc
}
}
puts “Finished #{Time.now}”
My problem is ensuring that only efficient combinations are output. If
we
look at a doctored example…
#P A B C
0 0 0 0
1 0 0 0
1 2 0 0 ← This is not interesting,
1 2 0 3 ← when this is possible.
1 0 1 0 ← However this still is, even though A+B+C is less
than
the line above.
Can anyone suggest a nice golfed algorithm to get rid of the useless
combinations? Remember that any of the three resources might be deemed
most
important, e.g. 1 1 0 0 is still interesting when 1 0 9 9 exists.
When this bit gets nailed down, the next step is integrating
permutations of
combinations over time and solving for maximum G and/or H, given that
acquiring enough F will increase P. It’s probably best to assume no
micro-management, i.e. when a city grows, the combination chosen for the
new
size remains in place until the next growth.
Thanks for getting this far, all comments welcome.