Esempio n. 1
0
def createRandom():
    error = True
    while error == True:
        error = False
        # probeer random kaart te bouwen tot het lukt
        combinatie = Combination(aantalhuizen, aantalwater)
        random.shuffle(combinatie.houses)
        for i in range(len(combinatie.houses)):
            if combinatie.placeRandom(combinatie.houses[i], i) != True:
                error = True
        if error == False:
            combinatie.evalueer()
            temp = combinatie.evaluatie
            hoogstewaarde = temp[1]
            best = combinatie
            best.evalueer()
            plt.draw()
            plt.savefig('graph.png', dpi=300, bbox_inches='tight')
    return combinatie
Esempio n. 2
0
    def calAll(self):
        self.errs = [0] * 5
        bias = Bias(self.data, self.test)
        bias.calculateBias()
        answers, predicts = bias.predict()
        err = evaluationRMSE(answers, predicts)
        self.errs[0] = err
        print("Bias: %f" % err)

        similarity = Similarity(self.data, self.test)
        similarity.calculateBias()
        similarity.calcSimiMatrix()
        answers, predicts = similarity.predict()
        err = evaluationRMSE(answers, predicts)
        self.errs[1] = err
        print("Similarity: %f" % err)

        svd = SVD(self.data, self.test)
        svd.generaterMat()
        svd.calcSVD()
        answers, predicts = svd.predict()
        err = evaluationRMSE(answers, predicts)
        self.errs[2] = err
        print("SVD: %f" % err)

        matFactory = MatFactory(self.data, self.test)
        matFactory.train(20, 35)
        answers, predicts = matFactory.predict()
        err = evaluationRMSE(answers, predicts)
        self.errs[3] = err
        print("MatFactory: %f" % evaluationRMSE(answers, predicts))

        combination = Combination(self.data)
        combination.separateData()
        combination.calculate()
        combination.train(alpha = 0.01, iter = 10000)
        answers, predicts = combination.predict(self.test)
        err = evaluationRMSE(answers, predicts)
        self.errs[4] = err
        print("Combination: %f" % err)
        return self.errs
Esempio n. 3
0
    def run(self):
        self.iteration = self.iteration + 1

        # Choose best cluster to update
        ranks = self.get_ranks(self.clusters)
        remain_f_allocation = Combination(self.problem.remaining_evaluations,
                                          len(self.clusters),
                                          self.n_points,
                                          ranks,
                                          model='linear',
                                          debug=False).combination

        self.remain_f_allocation += np.array(remain_f_allocation)
        best_arm = np.argmax(self.remain_f_allocation)
        remain_f = np.amax(remain_f_allocation)

        if self.algo_type == 'CMA':
            if self.algos[best_arm].stop():
                self.remain_f_allocation[
                    best_arm] = -self.remain_f_allocation[best_arm]
                if self.verbose:
                    print('CMA-ES at cluster %d stops!!' % (best_arm))
                #draw_contour( function_id, clusters=bandit.clusters )
                return

        print('Update cluster %d' % best_arm)

        # TODO
        #self.remain_f_allocation[best_arm] = 0
        self.remain_f_allocation[best_arm] -= len(
            self.clusters[best_arm].population)

        # Transform data points to [0-1] space and resume algorithm
        original_points = [
            p.phenome for p in self.clusters[best_arm].population
        ]
        trans_points = self.clusters[best_arm].transform(original_points).clip(
            0, 1)
        fitness_values = [
            p.objective_values for p in self.clusters[best_arm].population
        ]

        new_trans_positions = self.update_positions(best_arm, trans_points,
                                                    fitness_values)

        # Update cluster.population
        new_positions = self.clusters[best_arm].transform_inverse(
            new_trans_positions)
        solutions = [Individual(position) for position in new_positions]

        try:
            self.problem.batch_evaluate(solutions)
        except ResourcesExhausted:
            self.should_terminate = True
            return

        self.clusters[best_arm].population = sorted(
            solutions, key=attrgetter('objective_values'))
        ranks = self.get_ranks(self.clusters)
        for i, cluster in enumerate(self.clusters):
            cluster.ranks = ranks[i]

        self.best_solution = self.update_best_solution()

        # Check if need to recluster
        trans_mean_position = np.mean(new_trans_positions, axis=0)
        best_point = self.clusters[best_arm].population[0].phenome
        trans_best_point = self.clusters[best_arm].transform([best_point
                                                              ]).clip(0, 1)[0]
        margin = 0.05
        if ((trans_best_point < margin).any() or (trans_best_point > 1-margin).any()) and \
           ((trans_mean_position < 2*margin).any() or (trans_mean_position > 1-2*margin).any()):

            if self.verbose:
                print('Reclustering...')
                print('due to best_point of cluster %d at: %r' %
                      (best_arm, trans_best_point))
                print('                      and mean at: %r' %
                      (trans_mean_position))
            if args.draw_contour > 0:
                draw_contour(self.function_id, clusters=self.clusters)
            self.shift_matrix(best_arm, best_point)
            if args.draw_contour > 0:
                draw_contour(self.function_id, clusters=self.clusters)
            self.recluster(len(self.clusters))
            if args.draw_contour > 0:
                draw_contour(self.function_id, clusters=self.clusters)

        self.update_statistics(best_arm)
