예제 #1
0
 def __init__(self, planlib, goal=None):
     """ Assigns variables for this class, I'm assuming here that planlib is a set of Actions (from the planlib module)"""
     super(basic_norm_detector,self).__init__(planlib)
     self.reinitialise()
     self.past_observations = [] # Make sure this is not in reinitialise, since this will mess up with the #learn_norms algorithm below
     if(goal == None):
         self.goal=Goal(start_nodes(planlib).pop(), goal_nodes(planlib).pop())
     else:
         self.goal = goal
예제 #2
0
def test():
    goal = Goal('a', 'd')
    actions = set([
        Action(['a', 'b']),
        Action(['b', 'e']),
        Action(['b', 'c']),
        Action(['b', 'd']),
        Action(['a', 'f']),
        Action(['a', 'c', 'e']),
        Action(['e', 'd'])
    ])
    observation1 = ['a', 'c', 'e', 'd']
    observation2 = ['a', 'b', 'd', '!']
    nodes = {node for action in actions for node in action.path}
    successors = defaultdict(set)
    for action in actions:
        for i, node in enumerate(action.path[0:-1]):
            successors[node].add(action.path[i + 1])
    conditional_norms = [ (context, modality, node) for context in nodes for node in nodes \
                                                    for modality in 'eventually', 'never' ]
    conditional_norms += [ (context, modality, node) for context in nodes for node in nodes \
                                                     for modality in 'next', 'not next' \
                                                     if node in successors[context] ]
    unconditional_norms = [(modality, node) for node in nodes
                           for modality in 'eventually', 'never']
    poss_norms = unconditional_norms + conditional_norms

    hypotheses = dict.fromkeys(poss_norms, 0.05)
    hypotheses[None] = 1  # Set prior odds ratio for hypothesis None

    print "Goal: ", goal
    print "Actions: ", actions
    print "Norm hypotheses (with prior odds ratios): ", hypotheses

    suite = NormSuite(goal, hypotheses, actions)
    print "Plans: ", suite.plan_paths

    print "Updating odds ratios after observing ", observation1
    suite.UpdateOddsRatioVsNoNorm(observation1)

    print "The posterior odds ratios are:"
    suite.Print()

    print "Updating odds ratios after observing ", observation2
    suite.UpdateOddsRatioVsNoNorm(observation2)

    print "The posterior odds ratios are:"
    suite.Print()

    f = open('graph.dot', 'w')
    f.write('digraph {\n')
    for a in suite.actions:
        for i in range(len(a.path) - 1):
            f.write(a.path[i] + ' -> ' + a.path[i + 1] + '\n')
    f.write("}\n")
    f.close()
예제 #3
0
 def gen_scenario_large(self, prob_non_compliance=0.01, prob_viol_detection=0.99, \
                 prob_sanctioning=0.99, prob_random_punishment=0.01):
     planlib = dot_to_plan_library('large-planlib.dot')
     goal = Goal(
         list(start_nodes(planlib))[0],
         list(goal_nodes(planlib))[0])
     norms = self.nb.parse_norms('large-norms.txt')
     scenario = Scenario(norms, planlib, goal, prob_non_compliance, prob_viol_detection, \
                 prob_sanctioning, prob_random_punishment)
     return scenario
예제 #4
0
 def __init__(self,planlib, goal=None):
     super(threshold_norm_detector,self).__init__(planlib)
     self.planlib = planlib
     self.ot = 0.5 # Threshold for obligations
     self.ft = 0.5 # Threshold for prohibitions
     self.reinitialise()
     self.past_observations = []
     self.goal = goal
     if(goal == None):
         self.goal=Goal(start_nodes(planlib).pop(), goal_nodes(planlib).pop())
     else:
         self.goal = goal
예제 #5
0
 def setUp(self):
     self.goal = Goal('a', 'd')
     self.planlib = set([
         Action(['a', 'b']),
         Action(['b', 'e']),
         Action(['b', 'c']),
         Action(['b', 'd']),
         Action(['a', 'f']),
         Action(['a', 'c', 'e']),
         Action(['e', 'd'])
     ])
     self.observation1 = ['a', 'c', 'e', 'd']
     self.observation2 = ['a', 'b', 'd']
     self.observation3 = ['a', 'b', 'e', 'd']
     self.norm_detector = norm_detector(self.planlib)
