All unique combination script

I tried to do like this :

import random
from collections import defaultdict, deque

def generate_schedule(num_teams, num_days):
    if num_teams < 3:
        raise ValueError("Le nombre d'équipes doit être au moins de 3 pour créer des poules.")
    
    teams = list(range(1, num_teams + 1))
    best_schedule = None
    best_matches = None
    best_duplicates = None

    def create_initial_day(teams):
        random.shuffle(teams)
        day = []
        
        # Create pools of 3 teams as much as possible
        while len(teams) >= 3:
            day.append(teams[:3])
            teams = teams[3:]
        
        # Add one extra team to each pool of 3 if there are remaining teams
        for pool in day:
            if teams:
                pool.append(teams.pop(0))
        
        return day

    def evaluate_schedule(schedule):
        matches = defaultdict(int)
        for day in schedule.values():
            for pool in day:
                for i in range(len(pool)):
                    for j in range(i + 1, len(pool)):
                        match = tuple(sorted([pool[i], pool[j]]))
                        matches[match] += 1
        return matches

    def swap_teams_tabu(schedule, tabu_list):
        new_schedule = schedule.copy()
        day = random.choice(list(new_schedule.keys()))
        pool = random.choice(range(len(new_schedule[day])))
        team1, team2 = random.sample(range(len(new_schedule[day][pool])), 2)
        new_schedule[day][pool][team1], new_schedule[day][pool][team2] = new_schedule[day][pool][team2], new_schedule[day][pool][team1]
        tabu_list.append(((day, pool, team1), (day, pool, team2)))  # Ajouter le mouvement à la liste tabou
        if len(tabu_list) > 10:  # Limiter la taille de la liste tabou
            tabu_list.popleft()
        return new_schedule

    def minimize_duplicates_tabu(initial_schedule):
        current_schedule = initial_schedule
        best_matches = evaluate_schedule(initial_schedule)
        tabu_list = deque(maxlen=10)  # Liste tabou pour éviter les mouvements répétés
        iterations = 1000  # Nombre d'itérations
        for _ in range(iterations):
            new_schedule = swap_teams_tabu(current_schedule, tabu_list)
            new_matches = evaluate_schedule(new_schedule)
            if len(new_matches) < len(best_matches):
                current_schedule = new_schedule
                best_matches = new_matches
        return current_schedule, best_matches

    # Exécuter l'algorithme plusieurs fois
    for _ in range(300):  # Choisissez le nombre d'itérations
        initial_schedule = {day: create_initial_day(teams.copy()) for day in range(1, num_days + 1)}
        current_schedule, current_matches = minimize_duplicates_tabu(initial_schedule)
        if best_schedule is None or len(current_matches) < len(best_matches):
            best_schedule = current_schedule
            best_matches = current_matches

    return best_schedule, best_matches

# Fonction pour afficher le planning et les doublons
def print_schedule(schedule, duplicates):
    print("Planning:")
    for day, pools in schedule.items():
        print(f"Jour {day}: {pools}")

    print("nDoublons:")
    sum = 0
    for match, count in duplicates.items():
        if count > 1:
            sum += count
        print(f"Les équipes {match} se rencontrent {count} fois")
    print(f"Total du nombre de rencontre en doublon : {sum}")

# Demander les entrées de l'utilisateur
while True:
        # Demander les entrées de l'utilisateur
    if 'num_teams' not in locals():
        num_teams = int(input("Entrez le nombre d'équipes: "))
    if 'num_days' not in locals():
        num_days = int(input("Entrez le nombre de journées: "))
    schedule, duplicates = generate_schedule(num_teams, num_days)
    print_schedule(schedule, duplicates)

    # Demander à l'utilisateur s'il veut entrer de nouvelles variables ou réexécuter le script avec les mêmes variables
    choice = input("Voulez-vous :n1. Entrer de nouvelles variablesn2. Réexécuter le script avec les mêmes variablesn3. QuitternEntrez 1,2 ou 3: ").strip()
    if choice == '1':
        num_teams = int(input("Entrez le nombre d'équipes: "))
        num_days = int(input("Entrez le nombre de journées: "))
    elif choice != '2':
        break
    elif choice == '3':
        break

but it didn’t worked

I try to find a way to achieve this :

I let the user enter number of day and teams

I need to create as many as group of 3 i can and if i can’t many of group of 3 + some group of 4

for 20 team : 35 + 24

then i need to create for each number of day a unique combination if possible :

exemple :
Day 1 : [1,2,3] , [4,5,6] , [7,8,9]
Day 2 : 1 cannot be with 2 & 3, 2 cannot be with 1&3, 4 cannot be with 5 & 6 and so on
Day 3 : 1 cannot be with number on day 1 & 2

And so on

But when there is no more possibilities, optimize the duplicates as much as possible

how can i achieve that ?

i try python script but not getting unique for first days :

New contributor

user25270602 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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