柔性车间调度问题【人工智能导论项目C++实现】

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#define DECENT 4500
#define INF 65535
#define TOOL 4
#define TURN 3
#define MACHINE 6
#define TURN_SUM 12
#define NUM 45
#define PROBALITY 0.75 
#define CROSS 0.55
#define VARY 0.3 
using namespace std;
class Gene{
	public:
		Gene(){
			time = 0;
			Pro = 0;
		}
		Gene(const Gene& p){
			time = p.time;
			for(int i = 1;i <= 12;i++)que[i] = p.que[i];
			for(int i = 1;i <= 4;i++)
			for(int j = 1;j <= 3;j++)mac[i][j] = p.mac[i][j];
			Pro = p.Pro;
		}
	public:
		int que[20];
		int mac[10][10];
		double time;
		double Pro;
};
Gene g[NUM];
Gene g_cross[NUM];
Gene gs[NUM];
double MIN;
double Time[10][10][10];
int Tool[10][10][10];
int Len[10][10];
double SUM;
void Map_Make(){
	for(int i = 1;i <= TOOL;i++)
	for(int j = 1;j <= TURN;j++)
	for(int k = 1;k <= MACHINE;k++)Time[i][j][k] = INF;
	Time[1][1][1] = 2;Time[1][1][2] = 3;Time[1][1][3] = 4;
	Time[1][2][2] = 3;Time[1][2][4] = 2;Time[1][2][5] = 4;
	Time[1][3][1] = 1;Time[1][3][3] = 5;Time[1][3][2] = 4;
	Time[2][1][1] = 3;Time[2][1][3] = 5;Time[2][1][5] = 2;
	Time[2][2][1] = 4;Time[2][2][2] = 3;Time[2][2][4] = 6;
	Time[2][3][3] = 4;Time[2][3][5] = 7;Time[2][3][6] = 11;
	Time[3][1][1] = 5;Time[3][1][2] = 6;
	Time[3][2][2] = 4;Time[3][2][4] = 3;Time[3][2][5] = 5;
	Time[3][3][3] = 13;Time[3][3][5] = 9;Time[3][3][6] = 12;
	Time[4][1][1] = 9;Time[4][1][3] = 7;Time[4][1][4] = 9;
	Time[4][2][2] = 6;Time[4][2][4] = 4;Time[4][2][6] = 5;
	Time[4][3][1] = 1;Time[4][3][3] = 3;Time[4][3][6] = 3;
	
	for(int i = 1;i <= TOOL;i++)
	for(int j = 1;j <= TURN;j++)
	for(int k = 1;k <= MACHINE;k++)
	if(Time[i][j][k] != INF)Tool[i][j][Len[i][j]++] = k;
}
void Initial_Queue(){
	srand((unsigned)time(NULL));
	MIN = INF;
	int n = NUM;
	int INI[20];
	while(n--){
		int k = 0,val = 1;
	    for(int i = 0;i < 4;i++){
	       for(int j = 0;j < 3;j++)INI[k++] = val;
		   val++;
	    }
	    int len = TURN_SUM - 1;
	    for(int i = 1;i <= TURN_SUM;i++){
		   int pos = rand() % (len + 1);
		   g[n].que[i] = INI[pos];
		   INI[pos] = INI[len];
		   len--;
	    }
	}
}
void Initial_Machine(){
	int n = NUM;
	while(n--){
		for(int i = 1;i <= 4;i++)
		for(int j = 1;j <= 3;j++){
			int sle = rand() % Len[i][j];
			g[n].mac[i][j] = Tool[i][j][sle];
		}
	}
}
bool judge(int sle[NUM],int n){
	for(int i = 0;i < 2;i++)
	if(sle[i] == n)return true;
	return false;
}
void Cross(int index){
	queue<int> q1;
	queue<int> q2;
	int a = rand() % NUM;
	int b = rand() % NUM;
	Gene t1 = g[a],t2 = g[b];
	int temp1[20],temp2[20];
	for(int i = 1;i <= 12;i++){
		temp1[i] = -1;
		temp2[i] = -1;
	}
	int ini[5],sle[NUM];
	for(int i = 0;i < 4;i++)ini[i] = i + 1;
	int len = 3,k = 0;
	for(int i = 0;i < 2;i++){
		int pos = rand() % (len + 1);
		sle[k++] = ini[pos];
		ini[pos] = ini[len];
		len--;
	}
	len = k;
	for(int i = 1;i <= 12;i++){
		if(judge(sle,t1.que[i])){
			temp1[i] = t1.que[i];
			q1.push(t1.que[i]);
		}
		if(!judge(sle,t2.que[i])){
			q2.push(t2.que[i]);
			temp2[i] = t2.que[i];
		}
	}
	for(int i = 1;i <= 12;i++){
		if(temp1[i] == -1){
			temp1[i] = q2.front();
			q2.pop();
		}
		if(temp2[i] == -1){
			temp2[i] = q1.front();
			q1.pop();
		}
	}
	for(int i = 1;i <= 12;i++){
		t1.que[i] = temp1[i];
		t2.que[i] = temp2[i];
	}
	int P = rand() % 2;
	if(P)g_cross[index] = t1;
	else g_cross[index] = t2;
}
void Copy(){
	for(int i = 0;i < NUM;i++)
	g[i] = g_cross[i];
}
void Vary(){
	int pos = rand() % NUM;
	int pos1 = (rand() % 4) + 1;
	int pos2 = (rand() % 3) + 1;
	int pos3 = rand() % Len[pos1][pos2];
	g[pos].mac[pos1][pos2] = Tool[pos1][pos2][pos3];
}
void Change(){
	double chance = (double)(rand() % 10) / 10;
	if(chance < CROSS){
		for(int i = 0;i < NUM;i++)Cross(i);
		Copy();
	}
	chance = (double)(rand() % 10) / 10;
	if(chance < VARY)Vary();
}
double Get_Time(int index){
	double max = 0;
	double mp[10];
	int cnt[10];
	double tool_time[20];
	for(int i = 1;i <= 4;i++){
		cnt[i] = 1;
		tool_time[i] = 0;
	}
	for(int i = 1;i <= 6;i++)mp[i] = 0;
	for(int i = 1;i <= 12;i++){
		int pos = g[index].que[i];
		int m = g[index].mac[pos][cnt[pos]];
		double use = Time[pos][cnt[pos]][m];
		if(tool_time[pos] <= mp[m]){
			mp[m] += use;
			tool_time[pos] = mp[m];
		}
		else {
			mp[m] = tool_time[pos] + use;
			tool_time[pos] = mp[m];
		}
		if(tool_time[pos] > max)max = tool_time[pos];
		cnt[pos]++;
	}
	g[index].time = max;
	g[index].Pro = pow(max,-0.2);
	return g[index].Pro;
}
bool cmp(Gene a,Gene b){
	return a.Pro < b.Pro;
}
void Cal_Time(){
	
	SUM = 0;
	for(int i = 0;i < NUM;i++)SUM += Get_Time(i);
	for(int i = 0;i < NUM;i++)g[i].Pro = g[i].Pro/SUM;
	sort(g,g + NUM,cmp);
	//for(int i = 0;i < NUM;i++)cout<<"time "<<g[i].time<<"Pro: "<<g[i].Pro<<endl;
	cout<<"本代个体种类: ";
}
void Select(int index){

    double chance = 0;
	while(chance < PROBALITY)chance = (double)(rand() % 100) / 100.0; 
    double area = 0;
    for(int i = 0;i < NUM;i++){
    	area += g[i].Pro;
    	if(chance >= area)continue;
        else
        {
            gs[index] = g[i];
            if(MIN > g[i].time)MIN = g[i].time;
            return;
        }
    }
}
void Merge(){
	for(int i = 0;i < NUM;i++)g[i] = gs[i];
}
void Show(int total){
	int sum = DECENT;
	cout<<"遗传代数: "<<sum - total<<" 全局最小值: "<<MIN<<endl;
	int min = INF,max = 0;
	for(int i = 0; i < NUM; i++){
		if(min > g[i].time)min = g[i].time;
		if(max < g[i].time)max = g[i].time;
	};
	cout<<"本代最小值: "<<min<<" 本代最大值: "<<max<<endl;
	cout<<endl;
}
void Inherient(){
	int total = DECENT;
	while(total--){
		Change();
		Cal_Time();
		for(int i = 0;i < NUM;i++)Select(i);
		cout<<endl;
		Merge();
		Show(total);
		if(MIN == 17){
			cout<<"Found The Min Answer: "<<MIN<<endl;
			return;
		} 
	}
}
int main(){
	Map_Make();
	Initial_Queue();
	Initial_Machine();
	Inherient();
	if(MIN != 17)cout<<"Not Found The Min"<<endl; 
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值