I have a traveling tournament problem program in java. This program consumes considerable amount of time when running on a single computer. I want to be able to run one of it's function simu_schedule() on the distributed environment using RMI. I am posting below the whole code of traveling tournament.
import java.util.*;
class TTP_CH {
String individual;
int fitness=0;
int Penaulty,F_TET,T_COST;
String [] Schedule;
int no_Teams;
void CPY_Chro(TTP_CH chro)
{
individual = chro.individual;
fitness=chro.fitness;
Penaulty = chro.Penaulty;
Schedule=chro.Schedule;
no_Teams= chro.no_Teams;
F_TET = chro.F_TET;
T_COST = chro.T_COST;
}
TTP_CH(String [] S_Match,int match_cnt)
{
Random Rand = new Random();
int j,k,l,cntr;
individual = "";
int [] buff;
String [] S_Matches = new String[match_cnt];
buff = new int [match_cnt];
for(int i=0;i<match_cnt;i++)
{
buff[i] = i;
S_Matches[i] = "";
S_Matches[i] = S_Matches[i].concat(S_Match[i].substring(0,2));
}
cntr = match_cnt;
for(int i=0;i<match_cnt;i++)
{
j= Rand.nextInt(cntr);
individual = individual.concat(S_Matches[j]);
for(k=j;k<match_cnt-1;k++)
S_Matches[k]= S_Matches[k+1].substring(0,2);
cntr--;
}
}
String ret_individual()
{
return individual;
}
int get_fitness()
{
return fitness;
}
void Print_Schedule()
{
System.out.print("\n=========================================================\n");
System.out.println(" TOTAL TIME :: "+F_TET+" TOTAL COST :: "+T_COST +" PENAULTY Pt :: "+Penaulty);
System.out.println("\t FITNESS VALUE :: "+ fitness);
System.out.print("=========================================================\n");
for(int i=0;i<no_Teams;i++)
System.out.print("\t"+Schedule[0].charAt(i));
System.out.print("\n=========================================================\n");
for(int j=1;j<=Schedule[1].length()/2;j++)
{ System.out.print("\n");
for(int i=1;i<=no_Teams;i++)
{
System.out.print("\t"+Schedule[i].substring(2*(j-1),2*(j-1)+2));
}
}
System.out.print("\n=========================================================\n\n");
}
void set_fitness(int value)
{
fitness = value;
}
void Simu_schedule(int [][] distance,int n, int U,Match m, int PENPT)
{
int Total,Result,i,j,k,l,o;
int [] TET;
Penaulty=0;
char Prev_Venue,Current_Venue;
Result = 0;
int Cntr_4_UB;
int Home=U+1;
Cntr_4_UB = 0;
no_Teams = n;
Schedule = new String[n+1];
TET = new int[n];
for(i=1;i<=n;i++)
{
Schedule[i]= new String();
TET[i-1]=0;
}
Schedule[0]= new String();
Schedule[0] = m.Teams.substring(0,n);
for(i=0;i<n;i++)
{
Total = 0;
Current_Venue = m.Teams.charAt(i);
Prev_Venue = m.Teams.charAt(i);
for(j=0;j<n*(n-1);j++)
{
if(individual.substring(j*2,j*2+2).charAt(0) ==m.Teams.charAt(i) || individual.substring(j*2,j*2+2).charAt(1) ==m.Teams.charAt(i))
{
Schedule[i+1] = Schedule[i+1].concat(individual.substring(j*2,j*2+2));
if(Home == (U+1))
{
Cntr_4_UB = 1;
if(Current_Venue != individual.substring(j*2,j*2+2).charAt(0))
{
Total += distance[m.Team_no(m.Teams.charAt(i))][m.Team_no(Current_Venue)];
Home =0;
}
else
{
Home = 1;
}
}
else
{
if(individual.substring(j*2,j*2+2).charAt(0) ==m.Teams.charAt(i))
{
if(Home ==1)
{
Cntr_4_UB++;
if(Cntr_4_UB> U)
Penaulty++;
}
else
{
Home = 1;
Cntr_4_UB=1;
}
}
else
{
if(Home ==0)
{
Cntr_4_UB++;
if(Cntr_4_UB> U)
Penaulty++;
}
else
{
Home = 0;
Cntr_4_UB=1;
}
}
}
if(Current_Venue != individual.substring(j*2,j*2+2).charAt(0))
{
Prev_Venue = Current_Venue;
Current_Venue = individual.substring(j*2,j*2+1).charAt(0);
Total += distance[m.Team_no(Prev_Venue)][m.Team_no(Current_Venue)];
}
if(j<(n*(n-1)-1))
if ( (individual.substring(j*2,j*2+2).charAt(0)==individual.substring((j+1)*2,(j+1)*2+2).charAt(1))
&& (individual.substring(j*2,j*2+2).charAt(1)==individual.substring((j+1)*2,(j+1)*2+2).charAt(0))
)
{
Penaulty++;
}
}
}
if(Current_Venue != m.Teams.charAt(i))
{
Total += distance[m.Team_no(Current_Venue)][m.Team_no(m.Teams.charAt(i))];
}
Result += Total;
}
String sTmp = new String();
int itmp=0;
for(i=0;i<n*(n-1);i++)
{
sTmp = individual.substring(i*2,i*2+2);
itmp = max(TET[Schedule[0].indexOf(sTmp.charAt(0))],TET[Schedule[0].indexOf(sTmp.charAt(1))])+1;
TET[Schedule[0].indexOf(sTmp.charAt(0))] = itmp;
TET[Schedule[0].indexOf(sTmp.charAt(1))] = itmp;
}
F_TET = itmp;
T_COST = Result;
set_fitness((int)((Result)+(Penaulty+(F_TET-((n-1)*2)))*PENPT));
}
int max(int x, int y)
{
if(x>y)
return x;
else
return y;
}
public static void main(String[] args)
{
TTP_CH one,t,th,f,fi;
String [] S_Matches;
int match_cnt;
int n = 4,U = 0;
int [][] distance_mat= {{0,1,2,1},{1,0,1,2},{2,1,0,1},{1,2,1,0}};
Match m = new Match(n);
S_Matches= m.ret_Match();
one = new TTP_CH(S_Matches,m.total_Matches);
System.out.println(one.ret_individual()+one.ret_individual().length());
one.Simu_schedule(distance_mat,n,U,m,10);
System.out.println("Simulated schedule Time is "+one.get_fitness()+" with Penaulty Count :: "+one.Penaulty);
System.out.println("++++++++++++++++++++++++++++++++++++++++++");
one.Print_Schedule();
}
}