def test_make_decision(): dec = Decisions() decision, decision_point, sims = dec.make_decision( 'E', set(['S13', 'S14', 'H11']), [('W', 'S12'), ('N', 'H12')], [], random=True) # This will only work for naive decision maker of picking first valid card assert (decision in ['S13', 'S14']) assert_equal(decision_point, True) decision, decision_point, sims = dec.make_decision( 'E', set(['H13', 'H14', 'H11']), [('W', 'S12'), ('N', 'H12')], ['H13'], random=True) # This will only work for naive decision maker of picking first valid card assert (decision in ['H14', 'H13', 'H11']) assert_equal(decision_point, True) decision, decision_point, sims = dec.make_decision( 'E', set(['S13', 'S14', 'H11']), [('W', 'S12'), ('N', 'H12')], ['S13'], random=True) # This will only work for naive decision maker of picking first valid card assert_equal(decision, 'S14') assert_equal(decision_point, False)
def test_play_to_leaf_with_sims(): d = Deal(seed=0) disp = Displayer() dec = Decisions() sim = Simulator() e = GameEngine(disp, dec, sim) sims = sim.load_sims(0) d = e.play_to_leaf(d, sims, random=False) assert_equal(d.full_history[0][1], d.trick_tally)
def test_play_to_all_leaves(): d = Deal(seed=0) d.make_lead() # print(d.original_hands) disp = Displayer() dec = Decisions() sim = Simulator() e = GameEngine(disp, dec, sim) e.play_to_all_leaves(d) assert_equal(d.trick_no, 5) assert_equal(d.card_no, 17) assert_equal(d.current_trick, []) # we save this number from a previous run. Good to check we always traverse the whole tree assert_equal(d.leaf_nodes, 832)
def test_find_all_layouts_and_run_sims(): deal = { 'N': set(['C14', 'C13', 'C12', 'C11']), 'E': set(['S14', 'S13', 'H12', 'H11']), 'S': set(['D14', 'D13', 'H14', 'H13']), 'W': set(['D12', 'D11', 'S12', 'S11']) } d = Deal(deal=deal) sim = Simulator() dec = Decisions() disp = Displayer() e = GameEngine(disp, dec, sim) d.make_lead('D12') layouts = sim.find_layouts(d) # This should be the case because west has played a card already and north/south haven't assert (len(layouts[2]) > len(layouts[0]))
def test_play_to_leaf(): deal = { 'N': set(['C14', 'C13', 'C12', 'C11']), 'E': set(['S14', 'S13', 'H12', 'H11']), 'S': set(['D14', 'D13', 'H14', 'H13']), 'W': set(['D12', 'D11', 'S12', 'S11']) } d = Deal(deal=deal) disp = Displayer() dec = Decisions() sim = Simulator() e = GameEngine(disp, dec, sim) e.play_to_leaf(d) assert_equal(d.trick_no, 5) assert_equal(d.card_no, 17) assert_equal(d.current_trick, []) assert_equal(d.leaf_nodes, 1)
def test_generate_possible_layouts(): d = Deal(seed=0) disp = Displayer() dec = Decisions() sim = Simulator() e = GameEngine(disp, dec, sim) layouts = sim.generate_layouts('NS', set(['D12', 'D11', 'S12', 'S11']), set(['C14', 'C13', 'C12', 'C11']), d.all_cards, lead=set([])) assert_equal(len(layouts), scipy.special.comb(8, 4)) my_layout = { 'N': set(['C14', 'C13', 'C12', 'C11']), 'E': set(['S14', 'S13', 'H12', 'H11']), 'S': set(['D12', 'D11', 'S12', 'S11']), 'W': set(['D14', 'D13', 'H14', 'H13']) }
def __init__(self): """ Initialise """ self.decisions = Decisions() self.action_rules = ()
deal.save_leaf_node() return deal def play_to_all_leaves(self, deal): # Play hand for the first time, reaching our first leaf node and noting down all other # decision nodes in self.decision_points deal = self.play_to_leaf(deal, random=True) # While there are still unexplored areas of game tree, go back to most recent decision # and make another one while deal.decision_points: last_decision_position = deal.decision_points[-1] deal.restore_position(last_decision_position) # Play the hand again from a specific position, noting down the card number and card # of the last decision made deal = self.play_to_leaf(deal, random=True) return deal.full_history if __name__ == '__main__': decisions = Decisions() simulator = Simulator() game = Engine(decisions, simulator) # game.play_to_leaf() # game.play_to_all_leaves()
def autofis_onecv(file_zip, file_train, file_test, parameters): # General parameters t_norm = parameters[3] max_size_of_premise = parameters[5] association_method = parameters[11] aggregation_method = parameters[12] # Gathering parameters # Formulation parameters: par_area, par_over, par_pcd = toolfis.get_formulation_parameters(parameters) # 1. Lecture & Fuzzification out1 = toolfis.lecture_fuz_one_cv(file_zip, file_train, file_test, parameters) ux_train, cbin_train = out1[0] ux_test, cbin_test = out1[1] num_premises_by_attribute, premises_by_attribute, ref_attributes, premises_contain_negation = out1[2] freq_classes = out1[3] report = [] # To save our results try: # 3. Formulation f2 = Formulation(ux_train, cbin_train, ref_attributes, premises_by_attribute, num_premises_by_attribute, premises_contain_negation) # Inputs given by user arbol = f2.gen_ARB(max_size_of_premise, t_norm, par_area, par_over, par_pcd) status = [0 if not i[0] else 1 for i in arbol] sum_status = sum(status) if sum_status != len(arbol): if sum_status == 0: raise ValueError("Error in Formulation Module. Any premise survived. " "Sorry, you can not continue in the next stage." "\nTry to change the configuration") else: arb = [i for i in arbol if i[0]] arbol, arb = arb, arbol number_classes = cbin_train.shape[1] report.append("\nFormulation:\n-----------------") report.append("Elementos acorde a la profundidad " + str(len(arbol)) + " del arbol") for i in range(len(arbol)): report.append('Profundidad ' + str(i + 1) + ': ' + str(arbol[i][1].shape)) # print 'Profundidad ' + str(i + 1) + ': ' + str(arbol[i][1].shape) # 4. Association: ex-Division f3 = Association(arbol, cbin_train) premises_ux_by_class = f3.division(association_method) status = [0 if not i[0] else 1 for i in premises_ux_by_class] if sum(status) != number_classes: raise ValueError("Error in Division Module. Some classes did not get premises. " "Sorry, you can not continue in the next stage." "\nTry to change the configuration") # 5. Aggregation: f4 = Aggregation(premises_ux_by_class, cbin_train) output_aggregation = f4.aggregation(aggregation_method) premises_weights_names = output_aggregation[0] estimation_classes = output_aggregation[1] status = [0 if not i[0] else 1 for i in premises_weights_names] if sum(status) != number_classes: raise ValueError("Error in Aggregation Module. Some classes did not get premises. " "Sorry, you can not continue in the next stage." "\nTry to change the configuration") final_premises_classes = [] report.append("\n\nPremises:\n=========") for i in range(len(premises_weights_names)): report.append("Premises of Class " + str(i) + ": " + str(premises_weights_names[i][0])) final_premises_classes.append(premises_weights_names[i][0]) report.append("weights_" + str(i) + ": " + str(premises_weights_names[i][1].T)) # 6. Decision: f5 = Decisions(estimation_classes, freq_classes) train_bin_prediction = f5.dec_max_pert() # 7. Evaluation f6 = Evaluation(premises_weights_names, final_premises_classes, freq_classes) metrics_train = f6.eval_train(cbin_train, train_bin_prediction) metrics_test = f6.eval_test(cbin_test, ux_test, t_norm) report.append("\nEvaluation Training:\n---------------------------") report.append("Accuracy on train dataset: " + str(metrics_train[0])) report.append("AUC in train dataset: " + str(metrics_train[1])) report.append("Recall: " + str(metrics_train[3])) report.append('Confusion matrix:\n' + str(metrics_train[2])) report.append("\nEvaluation Testing:\n---------------------------") report.append("Accuracy on test dataset: " + str(metrics_test[0])) report.append("AUC in test dataset: " + str(metrics_test[1])) report.append("Recall: " + str(metrics_test[3])) report.append("Confusion matrix:\n" + str(metrics_test[2])) # Metrics to eval: accuracy_test, auc_test, # [num_regras, total_rule_length, tamano_medio_das_regras]] metricas = [1, [metrics_train[0], metrics_test[0], metrics_train[1], metrics_test[1], metrics_test[4]]] except ValueError as e: print e report = e # .append("\n" + str(e)) metricas = [0, "No se termino el proceso, se detuvo en algun etapa"] return report, metricas