Esempio n. 4
0
 def init_combination_info(self):
     trading_info = self.comb_info_client.get()
     for _, code_id in trading_info['code'].iteritems():
         if str(code_id) not in self.combination_objs:
             self.combination_objs[str(code_id)] = Combination(
                 code_id, self.dbinfo)
Esempio n. 5
0
 def add(self, prev_app):
     a = Combination()
     tkinter.messagebox.showinfo("ADDED", "SUCCESSFULY ADDED TO DATABASE")
     prev_app.destroy()
     self.adminMain()
Esempio n. 6
0
# bouwen grafiek
graphcolour = 'r'
plt.figure(1)
plt.xlabel('Iteraties')
plt.ylabel('Waarde in Euro\'s')
plt.suptitle("Hoogste huidige waarde: " + str(hoogstewaarde) + " Huidige iteratie: " + str(iteratie), fontsize=13)
filename = 'output/' + str(sys.argv[3]) + "/" + str(sys.argv[1]) + str(sys.argv[2])
graphtitle = str(sys.argv[2]) + " " + str(sys.argv[1])

if sys.argv[2] == "randsample":
    plt.title('Amstelhaege Random Sampling')
    while True:
        error = False
        update = False
        combinatie = Combination(aantalhuizen, aantalwater)
        for i in range(len(combinatie.houses)):
            if combinatie.placeRandom(combinatie.houses[i], i) != True:
                error = True
        if error == False:
            combinatie.evalueer()
            temp = combinatie.evaluatie
            if temp[criterium] > hoogstewaarde:
                hoogstewaarde = temp[criterium]
                best = combinatie
                best.evalueer()
                mapMaken(best.houses, filename, graphtitle, iteratie, hoogstewaarde)
                update = True
        index = int(combinatie.evaluatie[criterium] / waardeperbakje)
        if error != True:
            bakjes[index] += 1
Esempio n. 7
0
 def create_obj(self, code):
     try:
         Combination(code, should_create_db=True)
         return (code, True)
     except Exception as e:
         return (code, False)
Esempio n. 8
0
    def test_pairs(self):

        cards = [Card(name='2', suit='Pagoda'), Card(name='2', suit='Sword')]
        combination = Combination(cards_list=cards)
        self.assertEqual(combination.type, 'PAIR')
        self.assertEqual(combination.level, 2)
Esempio n. 9
0
 def test_straight_with_phoenix(self):
     cards = '2_Sw, 3_Pa, 5_Pa, Phoenix, 6_Pa'
     combination = Combination(cards_string=cards)
     self.assertEqual(combination.type, 'STRAIGHT')