예제 #6
0
 def gen_scenario_2(self, prob_non_compliance=0.01, prob_viol_detection=0.99, \
                 prob_sanctioning=0.99, prob_random_punishment=0.01):
     """Larger but acyclic graph, allows multiple goals"""
     goal = Goal("a", "y")
     planlib = set([
         Action(["a", "0"]),
         Action(["0", "y"]),
         Action(["0", "j"]),
         Action(["a", "w"]),
         Action(["a", "l"]),
         Action(["a", "e"]),
         Action(["a", "s"]),
         Action(["a", "d"]),
         Action(["a", "o"]),
         Action(["b", "q"]),
         Action(["c", "z"]),
         Action(["c", "f"]),
         Action(["c", "n"]),
         Action(["c", "g"]),
         Action(["d", "h"]),
         Action(["d", "r"]),
         Action(["d", "t"]),
         Action(["d", "z"]),
         Action(["e", "s"]),
         Action(["e", "b"]),
         Action(["f", "i"]),
         Action(["f", "2"]),
         Action(["f", "u"]),
         Action(["g", "k"]),
         Action(["h", "1"]),
         Action(["h", "v"]),
         Action(["j", "t"]),
         Action(["j", "3"]),
         Action(["k", "x"]),
         Action(["l", "s"]),
         Action(["m", "y"]),
         Action(["n", "p"]),
         Action(["r", "1"]),
         Action(["s", "m"]),
         Action(["o", "c"]),
         Action(["q", "y"])
     ])
     norms = set([('a', 'never', 'l')])
     scenario = Scenario(norms, planlib, goal, prob_non_compliance, prob_viol_detection, \
                 prob_sanctioning, prob_random_punishment)
     return scenario
예제 #7
0
    def gen_scenario_1(self, prob_non_compliance=0.01, prob_viol_detection=0.99, \
                    prob_sanctioning=0.99, prob_random_punishment=0.01):
        """Scenarios return plan libraries and sets of norms"""
        goal = Goal('a', 'd')
        planlib = set([
            Action(['a', 'b']),
            Action(['b', 'e']),
            Action(['b', 'c']),
            Action(['b', 'd']),
            Action(['a', 'f']),
            Action(['a', 'c', 'e']),
            Action(['e', 'd'])
        ])
        norms = set([('a', 'never', 'e')])
        scenario = Scenario(norms, planlib, goal, prob_non_compliance, prob_viol_detection, \
                    prob_sanctioning, prob_random_punishment)

        return scenario
 def setUp(self):
     super(BayesianNormDetectorTest, self).setUp()
     self.normdetector = bayesian_norm_detector(self.planlib,
                                                goal=Goal('a', 'd'))
예제 #9
0
                prob_compliance_and_norm_abiding_path = 0
            else:
                prob_path_via_norm_abiding_plans = len(
                    norm_abiding_plans_containing_path) / len(
                        norm_abiding_plans)
                prob_compliance_and_norm_abiding_path = (
                    1 - self.prob_non_compliance
                ) * prob_path_via_norm_abiding_plans
            return self.prob_non_compliance * prob_path_if_no_norm + prob_compliance_and_norm_abiding_path
        else:
            raise ValueError(
                "Invalid hypothesis passed to Likelihood function: %s" %
                hypothesis)


goal = Goal('a', 'd')
actions = set([
    Action(['a', 'b']),
    Action(['b', 'e']),
    Action(['b', 'c']),
    Action(['b', 'd']),
    Action(['a', 'f']),
    Action(['a', 'c', 'e']),
    Action(['e', 'd'])
])
observation = ['a', 'c', 'e', 'd']

nodes = {node for action in actions for path in action.path for node in path}
poss_norms = [
    norm for node in nodes for norm in ("forbidden", node), ("obliged", node)
]
from data_mining_norm_detector import data_mining_norm_detector
from planlib import Action, Goal

goal = Goal('a', 'd')
planlib = set([
    Action(['a', 'b']),
    Action(['b', 'e']),
    Action(['b', 'c']),
    Action(['b', 'd']),
    Action(['a', 'f']),
    Action(['a', 'c', 'e']),
    Action(['e', 'd'])
])
observation1 = ['a', 'c', '!', 'e', 'd']
observation2 = ['a', 'b', 'd', '!']
observation3 = ['a', 'b', 'e', 'd', '!']
dmnt = data_mining_norm_detector(planlib, Goal('a', 'd'))
dmnt.reinitialise()
dmnt.update_with_observations(observation1)
dmnt.update_with_observations(observation2)
dmnt.update_with_observations(observation3)
numTopNorms = 3
norms = dmnt.get_inferred_norms(numTopNorms)
print "Inferred norms: ", norms
n = len(norms)
if n != numTopNorms:
    print "Asked for", numTopNorms, "norms"
    print n, "norms returned due to ties or zero probabilities"