How to resolve the algorithm Round-robin tournament schedule step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Round-robin tournament schedule step by step in the Picat programming language

Table of Contents

Problem Statement

A round-robin tournament is also known as an all-play-all-tournament; each participant plays every other participant once. For N participants the number of rounds is N-1 if N is an even number. When there are an odd number of participants then each round one contestor has no opponent (AKA as a "bye"). The number of rounds is N in that case. Write a program that prints out a tournament schedule for 12 participants (represented by numbers 1 to 12).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Round-robin tournament schedule step by step in the Picat programming language

Source code in the picat programming language

import sat.

main =>
  nolog,
  N = 12,
  tournament_cp(N, NumRounds,NumPairs,_,X,Bye),
  print_tournament(X,NumRounds,NumPairs,Bye),
  nl.

tournament_cp(N, NumRounds,NumPairs,Extras, X,Bye) =>
  % Adjust for odd number of players.
  % The bye (Dummy) player is N+1.
  if N mod 2 == 1 then
    N := N + 1,
    Bye = N
  end,

  NumRounds = N-1,
  NumPairs = N div 2,

  X = new_array(NumRounds,NumPairs,2),
  X :: 1..N,

  % ensure that all players play each other
  foreach(P1 in 1..N, P2 in P1+1..N)
    sum([X[Round,P,1] #= P1 #/\ X[Round,P,2] #= P2 : Round in 1..NumRounds, P in 1..NumPairs]) #= 1
  end,
  
  foreach(Round in 1..NumRounds)
    all_different([X[Round,I,J] : I in 1..NumPairs, J in 1..2]),
    
    % symmetry breaking
    % - all first players in increasing order
    increasing_strict([X[Round,I,1] : I in 1..NumPairs]),
    % - player 1 < player 2
    foreach(P in 1..NumPairs)
       X[Round,P,1] #< X[Round,P,2]
    end
  end,

  if Extras != [] then
    foreach([P1,P2,Round] in Extras)
      sum([X[Round,P,1] #= P1 #/\ X[Round,P,2] #= P2 : P in 1..NumPairs]) #= 1
    end
  end,
  solve($[ff,split],X).

print_tournament(X,NumRounds,NumPairs,Bye) =>
  N = X[1].len,
  foreach(Round in 1..NumRounds)
    printf("Round %2d: ", Round),
    if N > 10 then nl end,
    foreach(P in 1..NumPairs)
      P2Val = X[Round,P,2],
      if var(Bye) ; P2Val != Bye then
        printf("(%2w vs %2w) ",X[Round,P,1],P2Val),
        if N > 10 then nl end
      end
    end,
    nl
  end,
  nl.

main =>
  nolog,
  N = 12,  
  Extras = [[1,2,3],
            [5,9,7],
            [2,3,N-1],
            [7,12,N-1]],
  tournament_cp(N, NumRounds,NumPairs,Extras,X,Bye),
  print_tournament(X,NumRounds,NumPairs,Bye).

import cp. 

main =>
  foreach(N in 2..2..8)
    Count = count_all(tournament_cp(N, _NumRounds,_NumPairs,_Extras,_X,_Bye)),
    println(N=Count)
  end.

  

You may also check:How to resolve the algorithm Terminal control/Cursor positioning step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm A+B step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm RCRPG step by step in the Python programming language
You may also check:How to resolve the algorithm Recaman's sequence step by step in the SETL programming language
You may also check:How to resolve the algorithm Munchausen numbers step by step in the Julia programming language