Esempio n. 10
0
    def test_fullhouse(self):

        cards = ' 2_Pa, 2_Sw, 2_Ja, 5_Pa, 5_Sw'
        combination = Combination(cards_string=cards)
        self.assertEqual(combination.type, 'FULLHOUSE')
Esempio n. 11
0
 def test_to_fix(self):
     cards = 'Phoenix, 2_Pa, 3_Pa, 4_Pa, 5_Pa, 5_Sw, 5_Ja, 5_St'
     combination = Combination(cards_string=cards)
     self.assertIsNone(combination.type)
Esempio n. 12
0
    def test_pairs_with_phoenix(self):

        cards = [Card(name='2', suit='Pagoda'), Phoenix()]
        combination = Combination(cards_list=cards)
        self.assertEqual(combination.type, 'PAIR')
        self.assertEqual(combination.level, 2)
Esempio n. 13
0
    def test_wrong_pairs(self):

        cards = [Card(name='2', suit='Pagoda'), Card(name='3', suit='Sword')]
        Combination(cards_list=cards)
Esempio n. 14
0
    def generate_optimal_code(self):
        logger.info('Start to combine the Compar combination')
        optimal_loops_data = []

        # copy final results into this folder
        compar_combination_folder_path = self.create_combination_folder(
            self.COMPAR_COMBINATION_FOLDER_NAME,
            base_dir=self.working_directory)
        final_files_list = self.make_absolute_file_list(
            compar_combination_folder_path)

        for file_id_by_rel_path, loops in self.files_loop_dict.items():
            current_file = {
                "file_id_by_rel_path": file_id_by_rel_path,
                'optimal_loops': []
            }
            for loop_id in range(1, loops[0] + 1):
                start_label = Fragmentator.get_start_label() + str(loop_id)
                end_label = Fragmentator.get_end_label() + str(loop_id)
                try:
                    current_optimal_id, current_loop = self.db.find_optimal_loop_combination(
                        file_id_by_rel_path, str(loop_id))
                    # update the optimal loops list
                    current_loop['_id'] = current_optimal_id
                    current_file["optimal_loops"].append(current_loop)
                except e.DeadCodeFile:
                    current_file["dead_code_file"] = True
                    break
                except e.DeadCodeLoop:
                    current_file["optimal_loops"].append({
                        '_id':
                        Database.SERIAL_COMBINATION_ID,
                        'loop_label':
                        str(loop_id),
                        'dead_code':
                        True
                    })
                    current_optimal_id = Database.SERIAL_COMBINATION_ID

                # if the optimal combination is the serial => do nothing
                if current_optimal_id != Database.SERIAL_COMBINATION_ID:
                    current_optimal_combination = Combination.json_to_obj(
                        self.db.get_combination_from_static_db(
                            current_optimal_id))
                    current_combination_folder_path = self.create_combination_folder(
                        ComparConfig.OPTIMAL_CURRENT_COMBINATION_FOLDER_NAME,
                        base_dir=self.working_directory)
                    files_list = self.make_absolute_file_list(
                        current_combination_folder_path)
                    current_comp_name = current_optimal_combination.compiler_name

                    # get direct file path to inject params
                    src_file_path = list(
                        filter(
                            lambda x: x['file_id_by_rel_path'] ==
                            file_id_by_rel_path, files_list))
                    src_file_path = src_file_path[0]['file_full_path']

                    # parallelize and inject
                    self.parallel_compilation_of_one_combination(
                        current_optimal_combination,
                        current_combination_folder_path)

                    # replace loop in c file using final_files_list
                    target_file_path = list(
                        filter(
                            lambda x: x['file_id_by_rel_path'] ==
                            file_id_by_rel_path, final_files_list))
                    target_file_path = target_file_path[0]['file_full_path']

                    Compar.replace_loops_in_files(src_file_path,
                                                  target_file_path,
                                                  start_label, end_label)
                    Compar.add_to_loop_details_about_comp_and_combination(
                        target_file_path, start_label, current_optimal_id,
                        current_comp_name)
                    sleep(1)  # prevent IO error
                    shutil.rmtree(current_combination_folder_path)
            optimal_loops_data.append(current_file)

        # remove timers code
        Timer.remove_timer_code(
            self.make_absolute_file_list(compar_combination_folder_path))
        # inject new code
        Timer.inject_timer_to_compar_mixed_file(
            os.path.join(compar_combination_folder_path,
                         self.main_file_rel_path),
            compar_combination_folder_path)
        self.generate_summary_file(optimal_loops_data,
                                   compar_combination_folder_path)
        try:
            logger.info('Compiling Compar combination')
            self.compile_combination_to_binary(compar_combination_folder_path,
                                               inject=False)
            job = Job(
                compar_combination_folder_path,
                Combination(Database.COMPAR_COMBINATION_ID,
                            ComparConfig.MIXED_COMPILER_NAME, None), [])
            logger.info('Running Compar combination')
            self.execute_job(job, self.serial_run_time)
        except Exception as ex:
            msg = f'Exception in Compar: {ex}\ngenerate_optimal_code: cannot compile compar combination'
            self.save_combination_as_failure(Database.COMPAR_COMBINATION_ID,
                                             msg,
                                             compar_combination_folder_path)
        logger.info(
            LogPhrases.NEW_COMBINATION.format(
                Database.FINAL_RESULTS_COMBINATION_ID))
        # Check for best total runtime
        best_runtime_combination_id = self.db.get_total_runtime_best_combination(
        )
        best_combination_obj = None
        if best_runtime_combination_id != Database.COMPAR_COMBINATION_ID:
            logger.info(
                f'Combination #{best_runtime_combination_id} is more optimal than Compar combination'
            )
            best_combination_obj = Combination.json_to_obj(
                self.db.get_combination_from_static_db(
                    best_runtime_combination_id))
            final_results_folder_path = self.create_combination_folder(
                self.FINAL_RESULTS_FOLDER_NAME, self.working_directory)
            try:
                if best_runtime_combination_id != Database.SERIAL_COMBINATION_ID:
                    self.parallel_compilation_of_one_combination(
                        best_combination_obj, final_results_folder_path)
                self.compile_combination_to_binary(final_results_folder_path)
                summary_file_path = os.path.join(
                    compar_combination_folder_path,
                    ComparConfig.SUMMARY_FILE_NAME)
                summary_file_new_path = os.path.join(
                    final_results_folder_path, ComparConfig.SUMMARY_FILE_NAME)
                shutil.move(summary_file_path, summary_file_new_path)
            except Exception as ex:
                raise Exception(
                    f"Total runtime calculation - The optimal file could not be compiled, combination"
                    f" {best_runtime_combination_id}.\n{ex}")
        else:
            logger.info(f'Compar combination is the optimal combination')
            final_folder_path = os.path.join(self.working_directory,
                                             self.FINAL_RESULTS_FOLDER_NAME)
            if os.path.exists(final_folder_path):
                shutil.rmtree(final_folder_path)
            shutil.copytree(compar_combination_folder_path, final_folder_path)
        # remove compar code from all the files in final result folder
        final_folder_path = os.path.join(self.working_directory,
                                         self.FINAL_RESULTS_FOLDER_NAME)
        Timer.remove_timer_code(
            self.make_absolute_file_list(final_folder_path))
        final_combination_results = self.db.get_combination_results(
            best_runtime_combination_id)
        if final_combination_results:
            final_combination_results[
                '_id'] = Database.FINAL_RESULTS_COMBINATION_ID
            final_combination_results[
                'from_combination'] = best_runtime_combination_id
            self.db.insert_new_combination_results(final_combination_results)
            with open(
                    os.path.join(final_folder_path,
                                 Timer.TOTAL_RUNTIME_FILENAME), 'w') as f:
                f.write(str(final_combination_results['total_run_time']))
            self.update_summary_file(
                final_folder_path, best_runtime_combination_id,
                final_combination_results['total_run_time'],
                best_combination_obj)
        # format all optimal files
        self.format_c_files([
            file_dict['file_full_path']
            for file_dict in self.make_absolute_file_list(final_folder_path)
        ])
        self.db.remove_unused_data(Database.COMPAR_COMBINATION_ID)
        self.db.remove_unused_data(Database.FINAL_RESULTS_COMBINATION_ID)
        final_result_speedup, final_result_runtime = self.db.get_final_result_speedup_and_runtime(
        )
        logger.info(
            LogPhrases.FINAL_RESULTS_SUMMARY.format(final_result_speedup,
                                                    final_result_runtime))
        if self.clear_db:
            self.clear_related_collections()
        self.db.close_connection()
