Java遗传算法并行多机调度程序

程序有两部分组成,分别是gen和DNA程序。其中gen用来生成50条原始链,作为DNA初始化数据。DNA程序在gen生成的50条链的基础上完成交叉,变异直到找到最优答案。

d:/工业/hk/bestLength.csv

d:/工业/hk/aaa.csv

d:/工业/hk/规划1.csv  初始化数据,30个工件和每个工件的耗时

d:/工业/hk/组合.csv

d:/工业/hk/矩阵.csv

d:/工业/hk/遗传.csv

这个程序中有6个本地路径,其中d:/工业/hk/规划1.csv是初始数据,其余保存的都是中间数据。

使用方法

1.在d:/工业/hk/规划1.csv这个位置手工制作规划1.csv文件,格式为序数,序数,工时。

2.调用gen程序,生成50条链,保存在矩阵.csv文件中

3.调用DNA程序

第一部分gen代码

第二部分DNA代码

第三部分 规划1.csv文件

第一部分gen代码

package tree1;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.Random;

 

public class gen {
	

		 //用于实现遗传算法,生成50条链
		 //种群数量 50  交叉概率 0.75  变异概率0.05  最大迭代数200

				   static int cityNum=30; //工件
				   static int ant=10;     //机器
				   static int iter=1000 ;
				   static int tiao=50;
	 
				   private static double alpha=0.0001;  //信息素重要程度的参数            1
				   private static double beta=14;   //启发式因子重要程度的参数     5
				   private static double rho=0.5;   //信息素蒸发系数                                 0.5
				   
				   
				   
				   static double bestLength=Integer.MAX_VALUE;   //最佳长度
				   static double[][] pheromone= new double[cityNum][cityNum]; //信息素矩阵
				   static double[][] distance= new double[cityNum][cityNum];   //距离矩阵
				   
				   static double[][] pher= new double[cityNum][cityNum]; //信息素矩阵
				   static double[][] bestTour  =new double[cityNum+1][1]; //最佳路径
				   

				   static double [][] tabu=new double[cityNum][1];
				   static int [][] allowedCities=new int[cityNum][1];
				   static double[][] delta = new double[cityNum][cityNum];; //信息素变化矩阵
				   static int firstCity;   //起始城市
				   static int currentCity; //当前城市
				   
				   static  int[] x= new int[cityNum];  
				   static  int[] y= new int[cityNum]; 

				   static int [][]ta=new int[ant][cityNum];
				   
				  // static int [][][]dgen=new int[tiao][cityNum][1];
				   
				   static int count1;
				   static int flag;

