summaryrefslogtreecommitdiff
path: root/prob/random_walk_oo.py
diff options
context:
space:
mode:
authorzhang <zch921005@126.com>2022-01-23 12:42:38 +0800
committerzhang <zch921005@126.com>2022-01-23 12:42:38 +0800
commit6f68e1818229e0d2dad760062e6b5bb137b88f5b (patch)
tree55ec8ad3f340d7a9d276b2e17641b8a9c1c52ae3 /prob/random_walk_oo.py
parent0451d59752f3b61a6f6dfdb56d1f431083be4c7d (diff)
随机游走 & ortools demo
Diffstat (limited to 'prob/random_walk_oo.py')
-rw-r--r--prob/random_walk_oo.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/prob/random_walk_oo.py b/prob/random_walk_oo.py
new file mode 100644
index 0000000..ce1baf5
--- /dev/null
+++ b/prob/random_walk_oo.py
@@ -0,0 +1,52 @@
+
+import random
+
+
+class Location(object):
+
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def move(self, dx, dy):
+ return Location(self.x + dx, self.y + dy)
+
+ def get_x(self):
+ return self.x
+
+ def get_y(self):
+ return self.y
+
+ def euclidean_distance_from(self, other):
+ x_dist = self.x - other.get_x()
+ y_dist = self.y - other.get_y()
+ return (x_dist ** 2 + y_dist **2)**0.5
+
+ def manhattan_distance_from(self, other):
+ x_dist = self.x - other.get_x()
+ y_dist = self.y - other.get_y()
+ return abs(x_dist) + abs(y_dist)
+
+ def __str__(self):
+ return f'<{self.x}, {self.y}>'
+
+
+class Drunk(object):
+ def __init__(self, name=None):
+ self.name = name
+
+ def __str__(self):
+ if self.name is not None:
+ return self.name
+ return 'Anonymous'
+
+
+def usual_take_step():
+ return random.choice([[-1, 0], [1, 0], [0, -1], [0, 1]])
+
+
+def north_take_step():
+ step_choices = [(0.0, 1.1), (0.0, -0.9), (1.0, 0.0), (-1.0, 0.0)]
+ return random.choice(step_choices)
+
+