Esempio n. 15
0
    elif card.flag == 1:
        return card.color + '_' + 'reverse'
    elif card.flag == 2:
        return card.color + '_' + 'skip'
    elif card.flag == 3:
        return card.color + '_' + '+2'
    elif card.flag == 4:
        return 'black_wildcard'
    elif card.flag == 5:
        return 'black_+4'


game = UNO_Game(Deck())
#default is two control players
game.player1 = Heuristic_v1(game, 'P1')
game.player2 = Combination(game, 'P2')
#options for different kinds of AIs to use
if len(sys.argv) > 1:
    if sys.argv[1] == 'bestfirst':
        print('cannot set player 1 to bestfirst, ai not yet implemented')
if len(sys.argv) > 2:
    if sys.argv[2] == 'bestfirst':
        print('cannot set player 1 to bestfirst, ai not yet implemented')

game.turn = game.player1  #P1 gets first turn

print('size of', game.player1.name, 'hand is',
      str(len(game.player1.hand.cards)))
print('size of', game.player2.name, 'hand is',
      str(len(game.player2.hand.cards)))
print('size of discard pile is', str(len(game.discard_pile.cards)))
Esempio n. 16
0
    def find_all_straights(cards: Cards):
        #TODO fix levels
        #TODO, put the level in there to differentiate phoenix at start and end
        #TODO Sort the combinations
        # remove Dog and Dragon from any straights
        cards = cards - Dog() - Dragon()

        buckets = Cards.bucketize_hands(cards.cards)
        power_values = buckets.keys()
        possible_straights_power_values = []

        # Get all possible power combinations
        for start in range(Mahjong().power, Dragon().power):

            length = 0
            current_straight_values = []

            for next_value in range(start, Dragon().power):
                found = False
                new_value = None

                if next_value in power_values:
                    found = True
                    new_value = next_value

                elif Phoenix().power in power_values and Phoenix(
                ).power not in current_straight_values:
                    if next_value != 1:
                        found = True
                        new_value = Phoenix().power

                if found and new_value not in current_straight_values:
                    current_straight_values.append(new_value)
                    length += 1
                    if length >= 5:
                        possible_straights_power_values.append(
                            current_straight_values.copy())

                elif not found:
                    break

        # Now that we have the powers, we get all possible straights
        straights = []
        for straight in possible_straights_power_values:
            straight_cards = [buckets[power] for power in straight]
            for combinations in itertools.product(*straight_cards):
                # straights.append(Cards(cards_list=list(combinations)))
                straights.append(Combination(cards_list=list(combinations)))

        # We replace the phoenix in all the straights where we can
        new_straights = []
        for straight in straights:
            if Phoenix() not in straight.cards:
                for card in straight.cards:
                    if not card == Mahjong():
                        new_cards = straight - card + Phoenix()
                        new_combo = Combination(cards_list=new_cards.cards)
                        new_straights.append(new_combo)
                        # new_straights.append(straight - card + Phoenix())
        straights.extend(new_straights)
        straights.sort()
        return straights