Ejemplo n.º 1
0
class Game:
    def __init__(self, size=6, numMines=3):
        self.size = size
        self.board = BoardGenerator(size, numMines).board
        self.fieldsRevealed = 0
        self.numMines = numMines
        self.gameOver = False
        self.victory = False

        self.network = BayesianNetwork(size=size, numMines=numMines)

    def reveal_field(self, x, y):
        def reveal_tile(x, y):
            revealed = []
            queue = [(x, y)]
            while len(queue) > 0:
                i, j = queue.pop()
                if not self.mine(i, j) and (i, j) not in revealed and not self.board[i][j].revealed:
                    revealed.append((i, j))
                    if self.neighbours_with_mines(i, j) < 1:
                        queue.extend(neighbours(i, j, self.size))

            if (x, y) not in revealed:
                revealed.append((x, y))

            return revealed

        tile = self.board[x][y]
        revealed_tiles = reveal_tile(x, y)

        self.fieldsRevealed += len(revealed_tiles)

        for (i, j) in revealed_tiles:
            self.board[i][j].reveal()

        if tile.mine:
            self.gameOver = True
            self.victory = False
        else:
            fields_values = map(lambda (x, y): self.neighbours_with_mines(x,y), revealed_tiles)
            self.network.reveal_fields_without_mine(zip(revealed_tiles, fields_values))
            if self.fieldsRevealed + self.numMines >= self.size * self.size:
                self.gameOver = True
                self.victory = True

        return revealed_tiles

    def mine(self, x, y):
        return self.board[x][y].mine

    def neighbours_with_mines(self, x, y):
        return self.board[x][y].neighbours_with_mines

    def get_safe(self):
        return self.network.find_best_nodes()

    def get_probability(self, x, y):
        return self.network.get_no_mine_probability(x, y)
Ejemplo n.º 2
0
    def __init__(self, size=6, numMines=3):
        self.size = size
        self.board = BoardGenerator(size, numMines).board
        self.fieldsRevealed = 0
        self.numMines = numMines
        self.gameOver = False
        self.victory = False

        self.network = BayesianNetwork(size=size, numMines=numMines)
Ejemplo n.º 3
0
def main(test_path,real_label):
    os.chdir(test_path)

    google_detector = GoogleDetector()
    image_preprocessor = ImagePreprocessing()
    sess = tf.Session()
    emotion_detector = CnnEmotionDetection(sess)
    emotion_detector.load_variables(
        './checkpoints/emotiW_detection_153429/cnn_emotiW_detection-1499.meta',
        './checkpoints/emotiW_detection_153429/')
    my_bayes_net = BayesianNetwork()
    is_correct = my_bayes_net.initModel("./wild_GAF_labels_histogram_train_global.csv", False)
    print("Model correct: " + str(is_correct))

    predicted_counter = [0,0,0]
    for image_path in sorted(glob.glob("*")):
        print image_path
        final_predictions = classify_image(test_path, image_path,real_label, emotion_detector, google_detector, image_preprocessor, my_bayes_net)
        predicted_counter[final_predictions] += 1
        print predicted_counter
        print ""