				   public static  void maco(String filename) throws IOException, ParseException {
					   
					   //初始化距离矩阵
					   //初始化信息素矩阵
					  
 
				         String strbuff;  
				         BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));  
				        
			 
				         
				         for (int i = 0; i < cityNum; i++) {  
				 
				             strbuff = data.readLine(); 

				             String[] strcol = strbuff.split(",");  
				             x[i] = Integer.valueOf(strcol[1]);  
				             y[i] = Integer.valueOf(strcol[2]);   
				             
				            // System.out.println(x[i]+"  "+ y[i]);
				         }  
				       
				         
				         for (int i = 0; i < cityNum  ; i++) {  
				             distance[i][i] = 0;  //对角线为0
				            	 for (int j = 0; j < cityNum; j++) {	 
				 
				        
				            		 if(i!=j)
				            		 {
				            distance[i][j] =y[j]  ;  
				            		 }
				            		 
				            		 if(i==j)
				            		 {
				            distance[i][j] =0  ;  
				            		 }	 
				            		 
				            		 
				             }
				   
				         }  

				     
				         

				         
				         for(int i=0;i<cityNum;i++)  
				         {  
				             for(int j=0;j<cityNum;j++)
				             {
				                 pheromone[i][j]=0.1;   
				                 
				               //  System.out.println(distance[i][j]+" "+i+"  "+j);
				                 
				              }
				             
				         }           
				         
				            
				         solve();
				         
				         
				   }
				   
				   public static void push(int td ,int [][] tad , int js) throws IOException, ParseException{
					   
					   int t=td;
					 
					   
					   int [][]tta=tad;
					   
					      int t1=0;
						  int tem1=0;
						  int max1=0;
						  
						  for(int a=0 ;a<cityNum ;a++)
						  { 
							  if(tta[js][a]!=0)
							  {
								  
								  max1++;
							  }
							  
						  }
						  
						
						// System.out.println("   *************** ");
						  
						  for(int a=0 ;a<max1 ;a++)
						  {
				  
							 // System.out.println( tta[js][a]+"  "+a );
							  
							  tem1=tem1+tta[js][a]; 

						  }
						  
						  t1=t-tem1;
						  
						  
						  
						 // System.out.println(t+"  "+max1+"   "+tem1+"  "+t1+"   *t1 ");
						  
						  if(t1-tta[js][max1]==0)
						  {
							  count1++;
							  
							//  System.out.println(  count1 +"  和  " ); 
							  
						  tta[js][max1]=selectNextCity(   count1   );
						  
						  //System.out.println(tta[max1]+"  ****  "+  count1 ); 
						  

						  }
						  
					   
					   
				   }
				   	   
				   public static void solve() throws IOException, ParseException{
					 
					  
						  
					   for(int gen=0 ;gen<iter ;gen++)
					   {

						   count1=ant-1;
						   
						 //  System.out.println("   迭代开始    "+count1);
						   
						   ta=new int[ant][cityNum];
						   tabu=new double[cityNum][1];
						   
						   for (int i = 0; i < cityNum; i++) {
							    allowedCities[i][0]=0;           //允许搜索城市列表
							  }
							  
							  
							  for (int i = 0; i < cityNum; i++) {
							    for (int j = 0; j < cityNum; j++) { 
							      delta[i][j] = 0.0;                   //信息素矩阵列表 都是0
							    }
							  }
	 
		
								   ta[0][0]=init(  );

								  for(int a=1 ;a<ant ;a++)
								  {
									  ta[a][0]=selectNextCity( a ); 
									  
								  }
							 /******************************/ //用于初始化
							
							for(int t=0 ;t<100 ;t++)  
							   {
		 
									int pan =0;
									
								  for(int c=0 ;c<ant ;c++ )
								  {
									  
									  pan=0;
								 
								  push(t ,ta ,c );
		 
									
									 for(int b=0 ;b<cityNum ;b++)
									  { 
										  if(allowedCities[b][0]==1)
										  {
											   
											  pan++;
										  }
									  }
									 

									 if(pan==cityNum)
									 {
										 break;
									 }
									 
								  }
								  
								  if(pan==cityNum)
									 {
										 break;
									 }
			 
									 
						       }
							

							
							 int max=0;
							
							int [][]rc=new int[ant][1];
							
							double ave=0.0;
						 
							for(int a=0 ;a<ant;a++) {
							 for(int b=0 ;b<cityNum ;b++)
							  { 
								 rc[a][0]=rc[a][0]+ta[a][b];
		   
								 ave=ave+ta[a][b];
							  }
							 
							 
							 
							 if(max<rc[a][0])
							 {
								 max=rc[a][0];
							 }
							
							// System.out.println(a+"      rc[a][0]    " +max );
								
							}
							
							ave=1000*ave/cityNum;
							 
							  // System.out.println(max+"  *** "+gen);
						
							 double disx=0.0;
							   
							 disx=max;
							 
							 
							    if (disx < bestLength) 
						         {
						        	 
						             for (int n = 0; n < cityNum; n++) {
						             
						             for (int m = 0; m < cityNum; m++) {
						            	 
						            	 if(tabu[m][0]==n)
						            	 {	 
							        bestTour[n][0]=m;
						            	 }
										
									  }
						             
						             }
						             
						             
						             bestTour[cityNum][0]=bestTour[0][0];
						             
						             bestLength=disx;
						             flag=gen;

						         }
							    
							    
							    for (int j = 0; j < cityNum-1; j++) 
						         {
						            delta[j ][j+1] = (double) (1./disx);
						            delta[j+1][j ] = (double) (1./disx); 
						            
						           // delta[j ][j+1] = (double) (1./ave);
						           // delta[j+1][j ] = (double) (1./ave); 
						         }    
							    
							
							    for(int i=0;i<cityNum;i++){
						             for(int j=0;j<cityNum;j++){ 
						            	 
						           pher[i][j] += delta[i][j];
						           
						             }  
						         }
			 
						
						

					   updatePheromone();
					   //printOptimal();
					   
				   }
	   
				     
				   }
				   

				   public static int init(  ) {
					   
						 //允许城市列表
					     //选出下一个城市
					     //并将这个城市加入禁忌表
					 	 //选出的城市被作为current city 返回
						
					  Random random2 = new Random(System.currentTimeMillis());
				      int s1 = random2.nextInt();
				      
				   
				      Random random1 = new Random( );
				      int s = random1.nextInt();
				      
				      Random random = new Random( (s+s1) );
				      firstCity = random.nextInt(cityNum);
				  
				     //firstCity = 4;
				     // System.out.println( firstCity  );

				 
					  
					  for (int i = 0; i < cityNum; i++) {
							 
						  if (i == firstCity) {

							      allowedCities[i][0]=1;  //去掉等于i的这项
							      break;
							    }
						  } 
					  
				 

					  for (int i = 0; i < cityNum; i++) {
							 
						  if (i == firstCity) {

							      tabu[i][0]=0;  //去掉等于i的这项
							      break;
							    }
						  } 
				
				
				  
					  currentCity = firstCity;
					  
					 // System.out.println( "currentCity  "+ currentCity   +"  "+y[currentCity]  );
					  
					  return x[currentCity];
					}
				   
				   
				   public static int selectNextCity( int  time  ) throws IOException, ParseException {
				 
					   
				  double [] p = new double[cityNum];
				  double sum = 0.0;
				  //计算分母部分

				  //System.out.println(  currentCity +"  currentCity  " );

				  
				  for (int i = 0; i < cityNum; i++) {
						 
					  if (allowedCities[i][0] != 1 ) {
						  
						  sum += Math.pow(pheromone[currentCity][i], alpha)*
								  Math.pow(1.0/distance[currentCity][i], beta);
						   
						    /*
						  System.out.println( i + "  "+      "   sum:    "  +  sum +"  "+
								  Math.pow(pheromone[currentCity][i], alpha)  +" a  "+ 
								  Math.pow(1.0/distance[currentCity][i], beta)+" b " +distance[currentCity][i] +" d "+
								  pheromone[currentCity][i]+" p "+alpha+"  "+beta+"  " +currentCity+ "  "
								  );
						  */
						  
								  
						    }
					  } 

				  

				    for (int i = 0; i < cityNum; i++) {
				    	
				    	boolean flag = false;
						 
						  if (allowedCities[i][0] != 1 ) {
				  
							  p[i] =   (Math.pow(pheromone[currentCity][i], alpha)*
						     		   Math.pow(1.0/distance[currentCity][i], beta) )   /sum;
							  
							  flag = true;
							  
							 // System.out.println(p[i]+"  * "+i);

							    }
						  
						  
						  if (flag == false) {
						      p[i] = 0.0;
						    }
						  
						
						  
						  } 
					  

				    
				      Random random3 = new Random(System.currentTimeMillis());
				      int s3 = random3.nextInt();
				   
				      Random random4 = new Random( );
				      int s4 = random4.nextInt();
				      
				      Random rand = new Random( (s3+s4) );
				      double slectP = rand.nextDouble( );
				    
				  
				  int selectCity = 0;
				  double ff = 0.0;
				  
				  
				 
				  
				  for (int i = 0; i < cityNum; i++) {
					  
				    ff += p[i];
				    
				    if (ff >= slectP) {
				       selectCity = i;

				      // System.out.println(slectP+"  slectP     " + ff+"   "+i +"  ");
				       
				      break;
				    }
				    
				  }
				  
				  
				 
				// System.out.println(selectCity+"     selectCity        "   );
			 
				  
				  for (int i = 0; i < cityNum; i++) {
						 
					  if (i== selectCity ) {
						  
						  allowedCities[i][0]=1;
						    }
					  } 
				  
				  
				  for (int i = 0; i < cityNum; i++) {
						 
					  if (i== selectCity ) {
						  
						  tabu[i][0]=time;
						//  System.out.println( tabu[i][0]+"  nant "+nant);
						    }
					  } 
				  
				  //将当前城市改为选择的城市  

				  currentCity = selectCity;
				  
				 
				  return x[currentCity];
				}
	 
				   
				   private static void updatePheromone()
				   {
					   
					  // System.out.println( "  updatePheromone    "  );

				         for(int i=0;i<cityNum;i++)  
				         {
				             for(int j=0;j<cityNum;j++)  
				             {
				                pheromone[i][j]=pheromone[i][j]*(1-rho);
				             }
				             
				         }

				         for(int i=0;i<cityNum;i++){
				        	 
				             for(int j=0;j<cityNum;j++){
				            	 
				                 
				           pheromone[i][j] += pher[i][j];
				           
				            
				             }  
				         }
				             
				         
				   }
				   
				     
			private static void printOptimal() throws IOException{
					   
					   FileWriter fileWriter2=new FileWriter("d:/工业/hk/bestLength.csv");  
					   
					   FileWriter fileWriter1=new FileWriter("d:/工业/hk/aaa.csv");  
					   
				    // System.out.println("The optimal length is: " + bestLength);
				     
				     fileWriter2.write( bestLength +"\r\n ");
				     fileWriter2.flush();
				     
				     
				     for (int i = 0; i < cityNum ; i++) {
				    	 
				      // System.out.println(bestTour[i][0]+"  ***  ");
				       
				       fileWriter1.write( (int)bestTour[i][0] +"\r\n ");
				       fileWriter1.flush();

				     }
				     
				     fileWriter1.write( (int)bestTour[0][0] +"\r\n ");
				       fileWriter1.flush();
				     
				     
				   }
				   
				  
			private static int[][] d3() throws IOException, ParseException{
				   
				    
				double b=100000;
				 
				  int count=-1;
					

						//	while(b>=48 && count < 100 )	
								
							 	while(b> 52 )			
					{
						 

						 // System.out.println( count+ "  count**** "+ tiao+"  "+b);

						
				   maco("d:/工业/hk/规划1.csv");
				   
				  // maco("d:/工业/hk/zz.csv");
				   
				  printOptimal();
				   

				   String aa= read.conv.read("d:/工业/hk/bestLength.csv" );
					
					aa=aa.replaceAll(",","");
			
				  b= Double.parseDouble(aa.trim());
				   
				 // System.out.println(  " count "+ b);

					}
			    
			     return ta;
			   }
		
			
			public static void shellSort(double[] data) 
		    {
		        int j = 0;
		        double temp = 0;
		        //每次将步长缩短为原来的一半
		        for (int increment = data.length / 2; increment > 0; increment /= 2)
		        {
		        for (int i = increment; i < data.length; i++) 
		        {
		            temp = data[i];
		            for (j = i; j >= increment; j -= increment) 
		            {
		            if(temp > data[j - increment])//如想从小到大排只需修改这里
		            	
		          //  	if(temp < data[j - increment])//如想从小到大排只需修改这里
		            {   
		                data[j] = data[j - increment];
		            }
		            else
		            {
		                break;
		            }
		            
		            } 
		            data[j] = temp;
		        }
		    
		        }
		    }
		 

			private static int[][] cd() throws IOException, ParseException{
				   //产生用于 交叉 变异的数组
				   
				int count=0;
				
			  int [][]tem=new int[ant][cityNum];
			  
			  int [][]dgen=new int[cityNum][tiao];
				
				for(int a=0 ;a<tiao ;a++)
				{
					tem=d3();
					
					
				//	static int [][]ta=new int[ant][cityNum];
					
					int [] dant=new int[cityNum];
		 
					for(int d=1 ;d<cityNum+1 ;d++)
					{
							
							for (int b = 0; b < ant; b++) {
						 		for (int c = 0; c < 5; c++) {
						 			
						 			
	//System.out.println(tem[b][c]+"  "+b+"  "+c);
						 			
	if(tem[b][c]==d)
	{
	//System.out.println(tem[b][c]+"  "+b+"   " +d+"  ");
	dant[d-1]=b;

	}

						 		}
						 	} 	
							
					}
					
					/*****************/
					
					
					for(int d=0 ;d<cityNum ;d++)
					{
						
						System.out.println(dant[d]+"  "+d+" *"+a);	

						dgen[d][a]=dant[d];
					}
			
					
				}
				
				/******************************************/

				
				System.out.println( "*********************");	
				
					
				for(int a=0 ;a<tiao ;a++)
				{		 	
					for(int d=0 ;d<cityNum ;d++)
					{	 	
						 	
						System.out.println(dgen[d][a]+"  "+a+"  "+d);	
						//机器编号    列    行
						  
					}

				}
				
				
				return dgen; 	
			   }
		
			
			
				   public static void main(String[] args) throws IOException, ParseException {

					   FileWriter fileWriter1=new FileWriter("d:/工业/hk/组合.csv");  

					   FileWriter fileWriter2=new FileWriter("d:/工业/hk/矩阵.csv"); 

					 
					  int [][]dgen=cd();
						

						System.out.println( "*********************");	
						
							
						for(int a=0 ;a<tiao ;a++)
						{		 	
							for(int d=0 ;d<cityNum ;d++)
							{	 		 	
								System.out.println(dgen[d][a]+"  "+a+"  "+d);	
								//机器编号    列    行 
								
								fileWriter2.write( dgen[d][a] +"\r\n ");
								fileWriter2.flush();
							}
						} 	
						 
					   
					   int bei=2;
					   
					   int [] s1=new int[tiao*bei];
					   int [] s2=new int[tiao*bei];
						
						for(int a=0 ; a< tiao*bei ;a++)
						{
					   
						Random rand1 =new Random();
						int ti1=rand1.nextInt(tiao);
						
						Random rand2 =new Random();
						int ti2=rand2.nextInt(tiao);
						
								 	System.out.println(ti1+"  "+ti2);
								 	
								 	s1[a]=ti1;
								 	s2[a]=ti2;
								 	
						}
						
						
						System.out.println("****************");
						
						for(int a=0 ; a< tiao*bei ;a++)
						{
							for(int b=a+1 ; b< tiao*bei ;b++)
							{
								if(s1[a]==s1[b]&&s2[a]==s2[b])
								{
									s1[a]=0;
									s2[a]=0;
									
									 System.out.println(a+" ** 相同 "+b);
								}	
							}
							
							
							
							for(int b=0 ; b< cityNum*bei ;b++)
							{
								if(s1[a]==s2[b]&&s2[a]==s1[b])
								{
									s1[a]=0;
									s2[a]=0;
									
									 System.out.println(a+" ** 反向  "+b);
									
								}	
							}
	
						}
						
						 
						
						//s1 s2 里面的就是没有重复 没有相同 没有反向相同 的组合
						int count=0;
						
						for(int a=0 ; a< tiao*bei ;a++)
						{
							
							if( (s1[a]!=0||  s2[a]!=0) && count<50)
							{
								
							 System.out.println(s1[a]+"  "+s2[a]+"  " +count  );
							 
							 fileWriter1.write( s1[a]+" , "+s2[a] +"\r\n ");
							 fileWriter1.flush();

							
							count++;
							}
						}
  

				   }
				 
				 }

