예제 #1
0
def NN_play():

    print("Hello I'm NN AI ")
    while(1):
        size = int(input("Please input the size you want(must be int and must more than zero): "))
        if size>0:
            break
    load_net = tr.load(path[size-1])
    game = matrix_modified.game_class(size)  #size of the cards

    while(1):
        game.start_one_round()
        game.show_game_condition_for_play()
        can_drop=0
        drop_action=0
        os.system("pause")
        print()
        while(1):
            test_item = game.cards.matrix.reshape((1, size * 52 * 5))
            test_item = torch.tensor(torch.from_numpy(test_item)).float()
            result = load_net(test_item)
            action=judge_chose(result[0],can_drop)
            if action=="drop":
                can_drop=1
            end, win = game.ai_input_action(action, drop_action)
            if end==1:
                break
            os.system("pause")
        game.end_one_round()
        val = input("Do you want NN start one new around(0 is yes; 1 is no)")
        if(val=="1"):break
예제 #2
0
def __main__():
    game = matrix_modified.game_class(1)  #size of the cards
    game.start_one_round()
    game.show_game_condition_for_test()

    algorithm = algo_class(game)
    '''
예제 #3
0
def __main__():

    # set
    size = 1

    game = matrix_modified.game_class(size)  # size of the cards
    game.start_one_round()
    algorithm = algo.algo_class(game)
    action, drop_action, nodes_number = algorithm.AI_chose_node(0)

    matrix_desk = numpy.zeros((1,10))
    matrix_player = numpy.zeros((1, 10))
    matrix_dealer = numpy.zeros((1, 10))

    for i in range(game.cards.matrix.shape[0]):
        for j in range(game.cards.matrix.shape[1]):
            if game.cards.matrix[i][j][0] == 1:
                number = j%13
                if number>=9: matrix_desk[0,9]+=1
                else: matrix_desk[0,number]+=1
            if game.cards.matrix[i][j][1] == 1:
                number = j % 13
                if number>=9: matrix_player[0,9]+=1
                else: matrix_player[0,number]+=1
            if game.cards.matrix[i][j][2] == 1:
                number = j % 13
                if number>=9: matrix_dealer[0,9]+=1
                else: matrix_dealer[0,number]+=1
    print(matrix_desk)
    print(matrix_player)
    print(matrix_dealer)
예제 #4
0
def __main__():
    # 设置每层节点个数
    size = 1
    input_nodes = 52 *5 * size
    hidden_nodes = 30
    output_nodes = 3

    # 设置学习率为0.3
    learning_rate = 0.3
    # 创建神经网络
    neural = NeuralNetwork(input_nodes, hidden_nodes, output_nodes, learning_rate)

    print("training initial *****************************************************")
    for i in range(100):
        # initial game
        game = matrix_modified.game_class(size)  # size of the cards
        for j in range(5):
            print("number:",i*j+j)
            game.start_one_round()
            algorithm = algo.algo_class(game)
            action, drop_action, nodes_number = algorithm.AI_chose_node(0)
            print("Chose Action:",action)

            targets=[]
            if(action == "stay"): targets=[1,0,0]
            if(action == "hit"): targets=[0,1,0]
            if(action == "drop"): targets=[0,0,1]

            neural.train(game.cards.matrix.reshape((52*size*5,1)), targets)
            game.end_one_round()

    print("test initial *****************************************************")
    sum_right_count=0
    for i in range(100):
        # initial game
        game = matrix_modified.game_class(size)  # size of the cards
        for j in range(5):
            print("number:",i*j+j)
            game.start_one_round()
            algorithm = algo.algo_class(game)
            action, drop_action, nodes_number = algorithm.AI_chose_node(0)
            print(action)
            result=neural.query(game.cards.matrix.reshape((52*size*5,1)))
            print(result)
            sum_right_count+=judge_right(action,result)
            print("sum_right_count:",sum_right_count)
            game.end_one_round()
예제 #5
0
def __main__():
    size = 5
    path = [
        'imple_2_size_1.pkl', 'imple_2_size_2.pkl', 'imple_2_size_3.pkl',
        'imple_2_size_4.pkl', 'imple_2_size_5.pkl'
    ]

    myNet = nn.Sequential(nn.Linear(size * 52 * 5, 30), nn.Softmax(),
                          nn.Linear(30, 3), nn.Sigmoid())
    opti_function = torch.optim.SGD(myNet.parameters(), lr=0.01)
    loss_func = nn.MSELoss()

    train_loss = []
    output_true, output_pred = [], []
    for iteration in range(100):
        train_data = np.zeros((1, size * 52 * 5))
        target_data = np.zeros((1, 3))
        for i in range(5):
            game = matrix_modified.game_class(size)  # size of the cards
            for j in range(5):
                game.start_one_round()
                algorithm = algo.algo_class(game)
                action, drop_action, nodes_number, win_rates = algorithm.AI_chose_node_2(
                    0)
                targets = [win_rates]
                for item in win_rates:
                    output_true.append(item)
                train_item = game.cards.matrix.reshape((1, size * 52 * 5))
                train_data = np.r_[train_data, train_item]
                target_data = np.r_[target_data, targets]
                game.end_one_round()

        train_data_torch = torch.tensor(
            torch.from_numpy(train_data[1:train_data.shape[0]])).float()
        target_data_torch = torch.tensor(
            torch.from_numpy(target_data[1:target_data.shape[0]])).float()

        for epoch in range(2000):
            out = myNet(train_data_torch)
            loss = loss_func(out, target_data_torch)  #loss
            train_loss.append(loss)
            opti_function.zero_grad()  #gra
            loss.backward()
            opti_function.step()
        out = myNet(train_data_torch)
        print(out)
        for i in range(len(out)):
            for j in range(3):
                output_pred.append(float(out[i][j]))
    # loss function figure
    x = np.arange(0, 100, 1 / 2000)
    plt.plot(x, train_loss)
    plt.show()
    # item figure
    plt.scatter(output_true, output_pred, color='red', s=100)
    plt.show()
    tr.save(myNet, path[size - 1])
예제 #6
0
def __main__():
    all_list_score = [0, 0, 0]
    size = 5
    load_net = tr.load(path[size - 1])
    for i in range(0,20):  # 5 * 100
        game = matrix_modified.game_class(size)  # size of the cards
        efficiency = efficiency_class(game,load_net,size)
        list_score= efficiency.calculate()

        all_list_score[0] += list_score[0]
        all_list_score[1] += list_score[1]
        all_list_score[2] += list_score[2]
        print(all_list_score)

    print("*********************************************************************************")
    print("size:",size)
    print(all_list_score)
    print("*********************************************************************************")
예제 #7
0
def __main__():

    print("")
    while (1):
        size = int(
            input(
                "Please input the size you want(int and must more than zero): "
            ))
        if size > 0:
            break
    game = matrix_modified.game_class(size)  #size of the cards

    while (1):
        game.start_one_round()
        #game.show_all_matrix_in_hands()
        game.show_game_condition_for_play()
        game.input_action()
        game.end_one_round()
        val = input("Do you want to start one new around(0 is yes; 1 is no)")
        if (val == "1"): break
예제 #8
0
def __main__():
    path = [
        'imple_3_size_1.pkl', 'imple_3_size_2.pkl', 'imple_3_size_3.pkl',
        'imple_3_size_4.pkl', 'imple_3_size_5.pkl'
    ]

    size = 1
    myNet = nn.Sequential(nn.Linear(30, 9), nn.Tanh(), nn.Linear(9, 3),
                          nn.Sigmoid())
    print(myNet)

    # set optimzer
    optimzer = torch.optim.SGD(myNet.parameters(), lr=0.001)
    loss_func = nn.MSELoss()

    print(
        "training initial *****************************************************"
    )

    train_loss = []
    output_true, output_pred = [], []
    it = 100
    for i in range(it):
        train_data = np.zeros((1, 30))
        target_data = np.zeros((1, 3))
        for k in range(5):
            game = matrix_modified.game_class(size)  # size of the cards

            for j in range(5):
                game.start_one_round()
                algorithm = algo.algo_class(game)
                action, drop_action, nodes_number, win_rates = algorithm.AI_chose_node_2(
                    0)
                targets = [win_rates]
                train_item = data_processing(game)
                output_true.append(win_rates[0])
                output_true.append(win_rates[1])
                output_true.append(win_rates[2])
                #print(train_data)
                train_data = np.r_[train_data, train_item]
                target_data = np.r_[target_data, targets]
                game.end_one_round()

        train_data_torch = torch.tensor(
            torch.from_numpy(train_data[1:train_data.shape[0]])).float()
        target_data_torch = torch.tensor(
            torch.from_numpy(target_data[1:target_data.shape[0]])).float()

        for epoch in range(2000):
            out = myNet(train_data_torch)
            loss = loss_func(out, target_data_torch)
            train_loss.append(loss)
            optimzer.zero_grad()
            loss.backward()
            optimzer.step()
        out = myNet(train_data_torch)
        for i in range(len(out)):
            output_pred.append(float(out[i][0]))
            output_pred.append(float(out[i][1]))
            output_pred.append(float(out[i][2]))

    # loss function figure
    r = np.arange(0, it, 1 / 2000)
    plt.plot(r, train_loss)
    plt.show()

    # error figure
    plt.scatter(output_true, output_pred, s=50)
    plt.show()
    tr.save(myNet, path[size - 1])
예제 #9
0
def AI_play():

    print("Hello I'm Black Jack AI ")
    while (1):
        size = int(
            input(
                "Please input the size you want(must be int and must more than zero): "
            ))
        if size > 0:
            break

    game = matrix_modified.game_class(size)  #size of the cards

    model = int(
        input(
            "Please input the model of AI (0 is expected tree AI; 1 is random AI): "
        ))
    if model == 0:
        while (1):

            game.start_one_round()
            time.sleep(1)
            game.show_game_condition_for_play()
            can_drop = 0
            time.sleep(1)
            os.system("pause")
            print()
            print()
            print()
            while (1):
                algorithm = algo.algo_class(game)
                action, drop_action, nodes_number = algorithm.AI_chose_node(
                    can_drop)
                print("action:", action)
                print("drop_action:", drop_action)
                end, win = game.ai_input_action(action, drop_action)
                if action == "drop":
                    can_drop = 1
                if end == 1:
                    break
                os.system("pause")
                print()
                print()
                print()

            game.end_one_round()
            val = input(
                "Do you want AI start one new around(0 is yes; 1 is no)")
            if (val == "1"): break
    if model == 1:
        while (1):

            game.start_one_round()
            time.sleep(1)
            game.show_game_condition_for_play()
            can_drop = 0
            time.sleep(1)
            os.system("pause")
            print()
            print()
            print()
            while (1):
                ran_01 = random.randint(0, 2)
                end = 0
                #print(ran_01)
                if ran_01 == 0:
                    print("Chose stay!!!")
                    game.ai_input_action("stay", 0)
                    break
                if ran_01 == 1:
                    print("Chose hit!!!")
                    end, win = game.ai_input_action("hit", 0)
                if ran_01 == 2 and can_drop == 1:
                    continue
                if ran_01 == 2 and can_drop == 0:
                    print("Chose drop!!!")
                    can_drop = 1
                    ran_02 = random.randint(0, len(game.list_player_cards) - 1)
                    end, win = game.ai_input_action("drop", ran_02)

                if end == 1:
                    break
                os.system("pause")
                print()
                print()
                print()

            game.end_one_round()
            val = input(
                "Do you want AI start one new around(0 is yes; 1 is no)")
            if (val == "1"): break
예제 #10
0
def __main__():
    path = [
        'imple_1_size_1.pkl', 'imple_1_size_2.pkl', 'imple_1_size_3.pkl',
        'imple_1_size_4.pkl', 'imple_1_size_5.pkl'
    ]

    size = 2
    myNet = nn.Sequential(nn.Linear(39, 20), nn.Tanh(), nn.Linear(20, 10),
                          nn.Tanh(), nn.Linear(10, 1), nn.Sigmoid())
    optimzer = torch.optim.SGD(myNet.parameters(), lr=0.001)
    loss_func = nn.MSELoss()

    train_loss = []
    output_true, output_pred = [], []
    it_num = 100
    for i in range(it_num):
        print(i)
        train_data = np.zeros((1, 39))
        target_data = np.zeros((1, 1))
        for k in range(5):
            game = matrix_modified.game_class(size)  # size of the cards
            for j in range(5):
                game.start_one_round()
                #action=""
                #if game.player_sum >= 17: action ="stay"
                #elif game.player_sum <= 11: action = "hit"
                #else: action = "drop"
                algorithm = algo.algo_class(game)
                action, drop_action, nodes_number, win_rates = algorithm.AI_chose_node_2(
                    0)
                target = 0
                if (action == "stay"): target = [[0.5]]
                elif (action == "hit"): target = [[1]]
                elif (action == "drop"): target = [[0]]
                output_true.append(target)
                train_item = np.zeros((1, 39))
                for x in range(0, game.cards.matrix.shape[0]):
                    for y in range(0, game.cards.matrix.shape[1]):
                        if game.cards.matrix[x][y][0] == 1:
                            train_item[0][0 + y % 13] += 1
                        if game.cards.matrix[x][y][1] == 1:
                            train_item[0][13 + y % 13] += 1
                        if game.cards.matrix[x][y][2] == 1:
                            train_item[0][26 + y % 13] += 1
                train_data = np.r_[train_data, train_item]
                target_data = np.r_[target_data, target]
                game.end_one_round()

        train_data_torch = torch.tensor(
            torch.from_numpy(train_data[1:train_data.shape[0]])).float()
        target_data_torch = torch.tensor(
            torch.from_numpy(target_data[1:target_data.shape[0]])).float()

        for epoch in range(5000):
            out = myNet(train_data_torch)
            loss = loss_func(out, target_data_torch)
            train_loss.append(loss)
            optimzer.zero_grad()
            loss.backward()
            optimzer.step()
        out = myNet(train_data_torch)
        for i in range(len(out)):
            output_pred.append(float(out[i][0]))

    # loss function figure
    ran = np.arange(0, it_num, 1 / 5000)
    plt.plot(ran, train_loss)
    plt.show()

    # error figure
    plt.scatter(output_true,
                output_pred,
                marker='o',
                color='red',
                s=100,
                label='First')
    plt.show()
    tr.save(myNet, path[size - 1])
예제 #11
0
def __main__():
    size = 1
    path = [
        'imple_4_size_1.pkl', 'imple_4_size_2.pkl', 'imple_4_size_3.pkl',
        'imple_4_size_4.pkl', 'imple_4_size_5.pkl'
    ]

    myNet = nn.Sequential(nn.Linear(size * 52 * 5, 300), nn.Tanh(),
                          nn.Linear(300, 9), nn.Tanh(), nn.Linear(9, 3),
                          nn.Sigmoid())
    print(myNet)

    # set optimzer
    optimzer = torch.optim.SGD(myNet.parameters(), lr=0.01)
    loss_func = nn.MSELoss()

    print(
        "training initial *****************************************************"
    )
    it_number = 100
    it_epo_num = 5000
    train_loss = []
    output_true, output_pred = [], []
    for i in range(it_number):
        # initial game
        target_num = 1
        sum_hit, sum_stay, sum_drop = 0, 0, 0
        train_data = np.zeros((1, size * 52 * 5))
        target_data = np.zeros((1, 3))
        while (1):
            if sum_hit == target_num and sum_stay == target_num and sum_drop == target_num:
                break
            game = matrix_modified.game_class(size)  # size of the cards
            for j in range(25):
                game.start_one_round()
                algorithm = algo.algo_class(game)
                action, drop_action, nodes_number = algorithm.AI_chose_node(0)

                if action == "hit":
                    if sum_hit == target_num: continue
                    else: sum_hit += 1
                if action == "stay":
                    if sum_stay == target_num: continue
                    else: sum_stay += 1
                if action == "drop":
                    if sum_drop == target_num: continue
                    else: sum_drop += 1
                print("Chose Action:", action)

                targets = []
                if (action == "stay"):
                    targets = [[1, 0, 0]]
                    output_true.append(1)
                if (action == "hit"):
                    targets = [[0, 1, 0]]
                    output_true.append(2)
                if (action == "drop"):
                    targets = [[0, 0, 1]]
                    output_true.append(3)

                train_item = game.cards.matrix.reshape((1, size * 52 * 5))
                train_data = np.r_[train_data, train_item]
                target_data = np.r_[target_data, targets]
                game.end_one_round()

        train_data_torch = torch.tensor(
            torch.from_numpy(train_data[1:train_data.shape[0]])).float()
        target_data_torch = torch.tensor(
            torch.from_numpy(target_data[1:target_data.shape[0]])).float()

        for epoch in range(it_epo_num):
            out = myNet(train_data_torch)
            loss = loss_func(out, target_data_torch)  #loss
            train_loss.append(loss)
            optimzer.zero_grad()  #gra
            loss.backward()
            optimzer.step()
        out = myNet(train_data_torch)
        print(out)
        for i in range(len(out)):
            output_pred.append(train_target(out[i]))
    # loss function figure
    x = np.arange(0, it_number, 1 / it_epo_num)
    plt.plot(x, train_loss)
    plt.show()
    # item figure
    #print(output_true)
    #print(output_pred)
    plt.scatter(output_true,
                output_pred,
                marker='x',
                color='red',
                s=50,
                label='First')
    plt.show()
    # save the file
    print("save")
    tr.save(myNet, path[size - 1])