Skip to content

Commit

Permalink
Allow the caller of script to continue execute when prob_sum <=1
Browse files Browse the repository at this point in the history
  • Loading branch information
connglli committed Mar 15, 2021
1 parent 03ccae2 commit eeefbd6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
40 changes: 29 additions & 11 deletions droidbot/input_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,14 +405,18 @@ def match(self, device_state):


class DroidBotAction():

"""
an action is what DroidBot would do on device at specific states
"""
@abstractmethod
def get_next_operation(self):
pass


class RoundRobinDroidBotAction(DroidBotAction):

"""
this action execute its operations round-robin
"""
def __init__(self, action, script, key_tag):
self.operations = []
for operation_id in action:
Expand All @@ -431,7 +435,9 @@ def get_next_operation(self):


class ProbalisticDroidBotAction(DroidBotAction):

"""
this action execute its operations probalistically according to the probability
"""
def __init__(self, action, script, key_tag):
prob_sum = 0
self.prob_operations = []
Expand All @@ -446,8 +452,15 @@ def __init__(self, action, script, key_tag):
}
self.prob_operations.append(operation)
prob_sum = tmp_prob_sum
if abs(prob_sum - 1) > 1e-5:
msg = '%s: sum of probability must be 1, %f is given' % (key_tag, prob_sum)
if 1 - prob_sum > 1e-5: # less than 1
# append a None operation to indicate the caller that
# the operation should not be executed
self.prob_operations.append({
'operation': None,
'prob_range': [prob_sum, 1]
})
elif prob_sum - 1> 1e-5: # greater than 1
msg = '%s: sum of probability must <=1, %f is given' % (key_tag, prob_sum)
raise ScriptSyntaxError(msg)

def get_next_operation(self):
Expand Down Expand Up @@ -563,13 +576,18 @@ def __init__(self, state_dict):
}
]
})
swipe, skip, total = 0, 0, 10000
swipe, skip, none, total = 0, 0, 0, 10000
for i in range(total):
operation = script.get_operation_based_on_state(welcome_state)
print('%s: %s' % (operation.id, operation.events))
if operation.id == 'swipe_operation':
swipe += 1
elif operation.id == 'skip_operation':
skip += 1
if not operation:
none += 1
print('None')
else:
print('%s: %s' % (operation.id, operation.events))
if operation.id == 'swipe_operation':
swipe += 1
elif operation.id == 'skip_operation':
skip += 1
print('swipe_operation: %f/%f (%f)' % (swipe, total, swipe / total))
print('skip_operation: %f/%f (%f)' % (skip, total, skip / total))
print('none_operation: %f/%f (%f)' % (none, total, none / total))
2 changes: 1 addition & 1 deletion script_samples/probalistic_script.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"welcome_state": [
{
"op_id": "swipe_operation",
"prob": 0.8
"prob": 0.6
},
{
"op_id": "skip_operation",
Expand Down

0 comments on commit eeefbd6

Please sign in to comment.