Example #1
0
def appendScenarioData(scenario_lst):
    for scenario in scenario_lst:
        df = df.append(scenario.toDict(), ignore_index=True)
    df.to_csv(out_path, index=False)
    log.printString('\t- Dataframe has been successfully printed\n',
                    log_path,
                    access='a')
Example #2
0
def performDecision( current_scenario, cardDeck, firstTurn=False, log_str='' ):
  # Initialize Variables
  playerTurn = True
  printLog = False

  # New Scenario
  if firstTurn: 
    # Deal cards to Player, Dealer and Player
    current_scenario.dealCard(cardDeck) 
    current_scenario.dealCard(cardDeck, player=False) 
    current_scenario.dealCard(cardDeck)
  # Make decision based on playerHand
  current_scenario.makeDecision()
  log_str += '\tcurrent scenario:\n' + log.logDict(current_scenario.toDict(), buffer='\t')

  new_scenario = current_scenario.copy()
  playerTurn = new_scenario.processDecision(cardDeck)
  
  scenario_lst = []
  if playerTurn and current_scenario.decision == 4: # Split
    scenario_0 = current_scenario[0]
    scenario_1 = current_scenario[1]
    """
    current_scenario = [scenario_0, performDecision(scenario_0.copy())]
    scenario_1.TotalCount = current_scenario[-1].TotalCount
    card = cardDeck.dealCard()
    scenario_1['PlayerHand'].append(card)

    current_scenario = [current_scenario[0]] + performDecision(current_scenario[0]) + [current_scenario[1]] + performDecision(current_scenario[1])
    """
  elif playerTurn:
    scenario_lst = performDecision(new_scenario.copy(), cardDeck, log_str=log_str, firstTurn=False) + [current_scenario.copy()]
  elif not playerTurn:
    printLog = True
    scenario_lst = [current_scenario.copy()]
  
  log_str += '\tPlayer\'s turn has ended\n'
  if printLog:
    log.printString(log_str, log_path, access='a', buffer=True)
  
  return scenario_lst
Example #3
0
def performAction(model, current_scenario, cardDeck):
    printLog = False

    current_scenario.dealCard(cardDeck, player=True)
    current_scenario.dealCard(cardDeck, player=False)
    current_scenario.dealCard(cardDeck, player=True)
    log_str = 'The Player has received 2 cards and the Dealer has received 1:\n' + log.logDict(
        current_scenario.toDict()) + '\n'

    prediction = model.predict(prepTrainData(current_scenario))
    log_str += '\n=========================\nPrediction:\t' + str(
        prediction) + '\nDecide Action:\t' + str(
            decideAction(prediction)) + '\n=========================\n'

    current_scenario.importDecision(decideAction(prediction), cardDeck)

    new_scenario = current_scenario.copy()
    playerTurn = new_scenario.processDecision(cardDeck)
    log_str += 'The Player decision has been processed:\n' + log.logDict(
        current_scenario.toDict()) + '\n'

    scenario_lst = []
    if playerTurn and current_scenario.decision == 4:  # Split
        scenario_0 = current_scenario[0]
        scenario_1 = current_scenario[1]
    elif playerTurn:
        scenario_lst = performAction(model, new_scenario.copy(),
                                     cardDeck) + [current_scenario.copy()]
    elif not playerTurn:
        printLog = True
        # Deal cards to dealer
        while current_scenario.getHandValue(cardDeck, player=False) < 17:
            current_scenario.dealCard(cardDeck, player=False)

        scenario_lst = [new_scenario.copy()]

    if printLog:
        log.printString(log_str, log_path, access='a')

    return scenario_lst
Example #4
0
def getOutcome(scenario, cardDeck):
  if scenario.getHandValue(cardDeck, player=True) > 21: # Player busts: Loss
    return -1
  elif scenario.getHandValue(cardDeck, player=False) > 21:  # Dealer busts: Win
    return 1
  elif scenario.getHandValue(cardDeck, player=True) > scenario.getHandValue(cardDeck, player=False): # Player beats dealer: Win
    return 1
  elif scenario.getHandValue(cardDeck, player=True) < scenario.getHandValue(cardDeck, player=False): # Dealer beats player: Loss
    return -1
  elif scenario.getHandValue(cardDeck, player=True) == scenario.getHandValue(cardDeck, player=False):
    return 0
  else:
    print(scenario.toDict())
    raise Exception('Outcome undetermined: ' + log.logDict(scenario.toDict()) + '\n')
