-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbusy_buoys_problem_ex1.py
173 lines (146 loc) · 5.27 KB
/
busy_buoys_problem_ex1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from abc import ABC, abstractmethod
import time
import random
# Base Toilet Class
class Toilet(ABC):
def __init__(self):
self.state = "Idle"
self.water_level = 100 # Water percentage (for flush)
self.toilet_paper = 100 # TP resource, 100% full
def use_toilet(self, user):
if self.state == "Idle":
print(f"{user.__class__.__name__} is using the toilet.")
self.state = "In Use"
user.use()
self.flush()
else:
print("Toilet is currently occupied.")
@abstractmethod
def flush(self):
pass
def check_status(self):
print(f"Toilet Status: State={self.state}, Water={self.water_level}%, TP={self.toilet_paper}%")
# Degree of Freedom 1: Basic flushing (P)
class BasicToilet(Toilet):
def flush(self):
print("Flushing toilet...")
time.sleep(1)
self.state = "Flushing"
self.water_level -= 10 # Basic water usage
self.toilet_paper -= 5 # Basic TP usage
time.sleep(1)
self.state = "Idle"
print("Toilet flushed and reset.")
# Degree of Freedom 2: TP constraint (P)
class ToiletWithTP(Toilet):
def flush(self):
print("Flushing toilet with TP constraint...")
if self.toilet_paper <= 0:
print("No toilet paper left!")
return False
time.sleep(1)
self.water_level -= 10
self.toilet_paper -= 5
time.sleep(1)
self.state = "Idle"
print("Toilet flushed and TP used.")
# Degree of Freedom 3: Friend Preferences (P-like problem)
class ToiletWithFriends(ToiletWithTP):
def __init__(self):
super().__init__()
self.friend_pairs = {}
def assign_friend(self, sailor1, sailor2):
self.friend_pairs[sailor1] = sailor2
self.friend_pairs[sailor2] = sailor1
def use_toilet(self, user):
if user.name in self.friend_pairs:
friend = self.friend_pairs[user.name]
print(f"{user.name} prefers to use the toilet with {friend} nearby.")
super().use_toilet(user)
# Degree of Freedom 4: Weight Class (NP-like problem)
class ToiletWithWeight(ToiletWithFriends):
def __init__(self):
super().__init__()
self.weight_capacity = 150 # Weight limit in kg
def use_toilet(self, user):
if user.weight > self.weight_capacity:
print(f"{user.name} exceeds the toilet's weight capacity!")
return False
super().use_toilet(user)
# Degree of Freedom 5: Optimization of resources (NP-hard-like)
class OptimizedToilet(ToiletWithWeight):
def flush(self):
print("Flushing with optimization...")
if self.water_level < 20 or self.toilet_paper < 10:
print("Low resources! Optimizing flush...")
time.sleep(2) # Simulating optimization
self.water_level -= 5 # Minimized usage
self.toilet_paper -= 2 # Minimized TP usage
else:
super().flush()
self.state = "Idle"
print("Optimized flush completed.")
# Sailor (User) Class
class Sailor(ABC):
def __init__(self, name, weight):
self.name = name
self.weight = weight
@abstractmethod
def use(self):
pass
# Simple Sailor who just uses the toilet
class SimpleSailor(Sailor):
def use(self):
print(f"{self.name} is using the toilet (P level) for basic urination.")
time.sleep(1)
print(f"{self.name} is done.")
# Sailor who uses toilet paper (P + TP)
class TPUser(Sailor):
def use(self):
print(f"{self.name} is using the toilet with TP (P + TP).")
time.sleep(2)
print(f"{self.name} is done.")
# Sailor with friend preferences (P + TP + Friend)
class FriendlySailor(Sailor):
def use(self):
print(f"{self.name} is using the toilet, checking for friends.")
time.sleep(1)
print(f"{self.name} is done.")
# Sailor who is filtered by weight (P + TP + Friend + Weight)
class HeavySailor(Sailor):
def use(self):
print(f"{self.name} is using the toilet, considering weight class.")
time.sleep(2)
print(f"{self.name} is done.")
# Example Simulation
def simulate_toilet_usage():
# Toilet objects with varying complexity
basic_toilet = BasicToilet()
tp_toilet = ToiletWithTP()
friend_toilet = ToiletWithFriends()
weight_toilet = ToiletWithWeight()
optimized_toilet = OptimizedToilet()
# Sailor objects
sailor1 = SimpleSailor("Sailor1", 70)
sailor2 = TPUser("Sailor2", 75)
sailor3 = FriendlySailor("Sailor3", 80)
sailor4 = HeavySailor("Sailor4", 160) # Exceeds weight class
# Assign friends
friend_toilet.assign_friend(sailor3.name, sailor1.name)
# Basic toilet usage (P)
basic_toilet.use_toilet(sailor1)
basic_toilet.check_status()
# TP Toilet usage (P + TP)
tp_toilet.use_toilet(sailor2)
tp_toilet.check_status()
# Friend toilet usage (P + TP + Friend)
friend_toilet.use_toilet(sailor3)
friend_toilet.check_status()
# Weight toilet usage (NP-like)
weight_toilet.use_toilet(sailor4) # Should fail due to weight
weight_toilet.check_status()
# Optimized toilet usage (NP-hard-like)
optimized_toilet.use_toilet(sailor2)
optimized_toilet.check_status()
# Simulate the scenario
simulate_toilet_usage()