Exemplo n.º 1
0
    def test_query_rule(self):
        # Evidence Rules
        fire_er = EvidenceRule("Fire", "Fire_Sensor", ["true", "false"],
                               ["true", "false"], fire_cprob, fire_sensor_cprob)
        human_er = EvidenceRule("Human", "Human_Sensor", ["true", "false"],
                                ["true", "false"], human_cprob, human_sensor_cprob)

        # Query Rules
        qr = QueryRule('Human', 'Human in Danger', ["true", "false"], human_cprob, "Fire", ["true", "false"], 10, joint_functions.mean)

        # Create engine
        engine = InferenceEngine([fire_er, human_er], [qr])


        ##### EVIDENCE
        e1 = Observation(10, 30, "Fire_Sensor", ["true", "false"], "true")
        e2 = Observation(10, 30, "Human_Sensor", ["true", "false"], "true")

        # bn, bn_evidences = engine.infer_bn([e1, e2])
        #
        # # Validate result
        # self.assertEqual(len(bn.V), 6, "number of vertices in query rule")
        # self.assertEqual(len(bn.E), 4, "number of edges for query rule")

        e3 = Observation(12, 30, "Fire_Sensor", ["true", "false"], "true")
        bn, bn_evidences = engine.infer_bn([e1, e2, e3])

        # Validate result
        self.assertEqual(len(bn.V), 8, "number of vertices in query rule")
        self.assertEqual(len(bn.E), 5, "number of edges for query rule")

        print "V: ", bn.V
        print "E: ", bn.E
        print "Evidences: ", bn_evidences
        print "Vertex loc", engine.vertex_locations
Exemplo n.º 2
0
    def rrrtest_evidence_rule(self):
        # Evidence Rules
        erules = []
        er = EvidenceRule("Fire", "Fire_Sensor", ["true", "false"],
                          ["true", "false"], fire_cprob, fire_sensor_cprob)
        erules.append(er)

        # Query Rules

        # Create engine
        engine = InferenceEngine(erules, [])

        # Evidence
        e1 = Observation(10, 30, "Fire_Sensor", ["true", "false"], "true")

        # run inference engine
        bn, bn_evidences = engine.infer_bn([e1])

        # Validate result
        self.assertEqual(len(bn.V), 2)

        # Another evidence
        e2 = Observation(50, 30, "Fire_Sensor", ["true", "false"], "true")
        # run inference engine
        bn, bn_evidences = engine.infer_bn([e1, e2])

        # Validate result
        self.assertEqual(len(bn.V), 4)

        # Another evidence in the same location of e1
        e3 = Observation(10, 30, "Fire_Sensor", ["true", "false"], "true")
        # run inference engine
        bn, bn_evidences = engine.infer_bn([e1, e2, e3])
        # Validate result
        self.assertEqual(len(bn.V), 5, "Error for evidence in the same location")
        self.assertEqual(len(bn.E), 3, "Error for evidence in the same location")
import pickle
from gi.repository import Gtk

from infengine.InferenceEngine import InferenceEngine
from infengine.gis import geojson
from lib_sallybn.disc_bayes_net.BoxDiscreteBN import BoxDiscreteBN, Mode
import rules2 as rules


with open('evidences.pkl', 'rb') as evidence_input:
    evidences = pickle.load(evidence_input)

## Engine
engine = InferenceEngine(rules.get_rules())

# create bayesian network
bn, bn_evidences = engine.infer_bn(evidences)

# Output Layer
geojson.write(bn, engine.vertex_locations, bn_evidences)

##### Show the resultant Bayesian Network
# for i in range(1, 16, 5):
#     bn, bn_evidences = engine.infer_bn(evidences[:i])
#     window = Gtk.Window()
#     window.set_size_request(800, 600)
#
#     box = BoxDiscreteBN(window, disc_bn=bn)
#     box.evidences = bn_evidences
#     box.organize_graph(random=False)
#     box.set_mode(Mode.run)
               joint_functions.mean)

cr = CommunityRule('Human', 'Community', ["true", "false"], COMMUNITY_RADIO,
                   joint_functions.mean)

cdr = QueryRule('Community', 'Community in Danger', ["true", "false"], human_danger_cprob, "Fire", ["true", "false"],
                10,
                joint_functions.mean)
# Grid rule
human_gridr = GridRule("Human", "Human_Sensor", ["true", "false"],
                       ["true", "false"], human_cprob, human_sensor_cprob, Grid(0, 0, 30, 60, 3, 7))


# Create engine
#engine = InferenceEngine([fire_er, human_er, qr, cr, cdr])
engine = InferenceEngine([human_gridr])


##### EVIDENCE
ef1 = Observation(10, 30, "Fire_Sensor", ["true", "false"], "true")
ef2 = Observation(19, 30, "Fire_Sensor", ["true", "false"], "false")

eh1 = Observation(10.0, 20.0, "Human_Sensor", ["true", "false"], "true")
eh2 = Observation(5, 19, "Human_Sensor", ["true", "false"], "false")
eh3 = Observation(15, 18, "Human_Sensor", ["true", "false"], "true")
eh4 = Observation(16, 17, "Human_Sensor", ["true", "false"], "true")
eh5 = Observation(-124.4577, 108.0135, "Human_Sensor", ["true", "false"], "true")

# bn, bn_evidences = engine.infer_bn([eh2, eh3, eh4, eh1, eh5, ])
bn, bn_evidences = engine.infer_bn([eh1, eh2, eh3, eh4])