第二部分

package tree1;
 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.util.Random;
import java.util.regex.Pattern;


public class DNA {

 
			//需要用gen 得到初始数组
			//遗传算法用于多机并行调度
		 
			
		 
			/*
			 * 40
			0	11	12					18	22			
			1	23	25					16	23			
			2	2	13	27				6	11	18		
			3	1	8	18	28			2	4	17	15	
			4	0	9	10	16			3	12	10	14	
			5	5	15	17				7	26	6		
			6	19	29					27	13			
			7	20	22					11	26			
			8	3	4	6	7	24		4	5	9	13	7
			9	14	21	26				8	17	15		
														
		*/
			
			
			
			static int tiao=50;
			static int cityNum=30;
			
			static int [][] dgen2=new int[cityNum][tiao];
			
			public static void shellSort(int[] data) 
		    {
		        int j = 0;
		        int temp = 0;
		        //每次将步长缩短为原来的一半
		        for (int increment = data.length / 2; increment > 0; increment /= 2)
		        {
		        for (int i = increment; i < data.length; i++) 
		        {
		            temp = data[i];
		            for (j = i; j >= increment; j -= increment) 
		            {
		           // if(temp > data[j - increment])//如想从小到大排只需修改这里
		            	
		           	if(temp < data[j - increment])//如想从小到大排只需修改这里
		            {   
		                data[j] = data[j - increment];
		            }
		            else
		            {
		                break;
		            }
		            
		            } 
		            data[j] = temp;
		        }
		    
		        }
		    }
		 
			
			private static int cross( int [][]xgen ) throws IOException, ParseException{
				
				
				int ant=10;
		    	int changerate=3; //变异概率  这个是   4%  
				double droprate=0.25;  //按照时间 淘汰的概率  这个就是 小数
				int bei=100;             //0.85 bei>3  要随着droprate 的增加变大 因为 去掉 90%以后要用10 个随机出50个这个重复概率加大
				 

				int [][] dgen=xgen;
			
				 
				int [] mina=new int[tiao];
				
				for(int a=0 ;a<tiao ;a++)
				{		 	
					int [] tem= new int[cityNum];
					for(int d=0 ;d<cityNum ;d++)
					{ 
						//  System.out.println(dgen [d][a]+"  "+a+"  "+d+"  dgen2  后 ");	
						  
						  tem[d]=dgen [d][a];
					}
					
					int tem1=value( tem);
					
						mina[a]=tem1;  //计算时间数组
				}
				
				
				
				shellSort( mina  );  //将时间按大小排序  进行 淘汰
				
			
				/****************************************/
				int [][] dgenorder=new int[cityNum][tiao];
				
				int count=0;
				
				for(int b=0 ;b<tiao ;b++)
				{
					int tem1=0;
					
				for(int a=0 ;a<tiao ;a++)
				{		 	
					int [] tem=new int[cityNum];
					
					for(int d=0 ;d<cityNum ;d++)
					{ 
						tem[d]=dgen [d][a];
					}	
					  tem1=value( tem); 
		 

				 //System.out.println(tem1+"  "+mina[b]+"  "+count+ " b "+b);
				
				 
				 if(tem1==mina[b])
					{
					 
					 // System.out.println(tem1+"  "+mina[b]+"  "+count+ " ****** b "+b);
					 
					  for(int d=0 ;d<cityNum ;d++)
						{ 
						  dgenorder[d][count]=dgen [d][a];
						}
					  
					  count++;
					  
					  break;
					}
				
				
				 
				}
				
				}
				
				
				//System.out.println("****************   "+ (int)(tiao*(1-droprate))*bei  );
				  
				   
				   int [] s1=new int[(int)(tiao*(1-droprate))*bei];
				   int [] s2=new int[(int)(tiao*(1-droprate))*bei];
					
				   
				   
					for(int a=0 ; a< (int)(tiao*(1-droprate))*bei ;a++)
					{
				   
					Random rand1 =new Random();
					int ti1=rand1.nextInt((int)(tiao*(1-droprate)));
					
					Random rand2 =new Random();
					int ti2=rand2.nextInt((int)(tiao*(1-droprate)));
					
							 //	System.out.println(ti1+"  "+ti2+"  "+ (int)(tiao*(1-droprate))*bei +" "+a );
							 	
							 	s1[a]=ti1;
							 	s2[a]=ti2;
							 	
					}
					
					
					
					
					for(int a=0 ; a< (int)(tiao*(1-droprate))*bei ;a++)
					{
						for(int b=a+1 ; b< (int)(tiao*(1-droprate))*bei ;b++)
						{
							if(s1[a]==s1[b]&&s2[a]==s2[b])
							{
								s1[a]=0;
								s2[a]=0;
								
							//	 System.out.println(a+" ** 相同 "+b);
							}
							
							
						}
						
						 //System.out.println("****************");
						
						for(int b=0 ; b< (int)(tiao*(1-droprate))*bei ;b++)
						{
							if(s1[a]==s2[b]&&s2[a]==s1[b])
							{
								s1[a]=0;
								s2[a]=0;
								
								// System.out.println(a+" ** 反向  "+b);
								
							}
							
							
						}
						
						
						
						
					}
					
					
					
					//s1 s2 里面的就是没有重复 没有相同 没有反向相同 的组合
					int count1=0;
					
					for(int a=0 ; a< (int)(tiao*(1-droprate))*bei ;a++)
					{
						
						if( (s1[a]!=0||  s2[a]!=0) && count1< tiao )
						{
							
						// System.out.println(s1[a]+"  "+s2[a]+"  ***  " +count1  );
						 

						count1++;
						}
					}
				
				
				
				/*************************************/

				//int [][] dgen2=new int[cityNum][tiao];
				
				for(int n=0 ;n<tiao;n++)
					
					//for(int n=0 ;n<1 ;n++)
				{
					//s1[n]=Integer.parseInt( as1[2*n] );
					//s2[n]=Integer.parseInt( as1[2*n+1] );
						
					 //System.out.println(s1[n]+"  "+s2[n]+"   " +n);
					
					
					int [] t1=new int[cityNum];
					int [] t2=new int[cityNum];
					
					int [] t3=new int[cityNum];
					int [] t4=new int[cityNum];
					
					
					for(int a=0 ;a<tiao ;a++)
					{		 	
						for(int d=0 ;d<cityNum ;d++)
						{	 		 	
							//System.out.println(dgen[d][a]+"  "+a+"  "+d);	
							//机器编号    列    行  
							
							if(a==s1[n])
							{
								t1[d]=dgenorder[d][a];
							}
							
							if(a==s2[n])
							{
								t2[d]=dgenorder[d][a];
							}
							
						}
					}
					
					Random rand1 =new Random();
					int ti1=rand1.nextInt(cityNum);
					
				 
					
					for(int d=0 ;d<cityNum ;d++)
					{
				 t3[d]=t1[d];
				 t4[d]=t2[d];
		 
					}
					//中继数组
					
			
					
					
					for(int d=0 ;d<cityNum ;d++)
					{
						if(d>ti1)
						{
							t1[d]=t4[d];
						}
		 
					}
					
					for(int d=0 ;d<cityNum ;d++)
					{
						if(d>ti1)
						{
							t2[d]=t3[d];
						}
		 
					}
					

					
					//t1 t2 完成交叉的数组
					/**********************************************/
					
							 	
						for(int d=0 ;d<cityNum ;d++)
						{
		 		 
							//System.out.println(n+"  "+ n*2+"  "+ (n*2+1)  );
							
								dgen2[d][n*2]=t1[d];
								dgen2[d][n*2+1]=t2[d];
		 
								
						}
					
						if(n*2+1 ==tiao-1 )
						{
							break;
						}
					
					
					//将交叉后的结果写入 dgen2
	

				}
				//s1 s2里面就是排列组合
				
				/************************************************/
			

				for(int a=0 ;a<tiao ;a++)
				{		 	
					for(int d=0 ;d<cityNum ;d++)
					{ 
						//  System.out.println(dgen2[d][a]+"  "+a+"  "+d+"  dgen2  前 ");	
						 
					}
					
				}
				
				
					for(int a=0 ;a<tiao ;a++)
					{		 	
						for(int d=0 ;d<cityNum ;d++)
						{ 
							// System.out.println(dgen2[d][a]+"  "+a+"  "+d+"  dgen2 ");	
							 
								Random rand1 =new Random();
								int ti1=rand1.nextInt(99);
								
								if(ti1<changerate)  //按照指定概率进行变异
								{
									 
									 
									Random rand2 =new Random();
									int ti2=rand2.nextInt(ant);
									
									//System.out.println(dgen2[d][a]+"  "+a+"  "+d+"    "+ ti2);	
									
									dgen2[d][a]=ti2;	
								}
								
								
						}
					}
					
					/*********************************/
				
					for(int a=0 ;a<tiao ;a++)
					{		 	
						for(int d=0 ;d<cityNum ;d++)
						{ 
							 //  System.out.println(dgen2[d][a]+"  "+a+"  "+d+"  dgen2  后 ");	
							 
						}
						
					}
				
					
					//dgen2[d][a] 里面存放的就是经过    交叉    变异   后数组
					/****************************************/
					
					int min=100000;
					
					for(int a=0 ;a<tiao ;a++)
					{		 	
						int [] tem= new int[cityNum];
						for(int d=0 ;d<cityNum ;d++)
						{ 
							//  System.out.println(dgen2[d][a]+"  "+a+"  "+d+"  dgen2  后 ");	
							  
							  tem[d]=dgen2[d][a];
	  
						}
						
						int tem1=value( tem);
						
						if(tem1<min)
						{
							min=tem1;
						}
						
					}
					
					//用于计算最小值
					
				 //	System.out.println(min+ " max " );
					
				
				return min;

			}
			
			
			private static  int  value( int [] test) throws IOException, ParseException{
				
				
				
				int cityNum=30;
				int ant=10;
				
				     int[] x= new int[cityNum];  
				     int[] y= new int[cityNum]; 
				
				
			
				int [] ar=test;
				
				  String strbuff;  
			     BufferedReader data = new BufferedReader(new InputStreamReader(new FileInputStream("d:/工业/hk/规划1.csv")));  
			        

			         for (int i = 0; i < cityNum; i++) {  
			 
			             strbuff = data.readLine(); 

			             String[] strcol = strbuff.split(",");  
			             x[i] = Integer.valueOf(strcol[1]);  
			             y[i] = Integer.valueOf(strcol[2]);   
			             
			           // System.out.println(x[i]+"  "+ y[i]);
			         }

			         
			         int max=0;
			         
			         int min=100000;
			         
			        // System.out.println( y[15]+"   *  y[15]  *   ");
			         
			         
			         for(int c=0 ;c<ant ;c++)
		        	 {
			         
			        	 int tem=0;
			        	 
				        	
			         for(int b=0 ; b<cityNum  ;b++)
			         {        	  
			        	 if(ar[b]==c)
			        	 {
			        		// System.out.println( y[b]+"  "+c+" * "+b);
			        		 tem=tem+y[b];
			        	 }
			        }
			        	 
			        // System.out.println(tem+"  tem  ");
			        	 
			        	 if(tem>max)
			        	 {
			        		 max=tem;
			        		// System.out.println(max+"  max  ");
			        	 }
			        	 
			        	 if(tem<min)
			        	 {
			        		 min=tem;
			        		// System.out.println(min+"  min  ");
			        	 }
			        	 
			        	 
			        	 
			         }
			 
			      //   System.out.println(max+"   max "+ min +"  min  ");
		 
				
				return max;
			}
			

