Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 tmpi = match_cnt;
		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);
		
		//for()		
		cntr--;
		
		}
	//	System.out.println(individual);
			
	}
String	ret_individual()
	
	{
	return individual;
	}
	
int get_fitness()
	{
//	if(fitness ==0)
	
	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;
//	System.out.println("value is "+value+" fitness " +fitness);
}	
	
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);
//	System.out.println("Teams are ::"+Schedule[0]);
	for(i=0;i<n;i++)
	{
	Total = 0;
//	System.out.println("++++++++++++++++++++++++++++++++++++++++++");	
//	System.out.println("+++ SCHEDULE FOR Team "+m.Teams.charAt(i)+" IS +++++++++++");
	
//	System.out.println("HOME of "+m.Teams.charAt(i)+" ::  ");
	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))
		{ // For the first Time
		
		Cntr_4_UB = 1;
		
			if(Current_Venue != individual.substring(j*2,j*2+2).charAt(0))	
			{	// update the distance for move from Home venue to first match venue if they are not same
				Total += distance[m.Team_no(m.Teams.charAt(i))][m.Team_no(Current_Venue)];		
				Home =0; 
			}
			else
			{	
				Home = 1;
			}
		}
		else
		{   
			//Update the penaulty for being played for more time that U
			if(individual.substring(j*2,j*2+2).charAt(0) ==m.Teams.charAt(i))
			{ // Team to play at Home
				if(Home ==1)
				{
					Cntr_4_UB++;
					
					if(Cntr_4_UB> U)
								Penaulty++;
					
				}
				else
				{
						Home = 1;
						Cntr_4_UB=1;
				}
			}
			else
			{ // Team to play Away
				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))	
			{	// update the distance for change in venue
				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++;
			}
	
		} // end of if for the current Teams schedule
		
}  // End of for j
	
	if(Current_Venue != m.Teams.charAt(i))	
			{
				Total += distance[m.Team_no(Current_Venue)][m.Team_no(m.Teams.charAt(i))];		
			}
			
	
//	System.out.println("Back to HOME for "+m.Teams.charAt(i));
//	
//	System.out.println("Cost of the Schedule is :: "+Total);
	Result += Total;
//	System.out.println("++++++++++++++++++++++++++++++++++++++");
//	System.out.println("");
	} // End of for i
	
//	System.out.println("Penaulty of "+Penaulty+" Times is :: "+Penaulty*5);
//	System.out.println("Result"+Result +"Penaulty of "+Penaulty+" Times is :: "+Penaulty*5);
//Schedule[0]
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;
//	System.out.println(" Match :: "+ sTmp +" :: Ground :"+ Schedule[0].indexOf(sTmp.charAt(0))+"   Time ::"+itmp);
	
	//Schedule[0].indexOf(sTmp[0].charAt(0));	
}
	F_TET = itmp;
	T_COST = Result;
//	set_fitness((int)((Result*2.5)+(Penaulty*PENPT)+F_TET*2.5));
//set_fitness(Result*(Penaulty+1)*(F_TET/((n-1)*2)*(n-1)*2+F_TET%(((n-1))*2)+1));
//set_fitness(Result+(Penaulty+(F_TET-(n-1)*2+1)*PENPT));
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}};
		
//		one = new TTP_CH();
		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();

//one.GA_TTP(50,200,4,3,distance_mat,m);

	}


}
Posted
Updated 20-Apr-11 13:51pm
v2
Comments
HimanshuJoshi 20-Apr-11 19:51pm    
Edited to add pre tags around the code.

1 solution

Well, imho, using the spring-framework will be the quickest way to get this going...

Look at http://www.springsource.org/[^]

Especially: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#remoting-rmi[^]

So this leads to:

1) setup an AppContext (in your App)
2) Configure the remote services (in an XML-file)
3) think of a communication pattern (which node communicates with which other node) and put this into your program (most simple might be 1 node (main node), which produces the jobs, and n nodes that will do the work and deliver their result to the main node...)

Hope this helps, Cheers,
Arndt
 
Share this answer
 
Comments
rba9999 21-Apr-11 9:31am    
If this can be done through basic java RMI, please provide me with the procedural steps

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900