package hp;
public class Crossover { /*提供worksheet*/
private int numberOfIndiv;
private int[] numberOfWorkers; /*参数 */ /*一个月中每天的工人数*/
private int numberOfJob; /*参数 */
private int period; /*参数 */
private int[][] skillOfWorkers; /*参数*/
private double[] ratio; /*参数*/
private int[] workload; /*参数*/
private int[][][] worksheet; /*参数,产生,选择后的个体数量 */
private int[] crossoverPoor; /*产生, 交配池*/
Crossover(Parameter parameter)
{
this.numberOfIndiv=parameter.getNumberOfIndiv();
this.numberOfWorkers=parameter.getNumberOfWorkers();
this.numberOfJob=parameter.getNumberOfJobs();
this.period=parameter.getperiod();
this.skillOfWorkers=parameter.getSkilltable();
this.crossoverPoor=new int[this.numberOfIndiv];
this.ratio=parameter.getRatio();
this.workload=parameter.getWorkload();
}
public int[][][] getWorksheet()
{
return this.worksheet;
}
public void setoldWorksheet(int[][][] worksheet)
{
this.worksheet=new int[this.numberOfIndiv][this.period][this.numberOfJob];
for(int i=0;i<this.numberOfIndiv;i++)
for(int j=0;j<this.period;j++)
for(int k=0;k<this.numberOfJob;k++)
this.worksheet[i][j][k]=worksheet[i][j][k];
}
public void run()
{
//this.print();
this.produceCrossoverPoor();
this.implementCrossover();
//this.print();
this.adjustWorker();
//this.print();
}
public void print()
{
int t=0;
for(int i=0;i<this.numberOfIndiv;i++)
{
System.out.printf("\n\n\n\n\n");
for(int j1=0;j1<this.period;j1++)
for(int k1=0;k1<this.numberOfJob;k1++)
if(this.worksheet[i][j1][k1]!=0)
t=j1;
for(int j=0;j<t+1;j++)
{
System.out.printf("\n");
for(int k=0;k<this.numberOfJob;k++)
System.out.printf(" %d ",this.worksheet[i][j][k]);
}
}
}
public void produceCrossoverPoor()
{
int point;
int temp;
for (int i=0;i<this.numberOfIndiv;i++)
{
this.crossoverPoor[i]=i;
}
java.util.Random r=new java.util.Random();
for (int i=0;i<this.numberOfIndiv;i++)
{
point=r.nextInt(this.numberOfIndiv-i);
temp=this.crossoverPoor[i];
this.crossoverPoor[i]=this.crossoverPoor[point+i];
this.crossoverPoor[point+i]=temp;
}
}
public void implementCrossover()
{
java.util.Random r=new java.util.Random();
int index1;
int index2;
double ratioOfCrossove; /*杂交几率*/
int[][] indiva=new int[this.period][this.numberOfJob];
int[][] indivb=new int[this.period][this.numberOfJob];
for(int i=0;i<this.numberOfIndiv-1;i=i+2) /* 注意 */
{
index1=this.crossoverPoor[i];
index2=this.crossoverPoor[i+1];
ratioOfCrossove=r.nextDouble();
if(ratioOfCrossove>0.50) /*交叉概率为0.5*/
this.implementIndivCrossover(this.worksheet[index1],this.worksheet[index2]);
}
}
public void adjustWorker()
{
for(int i=0;i<this.numberOfIndiv;i++)
adjustIndivWorker(this.worksheet[i]);
}
/*
*
*/
public void implementIndivCrossover(int[][] worksheetA,int[][] worksheetB) /*shanchu x*/
{
java.util.Random r=new java.util.Random();
int mintimeOfAchieve;
int timeOfAchieveA;
int timeOfAchieveB;
int locationRow; /*交叉的起使行*/
int locationLine; /*交叉的起使列*/
int numberOfWorkerA;
int numberOfWorkerB;
int temp;
timeOfAchieveA=0;
timeOfAchieveB=0;
for(int j=0;j<this.period;j++)
for(int k=0;k<this.numberOfJob;k++)
{
if(worksheetA[j][k]!=0)
timeOfAchieveA=j;
if(worksheetB[j][k]!=0)
timeOfAchieveB=j;
}
if(timeOfAchieveA<timeOfAchieveB)
mintimeOfAchieve=timeOfAchieveA;
else
mintimeOfAchieve=timeOfAchieveB;
locationRow=r.nextInt(mintimeOfAchieve+1);
locationLine=r.nextInt(this.numberOfJob);
for(int j=locationRow;j<mintimeOfAchieve+1;j++)
{
for(int k=0;k<this.numberOfJob;k++)
{
numberOfWorkerA=worksheetA[j][k];
numberOfWorkerB=worksheetB[j][k];
for(int k1=j+1;k1<this.numberOfJob;k1++)
{
if(numberOfWorkerA==worksheetB[j][k1])
{
temp=worksheetB[j][k];
worksheetB[j][k]=worksheetB[j][k1];
worksheetB[j][k1]=temp;
break;
}
}
for(int k1=j;k1<this.numberOfJob;k1++)
{
if(numberOfWorkerB==worksheetA[j][k1])
{
temp=worksheetA[j][k];
worksheetA[j][k]=worksheetA[j][k1];
worksheetA[j][k1]=temp;
break;
}
}
}
}
}
public void adjustIndivWorker(int[][] indiv)
{
int[] presentWorkload=new int[this.numberOfJob];
int[] needsOfWorker=new int[this.numberOfJob];
int worker;
int date;
int signOfAchieve;
int location;
java.util.Random r=new java.util.Random();
for(int j=0;j<this.period;j++) /*设置人数*/
{
date=j;
worker=0;
/*work已经做成了的工作量*/
signOfAchieve=this.achieveWorkload(presentWorkload, date); /*0不能在一天内完成,1能一天内完成 */
requirePople( presentWorkload, needsOfWorker, date) ;
int sum1=0; /*shanccu*/
// System.out.printf("\n needsofworker \n\n");
for(int z=0;z<this.numberOfJob;z++)
{
sum1=sum1+needsOfWorker[z];
//System.out.printf(" % d",needsOfWorker[z]);
}
if(sum1>60)
signOfAchieve=0;
else
signOfAchieve=1;
if( signOfAchieve==0) /*不能在一天完成任务 */
{
for(int k=0;k<this.numberOfJob;k++)
{
if(needsOfWorker[k]<