首页 / C++ / C++编程实现对工厂产品生产流程的模拟
C++编程实现对工厂产品生产流程的模拟
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++编程实现对工厂产品生产流程的模拟,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含101306字,纯文字阅读大概需要145分钟。
内容图文
花费二个多月的时间编写了可以实时模拟工厂产品生产流程的程序,工厂产品生产流程的模拟,就是计算在工艺文件所规定的工序下,不同种类的多件产品(同一类别的产品可以有多件)在不同类别的多台设备(同一类别的设备可以有多台)上全部生产完毕所需的总时间。每一件产品可以在生产流程中先后多次在同一类设备上生产而且生产时间可以不同,某一给定时刻多件产品有可能需要在同一台设备上生产,造成设备占用冲突,这时必须按一定的算法对造成冲突的产品在设备等待队列上按优先级排队。产品在设备上加工完毕准备进入下一台设备生产时有同类型的多台设备可供选择,此时必须设计算法选择加工该产品的设备使生产时间尽可能短。每一件产品在生产前可能会有生产准备时间,每一件产品在某一台设备上生产完毕后可能会存在自然时效时间。在生产过程中的任意时刻可以插入待生产的多件新产品,每一台设备都有与之关联的模具,每件模具可以生产特定种类的产品,模具可以由多个零件组成,一个零件又可以作为多件模具的组成部件。设备每过一段时间就要调整或维修,维修分为小修和大修,小修大修时间以及小修大修时间的时间间隔有明确的规定,不会改变。我们需要在以上诸多条件的限制和作用下通过编写程序让计算机计算产品全部生产完成且尽可能接近最短的生产时间,以为工厂处理客户订单提供参考和依据。要注意的是,本文要解决的问题和车间作业计划调度问题有本质区别,后者为NPC问题,需要设计算法自行安排产品在各设备上的加工顺序,使生产时间最短。而在前者中,产品在设备上的加工顺序由优先级关系给定,任务是模拟生产流程,在生产过程中有订单插入的情况下估算生产时间,以判断生产能力能否满足客户需求。笔者不自量力也专门思考过车间调度问题,但发现难度实在太大(光是解空间候选解的数量就让人望而却步),绞尽脑汁仍束手无策,在网上查找过相关资料,了解到最好的精确算法也是指数级的,而且解决问题的方法是在解空间中穷举,于是笔者就放弃了这一难题,转而研究当前问题,当然如果日后P=NP被否证笔者也许会好受一点,至少自信心不会受到打击。
本问题具体的限制条件的详细说明如下:
与订单处理有关的说明及约束条件
1、某工厂有若干种型号的设备,每种型号设备数量不等有若干台;每台设备有 “在线”(可用)和“下线”(不可用)两种状态。
2、工厂可以生产多种产品,每种产品对应一个产品生产工艺(称,由若干零件加工工艺、调试工艺和组装工艺文件组成)。工厂可能同时生产几种产品。每种产品都是由若干零、部件(部件由几个零件组合而成)组装而成,不同产品可以有相同的零部件。
产品生产工艺:又称为产品工艺文件,由若干零部件加工工艺文件组成,是从原材料到产品的生产过程,包含确定需要哪些设备、使用的顺序、占用的时间等内容(通常占用时间包括设备准备时间、加工时间)。
3、每种产品的每个零件也都有自己的零件生产工艺(称为零件加工工艺文件),各零件生产工艺不同,需要使用到的设备可能不同也可能相同,使用的先后顺序也不同,占用的时间也可能不同。由于零件的加工是且只能是按工艺文件中的工序顺序进行的,其加工周期等于加工工艺文件规定的各个工序加工时间的和。当某一零件加工过程中一道工序完毕进入下一道工序时,这道工序使用的设备如果被别的零件占用,则该零件只能等待而不能跳过这道工序。
零件加工工艺文件,规定从原材料到零件成品的加工步骤即工序,确定各工序使用哪些设备,需要的时间等参数。
4、某些工艺文件涉及到模具。通常模具与加工工艺的某一工序有关。模具也有两种状态“在线”(使用中)、“下线”(闲置)。模具可以是单件,也可以是由零件组合而成。这些零件的不同组合形成不同的模具。任何时刻,模具都只与一个零件关联,此时,可以与设备关联,也可能不与设备关联,具体要以工艺文件的规定来定。
5、如果一个产品的各零部件同时生产,则产品生产周期等于加工时间最长的零部件生产时间加上装配时间、调试时间、包装时间和运输时间;否则,产品生产周期等于第一个零件加工开始到最后一个零部件加工完成所花费的时间,加上装配时间、调试时间、包装时间和运输时间
6、任何时刻,一台设备只能加工一个零件。
7、工厂有多个用户,通常以用户购买工厂产品多少来确定用户的优先级,意味着多个用户同时提交订单时,优先级高的用户的订单会被优先处理,而且,这些用户的临时紧急需求,也需要尽量被满足。因此处理订单时,优先安排优先级高的用户,待其订单满足后再处理优先级较低的用户的订单。
八个初始化文件负责向程序提供输入数据,他们分别为:
StateQuery RepairTimeOfDevice ProductCategoryWorkingProcedure PriorityProducePerpareTime Mould Insert Device Component
StateQuery 规定每台设备在开始时刻最近一次进行的是大修还是小修
RepairTimeOfDevice 规定各设备类别下的设备的大修小修时间,以及大修小修之间的时间间隔
ProductCategoryWorkingProcedure 规定各产品类别在工艺文件描述的工序中需要经历的各设备的设备类别及对应的自然时效时间
PriorityProducePerpareTime 规定不同类别的设备上各类产品的生产优先级以及不同类别的设备在不同类别的产品对应的生产序列中的下标及相应的生产准备时间和生产时间
Mould 规定各模具的属性和状态
Insert 规定要插入产品的时间点及时间点下要插入的产品以及产品要插入的设备的类别
Device 规定与设备关联的模具的状态
Component 规定组成模具的零件的属性和状态
上述8文件格式规范:
ProductCategoryWorkingProcedure 各产品类别及对应的工艺流程的集合 注意各文件中的数据必须彼此相符,不能出现矛盾和冲突,否则程序的行为是未定义的!
设备编号即为"设备类别编号+设备序号" 产品编号即为"产品类别编号+产品序号" 模具编号即为"模具"能够生产的产品类别编号+模具序号
#b1
产品类别
#b2
设备类别 该产品在该设备上生产完成后的自然时效时间 ---
#e2
#e1
---
PriorityProducePerpareTime 设备类别和其能够生产的产品类别及对应的生产时间优先级构成的集合,外层map关键字为产品类别编号,映射值为映射表,即为该类别的设备在该产品类别的工艺流程表中对应各下标及相应的生产准备时间和生产时间
#b1
设备类别
#b2
产品类别
#b3
优先级
下标 生产准备时间 生产时间
---
#e3
#e2
---
#e1
---
RepairTimeOfDevice 设备类别和维修时间之间的映射关系
#b1
设备类别编号
#b2
小修至大修时间间隔 大修至小修的时间间隔 小修时长 大修时长
#e2
#e1
---
StateQuery 设备编号(关键字)和设备状态之间的映射关系
#b1
设备编号
#b2
最近一次进行的是大修还是小修的标志,0小修,1大修
#e2
#e1
---
Device 设备属性
#b1
设备编号
#b2
产品类别 模具编号 模具状态(T上线F下线)
---
#e2
#e1
---
Component 零件编号和其属性的映射关系
#b1
零件编号
#b2
该零件占用的模具能够生产的产品类别编号 该零件占用的模具的模具序号
#e2
#b3
由该零件组成的模具的编号的集合
#e3
#e1
---
Mould 模具编号和其属性的映射关系
#b1
模具编号
#b2
模具所属设备编号 模具状态(T上线F下线)
#e2
#b3
组成模具的零件编号 零件是否被该模具占用的标志(T是F否)
---
#e3
#e1
---
Insert 设备类别和各时间点下要插入的产品的对应关系
#b 插入产品的时间点,第一个必须为0 --- #e
#b1
带插入设备类别
#b对应时间点下要插入的产品编号的集合#e
---
#e1
---
上述8文件输入样例:
ProductCategoryWorkingProcedure.txt
#b1
1
#b2
1 4 2 4 1 4
#e2
#e1
#b1
2
#b2
2 4 1 4 2 4
#e2
#e1
PriorityProducePerpareTime.txt
#b1
1
#b2
1
#b3
3
0 4 32
2 4 36
#e3
#e2
#b2
2
#b3
4
1 4 40
#e3
#e2
#e1
#b1
2
#b2
2
#b3
4
0 4 32
2 4 36
#e3
#e2
#b2
1
#b3
3
1 4 40
#e3
#e2
#e1
RepairTimeOfDevice.txt
#b1
1
#b2
48 52 56 60
#e2
#e1
#b1
2
#b2
44 48 52 56
#e2
#e1
StateQuery.txt
#b1
1+1
#b2
0
#e2
#e1
#b1
1+2
#b2
0
#e2
#e1
#b1
2+1
#b2
1
#e2
#e1
#b1
2+2
#b2
1
#e2
#e1
Device.txt
#b1
1+1
#b2
1 1+1 T
2 2+2 F
#e2
#e1
#b1
1+2
#b2
1 1+3 F
2 2+4 T
#e2
#e1
#b1
2+1
#b2
2 2+1 F
1 1+2 F
#e2
#e1
#b1
2+2
#b2
2 2+3 T
1 1+4 F
#e2
#e1
Component.txt
#b1
1
#b2
2 3
#e2
#b3
2+3
#e3
#e1
#b1
2
#b2
2 3
#e2
#b3
2+3
#e3
#e1
#b1
3
#b2
1 1
#e2
#b3
1+1 1+2
#e3
#e1
#b1
4
#b2
1 1
#e2
#b3
1+1 2+1
#e3
#e1
#b1
5
#b2
2 1
#e2
#b3
2+1 2+2
#e3
#e1
#b1
6
#b2
1 2
#e2
#b3
1+2
#e3
#e1
#b1
7
#b2
2 2
#e2
#b3
1+3 2+2
#e3
#e1
#b1
8
#b2
1 3
#e2
#b3
1+3 1+4
#e3
#e1
#b1
9
#b2
1 4
#e2
#b3
1+4
#e3
#e1
#b1
10
#b2
2 4
#e2
#b3
2+4
#e3
#e1
#b1
11
#b2
2 4
#e2
#b3
2+4
#e3
#e1
Mould.txt
#b1
1+1
#b2
1+1 T
#e2
#b3
3 T
4 T
#e3
#e1
#b1
1+2
#b2
2+1 F
#e2
#b3
3 F
6 T
#e3
#e1
#b1
1+3
#b2
1+2 F
#e2
#b3
7 F
8 T
#e3
#e1
#b1
1+4
#b2
2+2 F
#e2
#b3
8 F
9 T
#e3
#e1
#b1
2+1
#b2
2+1 F
#e2
#b3
4 F
5 T
#e3
#e1
#b1
2+2
#b2
1+1 F
#e2
#b3
5 F
7 T
#e3
#e1
#b1
2+3
#b2
2+2 T
#e2
#b3
1 T
2 T
#e3
#e1
#b1
2+4
#b2
1+2 T
#e2
#b3
10 T
11 T
#e3
#e1
Insert.txt
#b 0 16 17 18 19 20 21 #e
#b1
1
#b 1+1 1+2 1+4 1+10 #e
#b 1+3 1+5 1+6 1+11 #e
#b 1+7 1+8 1+9 1+12 #e
#b 1+13 1+14 1+15 1+16 #e
#b 1+17 1+18 1+21 1+22 #e
#b 1+19 1+20 1+23 1+24 #e
#b 1+28 1+25 1+26 1+27 #e
#e1
#b1
2
#b 2+1 2+2 2+5 2+10 #e
#b 2+4 2+6 2+7 2+11 #e
#b 2+3 2+8 2+9 2+12 #e
#b 2+13 2+14 2+15 2+16 #e
#b 2+17 2+18 2+19 2+20 #e
#b 2+21 2+22 2+23 2+24 #e
#b 2+25 2+26 2+27 2+28 #e
#e1
本程序编译环境viusal studio 2017 将八个初始化文件放置在源文件文件所在目录下编译运行即可得到结果
本生产周期计算程序用C++语言编写,代码有2800余行,是博主个人心血的结晶,博主原本不想公开源码,但代码的使用价值不高,意义不大,所以左思右想后还是决定公开源码。请大家尊重博主的劳动成果,在转载时务必注明原文地址和作者ID。由于代码量较为庞大,所以可能隐藏着尚未发现的未知的BUG和漏洞,如果在对程序进行暴力测试过程中发现程序运行结果明显有误或程序运行崩溃以及程序陷入死循环,请及时在评论区留言并向我提供测试数据,我会及时修改源码完善程序,谢谢大家。
源代码(C++) 编译环境Visual Studio 2017 IDE
1 #include "stdafx.h" 2 #include <iostream> 3 #include <vector> 4 #include <map> 5 #include <set> 6 #include <string> 7 #include <list> 8 #include <algorithm> 9 #include <memory> 10 #include <fstream> 11 #include <time.h> 12 #include <cstdlib> 13usingnamespace std; 14 15struct comparatorless 16{ 17booloperator()(conststring &s1, conststring &s2) const; //重载函数调用运算符比较编号和类别编号或编号和编号,判断s1是否小于s2 18}; 19bool comparatorless::operator()(conststring &s1, conststring &s2) const 20{ 21if (s2.find_first_of(‘+‘) == string::npos) //类别编号s2和编号s1比较 22 { 23return s1.substr(0, s1.find_first_of(‘+‘)) < s2; 24 } 25else//编号s1和编号s2比较 26 { 27string::size_type index = s2.find_first_of(‘+‘, 0); 28string prefixP = s2.substr(0, index); //提取s2以+分隔的前后缀 29string suffixP = s2.substr(index + 1); 30 31 index = s1.find_first_of(‘+‘, 0); 32string prefix = s1.substr(0, index); //提取s1以+分隔的前后缀 33string suffix = s1.substr(index + 1); 34 35if (prefix < prefixP) 36 { 37returntrue; 38 } 39else 40 { 41if (prefix == prefixP) 42 { 43if (suffix < suffixP) 44 { 45returntrue; 46 } 47else 48 { 49returnfalse; 50 } 51 } 52else 53 { 54returnfalse; 55 } 56 } 57 } 58} 59 60//初始化过程即构造函数实现未定义,第一次运行前再定义,初始GCDTime的计算,类定义实现放入头文件 61struct CodePriorityNAT //设备等待队列中或设备上正在生产的产品的编号,在该设备上的生产优先级以及在上一个设备上生产完毕后已流逝的自然时效时间 62{ 63booloperator<(const CodePriorityNAT &P) const { return ProductionPriority < P.ProductionPriority; } //重载关系运算符用优先级比较实现CodePriorityNAT对象比较 64booloperator>(const CodePriorityNAT &P) const { return ProductionPriority > P.ProductionPriority; } 65booloperator>=(const CodePriorityNAT &P) const { return !(operator<(P)); } 66booloperator<=(const CodePriorityNAT &P) const { return !(operator>(P)); } 67booloperator==(const CodePriorityNAT &P) const { return !(operator<(P)) && !(operator>(P)); } 68booloperator!=(const CodePriorityNAT &P) const { return !(operator==(P)); } 69string ProductCode; //产品编号 70int ProductionPriority; //生产优先级 71int NaturalAgingTime; //已流逝的自然时效时间 72 CodePriorityNAT(string P1, int P2, int N) :ProductCode(P1), ProductionPriority(P2), NaturalAgingTime(N) {} 73 CodePriorityNAT() = default; 74}; 75 76class Priority_Queue //设备等待队列类(优先级队列),队列数据元素为CodePriorityNAT 77{ 78public: 79 typedef list<CodePriorityNAT>::iterator iterator; 80 Priority_Queue() = default; 81 ~Priority_Queue() = default; 82 pair<bool, Priority_Queue::iterator> Insert(const CodePriorityNAT &x); //插入操作,返回的pair的first指示插入是否成功,second为指向插入元素的迭代器 83bool RemoveTop(CodePriorityNAT &x); //删除最高优先级元素并用x将其返回 84bool GetTop(CodePriorityNAT &x) const; //获取最高优先级元素并用x将其返回 85void MakeEmpty() { Queue.clear(); } //清空队列 86bool isEmpty() const { return Queue.empty(); } //判断队列是否为空 87bool isFull() const {return Queue.size() == Queue.max_size(); } //判断队列是否已满 88 Priority_Queue::iterator erase(const Priority_Queue::iterator &p) { return Queue.erase(p); } //删除队列中p所指元素返回被删元素的下一元素 89 Priority_Queue::iterator insert(const Priority_Queue::iterator &p, const CodePriorityNAT &c) { return Queue.insert(p, c); } //将c插入至p所指位置,返回指向插入元素的迭代器 90 list<CodePriorityNAT>::size_type GetSize() const { return Queue.size(); } //获取队列实际大小 91 iterator begin() { return Queue.begin(); } //获取指向队列最高优先级元素的迭代器 92 iterator end() { return Queue.end(); } //获取队列尾后迭代器 93 94private: 95 list<CodePriorityNAT>::iterator adjust(); //新元素加入队列后调整元素位置,使队列中各元素保持优先级关系 96 list<CodePriorityNAT> Queue; 97}; 98 99 list<CodePriorityNAT>::iterator Priority_Queue::adjust() 100{ 101 CodePriorityNAT temp = Queue.back(); 102 auto p = Queue.end(); 103 --p; 104 p=Queue.erase(p); 105 106if (Queue.begin() != p) 107 --p; 108else 109 { 110return Queue.insert(p, temp); 111 } 112 113while (true) 114 { 115if ((*p) > temp) 116 { 117if (p != Queue.begin()) 118 { 119 --p; 120if (p == Queue.begin()) 121continue; 122 } 123 } 124else 125 { 126 ++p; 127return Queue.insert(p, temp); 128 } 129 130if (p == Queue.begin()) 131break; 132 } 133return Queue.insert(p, temp); 134} 135 136 pair<bool, Priority_Queue::iterator> Priority_Queue::Insert(const CodePriorityNAT &x) 137{ 138if (isFull()) 139return {false, end()}; 140else 141 { 142 Queue.push_back(x); 143return { true, adjust() }; 144 } 145} 146 147bool Priority_Queue::RemoveTop(CodePriorityNAT &x) 148{ 149if (isEmpty()) 150returnfalse; 151else 152 { 153 x = Queue.front(); 154 Queue.pop_front(); 155returntrue; 156 } 157} 158 159bool Priority_Queue::GetTop(CodePriorityNAT &x) const 160{ 161if (isEmpty()) 162returnfalse; 163else 164 { 165 x = Queue.front(); 166returntrue; 167 } 168} 169 170struct PrepareTimeProduceTime 171{ 172int PrepareTime; //生产准备时间 173int ProduceTime; //实际生产时间 174 PrepareTimeProduceTime(int Pre, int Pro) :PrepareTime(Pre), ProduceTime(Pro) {} 175 PrepareTimeProduceTime() = default; 176}; 177 178struct ProductionTimeAndPriority 179{ 180 181 map<int, PrepareTimeProduceTime> ProductionTime; //当前类别的设备在在ProductClassTimePriorityMappingTable中的给定的产品类别对应的生产工序vector中的下标(map关键字)及对应的准备及生产时间(映射值) 182int Priority; //给定类别的产品在当前设备的上的生产优先级 183}; 184 185struct DeviceCategoryAndNAT 186{ 187string DeviceCategoryCode; //设备类别编号 188int NAT; //当前类别的产品在该设备上生产完毕后的自然时效时间 189 DeviceCategoryAndNAT(string De, int N) :DeviceCategoryCode(De), NAT(N) {} 190}; 191 192class Product 193{ 194public: 195string DeviceID; //产品正在占用或等待的设备编号 196int index; //上述DeviceID标识的设备的设备类别在当前产品的类别对应的产品工艺流程vector中的下标 197int time; //产品正在等待DeviceID标识的设备为0,否则为产品已占用DeviceID标识的设备的时间 198int flag; //产品正在占用设备为1,产品正在等待设备为0 199static map<string, vector<DeviceCategoryAndNAT>> ProductCategoryWorkingProcedure; //各产品类别及对应的工艺流程的集合 200staticvoid initProductCategoryWorkingProcedure(); //初始化ProductCategoryWorkingProcedure 201 Product(string D, int i, int t, int f) :DeviceID(D), index(i), time(t), flag(f) {} 202}; 203 204void Product::initProductCategoryWorkingProcedure() //解析当前目录下ProductCategoryWorkingProcedure.txt从中提取初始化数据初始化ProductCategoryWorkingProcedure 205{ 206 ifstream input("ProductCategoryWorkingProcedure.txt"); 207string temp; 208int level; 209int flag; 210 map<string, vector<DeviceCategoryAndNAT>>::iterator p1; 211while (input >> temp) 212 { 213if (temp == "#b1" || temp == "#e1") 214 { 215if (temp == "#b1") 216 { 217 level = 1; 218 } 219else 220 { 221continue; 222 } 223 } 224else 225 { 226if (temp == "#b2" || temp == "#e2") 227 { 228if (temp == "#b2") 229 { 230 level = 2; 231 flag = 0; 232 } 233else 234 { 235continue; 236 } 237 } 238else 239 { 240if (level == 1) 241 { 242 p1 = ProductCategoryWorkingProcedure.insert(make_pair(temp, vector<DeviceCategoryAndNAT>())).first; 243 } 244else 245 { 246if (flag == 0) 247 { 248 p1->second.push_back(DeviceCategoryAndNAT(temp, 0)); 249 flag = 1; 250 } 251else 252 { 253 p1->second.back().NAT = stoi(temp); 254 flag = 0; 255 } 256 } 257 } 258 } 259 } 260} 261 262 map<string, vector<DeviceCategoryAndNAT>> Product::ProductCategoryWorkingProcedure; 263 264struct Mouldstate 265{ 266string MouldCode; //与产品类别对应的模具编号 267bool isOn; //标志模具状态为上线下线的bool变量(T上线F下线) 268 Mouldstate(string Mo, bool isOn) :MouldCode(Mo), isOn(isOn) {} 269 Mouldstate() = default; 270}; 271 272class Device 273{ 274public: 275static map<string, map<string, ProductionTimeAndPriority>> DeviceCategorySet; //设备类别和其能够生产的产品类别及对应的生产时间优先级构成的集合,外层关键字映射值为映射表,关键字为产品类别编号,映射值为该类别的设备能够生产的产品类别,及该类别的设备在该产品类别的工艺流程表中对应各下标及生产时间 276staticvoid initDeviceCategorySet(); //初始化DeviceCategorySet 277 Priority_Queue WaitingQueue; //当前设备的等待队列 278set<CodePriorityNAT> ProductisProducing; //设备当前正在生产的零件对应的三元组,set中只能有一个元素 279bool isTakenUp; //标志当前设备是否被占用,T占用(有产品生产或处于调整状态),F闲置 280string LatestProductCategory; //该设备最近一次生产完的产品的类别编号 281 map<string, Mouldstate> MouldOnDevice; //映射表,保存模具能够生产的产品类别(关键字)和模具及模具状态的对应关系 282}; 283 284void Device::initDeviceCategorySet() ////解析当前目录下PriorityProducePerpareTime.txt从中提取初始化数据初始化ProductCategoryWorkingProcedure 285{ 286 ifstream input("PriorityProducePerpareTime.txt"); 287string temp; 288int level; 289int rowinlevel; 290 map<string, map<string, ProductionTimeAndPriority>>::iterator p1; 291 map<string, ProductionTimeAndPriority>::iterator p2; 292 map<int, PrepareTimeProduceTime>::iterator p3; 293while (input >> temp) 294 { 295if (temp == "#b1" || temp == "#e1") 296 { 297if (temp == "#b1") 298 { 299 level = 1; 300 rowinlevel = 1; 301 } 302else 303 { 304continue; 305 } 306 } 307else 308 { 309if (temp == "#b2" || temp == "#e2") 310 { 311if (temp == "#b2") 312 { 313 level = 2; 314 rowinlevel = 1; 315 } 316else 317 { 318continue; 319 } 320 } 321else 322 { 323if (temp == "#b3" || temp == "#e3") 324 { 325if (temp == "#b3") 326 { 327 level = 3; 328 rowinlevel = 1; 329 } 330else 331 { 332continue; 333 } 334 } 335else 336 { 337if (level == 1) 338 { 339 p1 = DeviceCategorySet.insert(make_pair(temp, map<string, ProductionTimeAndPriority>())).first; 340 } 341elseif (level == 2) 342 { 343 p2 = p1->second.insert(make_pair(temp, ProductionTimeAndPriority())).first; 344 } 345elseif (level == 3) 346 { 347if (rowinlevel == 1) 348 { 349 p2->second.Priority = stoi(temp); 350 } 351else 352 { 353if (rowinlevel % 3 == 2) 354 { 355 p3 = p2->second.ProductionTime.insert(make_pair(stoi(temp), PrepareTimeProduceTime())).first; 356 } 357elseif (rowinlevel % 3 == 0) 358 { 359 p3->second.PrepareTime = stoi(temp); 360 361 } 362else 363 { 364 p3->second.ProduceTime = stoi(temp); 365 } 366 367 } 368 ++rowinlevel; 369 } 370 } 371 } 372 } 373 } 374} 375 376 map<string, map<string, ProductionTimeAndPriority>> Device::DeviceCategorySet; 377 378class mould //模具类 添加关键字对象比较 379{ 380public: 381 map<string, bool> AsPartComponent; //映射表,内容为组成模具的零件编号和标志零件是否被占用的标志变量之间的映射关系 382string ProductIsProducing; //正在使用该模具生产的产品编号 383string DeviceBelongTo; //模具所属设备编号 384bool MouldState; //模具状态,T上线,F下线 385}; 386 387class Component //零件类添加关键字对象比较 388{ 389public: 390set<string> MouldBelongTo; //零件所属模具编号的集合 391string ComponentCorrespondingMouldClass; //该零件占用的模具能够生产的产品类别编号 392string MouldSerialNumber; //该零件占用的模具序号 393}; 394 395struct ProductInsertion 396{ 397int InsertTime; //产品插入时间 398set<string> ProductToInsert; //时间InsertTime下要插入的产品的编号构成的集合 399 ProductInsertion(int I) :InsertTime(I) {} 400}; 401 402struct IncompleteOptimization 403{ 404string ProductCode; //目前已生成的不完全优化方案中对应队列的已加入产品中排在最末端的产品编号 405 Priority_Queue::iterator It; //在队列中指向该产品的迭代器 406int WaitTime; //该产品的等待时间 407 IncompleteOptimization(string P, Priority_Queue::iterator I, int W) :ProductCode(P), It(I), WaitTime(W) {} 408 IncompleteOptimization() = default; 409 410}; 411 412class ProductionCycleComputing 413{ 414public: 415int ProductionCycleCalculation(); //计算产品生产周期,返回生产完成时间 416 ProductionCycleComputing(); 417 418private: 419staticint PositionRelation(const Priority_Queue::iterator &it1, const Priority_Queue::iterator &it2, Priority_Queue &m); //判断优先级队列中it1是在it2之后还是在之前,在之前返回0,在之后返回1,调用者保证it1,it2不等 420int WaitingTimeComputing(Priority_Queue::iterator it, map<string, Device, comparatorless>::iterator m, int InsertScheme); //计算it所指元素在队列中的等待时间 421int TimeIncrementComputing(const Priority_Queue::iterator &BeCalculated, const Priority_Queue::iterator &CauseCalculating, map<string, Device, comparatorless>::iterator m, int InsertScheme); //计算CauseCalculating加入队列后导致修正BeCalculated等待时间所需时间增量 422bool NewProductInsert(int &t, constbool &TF); //处理t时刻新产品插入,TF为true表示在第二轮循环处理否则表示在第一轮循环处理 423 shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>> ProductInsertHandle(map<string, multiset<CodePriorityNAT>> &ProductInsert, constint &tstart, constint &t); //tstart时刻将ProductInsert中的产品加入对应设备集的等待队列 424void ProductAddToProduction(map<string, map<string, Device, comparatorless>::iterator> &SetofDevice, constint tstart, constint tend, constint &t, const size_t &end, map<string, map<string, map<string, Device, comparatorless>::iterator>> &DynamicList, map<string, pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator>> &NoStartInsert); //根据SetofDevice中的加入关系执行产品加入 425void GCDTimeUpdate(); //用于插入产品后更新最大公约数时间GCDTime 426staticint GCDComputing(constint n, constint m); //计算并返回n,m的最大公约数 427void initInsert(); 428void initMould(); 429void initComponent(); 430void initDevice(); 431void initStateQuery(); 432void initRepairTimeOfDevice(); 433 map<string, Device, comparatorless> SetofDevice; //所有同类或不同类的设备组成的集合,关键字设备编号(设备类别编号+设备序号) 434 map<string, Product, comparatorless> SetofProduct; //所有进入生产流程的产品组成的集合,关键字产品编号(产品类别编号+产品序号) 435 map<string, mould, comparatorless> SetofMould; //所有模具的集合,模具编号(模具能够生产的产品类别编号+模具序号) 436 map<string, Component> SetofComponent; //所有零件的集合,关键字零件编号 437 map<string, vector<ProductInsertion>> Insert; //设备类别和各时间点下要插入的产品的对应关系 438 vector<ProductInsertion>::size_type SearchPoint = 0; 439int GCDTime; //最大公约数时间(各类设备上生产各类产品的时间,各类产品在各类设备上生产完毕后的自然时效时间,各类产品在各类设备上的生产准备时间,各类设备小修大修时间及时间间隔,以及在运行时要计入的设备产品的剩余生产时间) 440struct DeviceState 441 { 442int T2; //距设备最近一次调整已流逝的时间 443int T1; //设备已调整时间 444int sign; //最近一次进行的是大修还是小修,0小修,1大修 445bool flag; //设备是否正在调整,T正在调整,F未在调整 446 }; 447 map<string, DeviceState> StateQuery; //设备编号(关键字)和设备状态之间的映射关系 448struct RepairTime 449 { 450int T2; //大修时长 451int T1; //小修时长 452int DeltaT1; //小修至大修的时间间隔 453int DeltaT2; //大修至小修的时间间隔 454 }; 455 map<string, RepairTime> RepairTimeOfDevice; //设备类别和维修时间之间的映射关系 456}; 457 458void ProductionCycleComputing::initRepairTimeOfDevice() 459{ 460 ifstream input("RepairTimeOfDevice.txt"); 461string temp; 462int level; 463 map<string, RepairTime>::iterator p1; 464while (input >> temp) 465 { 466if (temp == "#b1" || temp == "#e1") 467 { 468if (temp == "#b1") 469 { 470 level = 1; 471 } 472else 473 { 474continue; 475 } 476 } 477else 478 { 479if (temp == "#b2" || temp == "#e2") 480 { 481if (temp == "#b2") 482 { 483 level = 2; 484 } 485else 486 { 487continue; 488 } 489 } 490else 491 { 492if (level == 1) 493 { 494 p1 = RepairTimeOfDevice.insert(make_pair(temp, RepairTime())).first; 495 } 496else 497 { 498int flag = 1; 499while (temp != "#e2") 500 { 501if (flag == 1) 502 { 503 p1->second.DeltaT1 = stoi(temp); 504 flag = 2; 505 } 506elseif (flag == 2) 507 { 508 p1->second.DeltaT2 = stoi(temp); 509 flag = 3; 510 } 511elseif (flag == 3) 512 { 513 p1->second.T1 = stoi(temp); 514 flag = 4; 515 } 516else 517 { 518 p1->second.T2 = stoi(temp); 519 } 520 input >> temp; 521 } 522 } 523 } 524 } 525 } 526} 527void ProductionCycleComputing::initStateQuery() 528{ 529 ifstream input("StateQuery.txt"); 530string temp; 531int level; 532 map<string, DeviceState>::iterator p1; 533while (input >> temp) 534 { 535if (temp == "#b1" || temp == "#e1") 536 { 537if (temp == "#b1") 538 { 539 level = 1; 540 } 541else 542 { 543continue; 544 } 545 } 546else 547 { 548if (temp == "#b2" || temp == "#e2") 549 { 550if (temp == "#b2") 551 { 552 level = 2; 553 } 554else 555 { 556continue; 557 } 558 } 559else 560 { 561if (level == 1) 562 { 563 p1 = StateQuery.insert(make_pair(temp, DeviceState())).first; 564 } 565else 566 { 567 568 p1->second.flag = false; 569 p1->second.T1 = 0; 570 p1->second.T2 = 0; 571 p1->second.sign =stoi(temp); 572 input >> temp; 573 } 574 } 575 } 576 } 577} 578void ProductionCycleComputing::initDevice() 579{ 580 ifstream input("Device.txt"); 581string temp; 582int level; 583 map<string, Device, comparatorless>::iterator p1; 584while (input >> temp) 585 { 586if (temp == "#b1" || temp == "#e1") 587 { 588if (temp == "#b1") 589 { 590 level = 1; 591 } 592else 593 { 594continue; 595 } 596 } 597else 598 { 599if (temp == "#b2" || temp == "#e2") 600 { 601if (temp == "#b2") 602 { 603 level = 2; 604 } 605else 606 { 607continue; 608 } 609 } 610else 611 { 612if (level == 1) 613 { 614 p1 = SetofDevice.insert(make_pair(temp, Device())).first; 615 p1->second.isTakenUp = false; 616 p1->second.LatestProductCategory = ""; 617 } 618else 619 { 620int flag = 1; 621 map<string, Mouldstate>::iterator p2; 622while (temp != "#e2") 623 { 624if (flag == 1) 625 { 626 p2 = p1->second.MouldOnDevice.insert(make_pair(temp, Mouldstate())).first; 627 flag = 2; 628 } 629elseif (flag == 2) 630 { 631 p2->second.MouldCode = temp; 632 flag = 3; 633 } 634else 635 { 636if (temp == "T") 637 { 638 p2->second.isOn = true; 639 } 640else 641 { 642 p2->second.isOn = false; 643 } 644 flag = 1; 645 } 646 input >> temp; 647 } 648 } 649 } 650 } 651 } 652} 653void ProductionCycleComputing::initComponent() 654{ 655 ifstream input("Component.txt"); 656string temp; 657int level; 658int flag; 659 map<string, Component>::iterator p1; 660while (input >> temp) 661 { 662if (temp == "#b1" || temp == "#e1") 663 { 664if (temp == "#b1") 665 { 666 level = 1; 667 } 668else 669 { 670continue; 671 } 672 } 673else 674 { 675if (temp == "#b2" || temp == "#e2") 676 { 677if (temp == "#b2") 678 { 679 level = 2; 680 flag = 1; 681 } 682else 683 { 684continue; 685 } 686 } 687else 688 { 689if (temp == "#b3" || temp == "#e3") 690 { 691if (temp == "#b3") 692 { 693 level = 3; 694 } 695else 696continue; 697 } 698else 699 { 700if (level == 1) 701 { 702 p1 = SetofComponent.insert(make_pair(temp, Component())).first; 703 } 704else 705 { 706if (level == 2) 707 { 708if (flag == 1) 709 { 710 p1->second.ComponentCorrespondingMouldClass = temp; 711 flag = 2; 712 } 713else 714 { 715 p1->second.MouldSerialNumber = temp; 716 } 717 } 718else 719 { 720while (temp != "#e3") 721 { 722 p1->second.MouldBelongTo.insert(temp); 723 input >> temp; 724 } 725 } 726 } 727 } 728 } 729 } 730 } 731} 732void ProductionCycleComputing::initMould() 733{ 734 ifstream input("Mould.txt"); 735string temp; 736int level; 737int flag; 738 map<string, mould, comparatorless>::iterator p1; 739while (input >> temp) 740 { 741if (temp == "#b1" || temp == "#e1") 742 { 743if (temp == "#b1") 744 { 745 level = 1; 746 } 747else 748 { 749continue; 750 } 751 } 752else 753 { 754if (temp == "#b2" || temp == "#e2") 755 { 756if (temp == "#b2") 757 { 758 level = 2; 759 flag = 1; 760 } 761else 762 { 763continue; 764 } 765 } 766else 767 { 768if (temp == "#b3" || temp == "#e3") 769 { 770if (temp == "#b3") 771 { 772 level = 3; 773 flag = 1; 774 } 775else 776continue; 777 } 778else 779 { 780if (level == 1) 781 { 782 p1 = SetofMould.insert(make_pair(temp, mould())).first; 783 } 784else 785 { 786if (level == 2) 787 { 788if (flag == 1) 789 { 790 p1->second.DeviceBelongTo = temp; 791 flag = 2; 792 } 793else 794 { 795if (temp == "T") 796 p1->second.MouldState = true; 797else 798 p1->second.MouldState = false; 799 p1->second.ProductIsProducing = ""; 800 } 801 } 802else 803 { 804 map<string, bool>::iterator p2; 805while (temp != "#e3") 806 { 807if (flag == 1) 808 { 809 p2 = p1->second.AsPartComponent.insert(make_pair(temp, false)).first; 810 flag = 2; 811 } 812else 813 { 814if (temp == "T") 815 p2->second = true; 816else 817 p2->second = false; 818 flag = 1; 819 } 820 input >> temp; 821 } 822 } 823 } 824 } 825 } 826 } 827 } 828} 829 830void ProductionCycleComputing::initInsert() 831{ 832 ifstream input("Insert.txt"); 833 vector<int> templist; 834string temp; 835while (true) 836 { 837 input >> temp; 838if (temp == "#b") 839 { 840continue; 841 } 842 843if (temp == "#e") 844break; 845 846 templist.push_back(stoi(temp)); 847 } 848 849int rowinlevel; 850 map<string, vector<ProductInsertion>>::iterator p1; 851while (input >> temp) 852 { 853if (temp == "#b1" || temp == "#e1") 854 { 855if (temp == "#b1") 856 { 857 rowinlevel = 1; 858 } 859else 860 { 861continue; 862 } 863 } 864else 865 { 866if (rowinlevel == 1) 867 { 868 p1 = Insert.insert(make_pair(temp, vector<ProductInsertion>())).first; 869 ++rowinlevel; 870 } 871else 872 { 873 p1->second.push_back(ProductInsertion(templist[rowinlevel-2])); 874while (temp != "#e") 875 { 876if (temp == "#b") 877 { 878 input >> temp; 879continue; 880 } 881 p1->second.back().ProductToInsert.insert(temp); 882 input >> temp; 883 } 884 ++rowinlevel; 885 } 886 } 887 } 888} 889 890ProductionCycleComputing::ProductionCycleComputing() 891{ 892 initInsert(); 893 initMould(); 894 initComponent(); 895 initDevice(); 896 initStateQuery(); 897 initRepairTimeOfDevice(); 898 899//求各类设备上生产各类产品的时间, 各类产品在各类设备上的生产准备时间 900//各类产品在各类设备上生产完毕后的自然时效时间 901//各类设备小修大修时间及时间间隔的最大公约数更新GCDTime 902set<int> temp; 903for (map<string, map<string, ProductionTimeAndPriority>>::iterator p = Device::DeviceCategorySet.begin(); p != Device::DeviceCategorySet.end(); ++p) 904 { 905for (map<string, ProductionTimeAndPriority>::iterator q = p->second.begin(); q != p->second.end(); ++q) 906 { 907for (map<int, PrepareTimeProduceTime>::iterator s = q->second.ProductionTime.begin(); s != q->second.ProductionTime.end(); ++s) 908 { 909 temp.insert(s->second.PrepareTime); 910 temp.insert(s->second.ProduceTime); 911 } 912 } 913 } 914 915for (map<string, vector<DeviceCategoryAndNAT>>::iterator p = Product::ProductCategoryWorkingProcedure.begin(); p != Product::ProductCategoryWorkingProcedure.end(); ++p) 916 { 917for (vector<DeviceCategoryAndNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q) 918 { 919 temp.insert(q->NAT); 920 } 921 } 922 923for (map<string, RepairTime>::iterator p = RepairTimeOfDevice.begin(); p != RepairTimeOfDevice.end(); ++p) 924 { 925 temp.insert(p->second.DeltaT1); 926 temp.insert(p->second.DeltaT2); 927 temp.insert(p->second.T1); 928 temp.insert(p->second.T2); 929 } 930 931set<int>::iterator p = temp.begin(); 932set<int>::iterator q = p; 933 ++q; 934if (q != temp.end()) 935 { 936 GCDTime = GCDComputing(*p, *q); 937 } 938else 939 { 940 GCDTime = *p; 941 } 942for (set<int>::iterator s = q; s != temp.end(); ++s) 943 { 944if (s != q) 945 { 946 GCDTime = GCDComputing(GCDTime, *s); 947 } 948 } 949 950} 951 952int ProductionCycleComputing::GCDComputing(int n, int m) 953{ 954while (m != 0) 955 { 956int temp = m; 957 m = n % m; 958 n = temp; 959 } 960 961return n; 962} 963 964void ProductionCycleComputing::GCDTimeUpdate() 965{ 966 { 967 map<string, RepairTime>::iterator p = RepairTimeOfDevice.begin(); 968for (map<string, DeviceState>::iterator q = StateQuery.begin(); q != StateQuery.end(); ) //用各设备的剩余调整时间和未调整的时间更新最大公约数时间 969 { 970if (q->first.substr(0, q->first.find_first_of(‘+‘)) == p->first) 971 { 972if (q->second.flag == false) 973 { 974if ((q->second.sign == 0 && p->second.DeltaT1 != q->second.T2) || (q->second.sign == 1 && p->second.DeltaT2 != q->second.T2)) 975 { 976if (q->second.sign == 0) 977 GCDTime = GCDComputing(GCDTime, p->second.DeltaT1- q->second.T2); 978else 979 GCDTime = GCDComputing(GCDTime, p->second.DeltaT2 - q->second.T2); 980 } 981 } 982else 983 { 984if (q->second.sign == 0) 985 { 986 GCDTime = GCDComputing(GCDTime, p->second.T1 - q->second.T1); 987 } 988else 989 { 990 GCDTime = GCDComputing(GCDTime, p->second.T2 - q->second.T1); 991 } 992 } 993 ++q; 994 } 995else 996 { 997 ++p; 998 } 999 } 1000 } 10011002 { 1003for (map<string, Device, comparatorless>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p) //用各设备剩余自然时效时间更新最大公约数1004 { 1005if (p->second.WaitingQueue.isEmpty() == false) 1006 { 1007for (Priority_Queue::iterator q = p->second.WaitingQueue.begin(); q != p->second.WaitingQueue.end(); ++q) 1008 { 1009if (q->NaturalAgingTime != -1 && q->NaturalAgingTime != Product::ProductCategoryWorkingProcedure.find(q->ProductCode.substr(0, q->ProductCode.find_first_of(‘+‘)))->second[SetofProduct.find(q->ProductCode)->second.index - 1].NAT) 1010 GCDTime = GCDComputing(GCDTime, Product::ProductCategoryWorkingProcedure.find(q->ProductCode.substr(0, q->ProductCode.find_first_of(‘+‘)))->second[SetofProduct.find(q->ProductCode)->second.index - 1].NAT - q->NaturalAgingTime); 1011 } 1012 } 10131014if (p->second.ProductisProducing.empty() == false) //用设备正在生产的产品的剩余生产时间更新最大公约数1015 { 1016int t = 0; 1017conststring &temp = (*p->second.ProductisProducing.begin()).ProductCode; 1018 PrepareTimeProduceTime &temp1 = p->second.DeviceCategorySet.find(p->first.substr(0, p->first.find_first_of(‘+‘)))->second[temp.substr(0, temp.find_first_of(‘+‘))].ProductionTime[SetofProduct.find(temp)->second.index]; 1019 t += temp1.ProduceTime; 1020if (temp.substr(0, temp.find_first_of(‘+‘)) != p->second.LatestProductCategory) 1021 t += temp1.PrepareTime; 1022 t -= SetofProduct.find(temp)->second.time; 1023 GCDTime = GCDComputing(GCDTime, t); 1024 } 1025 } 1026 } 1027} 10281029bool ProductionCycleComputing::NewProductInsert(int &t, constbool &TF) 1030{ 1031if (Insert.empty() == false) 1032 { 1033 vector<ProductInsertion>::size_type start = SearchPoint; 1034for ( ; start != Insert.begin()->second.size(); ++start) //找出[t, GCDTime]时间区间内有产品插入的时间点集合1035 { 1036if (t < Insert.begin()->second[start].InsertTime) 1037break; 1038 } 10391040 vector<ProductInsertion>::size_type end; 1041if (start == Insert.begin()->second.size()) 1042 { 1043if (Insert.begin()->second[start - 1].InsertTime == t) 1044 { 1045if (TF == false) //不是第二轮循环1046 { 1047 --start; 1048 end = start; 1049 SearchPoint = Insert.begin()->second.size(); 1050 } 1051else1052 { 1053returnfalse; 1054 } 1055 } 1056else1057 { 1058returnfalse; 1059 } 1060 } 1061else1062 { 1063 vector<ProductInsertion>::size_type i = start; 1064for ( ; i != Insert.begin()->second.size(); ++i) 1065 { 1066if (Insert.begin()->second[i].InsertTime >= t + GCDTime) 1067break; 1068 } 10691070if (start == 0) 1071 { 1072if (i == 0) 1073 { 1074returnfalse; 1075 } 1076else1077 { 1078 SearchPoint = i; 1079 start = 0; 1080 end = i - 1; 1081 } 1082 } 1083else1084 { 1085if (i == start) 1086 { 1087if (t == Insert.begin()->second[start - 1].InsertTime) 1088 { 1089if (TF == false) 1090 { 1091 --start; 1092 end = start; 1093 SearchPoint = i; 1094 } 1095else1096 { 1097 SearchPoint = i; // SearchPoint = i;可有可无1098returnfalse; 1099 } 1100 } 1101else1102 { 1103 SearchPoint = i; 1104returnfalse; 1105 } 1106 } 1107else1108 { 1109if (t == Insert.begin()->second[start - 1].InsertTime) 1110 { 1111if (TF == false) 1112 { 1113 --start; 1114 } 1115 end = i - 1; 1116 SearchPoint = i; 1117 } 1118else1119 { 1120 end = i - 1; 1121 SearchPoint = i; 1122 } 1123 } 1124 } 1125 } 11261127if (Insert.begin()->second[start].InsertTime != t || (Insert.begin()->second[start].InsertTime == t && start != end)) 1128 { 1129 map<string, map<string, map<string, Device, comparatorless>::iterator>> DynamicList; //外层map关键字设备类别,内层设备编号,映射值为指向设备迭代器11301131 vector<ProductInsertion>::size_type first; 1132if (Insert.begin()->second[start].InsertTime == t) 1133 { 1134 first = start + 1; 1135 map<string, multiset<CodePriorityNAT>> temp; 1136for (map<string, vector<ProductInsertion>>::iterator q = Insert.begin(); q != Insert.end(); ++q) 1137 { 1138if (q->second[start].ProductToInsert.empty() == false) 1139 { 1140 map<string, multiset<CodePriorityNAT>>::iterator temp1 = temp.insert(make_pair(q->first, multiset<CodePriorityNAT>())).first; 1141for (set<string>::iterator p = q->second[start].ProductToInsert.begin(); p != q->second[start].ProductToInsert.end(); ++p) 1142 { 1143int Priority = Device::DeviceCategorySet.find(q->first)->second[(*p).substr(0, (*p).find_first_of(‘+‘))].Priority; 1144 temp1->second.insert(CodePriorityNAT(*p, Priority, -1)); 1145 } 1146 } 1147 } 1148 ProductInsertHandle(temp, t, t); 1149 } 1150else1151 { 1152 first = start; 1153 } 11541155 map<string, Device, comparatorless>::iterator before = SetofDevice.begin(); 1156 map<string, pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator>> NoStartInsert; //关键字设备类别,值该类别设备的迭代器范围1157for (map<string, vector<ProductInsertion>>::iterator p = Insert.begin(); p != Insert.end(); ++p) 1158 { 1159 vector<ProductInsertion>::size_type q = first; 1160for ( ; q != end + 1; ++q) 1161 { 1162if (p->second[q].ProductToInsert.empty() == false) 1163break; 1164 } 11651166if (q != end + 1) 1167 { 1168 DynamicList.insert(make_pair(p->first, map<string, map<string, Device, comparatorless>::iterator>())); 11691170while (p->first != before->first.substr(0, before->first.find_first_of(‘+‘))) 1171 { 1172 ++before; 1173 } 11741175 map<string, Device, comparatorless>::iterator after = before; 1176while (after != SetofDevice.end() && p->first == after->first.substr(0, after->first.find_first_of(‘+‘))) 1177 { 1178 ++after; 1179 } 11801181 NoStartInsert.insert(make_pair(p->first, make_pair(before, after))); 1182 before = after; 1183 } 1184 } 11851186//此处执行加入设备活动集和设备状态更新1187 { 1188 map<string, RepairTime>::iterator p = RepairTimeOfDevice.begin(); 1189for (map<string, DeviceState>::iterator q = StateQuery.begin(); q != StateQuery.end(); ) 1190 { 1191if (q->first.substr(0, q->first.find_first_of(‘+‘)) == p->first) 1192 { 1193if (q->second.flag == false) 1194 { 1195if ((q->second.sign == 0 && p->second.DeltaT1 != q->second.T2) || (q->second.sign == 1 && p->second.DeltaT2 != q->second.T2)) 1196 { 1197 q->second.T2 += Insert.begin()->second[end].InsertTime - t; 1198 } 1199 } 1200 ++q; 1201 } 1202else1203 { 1204 ++p; 1205 } 1206 } 1207 } 12081209 map<string, map<string, Device, comparatorless>::iterator> EquipmentToBeAdded; 1210for (map<string, Device, comparatorless>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p) 1211 { 1212 EquipmentToBeAdded.insert(make_pair(p->first, p)); 1213if (p->second.WaitingQueue.isEmpty() == false) 1214 { 1215for (Priority_Queue::iterator q = p->second.WaitingQueue.begin(); q != p->second.WaitingQueue.end(); ++q) 1216 { 1217if (q->NaturalAgingTime != -1 && q->NaturalAgingTime != Product::ProductCategoryWorkingProcedure.find(q->ProductCode.substr(0, q->ProductCode.find_first_of(‘+‘)))->second[SetofProduct.find(q->ProductCode)->second.index - 1].NAT) 1218 q->NaturalAgingTime += Insert.begin()->second[end].InsertTime - t; 1219 } 1220 } 1221 } 12221223 ProductAddToProduction(EquipmentToBeAdded, t, Insert.begin()->second[first].InsertTime, t, end, DynamicList, NoStartInsert); 1224for (vector<ProductInsertion>::size_type i = first; i <= end; ++i) 1225 { 1226 map<string, multiset<CodePriorityNAT>> temp; 1227for (map<string, vector<ProductInsertion>>::iterator q = Insert.begin(); q != Insert.end(); ++q) 1228 { 1229if (q->second[i].ProductToInsert.empty() == false) 1230 { 1231 map<string, multiset<CodePriorityNAT>>::iterator temp1 = temp.insert(make_pair(q->first, multiset<CodePriorityNAT>())).first; 1232for (set<string>::iterator p = q->second[i].ProductToInsert.begin(); p != q->second[i].ProductToInsert.end(); ++p) 1233 { 1234int Priority = Device::DeviceCategorySet.find(q->first)->second[(*p).substr(0, (*p).find_first_of(‘+‘))].Priority; 1235 temp1->second.insert(CodePriorityNAT(*p, Priority, -1)); 1236 } 1237 } 1238 } 12391240if (i != end) 1241 { 1242 map<string, map<string, Device, comparatorless>::iterator> SD; 1243 shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>> EquipmentToBeAdded = ProductInsertHandle(temp, Insert.begin()->second[i].InsertTime, t); 1244 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator pa = DynamicList.begin(); 1245 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator pb = EquipmentToBeAdded->begin(); 1246while (pa != DynamicList.end() && pb != EquipmentToBeAdded->end()) //通过动态表和有产品加入等待队列的设备集的交集确定可执行产品加入的设备集1247 { 1248if (pa->first == pb->first) 1249 { 1250 map<string, map<string, Device, comparatorless>::iterator>::iterator pc = pa->second.begin(); 1251 map<string, map<string, Device, comparatorless>::iterator>::iterator pd = pb->second.begin(); 1252while (pc != pa->second.end() && pd != pb->second.end()) 1253 { 1254if (pc->first == pd->first) 1255 { 1256 SD.insert(*pc); 1257 ++pc; 1258 ++pd; 1259 } 1260elseif (pc->first < pd->first) 1261 { 1262 ++pc; 1263 } 1264else1265 { 1266 ++pd; 1267 } 1268 } 1269 ++pa; 1270 ++pb; 1271 } 1272elseif (pa->first < pb->first) 1273 { 1274 ++pa; 1275 } 1276else1277 { 1278 ++pb; 1279 } 1280 } 1281 ProductAddToProduction(SD, Insert.begin()->second[i].InsertTime, Insert.begin()->second[i + 1].InsertTime, t, end, DynamicList, NoStartInsert); 1282 } 1283else1284 { 1285 ProductInsertHandle(temp, Insert.begin()->second[i].InsertTime, Insert.begin()->second[i].InsertTime); 1286 } 1287 } 1288//这里更新t和GCDTime1289 t = Insert.begin()->second[end].InsertTime; 1290 GCDTimeUpdate(); 1291 } 1292else1293 { 1294 map<string, multiset<CodePriorityNAT>> temp; 1295for (map<string, vector<ProductInsertion>>::iterator q = Insert.begin(); q != Insert.end(); ++q) 1296 { 1297if (q->second[start].ProductToInsert.empty() == false) 1298 { 1299 map<string, multiset<CodePriorityNAT>>::iterator temp1 = temp.insert(make_pair(q->first, multiset<CodePriorityNAT>())).first; 1300for (set<string>::iterator p = q->second[start].ProductToInsert.begin(); p != q->second[start].ProductToInsert.end(); ++p) 1301 { 1302int Priority = Device::DeviceCategorySet.find(q->first)->second[(*p).substr(0, (*p).find_first_of(‘+‘))].Priority; 1303 temp1->second.insert(CodePriorityNAT(*p, Priority, -1)); 1304 } 1305 } 1306 } 1307 ProductInsertHandle(temp, t, t); 1308 } 1309returntrue; 1310 } 1311} 13121313void ProductionCycleComputing::ProductAddToProduction(map<string, map<string, Device, comparatorless>::iterator> &SetofDevice, constint tstart, constint tend, constint &t, const size_t &end, map<string, map<string, map<string, Device, comparatorless>::iterator>> &DynamicList, map<string, pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator>> &NoStartInsert) 1314{ 1315if (SetofDevice.empty() == false) 1316 { 1317 map<string, multiset<CodePriorityNAT>> TempMap; //设备编号和temp集的映射关系1318 Priority_Queue TempQueue; //存放进入设备生产的候选产品的优先级队列1319for (map<string, map<string, Device, comparatorless>::iterator>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p) 1320 { 1321if (!(*p->second).second.ProductisProducing.empty()) 1322 { 1323if (tstart == t) 1324 { 1325set<CodePriorityNAT>::iterator q = (*p->second).second.ProductisProducing.begin(); 1326 map<string, Product, comparatorless>::iterator m = SetofProduct.find((*q).ProductCode); 1327if (NoStartInsert.find((*p->second).first.substr(0, (*p->second).first.find_first_of(‘+‘))) != NoStartInsert.end()) 1328 { 1329 (*m).second.time += tend - tstart; 1330 } 1331else1332 { 1333 (*m).second.time += Insert.begin()->second[end].InsertTime - t; 1334 } 1335 } 1336 } 1337else1338 { 1339if (t == tstart) 1340 { 1341if ((*p->second).second.isTakenUp == true) 1342 { 1343if (NoStartInsert.find((*p->second).first.substr(0, (*p->second).first.find_first_of(‘+‘))) != NoStartInsert.end()) 1344 { 1345 StateQuery[(*p->second).first].T1 += tend - tstart; 1346 } 1347else1348 { 1349 StateQuery[(*p->second).first].T1 += Insert.begin()->second[end].InsertTime - t; 1350 } 1351 } 1352 } 13531354if (!(*p->second).second.WaitingQueue.isEmpty()) 1355 { 1356if ((*p->second).second.isTakenUp == false) 1357 { 1358 multiset<CodePriorityNAT> temp; 1359 CodePriorityNAT temp1; 1360bool TF = false; 1361while ((*p->second).second.WaitingQueue.isEmpty() == false) 1362 { 1363 (*p->second).second.WaitingQueue.RemoveTop(temp1); 1364 map<string, Product, comparatorless>::iterator q = SetofProduct.find(temp1.ProductCode); 1365 map<string, vector<DeviceCategoryAndNAT>>::iterator t = Product::ProductCategoryWorkingProcedure.find(temp1.ProductCode.substr(0, temp1.ProductCode.find_first_of(‘+‘))); 1366if (temp1.NaturalAgingTime != -1 && (*t).second[(*q).second.index - 1].NAT != temp1.NaturalAgingTime) 1367 { 1368 temp.insert(temp1); 1369 } 1370else1371 { 1372 TF = true; 1373break; 1374 } 1375 } 13761377if ((*p->second).second.WaitingQueue.isEmpty() && TF == false) 1378 { 1379if (tstart == t) 1380 { 1381 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator temp2; 1382if ((temp2 = DynamicList.find((*p->second).first.substr(0, (*p->second).first.find_first_of(‘+‘)))) != DynamicList.end()) 1383 { 1384 temp2->second.insert(make_pair((*p->second).first, p->second)); 1385 } 1386 } 1387for (auto t = temp.begin(); t != temp.end(); t = temp.erase(t)) 1388 { 1389 (*p->second).second.WaitingQueue.Insert(*t); 1390 } 1391 } 1392else1393 { 1394 TempQueue.Insert(temp1); 1395 TempMap.insert(make_pair(p->second->first, temp)); 13961397 } 1398continue; 1399 } 1400 } 1401else1402 { 1403if (tstart == t) 1404 { 1405if ((*p->second).second.isTakenUp == false) 1406 { 1407if ((*p->second).second.ProductisProducing.empty() == true) 1408 { 1409 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator temp2; 1410if ((temp2 = DynamicList.find((*p->second).first.substr(0, (*p->second).first.find_first_of(‘+‘)))) != DynamicList.end()) 1411 { 1412 temp2->second.insert(make_pair((*p->second).first, p->second)); 1413 } 1414 } 1415 } 1416 } 1417 } 1418 } 1419 } 1420while (TempQueue.isEmpty() == false) 1421 { 1422 CodePriorityNAT temp; 1423 TempQueue.RemoveTop(temp); 1424 map<string, Product, comparatorless>::iterator q = SetofProduct.find(temp.ProductCode); 1425 map<string, map<string, Device, comparatorless>::iterator>::iterator p = SetofDevice.find(q->second.DeviceID); 1426 map<string, Mouldstate>::iterator m = p->second->second.MouldOnDevice.find(temp.ProductCode.substr(0, temp.ProductCode.find_first_of(‘+‘))); 14271428if (m->second.isOn)// 从这里开始1429 { 1430 q->second.flag = 1; 1431 p->second->second.ProductisProducing.insert(temp); 1432 cout << "产品" << temp.ProductCode << "于" << t << "时刻加入设备" << p->first << "开始生产" << endl; 1433 SetofMould.find(m->second.MouldCode)->second.ProductIsProducing = temp.ProductCode; ////1434 p->second->second.isTakenUp = true; 1435if (tstart == t) 1436 { 1437if (NoStartInsert.find(p->second->first.substr(0, p->second->first.find_first_of(‘+‘))) != NoStartInsert.end()) 1438 { 1439 q->second.time = tend - tstart; 1440 } 1441else1442 { 1443 q->second.time = Insert.begin()->second[end].InsertTime - t; 1444 } 1445 } 1446else1447 { 1448 DynamicList[p->second->first.substr(0, p->second->first.find_first_of(‘+‘))].erase(p->second->first);; 1449 } 1450 map<string, multiset<CodePriorityNAT>>::iterator temp1; 1451 temp1 = TempMap.find(q->second.DeviceID); 1452if (temp1->second.empty() == false) 1453 { 1454for (multiset<CodePriorityNAT>::iterator t = temp1->second.begin(); t != temp1->second.end(); t = temp1->second.erase(t)) 1455 p->second->second.WaitingQueue.Insert(*t); 1456 } 1457 TempMap.erase(temp1); 1458 } 1459else1460 { 1461 map<string, mould, comparatorless>::iterator v = SetofMould.find(m->second.MouldCode); 1462bool ismeet = true; 1463for (map<string, bool>::iterator s = v->second.AsPartComponent.begin(); s != v->second.AsPartComponent.end(); ++s) 1464 { 1465if (s->second == false) 1466 { 1467 map<string, Component>::iterator f = SetofComponent.find(s->first); 1468 map<string, mould, comparatorless>::iterator w = SetofMould.find(f->second.ComponentCorrespondingMouldClass + ‘+‘ + f->second.MouldSerialNumber); 1469if (w->second.MouldState == true && w->second.ProductIsProducing != "") 1470 { 1471 ismeet = false; 1472break; 1473 } 1474 } 1475 } 1476if (ismeet == true) 1477 { 1478 v->second.MouldState = true; 1479 v->second.ProductIsProducing = temp.ProductCode; 1480 m->second.isOn = true; 1481for (map<string, bool>::iterator s = v->second.AsPartComponent.begin(); s != v->second.AsPartComponent.end(); ++s) 1482 { 1483if (s->second == false) 1484 { 1485 map<string, Component>::iterator f = SetofComponent.find(s->first); 1486 map<string, mould, comparatorless>::iterator w = SetofMould.find(f->second.ComponentCorrespondingMouldClass + ‘+‘ + f->second.MouldSerialNumber); 1487if (w->second.MouldState == true) 1488 { 1489 w->second.ProductIsProducing = ""; 1490 w->second.MouldState = false; 1491 SetofDevice.find(w->second.DeviceBelongTo)->second->second.MouldOnDevice[w->first.substr(0, w->first.find_first_of(‘+‘))].isOn = false; 1492 } 14931494 w->second.AsPartComponent[f->first] = false; 1495 f->second.ComponentCorrespondingMouldClass = v->first.substr(0, v->first.find_first_of(‘+‘)); 1496 f->second.MouldSerialNumber = v->first.substr(v->first.find_first_of(‘+‘) + 1); 1497 v->second.AsPartComponent[f->first] = true; 1498 } 1499 } 1500 q->second.flag = 1; 1501 p->second->second.ProductisProducing.insert(temp); 1502 cout << "产品" << temp.ProductCode << "于" << t << "时刻加入设备" << p->first << "开始生产" << endl; 1503 p->second->second.isTakenUp = true; 15041505if (tstart == t) 1506 { 1507if (NoStartInsert.find(p->first.substr(0, p->first.find_first_of(‘+‘))) != NoStartInsert.end()) 1508 { 1509 q->second.time = tend - tstart; 1510 } 1511else1512 { 1513 q->second.time = Insert.begin()->second[end].InsertTime - t; 1514 } 1515 } 1516else1517 { 1518 DynamicList[p->first.substr(0, p->first.find_first_of(‘+‘))].erase(p->first); 1519 } 15201521 map<string, multiset<CodePriorityNAT>>::iterator temp1; 15221523 temp1 = TempMap.find(q->second.DeviceID); 1524if (temp1->second.empty() == false) 1525 { 1526for (multiset<CodePriorityNAT>::iterator t = temp1->second.begin(); t != temp1->second.end(); t = temp1->second.erase(t)) 1527 p->second->second.WaitingQueue.Insert(*t); 1528 } 1529 TempMap.erase(temp1); 1530 } 1531else1532 { 1533 TempMap[q->second.DeviceID].insert(temp); 1534if (!p->second->second.WaitingQueue.isEmpty()) 1535 { 1536 CodePriorityNAT temp1; 1537bool TF = false; 1538while ((*p).second->second.WaitingQueue.isEmpty() == false) 1539 { 1540 (*p).second->second.WaitingQueue.RemoveTop(temp1); 1541 map<string, Product, comparatorless>::iterator s = SetofProduct.find(temp1.ProductCode); 1542 map<string, vector<DeviceCategoryAndNAT>>::iterator v = Product::ProductCategoryWorkingProcedure.find(temp1.ProductCode.substr(0, temp1.ProductCode.find_first_of(‘+‘))); 1543if (temp1.NaturalAgingTime != -1 && (*v).second[(*s).second.index - 1].NAT != temp1.NaturalAgingTime) 1544 { 1545 TempMap[q->second.DeviceID].insert(temp1); 1546 } 1547else1548 { 1549 TF = true; 1550break; 1551 } 1552 } 15531554if (!(*p).second->second.WaitingQueue.isEmpty() || TF) 1555 { 1556 TempQueue.Insert(temp1); 1557continue; 1558 } 1559 } 15601561if (tstart == t) 1562 { 1563 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator temp2; 1564if ((temp2 = DynamicList.find((*p).first.substr(0, (*p).first.find_first_of(‘+‘)))) != DynamicList.end()) 1565 { 1566 temp2->second.insert(make_pair((*p).first, p->second)); 1567 } 1568 } 15691570for (auto x = TempMap[q->second.DeviceID].begin(); x != TempMap[q->second.DeviceID].end(); x = TempMap[q->second.DeviceID].erase(x)) 1571 { 1572 (*p).second->second.WaitingQueue.Insert(*x); 1573 } 1574 TempMap.erase(q->second.DeviceID); 1575 } 1576 } 1577 } 1578for (map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator p = DynamicList.begin(); p != DynamicList.end(); ) 1579 { 1580if (p->second.empty() == true) 1581 p = DynamicList.erase(p); 1582else1583 ++p; 1584 } 1585 } 15861587if (t != tstart) 1588 { 1589 map<string, DeviceState>::iterator before = StateQuery.begin(); 1590for (map<string, pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator>>::iterator p = NoStartInsert.begin(); p != NoStartInsert.end(); ++p) 1591 { 1592while (p->first != before->first.substr(0, before->first.find_first_of(‘+‘))) 1593 { 1594 ++before; 1595 } 15961597 map<string, DeviceState>::iterator after = before; 1598while (after != StateQuery.end() && p->first == after->first.substr(0, after->first.find_first_of(‘+‘))) 1599 { 1600 ++after; 1601 } 16021603 map<string, DeviceState>::iterator q = before; 1604for (map<string, Device, comparatorless>::iterator m = p->second.first; m != p->second.second; ++m, ++q) 1605 { 1606if (q->second.flag == true) 1607 { 1608 q->second.T1 += tend - tstart; 1609 } 16101611if (m->second.ProductisProducing.empty() == false) 1612 { 1613 map<string, Product, comparatorless>::iterator temp = SetofProduct.find(m->second.ProductisProducing.begin()->ProductCode); 1614 temp->second.time += tend - tstart; 1615 } 1616 } 1617 before = after; 1618 } 1619 } 1620} 16211622 shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>> ProductionCycleComputing::ProductInsertHandle(map<string, multiset<CodePriorityNAT>> &ProductInsert, constint &tstart, constint &t) 1623{ 1624if (ProductInsert.empty() == false) 1625 { 1626 shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>> insertinfo; 1627if (tstart != t) 1628 { 1629 insertinfo = shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>>(new map<string, map<string, map<string, Device, comparatorless>::iterator>>); 1630 } 16311632for (map<string, multiset<CodePriorityNAT>>::iterator p = ProductInsert.begin(); p != ProductInsert.end(); ++p) 1633 { 1634 pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator> tempIterator = SetofDevice.equal_range(p->first); 1635struct IteratorAndScheme 1636 { 1637int InsertScheme; //设备等待时间计算方案1638 map<string, Device, comparatorless>::iterator DeviceIterator; //指向设备的迭代器1639 IteratorAndScheme(map<string, Device, comparatorless>::iterator It, int I) :DeviceIterator(It), InsertScheme(I) {} 1640 }; 1641 vector<IteratorAndScheme> QueueIndex; 1642for (map<string, Device, comparatorless>::iterator q = tempIterator.first; q != tempIterator.second; ++q) 1643 { 1644if (StateQuery[q->first].flag == true) 1645 { 1646if (StateQuery[q->first].sign == 0) 1647 { 1648if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T1) 1649 { 1650 QueueIndex.push_back(IteratorAndScheme(q, 1)); 1651 } 1652else1653 { 1654 QueueIndex.push_back(IteratorAndScheme(q, 2)); 1655 } 1656 } 1657else1658 { 1659if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T2) 1660 { 1661 QueueIndex.push_back(IteratorAndScheme(q, 1)); 1662 } 1663else1664 { 1665 QueueIndex.push_back(IteratorAndScheme(q, 2)); 1666 } 1667 } 1668 } 1669else1670 { 1671 QueueIndex.push_back(IteratorAndScheme(q, 1)); 1672 } 1673 } 16741675 map<int, IncompleteOptimization> CurrentOptimization; //关键字队列编号1676struct PriorityInsertionTime 1677 { 1678 vector<int> ProductTime; //存放给定产品按优先级插入各队列后的等待时间1679 vector<IteratorAndScheme>::size_type index; //产品所在的等待时间最小的队列序号1680int tmin; //最小等待时间1681 }; 1682 map<string, PriorityInsertionTime> ProductQueueWaitingTime;//关键字产品编号1683int tmax; //映射表CurrentOptimization中各队列排在末端产品等待时间最大值1684 vector<IteratorAndScheme>::size_type index; //每一轮循环开始时为上一轮循环最优化插入的产品插入的队列编号1685 Priority_Queue::iterator it; //每一轮循环开始时为上一轮循环最优化插入产品插入队列中指向该产品的迭代器1686 vector<IteratorAndScheme>::size_type indexmax; //每一轮循环开始时为上一轮循环CurrentOptimization中各队列排在末端产品中等待时间最长的所在队列编号1687 Priority_Queue::iterator itmax; //每一轮循环开始时为指向上一轮循环CurrentOptimization中各队列排在末端产品中等待时间最长的产品所在位置的后一位置的迭代器1688 multiset<CodePriorityNAT>::size_type count = p->second.size(); 16891690 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator currentinsert; 1691while (p->second.empty() == false) 1692 { 1693if (p->second.size() == count) 1694 { 1695 vector<IteratorAndScheme>::size_type indextemp; 1696 Priority_Queue::iterator iteratortemp; 1697string codetemp; 1698int tmin; 1699 multiset<CodePriorityNAT>::iterator qmin; 1700for (multiset<CodePriorityNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q) 1701 { 1702int Tmint; 1703 vector<IteratorAndScheme>::size_type Indext; 1704 Priority_Queue::iterator Iteratort; 1705 map<string, PriorityInsertionTime>::iterator temp1 = ProductQueueWaitingTime.insert(make_pair(q->ProductCode, PriorityInsertionTime())).first; 1706for (vector<IteratorAndScheme>::size_type i = 0; i < QueueIndex.size(); ++i) 1707 { 1708 Priority_Queue::iterator temp = QueueIndex[i].DeviceIterator->second.WaitingQueue.Insert(*q).second; 1709int waittime = WaitingTimeComputing(temp, QueueIndex[i].DeviceIterator, QueueIndex[i].InsertScheme); 1710if (i == 0) 1711 { 1712 Tmint = waittime; 1713 Indext = i; 1714 Iteratort = temp; 1715 ++Iteratort; 1716 } 1717else1718 { 1719if (waittime < Tmint) 1720 { 1721 Tmint = waittime; 1722 Indext = i; 1723 Iteratort = temp; 1724 ++Iteratort; 1725 } 1726 } 1727 QueueIndex[i].DeviceIterator->second.WaitingQueue.erase(temp); 1728 temp1->second.ProductTime.push_back(waittime); 1729 } 1730 temp1->second.index = Indext; 1731 temp1->second.tmin = Tmint; 1732if (q == p->second.begin()) 1733 { 1734 qmin = q; 1735 tmin = Tmint; 1736 codetemp = q->ProductCode; 1737 indextemp = Indext; 1738 iteratortemp = Iteratort; 1739 } 1740else1741 { 1742if (Tmint < tmin) 1743 { 1744 qmin = q; 1745 tmin = Tmint; 1746 codetemp = q->ProductCode; 1747 indextemp = Indext; 1748 iteratortemp = Iteratort; 1749 } 1750 } 1751 } 17521753 tmax = tmin; 1754 it = QueueIndex[indextemp].DeviceIterator->second.WaitingQueue.insert(iteratortemp, *qmin); 1755 cout << t << "时刻产品" << qmin->ProductCode << "加入设备" << QueueIndex[indextemp].DeviceIterator->first << "等待队列等待生产" << endl; 1756if (t != tstart) 1757 { 1758 currentinsert = insertinfo->insert(make_pair(p->first, map<string, map<string, Device, comparatorless>::iterator>())).first; 1759 currentinsert->second.insert(make_pair(QueueIndex[indextemp].DeviceIterator->first, QueueIndex[indextemp].DeviceIterator)); 1760 } 1761 SetofProduct.insert(make_pair(qmin->ProductCode, Product(QueueIndex[indextemp].DeviceIterator->first, 0, 0, 0))); 1762 index = indextemp; 1763 indexmax = indextemp; 1764 itmax = iteratortemp; 1765 CurrentOptimization.insert(make_pair(indextemp, IncompleteOptimization(codetemp, --iteratortemp, tmin))); 1766 ProductQueueWaitingTime.erase(codetemp); 1767 p->second.erase(qmin); 1768 } 1769else1770 { 1771if (p->second.size() != 1) 1772 { 1773 Priority_Queue::iterator iteratorop; 1774 Priority_Queue::iterator iteratormaxop; 1775 vector<IteratorAndScheme>::size_type indexmaxop; 1776 vector<IteratorAndScheme>::size_type indexop; 1777 multiset<CodePriorityNAT>::iterator qop; 1778int tmaxop; 1779for (multiset<CodePriorityNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q) 1780 { 1781 Priority_Queue::iterator iteratort; 1782 Priority_Queue::iterator iteratormaxt; 1783 vector<IteratorAndScheme>::size_type indexmaxt; 1784int tmaxt; 17851786 iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second; 1787if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1) 1788 { 1789int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme); 1790 ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp; 17911792if (ProductQueueWaitingTime[q->ProductCode].index == index) 1793 { 1794int mintime; 1795 vector<int>::size_type minindex; 1796for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i) 1797 { 1798if (i == 0) 1799 { 1800 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 1801 minindex = i; 1802 } 1803else1804 { 1805if (ProductQueueWaitingTime[q->ProductCode].ProductTime[i] < mintime) 1806 { 1807 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 1808 minindex = i; 1809 } 1810 } 1811 } 1812 ProductQueueWaitingTime[q->ProductCode].index = minindex; //这里无符号类型不匹配,注意,可以采用使用迭代器的改进方案1813 ProductQueueWaitingTime[q->ProductCode].tmin = mintime; 1814 } 1815 } 1816 QueueIndex[index].DeviceIterator->second.WaitingQueue.erase(iteratort); 18171818 Priority_Queue::iterator tempinsert = QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.Insert(*q).second; 1819if (CurrentOptimization.find(ProductQueueWaitingTime[q->ProductCode].index) == CurrentOptimization.end()) 1820 { 1821 Priority_Queue::iterator after = tempinsert; 1822 ++after; 1823if (ProductQueueWaitingTime[q->ProductCode].tmin > tmax) 1824 { 1825 tmaxt = ProductQueueWaitingTime[q->ProductCode].tmin; 1826 iteratort = after; 1827 iteratormaxt = after; 1828 indexmaxt = ProductQueueWaitingTime[q->ProductCode].index; 1829 } 1830else1831 { 1832 tmaxt = tmax; 1833 iteratort = after; 1834 iteratormaxt = itmax; 1835 indexmaxt = indexmax; 1836 } 1837 } 1838else1839 { 1840 Priority_Queue::iterator after = tempinsert; 1841 ++after; 1842 Priority_Queue::iterator indexit = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].It; 1843if (PositionRelation(tempinsert, indexit, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue) == 1) 1844 { 1845if (ProductQueueWaitingTime[q->ProductCode].tmin > tmax) 1846 { 1847 tmaxt = ProductQueueWaitingTime[q->ProductCode].tmin; 1848 iteratort = after; 1849 iteratormaxt = after; 1850 indexmaxt = ProductQueueWaitingTime[q->ProductCode].index; 1851 } 1852else1853 { 1854 tmaxt = tmax; 1855 iteratort = after; 1856 iteratormaxt = itmax; 1857 indexmaxt = indexmax; 1858 } 1859 } 1860else1861 { 1862int DeltaTime = TimeIncrementComputing(indexit, tempinsert, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].InsertScheme); 1863int WaitTime = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].WaitTime; 1864if (WaitTime + DeltaTime > tmax) 1865 { 1866 tmaxt = WaitTime + DeltaTime; 1867 iteratort = after; 1868 iteratormaxt = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].It; 1869 ++iteratormaxt; 1870 indexmaxt = ProductQueueWaitingTime[q->ProductCode].index; 1871 } 1872else1873 { 1874 tmaxt = tmax; 1875 iteratort = after; 1876 iteratormaxt = itmax; 1877 indexmaxt = indexmax; 1878 } 1879 } 1880 } 18811882if (q == p->second.begin()) 1883 { 1884 tmaxop = tmaxt; 1885 iteratormaxop = iteratormaxt; 1886 iteratorop = iteratort; 1887 indexmaxop = indexmaxt; 1888 indexop = ProductQueueWaitingTime[q->ProductCode].index; 1889 qop = q; 1890 } 1891else1892 { 1893if (tmaxt < tmaxop) 1894 { 1895 tmaxop = tmaxt; 1896 iteratormaxop = iteratormaxt; 1897 iteratorop = iteratort; 1898 indexmaxop = indexmaxt; 1899 indexop = ProductQueueWaitingTime[q->ProductCode].index; 1900 qop = q; 1901 } 1902 } 1903 QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.erase(tempinsert); 1904 } 19051906 iteratorop = QueueIndex[indexop].DeviceIterator->second.WaitingQueue.insert(iteratorop, *qop); 1907 cout << t << "时刻产品" << qop->ProductCode << "加入设备" << QueueIndex[indexop].DeviceIterator->first << "等待队列等待生产" << endl; 1908if (t != tstart) 1909 { 1910 currentinsert->second.insert(make_pair(QueueIndex[indexop].DeviceIterator->first, QueueIndex[indexop].DeviceIterator)); 1911 } 1912 SetofProduct.insert(make_pair(qop->ProductCode, Product(QueueIndex[indexop].DeviceIterator->first, 0, 0, 0))); 1913if (CurrentOptimization.find(indexop) != CurrentOptimization.end()) 1914 { 1915if (PositionRelation(iteratorop, CurrentOptimization[indexop].It, QueueIndex[indexop].DeviceIterator->second.WaitingQueue) == 0) 1916 { 1917int TimeCorrrect = TimeIncrementComputing(CurrentOptimization[indexop].It, iteratorop, QueueIndex[indexop].DeviceIterator, QueueIndex[indexop].InsertScheme); 1918 CurrentOptimization[indexop].WaitTime += TimeCorrrect; 1919 } 1920else1921 { 1922 CurrentOptimization[indexop].ProductCode = qop->ProductCode; 1923 CurrentOptimization[indexop].It = iteratorop; 1924 CurrentOptimization[indexop].WaitTime = ProductQueueWaitingTime[qop->ProductCode].tmin; 1925 } 1926 } 1927else1928 { 1929 CurrentOptimization.insert(make_pair(indexop, IncompleteOptimization(qop->ProductCode, iteratorop, ProductQueueWaitingTime[qop->ProductCode].tmin))); 1930 } 1931 tmax = tmaxop; 1932 index = indexop; 1933 it = iteratorop; 1934 indexmax = indexmaxop; 1935 itmax = iteratormaxop; 19361937 ProductQueueWaitingTime.erase(qop->ProductCode); 1938 p->second.erase(qop); 1939 } 1940else1941 { 1942 multiset<CodePriorityNAT>::iterator q = p->second.begin(); 1943 Priority_Queue::iterator iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second; 1944if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1) 1945 { 1946int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme); 1947 ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp; 19481949if (ProductQueueWaitingTime[q->ProductCode].index == index) 1950 { 1951int mintime; 1952 vector<int>::size_type minindex; 1953for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i) 1954 { 1955if (i == 0) 1956 { 1957 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 1958 minindex = i; 1959 } 1960else1961 { 1962if (ProductQueueWaitingTime[q->ProductCode].ProductTime[i] < mintime) 1963 { 1964 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 1965 minindex = i; 1966 } 1967 } 1968 } 1969 ProductQueueWaitingTime[q->ProductCode].index = minindex; //这里无符号类型不匹配,注意,可以采用使用迭代器的改进方案1970 ProductQueueWaitingTime[q->ProductCode].tmin = mintime; 1971 } 1972 } 1973 QueueIndex[index].DeviceIterator->second.WaitingQueue.erase(iteratort); 1974 QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.Insert(*q); 1975 cout << t << "时刻产品" << q->ProductCode << "加入设备" << QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->first << "等待队列等待生产" << endl; 1976if (t != tstart) 1977 { 1978 currentinsert->second.insert(make_pair(QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->first, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator)); 1979 } 1980 SetofProduct.insert(make_pair(q->ProductCode, Product(QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->first, 0, 0, 0))); 1981 p->second.clear(); 1982 } 1983 } 1984 } 1985 } 1986return insertinfo; 1987 } 1988return shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>>(nullptr); 1989} 19901991int ProductionCycleComputing::ProductionCycleCalculation() 1992{ 1993 clock_t starttime = clock(); 1994int t = 0; 1995while (true) 1996 { 1997//此处检测并处理新产品插入1998bool TF = false; //第一轮循环1999while (NewProductInsert(t, TF) != false) 2000 { 2001if (TF == false) 2002 TF = true; 2003 } 20042005 map<string, multiset<CodePriorityNAT>> TempMap; //设备编号和temp集的映射关系2006 Priority_Queue TempQueue; //存放进入设备生产的候选产品的优先级队列2007for (map<string, Device, comparatorless>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p) 2008 { 2009if (!(*p).second.ProductisProducing.empty()) 2010 { 2011set<CodePriorityNAT>::iterator q=(*p).second.ProductisProducing.begin(); 2012 map<string, Product, comparatorless>::iterator m = SetofProduct.find((*q).ProductCode); 2013 (*m).second.time += GCDTime; 2014 } 2015else2016 { 2017if (!(*p).second.WaitingQueue.isEmpty()) 2018 { 2019if ((*p).second.isTakenUp == false) 2020 { 2021 multiset<CodePriorityNAT> temp; 2022 CodePriorityNAT temp1; 2023 TF = false; 2024while ((*p).second.WaitingQueue.isEmpty() == false) 2025 { 2026 (*p).second.WaitingQueue.RemoveTop(temp1); 2027 map<string, Product, comparatorless>::iterator q = SetofProduct.find(temp1.ProductCode); 2028 map<string, vector<DeviceCategoryAndNAT>>::iterator t = Product::ProductCategoryWorkingProcedure.find(temp1.ProductCode.substr(0, temp1.ProductCode.find_first_of(‘+‘))); 2029if (temp1.NaturalAgingTime != -1 && (*t).second[(*q).second.index-1].NAT != temp1.NaturalAgingTime) 2030 { 2031 temp1.NaturalAgingTime += GCDTime; 2032 temp.insert(temp1); 2033 } 2034else2035 { 2036 TF = true; 2037break; 2038 } 2039 } 20402041if ((*p).second.WaitingQueue.isEmpty() && TF == false) 2042 { 2043for (auto t = temp.begin(); t != temp.end(); t=temp.erase(t)) 2044 { 2045 (*p).second.WaitingQueue.Insert(*t); 2046 } 2047 } 2048else2049 { 2050 TempQueue.Insert(temp1); 2051 TempMap.insert(make_pair(p->first, temp)); 20522053 } 2054continue; 2055 } 2056 } 2057 } 2058if ((*p).second.WaitingQueue.isEmpty() == false) 2059 { 2060for (Priority_Queue::iterator q = p->second.WaitingQueue.begin(); q != p->second.WaitingQueue.end(); ++q) 2061 { 2062if (q->NaturalAgingTime != -1 && q->NaturalAgingTime != Product::ProductCategoryWorkingProcedure.find(q->ProductCode.substr(0, q->ProductCode.find_first_of(‘+‘)))->second[SetofProduct.find(q->ProductCode)->second.index - 1].NAT) 2063 q->NaturalAgingTime += GCDTime; 2064 } 2065 } 20662067 } 2068while (TempQueue.isEmpty() == false) 2069 { 2070 CodePriorityNAT temp; 2071 TempQueue.RemoveTop(temp); 2072 map<string, Product, comparatorless>::iterator q = SetofProduct.find(temp.ProductCode); 2073 map<string, Device, comparatorless>::iterator p = SetofDevice.find(q->second.DeviceID); 2074 map<string, Mouldstate>::iterator m = p->second.MouldOnDevice.find(temp.ProductCode.substr(0, temp.ProductCode.find_first_of(‘+‘))); 20752076if (m->second.isOn) 2077 { 2078 q->second.time = GCDTime; 2079 q->second.flag = 1; 2080 SetofMould.find(m->second.MouldCode)->second.ProductIsProducing = temp.ProductCode; ////2081 p->second.ProductisProducing.insert(temp); 2082 cout << "产品" << temp.ProductCode << "于" << t << "时刻加入设备" << p->first << "开始生产" << endl; 2083 map<string, multiset<CodePriorityNAT>>::iterator temp1; 20842085if (p->second.WaitingQueue.isEmpty() == false) 2086 { 2087for (Priority_Queue::iterator s = p->second.WaitingQueue.begin(); s != p->second.WaitingQueue.end(); ++s) 2088 { 2089if (s->NaturalAgingTime != -1 && s->NaturalAgingTime != Product::ProductCategoryWorkingProcedure.find(s->ProductCode.substr(0, s->ProductCode.find_first_of(‘+‘)))->second[SetofProduct.find(s->ProductCode)->second.index - 1].NAT) 2090 s->NaturalAgingTime += GCDTime; 2091 } 2092 } 20932094 temp1 = TempMap.find(q->second.DeviceID); 2095if (temp1->second.empty() == false) 2096 { 2097for (multiset<CodePriorityNAT>::iterator t = temp1->second.begin(); t != temp1->second.end(); t= temp1->second.erase(t)) 2098 p->second.WaitingQueue.Insert(*t); 2099 } 2100 TempMap.erase(temp1); 2101 } 2102else2103 { 2104 map<string, mould, comparatorless>::iterator t1 = SetofMould.find(m->second.MouldCode); 2105bool ismeet = true; 2106for (map<string, bool>::iterator s = t1->second.AsPartComponent.begin(); s != t1->second.AsPartComponent.end(); ++s) 2107 { 2108if (s->second == false) 2109 { 2110 map<string, Component>::iterator f = SetofComponent.find(s->first); 2111 map<string, mould, comparatorless>::iterator w = SetofMould.find(f->second.ComponentCorrespondingMouldClass +‘+‘+ f->second.MouldSerialNumber); 2112if (w->second.MouldState == true && w->second.ProductIsProducing != "") 2113 { 2114 ismeet = false; 2115break; 2116 } 2117 } 2118 } 2119if (ismeet == true) 2120 { 2121 t1->second.MouldState = true; 2122 t1->second.ProductIsProducing = temp.ProductCode; 2123 m->second.isOn = true; 2124for (map<string, bool>::iterator s = t1->second.AsPartComponent.begin(); s != t1->second.AsPartComponent.end(); ++s) 2125 { 2126if (s->second == false) 2127 { 2128 map<string, Component>::iterator f = SetofComponent.find(s->first); 2129 map<string, mould, comparatorless>::iterator w = SetofMould.find(f->second.ComponentCorrespondingMouldClass + ‘+‘ + f->second.MouldSerialNumber); 2130if (w->second.MouldState == true) 2131 { 2132 w->second.ProductIsProducing = ""; 2133 w->second.MouldState = false; 2134 SetofDevice.find(w->second.DeviceBelongTo)->second.MouldOnDevice[w->first.substr(0, w->first.find_first_of(‘+‘))].isOn = false; 2135 } 21362137 w->second.AsPartComponent[f->first] = false; 2138 f->second.ComponentCorrespondingMouldClass = t1->first.substr(0, t1->first.find_first_of(‘+‘)); 2139 f->second.MouldSerialNumber = t1->first.substr(t1->first.find_first_of(‘+‘) + 1); 2140 t1->second.AsPartComponent[f->first] = true; 2141 } 2142 } 2143 q->second.time = GCDTime; 2144 q->second.flag = 1; 2145 p->second.ProductisProducing.insert(temp); 2146 cout << "产品" << temp.ProductCode << "于" << t << "时刻加入设备" << p->first << "开始生产" << endl; 2147 map<string, multiset<CodePriorityNAT>>::iterator temp1; 21482149if (p->second.WaitingQueue.isEmpty() == false) 2150 { 2151for (Priority_Queue::iterator s = p->second.WaitingQueue.begin(); s != p->second.WaitingQueue.end(); ++s) 2152 { 2153if (s->NaturalAgingTime != -1 && s->NaturalAgingTime != Product::ProductCategoryWorkingProcedure.find(s->ProductCode.substr(0, s->ProductCode.find_first_of(‘+‘)))->second[SetofProduct.find(s->ProductCode)->second.index - 1].NAT) 2154 s->NaturalAgingTime += GCDTime; 2155 } 2156 } 21572158 temp1 = TempMap.find(q->second.DeviceID); 2159if (temp1->second.empty() == false) 2160 { 2161for (multiset<CodePriorityNAT>::iterator t = temp1->second.begin(); t != temp1->second.end(); t = temp1->second.erase(t)) 2162 p->second.WaitingQueue.Insert(*t); 2163 } 2164 TempMap.erase(temp1); 2165 } 2166else2167 { 2168 TempMap[q->second.DeviceID].insert(temp); 2169if (!p->second.WaitingQueue.isEmpty()) 2170 { 2171 CodePriorityNAT temp1; 2172bool TF = false; 2173while ((*p).second.WaitingQueue.isEmpty() == false) 2174 { 2175 (*p).second.WaitingQueue.RemoveTop(temp1); 2176 map<string, Product, comparatorless>::iterator s = SetofProduct.find(temp1.ProductCode); 2177 map<string, vector<DeviceCategoryAndNAT>>::iterator v = Product::ProductCategoryWorkingProcedure.find(temp1.ProductCode.substr(0, temp1.ProductCode.find_first_of(‘+‘))); 2178if (temp1.NaturalAgingTime != -1 && (*v).second[(*s).second.index - 1].NAT != temp1.NaturalAgingTime) 2179 { 2180 temp1.NaturalAgingTime += GCDTime; 2181 TempMap[q->second.DeviceID].insert(temp1); 2182 } 2183else2184 { 2185 TF = true; 2186break; 2187 } 2188 } 21892190if (!(*p).second.WaitingQueue.isEmpty() || TF) 2191 { 2192 TempQueue.Insert(temp1); 2193continue; 2194 } 2195 } 21962197for (auto x = TempMap[q->second.DeviceID].begin(); x != TempMap[q->second.DeviceID].end(); x = TempMap[q->second.DeviceID].erase(x)) 2198 { 2199 (*p).second.WaitingQueue.Insert(*x); 2200 } 2201 TempMap.erase(q->second.DeviceID); 2202 } 2203 } 2204 } 22052206 map<string, multiset<CodePriorityNAT>> ProductInsert; //设备类别和待加入所有该类别设备的等待队列的所有产品的集合的映射关系2207for (map<string, Product, comparatorless>::iterator p = SetofProduct.begin(); p != SetofProduct.end(); ) 2208 { 2209 map<string, Device, comparatorless>::iterator q = SetofDevice.find(p->second.DeviceID); 2210if (p->second.flag == 1) 2211 { 2212 map<string, map<string, ProductionTimeAndPriority>>::iterator t1 = q->second.DeviceCategorySet.find(p->second.DeviceID.substr(0, p->second.DeviceID.find_first_of(‘+‘))); 2213 map<string, ProductionTimeAndPriority>::iterator s = t1->second.find(p->first.substr(0, p->first.find_first_of(‘+‘))); 2214 map<int, PrepareTimeProduceTime>::iterator m = s->second.ProductionTime.find(p->second.index); 22152216/*cout << "[" << t << "," << t + GCDTime << ")时间段产品" << p->first << "于设备" << q->first << "上生产加工";*/2217int temp; 2218if (q->second.LatestProductCategory != p->first.substr(0, p->first.find_first_of(‘+‘))) 2219 { 2220 temp = m->second.PrepareTime; 2221 } 2222else2223 { 2224 temp = 0; 2225 } 22262227if (m->second.ProduceTime + temp == p->second.time) 2228 { 2229 map<string, vector<DeviceCategoryAndNAT>>::iterator v = p->second.ProductCategoryWorkingProcedure.find(p->first.substr(0, p->first.find_first_of(‘+‘))); 2230 map<string, Mouldstate>::iterator m = q->second.MouldOnDevice.find(p->first.substr(0, p->first.find_first_of(‘+‘))); ////2231 map<string, mould, comparatorless>::iterator t1 = SetofMould.find(m->second.MouldCode); 2232 t1->second.ProductIsProducing = ""; ////22332234 cout << t + GCDTime << "时刻产品" << p->first << "在设备" << q->first << "上生产完毕"; 2235if (p->second.index == v->second.size() - 1) 2236 { 2237 q->second.ProductisProducing.clear(); 2238 q->second.LatestProductCategory = p->first.substr(0, p->first.find_first_of(‘+‘)); 2239 cout << ",此时该产品已走完整个加工流程"; 2240if (v->second.back().NAT == 0) //p所指产品生产完成,生产完成时间为t+GCDTime2241 { 2242 cout << "且走完加工流程后无自然时效时间,该产品生产完毕" << endl; 2243 p = SetofProduct.erase(p); 2244 } 2245else2246 { 2247 cout << "且走完加工流程后该产品仍有自然时效时间,该产品开始等待自然时效时间流逝完毕" << endl; 2248 p->second.flag = 0; //p所指产品在最后一台设备上生产完毕后有自然时效时间,把产品状态更新为特殊值表示产品处于自然时效阶段2249 p->second.time = 0; 2250 p->second.DeviceID = ""; 2251 ++p; 2252 } 2253continue; 2254 } 2255else2256 { 2257string Cat = v->second[p->second.index + 1].DeviceCategoryCode; 2258 map<string, multiset<CodePriorityNAT>>::iterator c = ProductInsert.find(Cat); 2259 CodePriorityNAT temp(p->first, Device::DeviceCategorySet.find(Cat)->second[q->second.ProductisProducing.begin()->ProductCode.substr(0, q->second.ProductisProducing.begin()->ProductCode.find_first_of(‘+‘))].Priority, 0); 2260 cout << ",此时该产品尚未走完整个加工流程,移出当前设备,等待加入下一设备的生产队列" << endl; 2261if (c == ProductInsert.end()) 2262 { 2263 multiset<CodePriorityNAT> temp1{ temp }; 2264 ProductInsert.insert(make_pair(Cat, temp1)); 2265 } 2266else2267 { 2268 c->second.insert(temp); 2269 } 22702271 p->second.flag = 0; 2272 p->second.time = 0; 2273 ++(p->second.index); 2274 q->second.ProductisProducing.clear(); //该产品在当前设备上生产完成时间为t+GCDTime2275 } 2276 q->second.LatestProductCategory = p->first.substr(0, p->first.find_first_of(‘+‘)); 2277 ++p; 2278 } 2279else2280 { 2281/*cout << "," << t + GCDTime << "时刻该产品在设备" << q->first << "上尚未生产完毕,继续生产"<<endl*/; 2282 ++p; 2283 } 22842285 } 2286else2287 { 2288if (p->second.DeviceID == "") 2289 { 2290/*cout << "[" << t << "," << t + GCDTime << ")时间段产品" << p->first << "之前已经走完整个加工流程,正在等待自然时效时间流逝完毕.";*/2291 p->second.time += GCDTime; 2292if (p->second.ProductCategoryWorkingProcedure.find(p->first.substr(0, p->first.find_first_of(‘+‘)))->second.back().NAT == p->second.time) 2293 { 2294 cout << t + GCDTime << "时刻产品" << p->first << "自然时效时间已经流逝完毕,产品生产完毕" << endl; 2295 p = SetofProduct.erase(p); 2296continue; 2297 } 2298else2299 { 2300/*cout << t + GCDTime << "时刻该产品自然时效时间尚未流逝完毕,继续等待" << endl*/; 2301 } 2302 } 2303else2304 { 2305/*cout << "[" << t << "," << t + GCDTime << ")时间段产品" << p->first << "在设备" << q->first << "的生产队列中等待生产" << endl*/; 2306 } 2307 ++p; 2308 } 2309 } 23102311if (SetofProduct.empty()) 2312 { 2313//所有产品生产完成,时间为t+GCDTime,结束2314 cout << "所有产品生产完成" << endl; 2315 clock_t endtime = clock(); 2316 cout << "生产时间计算耗费时间为" << (endtime - starttime) << "毫秒" << endl; 2317return t+GCDTime; 2318 } 23192320//此处利用ProductInsert执行多重优化插入,先判映射表是否为空2321if (ProductInsert.empty() == false) 2322 { 2323for (map<string, multiset<CodePriorityNAT>>::iterator p = ProductInsert.begin(); p != ProductInsert.end(); ++p) 2324 { 2325 pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator> tempIterator = SetofDevice.equal_range(p->first); 2326struct IteratorAndScheme 2327 { 2328int InsertScheme; //设备等待时间计算方案2329 map<string, Device, comparatorless>::iterator DeviceIterator; //指向设备的迭代器2330 IteratorAndScheme(map<string, Device, comparatorless>::iterator It, int I) :DeviceIterator(It), InsertScheme(I) {} 2331 }; 2332 vector<IteratorAndScheme> QueueIndex; 2333for (map<string, Device, comparatorless>::iterator q = tempIterator.first; q != tempIterator.second; ++q) 2334 { 2335if (StateQuery[q->first].flag == true) 2336 { 2337if (StateQuery[q->first].sign == 0) 2338 { 2339if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T1) 2340 { 2341 QueueIndex.push_back(IteratorAndScheme(q, 1)); 2342 } 2343else2344 { 2345 QueueIndex.push_back(IteratorAndScheme(q, 2)); 2346 } 2347 } 2348else2349 { 2350if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T2) 2351 { 2352 QueueIndex.push_back(IteratorAndScheme(q, 1)); 2353 } 2354else2355 { 2356 QueueIndex.push_back(IteratorAndScheme(q, 2)); 2357 } 2358 } 2359 } 2360else2361 { 2362 QueueIndex.push_back(IteratorAndScheme(q, 1)); 2363 } 2364 } 23652366 map<int, IncompleteOptimization> CurrentOptimization; //关键字队列编号2367struct PriorityInsertionTime 2368 { 2369 vector<int> ProductTime; //存放给定产品按优先级插入各队列后的等待时间2370 vector<IteratorAndScheme>::size_type index; //产品所在的等待时间最小的队列序号2371int tmin; //最小等待时间2372 }; 2373 map<string, PriorityInsertionTime> ProductQueueWaitingTime;//关键字产品编号2374int tmax; //映射表CurrentOptimization中各队列排在末端产品等待时间最大值2375 vector<IteratorAndScheme>::size_type index; //每一轮循环开始时为上一轮循环最优化插入的产品插入的队列编号2376 Priority_Queue::iterator it; //每一轮循环开始时为上一轮循环最优化插入产品插入队列中指向该产品的迭代器2377 vector<IteratorAndScheme>::size_type indexmax; //每一轮循环开始时为上一轮循环CurrentOptimization中各队列排在末端产品中等待时间最长的所在队列编号2378 Priority_Queue::iterator itmax; //每一轮循环开始时为指向上一轮循环CurrentOptimization中各队列排在末端产品中等待时间最长的产品所在位置的后一位置的迭代器2379 multiset<CodePriorityNAT>::size_type count = p->second.size(); 2380while (p->second.empty() == false) 2381 { 2382if (p->second.size() == count) 2383 { 2384 vector<IteratorAndScheme>::size_type indextemp; 2385 Priority_Queue::iterator iteratortemp; 2386string codetemp; 2387int tmin; 2388 multiset<CodePriorityNAT>::iterator qmin; 2389for (multiset<CodePriorityNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q) 2390 { 2391int Tmint; 2392 vector<IteratorAndScheme>::size_type Indext; 2393 Priority_Queue::iterator Iteratort; 2394 map<string, PriorityInsertionTime>::iterator temp1 = ProductQueueWaitingTime.insert(make_pair(q->ProductCode, PriorityInsertionTime())).first; 2395for (vector<IteratorAndScheme>::size_type i = 0; i < QueueIndex.size(); ++i) 2396 { 2397 Priority_Queue::iterator temp = QueueIndex[i].DeviceIterator->second.WaitingQueue.Insert(*q).second; 2398int waittime = WaitingTimeComputing(temp, QueueIndex[i].DeviceIterator, QueueIndex[i].InsertScheme); 2399if (i == 0) 2400 { 2401 Tmint = waittime; 2402 Indext = i; 2403 Iteratort = temp; 2404 ++Iteratort; 2405 } 2406else2407 { 2408if (waittime < Tmint) 2409 { 2410 Tmint = waittime; 2411 Indext = i; 2412 Iteratort = temp; 2413 ++Iteratort; 2414 } 2415 } 2416 QueueIndex[i].DeviceIterator->second.WaitingQueue.erase(temp); 2417 temp1->second.ProductTime.push_back(waittime); 2418 } 2419 temp1->second.index = Indext; 2420 temp1->second.tmin = Tmint; 2421if (q == p->second.begin()) 2422 { 2423 qmin = q; 2424 tmin = Tmint; 2425 codetemp = q->ProductCode; 2426 indextemp = Indext; 2427 iteratortemp = Iteratort; 2428 } 2429else2430 { 2431if (Tmint < tmin) 2432 { 2433 qmin = q; 2434 tmin = Tmint; 2435 codetemp = q->ProductCode; 2436 indextemp = Indext; 2437 iteratortemp = Iteratort; 2438 } 2439 } 2440 } 2441 tmax = tmin; 2442 it = QueueIndex[indextemp].DeviceIterator->second.WaitingQueue.insert(iteratortemp, *qmin); 2443 cout << t+GCDTime << "时刻产品" << qmin->ProductCode << "加入设备" << QueueIndex[indextemp].DeviceIterator->first << "等待队列等待生产" << endl; 2444 map<string, Product, comparatorless>::iterator s = SetofProduct.find(qmin->ProductCode); 2445 s->second.DeviceID = QueueIndex[indextemp].DeviceIterator->first; 2446 index = indextemp; 2447 indexmax = indextemp; 2448 itmax = iteratortemp; 2449 CurrentOptimization.insert(make_pair(indextemp, IncompleteOptimization(codetemp, --iteratortemp, tmin))); 2450 ProductQueueWaitingTime.erase(codetemp); 2451 p->second.erase(qmin); 2452 } 2453else2454 { 2455if (p->second.size() != 1) 2456 { 2457 Priority_Queue::iterator iteratorop; 2458 Priority_Queue::iterator iteratormaxop; 2459 vector<IteratorAndScheme>::size_type indexmaxop; 2460 vector<IteratorAndScheme>::size_type indexop; 2461 multiset<CodePriorityNAT>::iterator qop; 2462int tmaxop; 2463for (multiset<CodePriorityNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q) 2464 { 2465 Priority_Queue::iterator iteratort; 2466 Priority_Queue::iterator iteratormaxt; 2467 vector<IteratorAndScheme>::size_type indexmaxt; 2468int tmaxt; 24692470 iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second; 2471if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1) 2472 { 2473int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme); 2474 ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp; 24752476if (ProductQueueWaitingTime[q->ProductCode].index == index) 2477 { 2478int mintime; 2479 vector<int>::size_type minindex; 2480for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i) 2481 { 2482if (i == 0) 2483 { 2484 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 2485 minindex = i; 2486 } 2487else2488 { 2489if (ProductQueueWaitingTime[q->ProductCode].ProductTime[i] < mintime) 2490 { 2491 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 2492 minindex = i; 2493 } 2494 } 2495 } 2496 ProductQueueWaitingTime[q->ProductCode].index = minindex; //这里无符号类型不匹配,注意,可以采用使用迭代器的改进方案2497 ProductQueueWaitingTime[q->ProductCode].tmin = mintime; 2498 } 2499 } 2500 QueueIndex[index].DeviceIterator->second.WaitingQueue.erase(iteratort); 25012502 Priority_Queue::iterator tempinsert = QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.Insert(*q).second; 2503if (CurrentOptimization.find(ProductQueueWaitingTime[q->ProductCode].index) == CurrentOptimization.end()) 2504 { 2505 Priority_Queue::iterator after = tempinsert; 2506 ++after; 2507if (ProductQueueWaitingTime[q->ProductCode].tmin > tmax) 2508 { 2509 tmaxt = ProductQueueWaitingTime[q->ProductCode].tmin; 2510 iteratort = after; 2511 iteratormaxt = after; 2512 indexmaxt = ProductQueueWaitingTime[q->ProductCode].index; 2513 } 2514else2515 { 2516 tmaxt = tmax; 2517 iteratort = after; 2518 iteratormaxt = itmax; 2519 indexmaxt = indexmax; 2520 } 2521 } 2522else2523 { 2524 Priority_Queue::iterator after = tempinsert; 2525 ++after; 2526 Priority_Queue::iterator indexit = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].It; 2527if (PositionRelation(tempinsert, indexit, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue) == 1) 2528 { 2529if (ProductQueueWaitingTime[q->ProductCode].tmin > tmax) 2530 { 2531 tmaxt = ProductQueueWaitingTime[q->ProductCode].tmin; 2532 iteratort = after; 2533 iteratormaxt = after; 2534 indexmaxt = ProductQueueWaitingTime[q->ProductCode].index; 2535 } 2536else2537 { 2538 tmaxt = tmax; 2539 iteratort = after; 2540 iteratormaxt = itmax; 2541 indexmaxt = indexmax; 2542 } 2543 } 2544else2545 { 2546int DeltaTime = TimeIncrementComputing(indexit, tempinsert, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].InsertScheme); 2547int WaitTime = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].WaitTime; 2548if (WaitTime + DeltaTime > tmax) 2549 { 2550 tmaxt = WaitTime + DeltaTime; 2551 iteratort = after; 2552 iteratormaxt = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].It; 2553 ++iteratormaxt; 2554 indexmaxt = ProductQueueWaitingTime[q->ProductCode].index; 2555 } 2556else2557 { 2558 tmaxt = tmax; 2559 iteratort = after; 2560 iteratormaxt = itmax; 2561 indexmaxt = indexmax; 2562 } 2563 } 2564 } 25652566if (q == p->second.begin()) 2567 { 2568 tmaxop = tmaxt; 2569 iteratormaxop = iteratormaxt; 2570 iteratorop = iteratort; 2571 indexmaxop = indexmaxt; 2572 indexop = ProductQueueWaitingTime[q->ProductCode].index; 2573 qop = q; 2574 } 2575else2576 { 2577if (tmaxt < tmaxop) 2578 { 2579 tmaxop = tmaxt; 2580 iteratormaxop = iteratormaxt; 2581 iteratorop = iteratort; 2582 indexmaxop = indexmaxt; 2583 indexop = ProductQueueWaitingTime[q->ProductCode].index; 2584 qop = q; 2585 } 2586 } 2587 QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.erase(tempinsert); 2588 } 25892590 iteratorop = QueueIndex[indexop].DeviceIterator->second.WaitingQueue.insert(iteratorop, *qop); 2591 cout << t + GCDTime << "时刻产品" << qop->ProductCode << "加入设备" << QueueIndex[indexop].DeviceIterator->first << "等待队列等待生产" << endl; 2592 map<string, Product, comparatorless>::iterator s = SetofProduct.find(qop->ProductCode); 2593 s->second.DeviceID = QueueIndex[indexop].DeviceIterator->first; 2594if (CurrentOptimization.find(indexop) != CurrentOptimization.end()) 2595 { 2596if (PositionRelation(iteratorop, CurrentOptimization[indexop].It, QueueIndex[indexop].DeviceIterator->second.WaitingQueue) == 0) 2597 { 2598int TimeCorrrect = TimeIncrementComputing(CurrentOptimization[indexop].It, iteratorop, QueueIndex[indexop].DeviceIterator, QueueIndex[indexop].InsertScheme); 2599 CurrentOptimization[indexop].WaitTime += TimeCorrrect; 2600 } 2601else2602 { 2603 CurrentOptimization[indexop].ProductCode = qop->ProductCode; 2604 CurrentOptimization[indexop].It = iteratorop; 2605 CurrentOptimization[indexop].WaitTime = ProductQueueWaitingTime[qop->ProductCode].tmin; 2606 } 2607 } 2608else2609 { 2610 CurrentOptimization.insert(make_pair(indexop, IncompleteOptimization(qop->ProductCode, iteratorop, ProductQueueWaitingTime[qop->ProductCode].tmin))); 2611 } 2612 tmax = tmaxop; 2613 index = indexop; 2614 it = iteratorop; 2615 indexmax = indexmaxop; 2616 itmax = iteratormaxop; 26172618 ProductQueueWaitingTime.erase(qop->ProductCode); 2619 p->second.erase(qop); 2620 } 2621else2622 { 2623 multiset<CodePriorityNAT>::iterator q = p->second.begin(); 2624 Priority_Queue::iterator iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second; 2625if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1) 2626 { 2627int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme); 2628 ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp; 26292630if (ProductQueueWaitingTime[q->ProductCode].index == index) 2631 { 2632int mintime; 2633 vector<int>::size_type minindex; 2634for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i) 2635 { 2636if (i == 0) 2637 { 2638 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 2639 minindex = i; 2640 } 2641else2642 { 2643if (ProductQueueWaitingTime[q->ProductCode].ProductTime[i] < mintime) 2644 { 2645 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i]; 2646 minindex = i; 2647 } 2648 } 2649 } 2650 ProductQueueWaitingTime[q->ProductCode].index = minindex; //这里无符号类型不匹配,注意,可以采用使用迭代器的改进方案2651 ProductQueueWaitingTime[q->ProductCode].tmin = mintime; 2652 } 2653 } 2654 QueueIndex[index].DeviceIterator->second.WaitingQueue.erase(iteratort); 2655 QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.Insert(*q); 2656 cout << t + GCDTime << "时刻产品" << q->ProductCode << "加入设备" << QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->first << "等待队列等待生产" << endl; 2657 map<string, Product, comparatorless>::iterator s = SetofProduct.find(q->ProductCode); 2658 s->second.DeviceID = QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->first; 2659 p->second.clear(); 2660 } 2661 } 2662 } 2663 } 2664 } 2665//设备状态调整2666 map<string, RepairTime>::iterator p = RepairTimeOfDevice.begin(); 2667 map<string, Device, comparatorless>::iterator s = SetofDevice.begin(); 2668for (map<string, DeviceState>::iterator q = StateQuery.begin(); q != StateQuery.end(); ) 2669 { 2670if (q->first.substr(0, q->first.find_first_of(‘+‘)) == p->first) 2671 { 2672if (q->second.flag == true) 2673 { 2674 q->second.T1 += GCDTime; 2675if (q->second.sign == 0) 2676 { 2677if (p->second.T1 == q->second.T1) 2678 { 2679 q->second.T1 = 0; 2680 q->second.flag = false; 2681 s->second.isTakenUp = false; 2682 } 2683 } 2684else2685 { 2686if (q->second.T1 == p->second.T2) 2687 { 2688 q->second.T1 = 0; 2689 q->second.flag = false; 2690 s->second.isTakenUp = false; 2691 } 2692 } 2693 } 2694else2695 { 2696if (q->second.sign == 0) 2697 { 2698if (q->second.T2 == p->second.DeltaT1 || (q->second.T2 += GCDTime, q->second.T2 == p->second.DeltaT1)) 2699 { 2700if (s->second.ProductisProducing.empty() == true) 2701 { 2702 q->second.T2 = 0; 2703 q->second.sign = 1; 2704 q->second.flag = true; 2705 s->second.isTakenUp = true; 2706 } 2707 } 2708else2709 { 2710if (s->second.ProductisProducing.empty() == true) 2711 s->second.isTakenUp = false; 2712 } 2713 } 2714else2715 { 2716if (q->second.T2 == p->second.DeltaT2 || (q->second.T2 += GCDTime, q->second.T2 == p->second.DeltaT2)) 2717 { 2718if (s->second.ProductisProducing.empty() == true) 2719 { 2720 q->second.T2 = 0; 2721 q->second.sign = 0; 2722 q->second.flag = true; 2723 s->second.isTakenUp = true; 2724 } 2725 } 2726else2727 { 2728if (s->second.ProductisProducing.empty() == true) 2729 s->second.isTakenUp = false; 2730 } 2731 } 2732 } 2733 ++q; 2734 ++s; 2735 } 2736else2737 { 2738 ++p; 2739 } 2740 } 2741 t += GCDTime; 2742 } 2743} 27442745int ProductionCycleComputing::WaitingTimeComputing(Priority_Queue::iterator it, map<string, Device, comparatorless>::iterator m, int InsertScheme) 2746{ 2747 Priority_Queue::iterator before = m->second.WaitingQueue.begin(); 2748 Priority_Queue::iterator after = before; 2749 ++after; 2750int t = 0; 2751if (InsertScheme == 1) 2752 { 2753if (m->second.ProductisProducing.empty() == false) 2754 { 2755conststring &temp = (*m->second.ProductisProducing.begin()).ProductCode; 2756 PrepareTimeProduceTime &temp1 = m->second.DeviceCategorySet.find(m->first.substr(0, m->first.find_first_of(‘+‘)))->second[temp.substr(0, temp.find_first_of(‘+‘))].ProductionTime[SetofProduct.find(temp)->second.index]; 2757 t+=temp1.ProduceTime; 2758if (temp.substr(0, temp.find_first_of(‘+‘)) != m->second.LatestProductCategory) 2759 t += temp1.PrepareTime; 2760 t -= SetofProduct.find(temp)->second.time; 2761 } 2762 } 2763else2764 { 2765if (StateQuery[m->first].sign == 0) 2766 t += RepairTimeOfDevice[m->first.substr(0, m->first.find_first_of(‘+‘))].T1; 2767else2768 t += RepairTimeOfDevice[m->first.substr(0, m->first.find_first_of(‘+‘))].T2; 2769 t -= StateQuery[m->first].T1; 2770 } 27712772if (before->ProductCode != it->ProductCode) 2773 { 2774string temp = before->ProductCode; 2775 PrepareTimeProduceTime &temp1 = m->second.DeviceCategorySet.find(m->first.substr(0, m->first.find_first_of(‘+‘)))->second[temp.substr(0, temp.find_first_of(‘+‘))].ProductionTime[SetofProduct.find(temp)->second.index]; 2776 t += temp1.ProduceTime; 2777if (InsertScheme == 1 && m->second.ProductisProducing.empty() == false) 2778 { 2779if (temp.substr(0, temp.find_first_of(‘+‘)) != (*m->second.ProductisProducing.begin()).ProductCode.substr(0, (*m->second.ProductisProducing.begin()).ProductCode.find_first_of(‘+‘))) 2780 t += temp1.PrepareTime; 2781 } 2782else2783 { 2784if (temp.substr(0, temp.find_first_of(‘+‘)) != m->second.LatestProductCategory) 2785 t += temp1.PrepareTime; 2786 } 27872788if (after->ProductCode != it->ProductCode) 2789 { 2790while (after != it) 2791 { 2792string temp = after->ProductCode; 2793 PrepareTimeProduceTime &temp1 = m->second.DeviceCategorySet.find(m->first.substr(0, m->first.find_first_of(‘+‘)))->second[temp.substr(0, temp.find_first_of(‘+‘))].ProductionTime[SetofProduct.find(temp)->second.index]; 2794 t += temp1.ProduceTime; 27952796if (temp.substr(0, temp.find_first_of(‘+‘))!=before->ProductCode.substr(0, before->ProductCode.find_first_of(‘+‘))) 2797 t+=temp1.PrepareTime; 27982799 ++after; 2800 ++before; 2801 } 2802 } 2803 } 2804return t; 2805} 28062807int ProductionCycleComputing::TimeIncrementComputing(const Priority_Queue::iterator &BeCalculated, const Priority_Queue::iterator &CauseCalculating, map<string, Device, comparatorless>::iterator m, int InsertScheme) 2808{ 2809int t = 0; //修正等待时间的时间增量2810string code; 2811if (CauseCalculating == m->second.WaitingQueue.begin()) 2812 { 2813if (InsertScheme == 1 && m->second.ProductisProducing.empty() == false) 2814 { 2815 code = (*m->second.ProductisProducing.begin()).ProductCode; 2816 code = code.substr(0, code.find_first_of(‘+‘)); 2817 } 2818else2819 { 2820 code = m->second.LatestProductCategory; 2821 } 2822 } 2823else2824 { 2825 Priority_Queue::iterator temp = CauseCalculating; 2826 --temp; 2827 code = temp->ProductCode.substr(0, temp->ProductCode.find_first_of(‘+‘)); 2828 } 28292830 { 2831 PrepareTimeProduceTime temp1; 2832if (SetofProduct.find(CauseCalculating->ProductCode) != SetofProduct.end()) 2833 { 2834 temp1 = m->second.DeviceCategorySet.find(m->first.substr(0, m->first.find_first_of(‘+‘)))->second[CauseCalculating->ProductCode.substr(0, CauseCalculating->ProductCode.find_first_of(‘+‘))].ProductionTime[SetofProduct.find(CauseCalculating->ProductCode)->second.index]; 2835 } 2836else2837 { 2838 temp1 = m->second.DeviceCategorySet.find(m->first.substr(0, m->first.find_first_of(‘+‘)))->second[CauseCalculating->ProductCode.substr(0, CauseCalculating->ProductCode.find_first_of(‘+‘))].ProductionTime[0]; 2839 } 2840 t += temp1.ProduceTime; 2841if (CauseCalculating->ProductCode.substr(0, CauseCalculating->ProductCode.find_first_of(‘+‘)) != code) 2842 t += temp1.PrepareTime; 2843 } 28442845 Priority_Queue::iterator temp = CauseCalculating; 2846 ++temp; 2847if (temp != BeCalculated) 2848 { 2849string Cat1 = temp->ProductCode.substr(0, temp->ProductCode.find_first_of(‘+‘)); 2850string Cat2 = CauseCalculating->ProductCode.substr(0, CauseCalculating->ProductCode.find_first_of(‘+‘)); 2851if ((Cat1 != code && Cat1 == Cat2) || (Cat1 == code && Cat1 != Cat2)) 2852 { 2853 PrepareTimeProduceTime &temp1 = m->second.DeviceCategorySet.find(m->first.substr(0, m->first.find_first_of(‘+‘)))->second[Cat1].ProductionTime[SetofProduct.find(temp->ProductCode)->second.index]; 2854if (Cat1 != code && Cat1 == Cat2) 2855 { 2856 t -= temp1.PrepareTime; 2857 } 2858else2859 { 2860 t += temp1.PrepareTime; 2861 } 2862 } 2863 } 2864return t; 2865} 28662867int ProductionCycleComputing::PositionRelation(const Priority_Queue::iterator &it1, const Priority_Queue::iterator &it2, Priority_Queue &m) //判断优先级队列中it1是在it2之后还是在之前,在之前返回0,在之后返回1,调用者保证it1,it2不等2868{ 2869 Priority_Queue::iterator before = it1; 2870 Priority_Queue::iterator after = it1; 28712872while (before != it2 && (after != m.end() && after != it2)) 2873 { 2874if (before != m.begin()) 2875 { 2876 --before; 2877 } 2878 ++after; 2879 } 28802881if (before == it2) 2882 { 2883return1; 2884 } 2885else2886 { 2887if (after == m.end()) 2888 { 2889if (it2 == m.end()) 2890return0; 2891else2892return1; 2893 } 2894else2895 { 2896return0; 2897 } 2898 } 2899} 29002901int main() 2902{ 2903 Product::initProductCategoryWorkingProcedure(); 2904 Device::initDeviceCategorySet(); 2905 ProductionCycleComputing CalculationExample; 2906 cout << "生产完成时间为" << CalculationExample.ProductionCycleCalculation() << endl; 2907 system("Pause"); 2908return0; 2909 }
运行结果(输入文件内容即所给样例文件内容) :
原文:https://www.cnblogs.com/WSKIT/p/9160601.html
内容总结
以上是互联网集市为您收集整理的C++编程实现对工厂产品生产流程的模拟全部内容,希望文章能够帮你解决C++编程实现对工厂产品生产流程的模拟所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。