-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from MM1nd/prepare_faster_random_land
Prepare faster random land
- Loading branch information
Showing
4 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import unittest | ||
|
||
import numpy | ||
|
||
from worldengine.simulations.hydrology import WatermapSimulation | ||
from worldengine.model.world import World, Size, GenerationParameters | ||
|
||
class TestSimulation(unittest.TestCase): | ||
|
||
|
||
# The hydrology simulation indirectly calls the global rng. | ||
# We want different implementations of _watermap | ||
# and internally called functions (especially random_land) | ||
# to show the same rng behaviour and not contamine the state of the global rng | ||
# should anyone else happen to rely on it. | ||
# This does only test that watermap leaves the rng in the state that the | ||
# original implementation would have left it in and that a very small sample | ||
# of results is the same as the original results. | ||
# It does not test if the implementation is actually correct. | ||
# In fact it is hard to tell how "correctness" of a Monte Carlo process should | ||
# be judged. | ||
# Should it ever be decided that a "correct" implementation needs fewer or | ||
# more calls to the rng, relative to the n parameter, | ||
# of course compatibility will be broken. | ||
# Implicitly this is tested by the blessed images but it might be useful to | ||
# have a finer grained picture. | ||
|
||
def test_watermap_rng_stabilty(self): | ||
|
||
seed=12345 | ||
numpy.random.seed(seed) | ||
|
||
size = Size(16,8) | ||
|
||
ocean = numpy.fromfunction(lambda y, x: y==x, (size.height, size.width)) | ||
|
||
percipitation = numpy.ones((size.height, size.width)) | ||
|
||
elevation = numpy.fromfunction(lambda y, x: y*x, (size.height, size.width)) | ||
|
||
t = numpy.zeros(5) | ||
|
||
w = World("watermap", size, seed, GenerationParameters(0, 1.0, 0)) | ||
w.ocean = ocean | ||
w.precipitation = (percipitation, t) | ||
w.elevation = (elevation, t) | ||
|
||
d = numpy.random.randint(0,100) | ||
self.assertEqual(d, 98) | ||
|
||
data, t = WatermapSimulation._watermap(w, 200) | ||
|
||
self.assertAlmostEqual(data[4,4], 0.0) | ||
self.assertAlmostEqual(data[3,5], 4.20750776) | ||
|
||
d = numpy.random.randint(0,100) | ||
self.assertEqual(d, 59) | ||
|
||
def test_random_land_returns_only_land(self): | ||
|
||
size = Size(100,90) | ||
|
||
ocean = numpy.fromfunction(lambda y, x: y>=x, (size.height, size.width)) | ||
|
||
w = World("random_land", size, 0, GenerationParameters(0, 1.0, 0)) | ||
w.ocean = ocean | ||
|
||
num_samples = 1000 | ||
|
||
land_indices = w.random_land(num_samples) | ||
|
||
for i in range(0, num_samples*2, 2): | ||
self.assertFalse(ocean[land_indices[i+1],land_indices[i]]) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import sys | ||
import numpy #for the _equal method only | ||
import numpy | ||
|
||
# ---------------- | ||
# Global variables | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters