简体   繁体   中英

How to derive efficient way in one list

suppose there are N1 ~ N9, and efficiency of them:

efficiency = [0.12, 0.23, 0.34, 0.45, 0.56, 0.67, 0.78, 0.89, 1]

cost of them:

cost = [23371, 48543, 98714, 194859, 220429, 316429, 348286, 390143, 414714]

there is assumption

efficiency[A] > efficiency[b]  <=>  cost[A] > cost[B]

How can i derive the cheapest method to get total efficiency >=1 with lesser then 5 elements (repeating allowed)?

Use this algorithm:

import itertools
efficiency = [0.12, 0.23, 0.34, 0.45, 0.56, 0.67, 0.78, 0.89, 1]
cost = [23371, 48543, 98714, 194859, 220429, 316429, 348286, 390143, 414714]
for item_num in range(1,5):
    options = list(itertools.combinations_with_replacement(range(len(efficiency)), item_num))
    valid_options = [o for o in options if sum([efficiency[o[i]] for i in range(len(o))]) >= 1]
    valid_costs = {vo: sum([cost[vo[i]] for i in range(len(vo))]) for vo in valid_options}
    item_num_best_option = min(valid_costs, key=valid_costs.get)
    item_num_best_cost = valid_costs[item_num_best_option]
    if item_num > 1:
        if item_num_best_cost < best_cost:
            best_option, best_cost = item_num_best_option, item_num_best_cost
    else:
        best_option, best_cost = item_num_best_option, item_num_best_cost
best_option

Output:

(1, 1, 1, 2) # Meaning use item 1 once, and item 2 three times

Concept:

You create all the possible options with itertools allowing repeatations. Then You filter out the options that don't reach efficiency 1. Then create a dictionary that calculates and holds the cost of each option. Then find the option with the least cost. And the whole process is inside a loop that checks all the combination numbers between 1 and 4 (inclusive).

Doing some research, it seems an integer linear programming problem (binary in this case). Because the objective function and the constraints are linear. Integer programming problem are know for being NP-complete . And NP-complete problems can be solved by a restricted class of brute force search algorithms .

Given your problem, doing a simplification for a toy problem, in which you have only 2 values (called y1 and y2 here) and 2 costs (called x1 and x2 here), using max 2 values, one could formalize it in this way:

在此处输入图像描述

Where b1, b2, b3, b4 are the binary variables to set (0 or 1).

You can solve this problem (with exact solution ) only by brute force. Using your real example, given all the useful combination with k repetition, with at least 1 value and max 4 values, you need to explore ( using number of combinations with repetition ):

在此处输入图像描述

714 solutions . Which is feasible on a modern machine, and the cheapest way to solve your problem.

Alternatively one could use an euristic algorithm to get an approximate solution .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM