예제 #1
0
def Kbundle_QP_relaxation(mu_vec, Cov_mat, alpha, K, separate_profit_vec,
                          mandatory_products):
    N = len(mu_vec)
    # M is the parameter for the number of candidates. When M is small, the remaining gap is more important and reducing the variance is more important when M is large.
    gap_vec = [None] * N

    for i in range(N):
        gap_vec[i] = (mu_vec[i] - separate_profit_vec[i])

    sorted_indices = [
        i[0]
        for i in sorted(enumerate(gap_vec), key=lambda x: x[1], reverse=True)
    ]

    # Insere os produtos obrigatórios no início do índice de produtos mais lucrativos
    sorted_indices = [x for x in sorted_indices if x not in mandatory_products]
    for product_index in mandatory_products:
        sorted_indices.insert(0, product_index)

    max_profit = -math.inf
    max_bundle = None

    chunk = 20

    opt_profit_vec = []

    for M in range(K, min(3 * K + 1, N), max(2 * K // chunk, 1)):
        # print ('====considering the first', M, 'product with highest gap')
        opt_soln = Kbundle_candidate(sorted_indices[:M], Cov_mat, K,
                                     len(mandatory_products))

        # inner sampling
        opt_profit = -math.inf
        opt_bundle = None
        sample_size = 5  # we do randomized rounding for 5 times and obtain the best
        for i in range(sample_size):
            bundle_set, bundle_vec = random_set(opt_soln, K)

            variance_bundle = profit.variance(bundle_vec, Cov_mat)
            variance_opt = profit.variance(opt_soln, Cov_mat)
            # print (pi.bcolors.FAIL,'opt=', variance_opt, 'bundle=', variance_bundle  , pi.bcolors.ENDC)
            profit_ = profit.profit_bundle(bundle_set, bundle_vec, mu_vec,
                                           Cov_mat, alpha, separate_profit_vec)

            if profit_ > opt_profit:
                opt_profit = profit_
                opt_bundle = bundle_set

        opt_profit_vec.append(opt_profit)
        if opt_profit > max_profit:
            max_profit = opt_profit
            max_bundle = opt_bundle
    return max_bundle, max_profit
예제 #2
0
def opt_enumerate(mean_vec, cov_mat, k, separate_profit_vec):
    alpha = 0  # dummy
    N = len(mean_vec)
    products = list(range(N))
    max_profit = -math.inf
    opt_bundle = None
    for bundle_set in itertools.combinations(products, k):
        bundle_vec = [0] * N
        for i in bundle_set:
            bundle_vec[i] = 1
        revenue = profit.profit_bundle(bundle_set, bundle_vec, mean_vec, cov_mat, alpha, separate_profit_vec)
        if revenue > max_profit:
            max_profit = revenue
            opt_bundle = bundle_set

    return float(max_profit)
예제 #3
0
    bundle_size_vec = exp_setting.bundle_size_vec
    profit_vec = []
    sample_size = 50
    for k in bundle_size_vec:
        print('bundle size:', k)

        sample_revenue_vec = []
        for s in range(sample_size):
            choices = np.random.choice(N, k)
            bundle_set = set(choices)
            bundle_vec = [0] * N
            for i in range(N):
                if i in bundle_set:
                    bundle_vec[i] = 1

            revenue = profit.profit_bundle(bundle_set, bundle_vec, mean_vec,
                                           cov_mat, alpha, separate_profit_vec)
            print('profit:', revenue)
            sample_revenue_vec.append(revenue)

        profit_vec.append(sum(sample_revenue_vec) / len(sample_revenue_vec))

    with open('data/result_baseline_random_profits.json', 'w') as output_file:
        json.dump(
            {
                "bundle_size_vec": bundle_size_vec,
                "profit_vec": profit_vec
            },
            output_file,
            indent=4)