Python: Generating Combinations with Specific Constraints

We are working on a Python program where we need to calculate combinations over four different variables: ‘lp’, ‘futures’, ‘option_long’, and ‘option_short’. The ‘lp’ variable should iterate within the range defined by ‘lp_min’ and ‘lp_max’. However, we have specific constraints that need to be considered:

‘lp’ values must be generated from smaller to larger numbers.
Numbers cannot be repeated within a combination.
Our objective is to find the best combination for our model. We plan to execute this later in the program, where we save all the combinations generated. Meanwhile, the other three variables (‘futures’, ‘option_long’, and ‘option_short’) can all be the same within a combination, and their values can be in random order.

We need assistance in implementing a Python function that can generate these combinations while adhering to the specified constraints. Specifically, we’re looking for guidance on how to structure the iteration over ‘lp’ values and how to ensure that the combinations meet the given constraints. I have tried multiple options but lp’s is not generating right way. this is my last try:

Thank you for your help!

def find_best_combination(lp_min, lp_max, budget, tables_by_expiration):
    """ Find the best combination of parameters for each expiration date given a budget and tables of options data """
    
    try:
        best_combinations = {}  # Dictionary to store the best combinations of parameters for each expiration date
        
        for expiration_date, tables_by_exp in tables_by_expiration.items():
            # Initialize minimum deviation and best combination for the current expiration date
            min_deviation_exp = float('inf')
            best_combination_exp = None
            
            # Initialize lists to store the best combination for each LP position
            lp_list, futures_list, opt_long_list, opt_short_list = [], [], [], []
            
            # Fetch strikes
            strikes = [row['strike'] for _, row in tables_by_exp.iterrows()]
            
            for strike in strikes:
                # Initialize best combination for the current strike
                best_combination_strike = None
                min_deviation_strike = float('inf')
                
                # Loop over LP positions within the range of lp_min and lp_max
                for lp_position in range(lp_min, lp_max + 1):
                    lp_cost = calculate_cost(lp_position, budget_percent, df_market)
                    if lp_cost > budget:
                        print(f"{lp_position} LP cost exceeds budget for strike {strike}")
                        break  # Break if LP cost exceeds the budget
                        
                    # Generate combinations of futures, option_long, and option_short
                    for futures, opt_long, opt_short in itertools.product(range(1, 11), range(1, 11), range(1, 11)):
                        if lp_position < futures < opt_long < opt_short < lp_max:  # Ensuring LP position is smaller
                            if len(set([lp_position, futures, opt_long, opt_short])) == 4:
                                # Calculate deviation
                                deviation = abs(lp_cost - (futures + opt_long + opt_short))
                                if deviation < min_deviation_strike:
                                    min_deviation_strike = deviation
                                    best_combination_strike = (lp_position, futures, opt_long, opt_short)
                
                # Append the best combination for the current strike
                if best_combination_strike:
                    lp_list.append(best_combination_strike[0])
                    futures_list.append(best_combination_strike[1])
                    opt_long_list.append(best_combination_strike[2])
                    opt_short_list.append(best_combination_strike[3])
            
            # Store the best combination and minimum deviation for the current expiration date
            best_combinations[expiration_date] = {
                'lp': lp_list,
                'futures': futures_list,
                'opt_long': opt_long_list,
                'opt_short': opt_short_list,
                'min_deviation': min_deviation_strike
            }

        return best_combinations
    
    except Exception as e:
        print(f"Error in find_best_combination: {e}")
        return None
    
strike LP futures opt_long_list opt_short_list
$3,350 0.00 25 10 4
$3,325 10.00 25 10 4
$3,300 20.00 25 10 4
$3,275 30.00 25 10 4
$3,250 40.00 25 10 4
$3,225 50.00 25 10 4
$3,200 60.00 25 10 4
$3,175 70.00 25 10 4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật