Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,152,801 members, 7,817,316 topics. Date: Saturday, 04 May 2024 at 10:06 AM

Challenge: League Fixture Algorithm - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Challenge: League Fixture Algorithm (2789 Views)

The Greatest Programmer On Nairaland / Your Toughest Algorithm / Ludo Game Algorithm Wanted For AI Project (2) (3) (4)

(1) (Reply) (Go Down)

Challenge: League Fixture Algorithm by kodewrita(m): 8:41am On Oct 21, 2013
The Problem:

You have a 12 team league (A-L).

Teams A-C are the high profile teams.

Team A and G are from the same city.

same for team C, D and E which all come from another city.

Write an algorithm to schedule matches for the entire season given the following.

--Two derby matches must not follow each other. ( derby meaning matches between teams from the same city).
--high profile teams can only play each other once in each half of the season.
--no team can have more than 3 away matches in succession.

(Not certain if this is hard or easy but it seemed to be an interesting challenge).

All languages permitted. Post your solutions here.

Regards.

1 Like

Re: Challenge: League Fixture Algorithm by Javanian: 5:53pm On Oct 21, 2013
Re: Challenge: League Fixture Algorithm by codeaddict(m): 5:58pm On Oct 21, 2013
Well, some of us don't watch football, undecided

So by entire season, how many home | away matches are allowed for each team?
And, does half season mean half of the total number of away & home matches scheduled?

1 Like

Re: Challenge: League Fixture Algorithm by kodewrita(m): 6:21pm On Oct 21, 2013
codeaddict: Well, some of us don't watch football, undecided

So by entire season, how many home | away matches are allowed for each team?
And, does half season mean half of the total number of away & home matches scheduled?

assume that a season is composed of matches between all teams with two teams playing one home match and one away match against each other.

A plays C at C's home.
C plays A at A's home.
Re: Challenge: League Fixture Algorithm by codeaddict(m): 9:03am On Oct 23, 2013
Re: Challenge: League Fixture Algorithm by ToyinDipo(m): 9:21pm On Oct 25, 2013
Here is a version in java. It ensures the following:
1. No team plays more than two consecutive home or away games.
2. No team plays consecutive derby.
3. No team at anytime has number of home games or away games exceeding half of games played so far plus one.
4. No team can play another more than once in each half of the season.


However, because of these restrictions, the algorithm is only able to execute perfectly like one in every 8 possible combinations.

Here are the classes:

Class Team
 /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fixtures;

import java.util.ArrayList;

/**
*
* @author Toyin
*/
public class Team{
private char name;
private int dc=0,ac=0,hc =0,hg=0,ag=0,city;
private boolean fixed=false;
private int index;
private ArrayList<Integer> played;

public int getHc() {
return hc;
}

public void incHc() {
++hc;

}
public int getAg() {
return ag;
}

public void incAg() {
++ag;

}


public ArrayList<Integer> getPlayed() {
return played;
}

public int getHg() {
return hg;
}

public void IncHg() {
++hg;
}

public void setPlayed(ArrayList<Integer> played) {
this.played = played;
}

public Team(int index, char name, int city) {
this.index = index;
this.name = name;
this.city = city;
played = new ArrayList<Integer>();
}

public void incDc() {
++dc;
}

public void incAc() {
++ac;
}

public int getIndex() {
return index;
}

public void setIndex(int index) {
this.index = index;
}

public void setFixed(boolean fixed) {
this.fixed = fixed;
}

public char getName() {
return name;
}

public int getDc() {
return dc;
}

public int getAc() {
return ac;
}

public int getCity() {
return city;
}

public boolean isFixed() {
return fixed;
}
public void resetDc(){
dc=0;
}
public void resetAc(){
ac=0;
}
public void resetHc(){
hc=0;
}
public void addPlayed(int t){
played.add(t);
}
public boolean hasPlayed(int t){
if (played.contains(t))return true;
else return false;
}
}


Class Match

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fixtures;

/**
*
* @author Toyin
*/
public class Match {
Team home,away;

public Match(Team home, Team away) {
this.home = home;
this.away = away;
}



}


Class Fixtures

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fixtures;

import java.util.Random;