Example #5
0
def playIteration( cardDeck, TotalCount ):
  df = pd.DataFrame()

  scenarios = []
  log_str = ''

  current_scenario = scenario(count=TotalCount, dealercard=0, playerhandvalue=0,
                      numaces=0, numplayercards=0, dec=0, winloss=-999, 
                      correctdecision=[0,0,0,0], playerhand=[], dealerhand=[])
  
  scenario_lst = performDecision(current_scenario, cardDeck, firstTurn=True)

  if isinstance(scenario_lst[0], scenario):
    # Dealer gets second card
    scenario_lst[0].dealCard(cardDeck, player=False)

    while scenario_lst[0].getHandValue(cardDeck, player=False) < 17:
      scenario_lst[0].dealCard(cardDeck, player=False)

    for i in range(1, len(scenario_lst)):
      scenario_lst[i].DealerHand = scenario_lst[0].DealerHand

    output = getCorrectDecision(scenario_lst[0], cardDeck)
    scenarios = scenarios + output
  else:
    raise Exception('performDecision returned incorrect data type: list<list>\n')
  
  for s in scenarios:
    if DEBUG:
      df = df.append(s.toDict(), ignore_index=True)
    else:
      log_str += '\tPlayerHand and DealerHand fields have been removed\n'
      df = df.append(s.toDict(), ignore_index=True)

  log.printString(log_str, log_path, access='a')

  return df
Example #6
0
 def generateCorrectDecision(self, last_scenario, cardDeck):
     self.getHandValue(cardDeck)  # update PlayerHandValue
     if self.PlayerHandValue >= 20:  # Player Busts
         self.CorrectDecision = [0, 1, 0, 0]
     elif last_scenario != None and last_scenario.PlayerHandValue > 21 and len(
             self.PlayerHand) + 1 == len(
                 last_scenario.PlayerHand
             ):  # Not Last Scenario and Player Busts Next
         self.CorrectDecision = [0, 1, 0, 0]
         # Not Last Scenario, Player Double Down and Lose to Dealer
     elif last_scenario == None and self.getHandValue(
             cardDeck, player=False) > 21:  # Dealer Busts and Last Scenario
         self.CorrectDecision = [0, 1, 0, 0]
     elif dd_allow and last_scenario != None and last_scenario.getHandValue(
             cardDeck, player=False
     ) > 21 and self.NumPlayerCards == 3:  # Dealer Busts, not Last Scenario, and 2 cards
         self.CorrectDecision = [1, 0, 1, 0]
     elif last_scenario != None and last_scenario.getHandValue(
             cardDeck, player=False
     ) > 21 and self.NumPlayerCards == 3:  # Dealer Busts, not Last Scenario, and 2 cards
         self.CorrectDecision = [1, 0, 0, 0]
     elif last_scenario != None and last_scenario.getHandValue(
             cardDeck, player=False
     ) > 21:  # Dealer Busts, not Last Scenario, and not 2 cards
         self.CorrectDecision = [
             0, 1, 0, 0
         ] if self.decision == 0 else self.toCorrectDecision()
     elif last_scenario == None and self.getHandValue(
             cardDeck, player=False
     ) > self.PlayerHandValue:  # Dealer beats player and Last Scenario
         self.CorrectDecision = [1, 0, 0, 0]
     elif last_scenario != None and last_scenario.getHandValue(
             cardDeck, player=False
     ) > last_scenario.PlayerHandValue:  # Dealer beats player and Not last Scenario
         self.CorrectDecision = [
             1, 0, 0, 0
         ] if self.decision == 0 else self.toCorrectDecision(self.decision)
     elif last_scenario == None and self.PlayerHandValue == self.getHandValue(
             cardDeck, player=False
     ) and self.PlayerHand != 20:  # Dealer ties player and Last Scenario
         self.CorrectDecision = [1, 0, 0, 0]
     elif last_scenario != None and last_scenario.PlayerHandValue == last_scenario.getHandValue(
             cardDeck, player=False
     ) and self.PlayerHand != 20:  # Dealer ties Player and Not Last Scenario
         self.CorrectDecision = [
             1, 0, 0, 0
         ] if self.decision == 0 else self.toCorrectDecision(self.decision)
     elif last_scenario == None and self.PlayerHandValue > self.getHandValue(
             cardDeck,
             player=False):  # Dealer loses Player and Last Scenario
         self.CorrectDecision = [
             0, 1, 0, 0
         ] if self.decision == 0 else self.toCorrectDecision(self.decision)
     elif dd_allow and last_scenario != None and last_scenario.PlayerHandValue > last_scenario.getHandValue(
             cardDeck, player=False
     ) and self.NumPlayerCards == 3:  # Dealer loses Player, Not Last Scenario and 2 cards
         self.CorrectDecision = [1, 0, 1, 0]
     elif last_scenario != None and last_scenario.PlayerHandValue > last_scenario.getHandValue(
             cardDeck, player=False
     ) and self.NumPlayerCards == 3:  # Dealer loses Player, Not Last Scenario and 2 cards
         self.CorrectDecision = [1, 0, 0, 0]
     elif last_scenario != None and self.PlayerHandValue > self.getHandValue(
             cardDeck, player=False
     ):  # Dealer loses Player, Not Last Scenario and not 2 cards
         self.CorrectDecision = [0, 1, 0, 0]
     else:
         log.printString(
             str(self.toDict()) + '\n',
             'C:/Users/akami/Documents/GitHub/Projects/BlackJackNeuralNetwork/Log/MissingScenario.log',
             access='a',
             buffer=True)