			public static void main(String[] args) throws IOException, ParseException {

				 int ww=100;
				 int cityNum=30;
				// int tiao=50;
				 
				 FileWriter fileWriter1=new FileWriter("d:/工业/hk/遗传.csv");  
				 
		 			int [][] dgen=new int[cityNum][tiao];
					
					
					String aw=read.conv.read("d:/工业/hk/矩阵.csv");
							
					String[] as =Pattern.compile(",").split(aw);	
					
					
					for(int n=0 ;n<as.length;n++)
					{
						//System.out.println(as[n]+"  "+n);
						dgen[n-cityNum*(n/cityNum)][n/cityNum]=Integer.parseInt( as[n].trim() );
					}
					
					
					int count=0;
					
						// while (ww >41 && count<200)
					
					int find=40;
							 
							 while (ww >find )
						 {
							 long sysDate1 = System.currentTimeMillis();
							 
				  ww= cross(dgen);
				  
				  long sysDate2 = System.currentTimeMillis();
				  
				  for(int a=0 ;a<tiao ;a++)
					{		 	
					 
						for(int d=0 ;d<cityNum ;d++)
						{ 
		 		  
							   dgen[d][a]= dgen2[d][a] ;
						}
						
						
					}
				  
				  count++;
				  
				  System.out.println(ww+ "  max  "+count );
				  
				  fileWriter1.write( ww+","+ (sysDate2-sysDate1  ) +"\r\n ");
				  fileWriter1.flush();

				  
						 }
	
						 
					 
						  //查看最优解
							for(int a=0 ;a<tiao ;a++)
							{		 	
								int [] tem= new int[cityNum];
								for(int d=0 ;d<cityNum ;d++)
								{ 
									 //  System.out.println(dgen2[d][a]+"  "+a+"  "+d+"  dgen2  后 ");	
									  //dgen2[d][a] 30个工件对应的机器编号
									   
									  tem[d]=dgen2[d][a];
		  
								}
								
								int tem1=value( tem); //最优值工时
								
								if(tem1==find){
									
									System.out.println(tem1+"    tem1   ");	
									for(int d=0 ;d<cityNum ;d++)
									{ 
										   System.out.println(dgen2[d][a]+"  "+a+"  "+d );	//最优解

									}
									
									break;
								}
								
								
								
							}
						 
						 
					 
			   }
			   
			 
			 }

程序中出现的read.conv.read( string );

public static String read(String a) throws IOException{
		String as;
	{       
		  String tops="";
			BufferedReader ins=new BufferedReader(new FileReader(a));
			String ss;
			List<String> nns=new LinkedList<String>();
			while((ss=ins.readLine())!=null)
				nns.add(ss);
			String kps = nns.toString();
			kps = kps.substring(1,kps.length()-1);
			
			//System.out.println(kp+"*-*");
          tops=kps;
        // System.out.println(top+"*-*");
			ins.close();
		
		 as=tops;
	}
	
	as=as.trim();
	 return as;
	
	}

第三部分

文中规划1.csv如下,用10台机器加工30个工件

1

1

3

2

2

2

3

3

6

4

4

4

5

5

5

6

6

7

7

7

9

8

8

13

9

9

4

10

10

12

11

11

10

12

12

18

13

13

22

14

14

11

15

15

8

16

16

26

17

17

14

18

18

6

19

19

17

20

20

27

21

21

11

22

22

17

23

23

26

24

24

16

25

25

7

26

26

23

27

27

15

28

28

18

29

29

15

30

30

13

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑榆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值