/**
*
* @author Toyin
*/
public class Fixtures {
private Team[] teams;
private Match[][] matches;
private Random random;
private boolean exit = false, done = false;
int test=0;
public Fixtures() {


while(!done){
initialize();
shuffle();
process();
}
printOut();

}
private void initialize(){
teams = new Team[12];
for(int i=0; i<teams.length; i++){
switch(i){
case 0:
case 6:
teams[i] = new Team(i,(char) (i+65),1);
break;
case 2:
case 3:
case 4:
teams[i] = new Team(i,(char) (i+65),2);
break;
default:
teams[i] = new Team(i,(char) (i+65),0);
break;

}

}

}
private void printOut(){
for (int a=0; a<matches.length; a++)
{
System.out.println("Day " + (a+1));
System.out.println();
System.out.println("<===========================>"wink;
System.out.println();
for (int b=0; b<matches[a].length; b++)
{
System.out.println(matches[a][b].home.getName() + " vs " + matches[a][b].away.getName());



}

}

}
private void shuffle() {

random = new Random();


for (int i=0; i<teams.length; i++){
int t = random.nextInt(teams.length);
swap(i,t);

}

}
private void swap (int i, int t){

Team temp = teams[i];
teams[i] = teams[t];
teams[t] = temp;
}
private boolean isDerby(Team a, Team b){
if ((a.getCity()== b.getCity())&& (a.getCity()!=0))
return true;
else return false;
}

private void resetFixed(){
for (int i=0; i<teams.length; i++){
teams[i].setFixed(false);
}
}
private boolean played(Team t, int i){
if (t.hasPlayed(i)) return true;
return false;
}




private void process(){
matches = new Match[22][6];
int cnt =0;
exit = false;
for (int a=0; a<22; a++){

resetFixed();

int b=0;

int ecode =0;

while ((b<6)&& ecode<6){


int x1=-1,y1=-1,pc=-1;

for(int x=0; x<11; x++){
for(int z=x+1; z<12; z++){
if(played(0,z)>played(0,x)) swap(x,z);


}

if(teams[x].isFixed()) continue;
for(int y=11; y>=0; y--){

if (y==x) continue;

if(teams[y].isFixed()) continue;
if(played(x,y)<pc) continue;
if((played(x,y)==pc)&&(!isDerby(teams[x],teams[y])))
{
if (y1!=-1){
if( teams[y].getHg()<=teams[y1].getHg())
continue;
}
else continue;

}

if (!isPlayable(x,y,a)) {
if(pc==-1 ) { ++ecode; break;}

continue;}

if((teams[x].getHg()>((a+1)/2))||(teams[x].getHg()>teams[y].getHg())||(teams[x].getHc()>1)){
if((teams[y].getHg()<=((a+1)/2))&&(teams[y].getHc()<2)&&(teams[x].getAc()<2)){
if(played(y,x)>=pc)
x1 = y; y1 =x; pc = played(y,x);

} else {
if(pc==-1) { ++ecode; break;}
else
continue;
}

}
else if((teams[y].getAg()>((a+1)/2))||(teams[y].getAg()>teams[x].getAg())||(teams[y].getAc()>1)){

if((teams[x].getAg()<=((a+1)/2))&&(teams[x].getAc()<2)&&(teams[y].getHc()<2)){
if(played(y,x)>=pc)
x1 = y; y1 =x; pc = played(y,x);

} else {
if(pc==-1) { ++ecode;

break;}
continue;
}

}

else
{
x1 = x; y1 =y; pc = played(x,y);

}

}
}
if(pc!=-1){
fixMatch(x1,y1,a,b);
++cnt;
++b;

}

}
if (ecode>=6) { exit = true;}
if(cnt==132){done=true; System.out.println("done"wink;}
}
}

private int played(int x, int y){
int count =0;
for (int i=0; i<teams.length; i++){
if((i!=x)&&(i!=y)&&(!teams[i].isFixed())){
if(teams[y].hasPlayed(teams[i].getIndex())) ++count;

}


}

return count;
}
private boolean isPlayable(int x, int y, int a){
if(teams[x].hasPlayed(teams[y].getIndex())) {

if(teams[y].hasPlayed(teams[x].getIndex()) && a<11) {
return false;
}
if((isDerby(teams[x],teams[y])) && ((teams[x].getDc()>0)||(teams[y].getDc()>0)))
{
return false;

}
}


return true;
}
private void fixMatch(int x, int y, int a, int b){
if(isDerby(teams[x],teams[y]))
{
teams[x].incDc();
teams[y].incDc();
}
else
{
teams[x].resetDc();
teams[y].resetDc();
}
teams[y].incAc();
teams[y].incAg();
teams[y].resetHc();
teams[x].incHc();
teams[x].IncHg();
teams[x].resetAc();
teams[x].addPlayed(teams[y].getIndex());
teams[x].setFixed(true);
teams[y].setFixed(true);
matches[a][b] = new Match(teams[x],teams[y]);


}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Fixtures();
}
}

1 Like

Re: Challenge: League Fixture Algorithm by Javanian: 9:34pm On Oct 26, 2013
Back to this Thread, i am tired of seeing apps apps apps every where angry Everybody wants to win Hyundai IX 35 car. grin grin

Anyone who tried this would agree this is not near as easy as it looks tongue

1 Like

Re: Challenge: League Fixture Algorithm by dsypha(m): 10:50pm On Oct 26, 2013
Javanian: Back to this Thread, i am tired of seeing apps apps apps every where angry Everybody wants to win Hyundai IX 35 car. grin grin

Anyone who tried this would agree this is not near as easy as it looks tongue
lols...
Re: Challenge: League Fixture Algorithm by Javanian: 5:08pm On Dec 25, 2013
bump
Re: Challenge: League Fixture Algorithm by naijaswag1: 1:30am On Dec 26, 2013
The Hyundia car would be good. if I sort this, am I gonna get it?

(1) (Reply)

Problem with tracking word frequency[Java] / Who Is The Best Programmer In Nigeria/world? / Site To Learn Java From Beginners To Advanced Level

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 37
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.