在JAVA中分发的顺序程序 [英] Sequential Program to Distributed in JAVA

查看:65
本文介绍了在JAVA中分发的顺序程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java的旅行锦标赛问题程序.在单台计算机上运行时,此程序会消耗大量时间.我希望能够使用RMI在分布式环境上运行它的功能之一simu_schedule().我在旅游锦标赛的全部代码下方发布.

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);

	}


}

推荐答案

嗯,恕我直言,使用spring-framework是实现这一目标的最快方法...

查看 http://www.springsource.org/ [ ^ ]

特别是: http://static.springsource. org/spring/docs/3.0.x/spring-framework-reference/html/remoting.html#remoting-rmi [
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


这篇关于在JAVA中分发的顺序程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