/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package FT06;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
/**
*
* @author Administrator
*/
public class FT06 {
public static int Popsize=20;
public double v=0.9;//退温速率
public double Pr=0.1;
public static int n=6;//工件数量
public static int m=6;//机器数量
public int[][] J={{3,1,2,4,6,5},{2,3,5,6,1,4},{3,4,6,1,2,5},{2,1,3,4,5,6},{3,2,5,6,1,4},{2,4,6,1,5,3}};/*
机器矩阵*/
public static int[][] T={{3,10,9,5,3,10},{6,8,1,5,3,3},{1,5,5,5,9,1},{7,4,4,3,1,3},{6,10,7,8,5,4},{3,10,8,9,4,9}};/*
加工时间矩阵*/
public int[]t=new int[Popsize];//种群各染色体加工时间
public int []tBest=new int[1];//种群中最佳时间
public int []scheduling=new int[n*m];//种群最优调度编码
public int [][]parentpopulation;//父代种群
public int[][]childpopulation=new int[Popsize][n*m];//子代种群
public int Kindex=0;//子代行数
public int[]Best=new int[n*m+1];
public int[][]selectpopulation=new int[2*Popsize][n*m+1];
public int[][]Psize=new int[Popsize][n*m+1];
public int finalgeneration=100;
public int[][]Finalbest=new int[finalgeneration][n*m+1];
public int [] Bianma(){
int k=0;
int l;
int o;
int[][]x=new int[n][m];
int []s=new int [n*m];
do{
do{
l=(int)(Math.random()*6+1);
o=J[l-1][0];
}
while(o==0);
s[k]=l;
k=k+1;
for(int i=0;i<J[0].length;i++){
if(i<J[0].length-1){
J[l-1][i]=J[l-1][i+1];
}else{
J[l-1][i]=0;
}
}
}
while(!Arrays.deepEquals(J,x));
return s;
}
public void shuaxin(){
int[][]J={{3,1,2,4,6,5},{2,3,5,6,1,4},{3,4,6,1,2,5},{2,1,3,4,5,6},{3,2,5,6,1,4},{2,4,6,1,5,3}};
this.J=J;
}
public int[][] population(){
shuaxin();
int[][]xx=new int[Popsize][n*m];
for(int i=0;i<Popsize;i++){
xx[i]=Bianma();
shuaxin();
}
/*for (int k = 0; k < Popsize; k++) {
for (int j = 0; j < n * m; j++) {
System.out.print(xx[k][j]);
}
System.out.println("");
}*/
this.parentpopulation=xx;
return xx;
}
public int[] decoding(int [][]Chromosome){
int []time=new int[Chromosome.length];
for(int i=0;i<Chromosome.length;i++){
int []k=new int[n];
int []Jmin=new int [n];
int [][]MT=new int[m][2*n+1];
int []y=new int[m];//控制机器加工次数索引
int[][]MJ=new int[m][n];//机器工件排列阵
for(int j=0;j<n*m;j++){
int Jindex=Chromosome[i][j]-1;//工件在矩阵中索引
int x=J[Jindex][k[Jindex]];//加工机器号
k[Jindex]=k[Jindex]+1;
int Tp=T[x-1][Jindex];
y[x-1]=y[x-1]+2;
for(int ii=0;ii<=y[x-1]-2;ii=ii+2){
if(Math.max(Jmin[Jindex],(MT[x-1][ii]))+Tp<=MT[x-1][ii+1]){
for(int kk=y[x-1];kk>ii+2;kk=kk-2){
MT[x-1][kk-1]=MT[x-1][kk-3];
MT[x-1][kk]=MT[x-1][kk-2];
MJ[x-1][kk/2-1]=MJ[x-1][kk/2-2];
}
MJ[x-1][ii/2]=Jindex+1;
MT[x-1][ii+1]=Math.max(Jmin[Jindex],(MT[x-1][ii]));
MT[x-1][ii+2]=MT[x-1][ii+1]+Tp;
Jmin[Jindex]=Math.max(Jmin[Jindex],(MT[x-1][ii]))+Tp;//刷新最早加工时间
break;
}else {if(ii==y[x-1]-2){
MJ[x-1][y[x-1]/2-1]=Jindex+1;
MT[x-1][y[x-1]-1]=Math.max(Jmin[Jindex],MT[x-1][y[x-1]-2]);
MT[x-1][y[x-1]]=MT[x-1][y[x-1]-1]+Tp;
Jmin[Jindex]=Math.max(Jmin[Jindex],MT[x-1][y[x-1]-2])+Tp;
}
}
}
}
for(int o=0;o<m;o++){
for(int p=0;p<2*n+1;p++){
System.out.print(MT[o][p]+" ");
}
System.out.println("");
}//打印加工时间矩阵
/*System.out.println("");
for(int o=0;o<m;o++){
for(int p=0;p<n;p++){
System.out.print(MJ[o][p]+" ");
}
System.out.println("");
}
System.out.println("");//打印加工工件排列阵*/
int[]tt=new int[m];
for(int z=0;z<m;z++){
tt[z]=MT[z][2*n];
}
Arrays.sort(tt);
time[i]=tt[m-1];
}
int best=time[0];
for(int i=0;i<Chromosome.length;i++){
if(time[i]<=best){
best=time[i];
tBest[0]=time[i];
scheduling=Chromosome[i];
}
}
/*System.out.print(tBest[0]);
System.out.println("");
for(int k=0;k<n*m;k++){
System.out.print(scheduling[k]);
}
System.out.println("");*/
this.t=time;
return time;
}
public void cross(){
int x=(int)(Math.random()*Popsize);
int[]select1=parentpopulation[x];
int[]select2=scheduling;
int[] a = { 1, 2, 3, 4, 5, 6 };
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i:a) {
list.add(i);
}
int[]b=new int[list.size()];
Collections.shuffle(list);
Integer []array=new Integer[list.size()];
list.toArray(array);
for(int i=0;i<a.length;i++){
b[i]=array[i].intValue();
}
int c=(int)(Math.random()*(a.length-1))+1;
int[] sub1 = Arrays.copyOf(b, c);//将工件集合分成两个互补集合
int[] sub2 = Arrays.copyOfRange(b, c, b.length);//确保遗传算子合法性
/*for(int i=0;i<c;i++){
System.out.print(sub1[i]);
}
System.out.println("");
for(int i=0;i<b.length-c;i++){
System.out.print(sub2[i]);
}
System.out.println("");
System.out.print(c);
System.out.println("");打印随机分成的子集*/
int g=0;//控制列寻找满足条件值
int gg=0;
for(int i=0;i<n*m;i++){
for(int j=0;j<c;j++){
if(parentpopulation[x][i]==sub1[j]){
childpopulation[Kindex][g]=parentpopulation[x][i];
g=g+1;
}
if(scheduling[i]==sub1[j]){
childpopulation[Kindex+1][gg]=scheduling[i];
gg=gg+1;
}
}
}
for(int i=0;i<n*m;i++){
for(int j=0;j<b.length-c;j++){
if(scheduling[i]==sub2[j]){
childpopulation[Kindex][g]=scheduling[i];
g=g+1;
}
if(parentpopulation[x][i]==sub2[j]){
childpopulation[Kindex+1][gg]=parentpopulation[x][i];
gg=gg+1;
}
}
}
}
public void freshbest(){
Best[0]=tBest[0];
for(int i=0;i<n*m;i++){
Best[i+1]=scheduling[i];
}
int p[];
p=decoding(childpopulation);
for(int i=0;i<childpopulation.length;i++){
System.out.println(p[i]);
}
if(tBest[0]<=Best[0]){
Best[0]=tBest[0];
评论1