Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

python - Is there a way to generate all possible subsets of a set?

** I will preface this with saying that there is a very good chance that I am going about this whole thing the wrong way! **

That said....I am trying to build a program that will minimise the amount of waste from a given number of input lengths.

The scenario is to enter the lengths of timber you need, and then the program will compare them against the stock lengths of timber, and return to you the optimum combination of stock lengths you should purchase. For the purpose of this question I have just put the input lengths into the code.

I am currently working out a list with all the possible permutations of the set of numbers:

import itertools
from itertools import combinations, permutations


standard_lengths = [2400, 3000, 3600, 4000]
input_lengths = []
optimum_scenario_waste = 0
optimum_scenario_cut_list = []

inputs = [2700 ,3500, 1200, 1900, 200]

order_combos = (list(permutations(inputs, len(inputs))))

I am then trying to write a piece of code that will give me all the possible subsets of these variations

ie for a permutation [3500, 1900, 1200, 200, 2700]

I would be looking to get: [(3500), (1900), (1200), (200), (2700)] [(3500, 1900), (1200), (200), (2700)] [(3500, 1900), (1200, 200), (2700)] etc....

The main problem I don't understand here is how to create a varying number of subsets (or subsets which equal 0, ie if len(inputs) = 5 = number of subsets, how to have some subsets = 0 in some instances and then subsets with differing number of elements).

This is what i've currently done and I know its wrong and doesn't work and is only approximate for an input of 5.

sum_combos = []

for ordered_list in order_combos:
    for x in range(len(ordered_list)):
        first_combo = ordered_list[:x+1]
        second_combo = ordered_list[x+1:-1]
        third_combo = [ordered_list[-1]]
        if len(second_combo) > 0:
            sum_combos.append([first_combo, second_combo, third_combo])
  1. I am then calculating the waste from each subset (or "combination of timber") and then summing the total waste of each permutation of the inputs. I am then comparing this against the other permutations and then returning the combination of standard lengths corresponsing to the permutation of subsets with the lowest waste.

I know this last bit is messy but it seems to be working

THanks very much!

combo_sum_totals = []

for combo in sum_combos:
    individual_combo_sum_total = []
    for sum_term in combo:
        sum_of_constituents = 0
        for x in sum_term:
            sum_of_constituents += x
        individual_combo_sum_total.append(sum_of_constituents)
    combo_sum_totals.append(individual_combo_sum_total)

    
def addition(summed_length):
    for i in range(len(standard_lengths)):
        if summed_length > 4000:
            waste = 99999999
            optimum_length = 0
            return [waste, optimum_length]
        elif standard_lengths[i] >= summed_length:
            waste = standard_lengths[i] - summed_length
            optimum_length = (standard_lengths[i])
            return [waste, optimum_length]
            break
        else:
            pass


waste_lengths_list = []

for sum_terms in combo_sum_totals:
    waste_lengths_list_individual = []
    for sum_term in sum_terms:
        waste, optimum_length = addition(sum_term)
        waste_lengths_list_individual.append([sum_term, waste, optimum_length])
    waste_lengths_list.append(waste_lengths_list_individual)

lowest_waste = 9999999
best_option = 0

for individual_lengths in waste_lengths_list:
    individual_waste_total = 0
    for individual_waste in individual_lengths:
        individual_waste_total += individual_waste[1]
    print(individual_waste_total)
    if individual_waste_total < lowest_waste:
        lowest_waste = individual_waste_total
        best_option = individual_lengths


print(best_option)
print(lowest_waste)
question from:https://stackoverflow.com/questions/65846540/is-there-a-way-to-generate-all-possible-subsets-of-a-set

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...