Exemplo n.º 1
0
    def test_heuristic_gen(self):
        root = self.gen_tree()
        pvar = pred_variance(root)
        max_var = 1.5 * max(pvar.values())
        pvar1 = dict((p, max_var - v) for p, v in pvar.items())
        pvar1s = sum(pvar1.values())
        weights = dict((p, root.num_occurrences(p) * v / pvar1s)
                       for p, v in pvar1.items())
        for p, w in weights.items():
            print '%s: %f' % (p, w)

        preds = list(root.get_all_predicates())
        preds.sort()
        weights = fmincon.run_fmincon(root)

        h = heuristic.Heuristic(dict(zip(preds, weights)))

        root.make_graphviz(open('temp.dot', 'w'))
        search = AStar(root, lambda x: h(x.preds))
        iter = search.gen
        iterations = 0
        try:
            while not iter.next():
                iterations += 1
        except StopIteration:
            print iterations
            print[''.join(s.preds) for s in search.solution]
Exemplo n.º 2
0
    def __init__(self,
                 name,
                 theBoard,
                 theBag,
                 theDifficulty=10.0,
                 theHeuristic=None):
        if not Player.initialized:
            Player.initialize()

        self.tray = []
        self.score = 0
        self.name = name
        self.theBoard = theBoard
        self.theBag = theBag
        self.lastScorePulse = 0
        self.lastScore = 0

        self.usageLimit = self.theBoard.dictionary.difficultyToUsage(
            theDifficulty)

        print str(theDifficulty) + ", " + str(self.usageLimit)

        if theHeuristic == None:
            self.heuristic = heuristic.Heuristic()
        else:
            self.heuristic = theHeuristic

        #start with a full set of tiles
        self.grab()
Exemplo n.º 3
0
    def on_message(self, message):
        """
        message: String received from javascript websocket
        This function is called everytime a message is received from the socket connection. 
        Message can be parsed using a json loads
        """

        if not self.presentation_started:
            print("Starting Slideshow ...")
            self.presentation.run_slideshow()
            self.presentation_started = True

        response_dict = json.loads(message)
        body_gesture = None
        hand_gesture = None

        if self.enable_heuristic:
            waiting = True
            if response_dict["body_pose"] and response_dict["handpose"]:
                # Body classifcation handler update is called to send the body pose details to the handler.
                heux = heuristic.Heuristic(
                    response_dict,
                    self.body_classification_handler.minPoseConfidence,
                    self.hand_classification_handler.minPoseConfidence)
                check_body_validation, check_hand_validation = heux.heuristic_checks(
                )
                if check_body_validation:
                    body_gesture = self.body_classification_handler.update(
                        response_dict["body_pose"][0],
                        xmax=response_dict["image_width"],
                        ymax=response_dict["image_height"])
                    waiting = False
                #else:
                #    print("skipped position classification")
                if check_hand_validation:
                    hand_gesture = self.hand_classification_handler.update(
                        response_dict["handpose"],
                        xmax=response_dict["image_width"],
                        ymax=response_dict["image_height"])
                    waiting = False
                #else:
                #    print("skipped hand classification")
            if waiting:
                print(". . .")
        else:
            if response_dict["body_pose"] and response_dict["handpose"]:
                body_gesture = self.body_classification_handler.update(
                    response_dict["body_pose"][0],
                    xmax=response_dict["image_width"],
                    ymax=response_dict["image_height"])
                hand_gesture = self.hand_classification_handler.update(
                    response_dict["handpose"],
                    xmax=response_dict["image_width"],
                    ymax=response_dict["image_height"])

        self.perform_gesture_action(body_gesture, hand_gesture)
Exemplo n.º 4
0
def test_goal_dist_heuristic():
    min_len = 4
    max_len = 10
    indicators = dict((d, ['I%d' % d]) for d in range(1, max_len))
    indicator_set = set(reduce(lambda x, y: x + y, indicators.values()))
    tree_gen = TreeGen()
    tree_gen.min_branch_len = min_len
    tree_gen.max_branch_len = max_len

    src_root = tree_gen.generate_indicators(indicators)
    src_root.make_graphviz(open('src_tree.dot', 'w'))
    tgt_root = src_root.deep_copy()
    # change all predicates in the target to be lowercase version of source
    tgt_root.map(state_preds_lower)
    tgt_root.make_graphviz(open('tgt_tree.dot', 'w'))

    src_preds = list(src_root.get_all_predicates())

    num_preds = len(src_preds)

    src_preds.sort()
    src_weights = fmincon.run_fmincon(src_root)

    # the heuristic for the source
    hs = heuristic.Heuristic(dict(zip(src_preds, src_weights)))
    #hs = heuristic.Heuristic(indicators)

    src_search = AStar(src_root, lambda x: hs(x.preds))
    iter = src_search.gen
    iterations = 0
    try:
        while not iter.next():
            iterations += 1
    except StopIteration:
        src_iters = iterations

    non_ind_preds = [p for p in src_preds if p not in indicator_set]

    data = []

    for n_ind, n_rest, mapping in ind_mapping_gen(indicator_set, non_ind_preds,
                                                  num_preds, 100):
        #for n_matches, mapping in reg_mapping_gen(src_preds, 100):
        #print "The mapping is ..."
        #for s,t in mapping.items():
        #	print s, t

        tgt_preds2weights = {}
        for p, w in zip(src_preds, src_weights):
            if p in mapping:
                # we assign the weight of the source predicate to the target
                tgt_preds2weights[mapping[p]] = w

        ht = heuristic.Heuristic(tgt_preds2weights)
        tgt_search = AStar(tgt_root, lambda x: ht(x.preds))
        iter = tgt_search.gen
        iterations = 0
        try:
            while not iter.next():
                iterations += 1
        except StopIteration:
            tgt_iters = iterations

        #print 'The target problem takes %d iterations' % tgt_iters
        #print 'Solution is', [''.join(s.preds) for s in tgt_search.solution]

        data.append((n_ind, n_rest, tgt_iters))
        #data.append((n_matches, tgt_iters))

    n_preds = len(src_preds)
    return (n_preds, src_iters, data)
Exemplo n.º 5
0
def main():
    print("--------------------------------------")
    print("-----     Connect Four           -----")
    # Init Board
    print("-----     Init Board             -----")
    board = connect_four.Board()

    # Init Rule, Heuristic, NN_heuristic
    print("-----     Load Rule              -----")
    rule = connect_four_rule.Rule()
    print("-----     Load Heuristic         -----")
    heuristic = connect_four_heuristic.Heuristic()
    print("-----     Load NN_Heuristic      -----")
    NN_heuristic = connect_four_heuristic.NN_Heuristic()
    
    print("-----     Load All, Play game!   -----")
    print("--------------------------------------")
    print("")

    # Init Values
    free_cells = 42
    users_turn = True

    # Set first Movement
    choice = input("Would you like to go first? (y or n): ")

    if (choice == 'y' or choice=='Y'):
        users_turn = True
    elif (choice == 'n' or choice =='N') :
        users_turn = False        
    else:
        print ('invalid input')

    # Play game
    while not winner(board) and (free_cells > 0):
        # Display board
        board.display()

        # User turn or Computer turn
        if users_turn:
            # User turn
            make_user_move(board)

            # Change turn
            users_turn = not users_turn
        else:
            # First move condition (do not put in middle col)
            if free_cells == 42:
                firstMove(board)
                users_turn = not users_turn
                continue

            # Computer turn
            mode = getMode()
            
            # Computer Run Mode (Rule / Heuristic / Neural Network)
            if mode == 1:
                # Rule base
                rule.make_move(board)
            elif mode == 2:
                # Heuristic base
                heuristic.make_ab_move(board)
            else:
                # Neural Network Heuristic base
                NN_heuristic.make_ab_move(board)

            # Change turn
            users_turn = not users_turn

        free_cells -= 1

    # Game end
    board.display()

    if (winner(board) == 'X'):
        print ("You Won!")
    elif (winner(board) == 'O'):
        print ("The Computer Won!")
        print ("\nGAME OVER")
    else:
        print ("Draw!")
        print ("\nGAME OVER \n")
Exemplo n.º 6
0
    def test_all(self, nbins, split_prob):
        #src_root, src_rs, tgt_root, tgt_rs = self.gen_src_tgt_split(split_prob)
        src_root, src_rs, tgt_root, tgt_rs = self.gen_contradict()

        src_file = 'source.kif'
        tgt_file = 'target.kif'
        src_rs.make_kif(src_root.preds, open(src_file, 'w'))
        tgt_rs.make_kif(tgt_root.preds, open(tgt_file, 'w'))

        src_preds = list(src_root.get_all_predicates())
        src_preds.sort()
        src_weights = fmincon.run_fmincon(src_root)

        # the heuristic for the source
        hs = heuristic.Heuristic(dict(zip(src_preds, src_weights)))

        src_search = AStar(src_root, lambda x: hs(x.preds))
        iter = src_search.gen
        iterations = 0
        try:
            while not iter.next():
                iterations += 1
        except StopIteration:
            src_iters = iterations

        print 'The source problem takes %d iterations' % src_iters
        print 'Solution is', [''.join(s.preds) for s in src_search.solution]

        src_bins = dict((Predicate(p, Sentence.STATE, []), b)
                        for p, b in make_bins(src_preds, nbins).items())
        tgt_bins = dict(
            (Predicate(p.get_name().lower(), Sentence.STATE, []), b)
            for p, b in src_bins.items())

        mapping = rule_mapper2.map_kifs(src_file, tgt_file, src_bins, tgt_bins)
        str_mapping = dict(
            (s.get_name(), t.get_name()) for s, t in mapping.items())
        print "The mapping is ..."
        for s, t in str_mapping.items():
            print s, t

        num_correct = 0
        num_correct_inds = 0
        for s, t in str_mapping.items():
            if s.lower() == t:
                num_correct += 1
                if s[0] == 'I' and s[-1] in '0123456789':
                    num_correct_inds += 1

        print "Number of correct predicates:", num_correct

        tgt_preds = []
        tgt_weights = []
        for p, w in zip(src_preds, src_weights):
            if p in str_mapping:
                # we assign the weight of the source predicate to the target
                tgt_preds.append(str_mapping[p])
                tgt_weights.append(w)

        ht = heuristic.Heuristic(dict(zip(tgt_preds, tgt_weights)))
        tgt_search = AStar(tgt_root, lambda x: ht(x.preds))
        iter = tgt_search.gen
        try:
            while not iter.next():
                iterations += 1
        except StopIteration:
            tgt_iters = iterations

        print 'The target problem takes %d iterations' % tgt_iters
        print 'Solution is', [''.join(s.preds) for s in tgt_search.solution]

        os.remove(src_file)
        os.remove(tgt_file)

        ret_labels = [
            'len(src_preds)', 'num_correct', 'num_correct_inds', 'src_iters',
            'tgt_iters'
        ]
        ret_vals = eval('[%s]' % ','.join(ret_labels))
        return (ret_labels, ret_vals)