Example #7
0
    model.add(Dense(2, activation='sigmoid'))  # final layer
    # model.add(Dense(1, activation='sigmoid'))    # final layer
    model.compile(loss='binary_crossentropy', optimizer='sgd')
    log_str += 'Neural Network has been built\n'
else:
    model = keras.models.load_model(
        'C:/Users/akami/Documents/GitHub/Projects/BlackJackNeuralNetwork/TrainingData/GenerationFiles/ModelFiles/epoch_'
        + str(epoch_num) + '.h5')

model.fit(train_X, train_Y, epochs=32, batch_size=256, verbose=1)
model.save(
    'C:/Users/akami/Documents/GitHub/Projects/BlackJackNeuralNetwork/TrainingData/GenerationFiles/ModelFiles/epoch_'
    + str(epoch_num) + '.h5')
log_str += 'Neural Network has been trained\nThere will be ' + str(
    num_scenario) + ' ran\n===============================\n\n'
log.printString(log_str, log_path, access='w')

cardDeck = CardDeck(num_decks)
TotalCount = 0
scenarios = []

df_out = pd.DataFrame()

for i in range(num_scenario):
    log_str = 'Scenario ' + str(
        i) + ':\n==============================================\n'

    if (cardDeck.NumDeck * 52) - cardDeck.currentCard >= 20:
        cardDeck.shuffleCards()
        TotalCount = 0
        log_str += '\t- Deck Shuffled\n'
Example #8
0
        df = pd.read_csv(
            'C:/Users/akami/Documents/GitHub/Projects/BlackJackNeuralNetwork/TrainingData/Corpus/Log/Generation_Performance.csv',
            dtype=str)
        df = df.append(dic, ignore_index=True)
    else:
        df = pd.DataFrame.from_dict(dic)
    df.to_csv(
        'C:/Users/akami/Documents/GitHub/Projects/BlackJackNeuralNetwork/TrainingData/Corpus/Log/Generation_Performance.csv',
        index=False,
    )


start_time = time.time()
Log.printString('The Script has started at ' +
                str(time.asctime(time.localtime())) + '\n',
                log_path,
                access="w",
                buffer=True)

epoch_win = []

df_training = pd.DataFrame()
for path in glob.glob(os.path.join(input_dir, 'epoch_*.csv')):
    df_temp = pd.read_csv(path, dtype=str)
    num_games = len(df_temp)
    log_str = 'File: ' + path + '\n\tLength: ' + str(num_games) + '\n'

    df_temp.WinLoss = df_temp.WinLoss.astype(float).astype(int)
    calc_win = len(df_temp[df_temp.WinLoss > 0]) / (
        len(df_temp[df_temp.WinLoss > 0]) + len(df_temp[df_temp.WinLoss < 0]))
    print(df_temp.WinLoss.value_counts())
Example #9
0
    if DEBUG:
      df = df.append(s.toDict(), ignore_index=True)
    else:
      log_str += '\tPlayerHand and DealerHand fields have been removed\n'
      df = df.append(s.toDict(), ignore_index=True)

  log.printString(log_str, log_path, access='a')

  return df

# =============================================
#               Main
# =============================================
if __name__ == "__main__":
  start_time = time.time()
  log.printString('Training Started at ' + str(time.asctime(time.localtime())) + '\n', log_path, access='w', buffer=True)

  try:
    os.remove(out_path)
  except Exception as ex:
    pass

  df = pd.DataFrame()

  cardDeck = CardDeck(num_deck)
  TotalCount = 0

  for i in range(num_training):
    log.printString('\nTraining Number ' + str(i) + ':\n', log_path, access='a')
    
    if (cardDeck.NumDeck * 52) - cardDeck.currentCard <= 20: