-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_pill_sort.py
98 lines (67 loc) · 3.22 KB
/
multi_pill_sort.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
import pyhop
# Operators
# Helper function to be used in just about every operator
# The light is on if the power is on, the light is plugged in, the switch is on, and the bulb is good
def add_pill(state, pill, day, time):
state.days[str(pill)][int(day)][int(time)] += 1
return state
def remove_pill(state, pill, day, time):
if state.days[str(pill)][int(day)][int(time)] > 0:
state.days[str(pill)][int(day)][int(time)] -= 1
return state
else:
return False
pyhop.declare_operators(add_pill, remove_pill)
# Methods
def one_pill_at_a_time(state, goal):
pill = state.pills.pop(0)
if len(state.pills) == 0:
return [('sort_pill', pill, 0, goal)]
else:
return [('sort_pill', pill, 0, goal), ('sort_meds', goal)]
def one_day_at_a_time(state, pill, day, goal):
if day == len(state.days[pill]) - 1:
return [('fix_day', pill, day, goal)]
else:
return [('fix_day', pill, day, goal), ('sort_pill', pill, day + 1, goal)]
def wrong_time(state, pill, day, goal):
num_times = len(state.days[pill][day])
# Returns number of times in the day ex.(lunch, dinner) = 2
for time in range(0, num_times):
# Iterates through times of day
if state.days[pill][day][time] < goal.days[pill][day][time]:
# If state time's # of pills less than goal time's # of pills:
for otherTime in range(0, num_times):
# Iterate through times of day again
if time != otherTime and state.days[pill][day][otherTime] > goal.days[pill][day][
otherTime]:
# If time is different and other time has more pills during state:
return [('remove_pill', pill, day, otherTime), ('add_pill', pill, day, time),
('fix_day', pill, day, goal)]
# Remove pill from other time, put at correct time
return False
def missing_pill(state, pill, day, goal):
num_times = len(state.days[pill][day])
for time in range(0, num_times):
if state.days[pill][day][time] < goal.days[pill][day][time]:
for otherTime in range(0, num_times):
if time != otherTime and state.days[pill][day][otherTime] <= goal.days[pill][day][otherTime]:
return [('add_pill', pill, day, time), ('fix_day', pill, day, goal)]
return False
def extra_pill(state, pill, day, goal):
num_times = len(state.days[pill][day])
for time in range(0, num_times):
if state.days[pill][day][time] > goal.days[pill][day][time]:
for otherTime in range(0, num_times):
if time != otherTime and state.days[pill][day][otherTime] >= goal.days[pill][day][otherTime]:
return [('remove_pill', pill, day, time), ('fix_day', pill, day, goal)]
return False
def no_problem(state, pill, day, goal):
num_times = len(state.days[pill][day])
for time in range(0, num_times):
if state.days[pill][day][time] != goal.days[pill][day][time]:
return False
return []
pyhop.declare_methods('sort_meds', one_pill_at_a_time)
pyhop.declare_methods('sort_pill', one_day_at_a_time)
pyhop.declare_methods('fix_day', wrong_time, missing_pill, extra_pill, no_problem)