Ejemplo n.º 4
0
class Main(threading.Thread):
	"""docstring for Main"""

	MAX_SEL = 10

	MSG_WELCOME = "Welcome to Bayesian Network solver! Press q to quit"
	MSG_SEL_LMT = "You cannot select more than 10 variables!"
	MSG_WORKING = "Calculating probability..."

	def __init__(self, qu_usr_ip, qu_cmd, input_filename="input1.txt"):
		super(Main, self).__init__()
		self.qu_usr_ip = qu_usr_ip
		self.qu_cmd = qu_cmd

		self.input_filename = input_filename
		self.init_network()

		self.cur_names_qry = []
		self.cur_names_cond = []
		self.cur_name_mrkv = None


	def init_network(self):
		self.bn = BayesianNetwork()
		self.bn.init_from_file(self.input_filename)
		self.node_names = sorted([n.name for n in self.bn.nodes])


	def send_cmd(self, cmd, *args):
		msg = (cmd, args)
		self.qu_cmd.put(msg)


	def run(self):

		self.send_cmd("init_node_names", self.node_names)
		self.send_cmd("draw_base")
		self.send_cmd("display_msg", self.MSG_WELCOME)

		while True:
			usr_ip = self.qu_usr_ip.get()
			t, dev, arg = usr_ip
			# print "Main rcvd", usr_ip

			if dev == "keypress":
				if arg == 'q':
					self.send_cmd("quit")
					break

			else:
				if arg == None:
					pass

				elif arg[0] == "qry" or arg[0] == "cond":

					if arg[0] == "qry":
						if arg[1] in self.cur_names_qry:
							self.send_cmd("off", arg[0], arg[1])
							self.cur_names_qry.remove(arg[1])
						elif arg[1][1:] in self.cur_names_qry:
							self.send_cmd("off", arg[0], arg[1][1:])
							self.cur_names_qry.remove(arg[1][1:])
							self.send_cmd("on", arg[0], arg[1])
							self.cur_names_qry.append(arg[1])
						elif "~"+arg[1] in self.cur_names_qry:
							self.send_cmd("off", arg[0], "~"+arg[1])
							self.cur_names_qry.remove("~"+arg[1])
							self.send_cmd("on", arg[0], arg[1])
							self.cur_names_qry.append(arg[1])
						elif len(self.cur_names_qry) < self.MAX_SEL:
							self.send_cmd("on", arg[0], arg[1])
							self.cur_names_qry.append(arg[1])
						else:
							self.send_cmd("display_msg", self.MSG_SEL_LMT)
							continue

					elif arg[0] == "cond":
						if arg[1] in self.cur_names_cond:
							self.send_cmd("off", arg[0], arg[1])
							self.cur_names_cond.remove(arg[1])
						elif arg[1][1:] in self.cur_names_cond:
							self.send_cmd("off", arg[0], arg[1][1:])
							self.cur_names_cond.remove(arg[1][1:])
							self.send_cmd("on", arg[0], arg[1])
							self.cur_names_cond.append(arg[1])
						elif "~"+arg[1] in self.cur_names_cond:
							self.send_cmd("off", arg[0], "~"+arg[1])
							self.cur_names_cond.remove("~"+arg[1])
							self.send_cmd("on", arg[0], arg[1])
							self.cur_names_cond.append(arg[1])
						elif len(self.cur_names_cond) < self.MAX_SEL:
							self.send_cmd("on", arg[0], arg[1])
							self.cur_names_cond.append(arg[1])
						else:
							self.send_cmd("display_msg", self.MSG_SEL_LMT)
							continue

					# Calc
					p = get_prob_from_names(self.bn, self.cur_names_qry, self.cur_names_cond)
					self.send_cmd("display_expr", p.to_string())
					self.send_cmd("display_msg", self.MSG_WORKING)
					val = p.evaluate(self.bn)
					self.send_cmd("display_prob", val)

				elif arg[0] == "mrkv":
					if self.cur_name_mrkv != None:
						self.send_cmd("off", arg[0], self.cur_name_mrkv)
					self.send_cmd("on", arg[0], arg[1])
					self.cur_name_mrkv = arg[1]

					cur_node_mrkv = self.bn.nodes_by_name[self.cur_name_mrkv]
					mrkv_blanket = cur_node_mrkv.get_markov_blanket()
					self.send_cmd("draw_mrkv", [n.name for n in mrkv_blanket])
Ejemplo n.º 5
0
	def init_network(self):
		self.bn = BayesianNetwork()
		self.bn.init_from_file(self.input_filename)
		self.node_names = sorted([n.name for n in self.bn.nodes])
Ejemplo n.º 6
0
from bayesian_network import BayesianNetwork
from binary_file_reader import *

bayesian_network = BayesianNetwork()
bayesian_network.calculate_probabilities(variables)
rank_probabilities = bayesian_network.rank_probabilities
gpa_probabilities = bayesian_network.gpa_probabilities
gre_probabilities = bayesian_network.gre_probabilities
admit_probabilities = bayesian_network.admit_probabilities

# a

result = 0
admit = 0
rank = 1

for gre in [GRE_LESS_THAN_500, GRE_MORE_THAN_500]:
    for gpa in [GPA_LESS_THAN_3, GPA_MORE_THAN_3]:
        result += bayesian_network.get_joint_probability(admit, gpa, gre, rank)

result = result / rank_probabilities[rank]

print("a. Probability of not being admitted going to rank 1 school:", result)

# b

rank = 2
gre = GRE_LESS_THAN_500
gpa = GPA_MORE_THAN_3

denominator = 0
Ejemplo n.º 7
0
from bayesian_network import BayesianNetwork, compute_p

print("=== TEST CASE 1 ===")
TESTCASE_1_singleProb = (("A", 0.99), )
TESTCASE_1_conditionalProb = (("B", (({
    "A": True
}, 0.2), ({
    "A": False
}, 0.8))), )

BN = BayesianNetwork(TESTCASE_1_singleProb, TESTCASE_1_conditionalProb)
compute_p(BN, {"B": True}, {"A": False})
compute_p(BN, {"B": True}, {})
compute_p(BN, {"B": True, "A": False}, {})

print("=== TEST CASE 1 ===")
TESTCASE_1_singleProb = (("A", 0.5), )
TESTCASE_1_conditionalProb = (("B", (({
    "A": True
}, 0.2), ({
    "A": False
}, 0.8))), )

BN = BayesianNetwork(TESTCASE_1_singleProb, TESTCASE_1_conditionalProb)
compute_p(BN, {"A": True}, {"B": True})

CONDITION = (({"A": True}, 0.2), ({"A": False}, 0.6))

TESTCASE_2_singleProb = (("A", 0.5), )
TESTCASE_2_conditionalProb = (
    ("B", CONDITION),