C++编程实现对工厂产品生产流程的模拟

花费二个多月的时间编写了可以实时模拟工厂产品生产流程的程序,工厂产品生产流程的模拟,就是计算在工艺文件所规定的工序下,不同种类的多件产品(同一类别的产品可以有多件)在不同类别的多台设备(同一类别的设备可以有多台)上全部生产完毕所需的总时间。每一件产品可以在生产流程中先后多次在同一类设备上生产而且生产时间可以不同,某一给定时刻多件产品有可能需要在同一台设备上生产,造成设备占用冲突,这时必须按一定的算法对造成冲突的产品在设备等待队列上按优先级排队。产品在设备上加工完毕准备进入下一台设备生产时有同类型的多台设备可供选择,此时必须设计算法选择加工该产品的设备使生产时间尽可能短。每一件产品在生产前可能会有生产准备时间,每一件产品在某一台设备上生产完毕后可能会存在自然时效时间。在生产过程中的任意时刻可以插入待生产的多件新产品,每一台设备都有与之关联的模具,每件模具可以生产特定种类的产品,模具可以由多个零件组成,一个零件又可以作为多件模具的组成部件。设备每过一段时间就要调整或维修,维修分为小修和大修,小修大修时间以及小修大修时间的时间间隔有明确的规定,不会改变。我们需要在以上诸多条件的限制和作用下通过编写程序让计算机计算产品全部生产完成且尽可能接近最短的生产时间,以为工厂处理客户订单提供参考和依据。要注意的是,本文要解决的问题和车间作业计划调度问题有本质区别,后者为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>
  13 using namespace std;
  14
  15 struct comparatorless
  16 {
  17     bool operator()(const string &s1, const string &s2) const;    //重载函数调用运算符比较编号和类别编号或编号和编号,判断s1是否小于s2
  18 };
  19 bool comparatorless::operator()(const string &s1, const string &s2) const
  20 {
  21     if (s2.find_first_of(‘+‘) == string::npos)   //类别编号s2和编号s1比较
  22     {
  23         return s1.substr(0, s1.find_first_of(‘+‘)) < s2;
  24     }
  25     else             //编号s1和编号s2比较
  26     {
  27         string::size_type index = s2.find_first_of(‘+‘, 0);
  28         string prefixP = s2.substr(0, index);   //提取s2以+分隔的前后缀
  29         string suffixP = s2.substr(index + 1);
  30
  31         index = s1.find_first_of(‘+‘, 0);
  32         string prefix = s1.substr(0, index);   //提取s1以+分隔的前后缀
  33         string suffix = s1.substr(index + 1);
  34
  35         if (prefix < prefixP)
  36         {
  37             return true;
  38         }
  39         else
  40         {
  41             if (prefix == prefixP)
  42             {
  43                 if (suffix < suffixP)
  44                 {
  45                     return true;
  46                 }
  47                 else
  48                 {
  49                     return false;
  50                 }
  51             }
  52             else
  53             {
  54                 return false;
  55             }
  56         }
  57     }
  58 }
  59
  60 //初始化过程即构造函数实现未定义,第一次运行前再定义,初始GCDTime的计算,类定义实现放入头文件
  61 struct CodePriorityNAT  //设备等待队列中或设备上正在生产的产品的编号,在该设备上的生产优先级以及在上一个设备上生产完毕后已流逝的自然时效时间
  62 {
  63     bool operator<(const CodePriorityNAT &P) const { return ProductionPriority < P.ProductionPriority; }  //重载关系运算符用优先级比较实现CodePriorityNAT对象比较
  64     bool operator>(const CodePriorityNAT &P) const { return ProductionPriority > P.ProductionPriority; }
  65     bool operator>=(const CodePriorityNAT &P) const { return !(operator<(P)); }
  66     bool operator<=(const CodePriorityNAT &P) const { return !(operator>(P)); }
  67     bool operator==(const CodePriorityNAT &P) const { return !(operator<(P)) && !(operator>(P)); }
  68     bool operator!=(const CodePriorityNAT &P) const { return !(operator==(P)); }
  69     string ProductCode; //产品编号
  70     int ProductionPriority;  //生产优先级
  71     int NaturalAgingTime;  //已流逝的自然时效时间
  72     CodePriorityNAT(string P1, int P2, int N) :ProductCode(P1), ProductionPriority(P2), NaturalAgingTime(N) {}
  73     CodePriorityNAT() = default;
  74 };
  75
  76 class Priority_Queue    //设备等待队列类(优先级队列),队列数据元素为CodePriorityNAT
  77 {
  78 public:
  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为指向插入元素的迭代器
  83     bool RemoveTop(CodePriorityNAT &x);     //删除最高优先级元素并用x将其返回
  84     bool GetTop(CodePriorityNAT &x) const;  //获取最高优先级元素并用x将其返回
  85     void MakeEmpty() { Queue.clear(); }    //清空队列
  86     bool isEmpty() const { return Queue.empty(); }   //判断队列是否为空
  87     bool 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
  94 private:
  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
 106     if (Queue.begin() != p)
 107         --p;
 108     else
 109     {
 110         return Queue.insert(p, temp);
 111     }
 112
 113     while (true)
 114     {
 115         if ((*p) > temp)
 116         {
 117             if (p != Queue.begin())
 118             {
 119                 --p;
 120                 if (p == Queue.begin())
 121                     continue;
 122             }
 123         }
 124         else
 125         {
 126             ++p;
 127             return Queue.insert(p, temp);
 128         }
 129
 130         if (p == Queue.begin())
 131             break;
 132     }
 133     return Queue.insert(p, temp);
 134 }
 135
 136 pair<bool, Priority_Queue::iterator> Priority_Queue::Insert(const CodePriorityNAT &x)
 137 {
 138     if (isFull())
 139         return {false, end()};
 140     else
 141     {
 142         Queue.push_back(x);
 143         return { true, adjust() };
 144     }
 145 }
 146
 147 bool Priority_Queue::RemoveTop(CodePriorityNAT &x)
 148 {
 149     if (isEmpty())
 150         return false;
 151     else
 152     {
 153         x = Queue.front();
 154         Queue.pop_front();
 155         return true;
 156     }
 157 }
 158
 159 bool Priority_Queue::GetTop(CodePriorityNAT &x) const
 160 {
 161     if (isEmpty())
 162         return false;
 163     else
 164     {
 165         x = Queue.front();
 166         return true;
 167     }
 168 }
 169
 170 struct PrepareTimeProduceTime
 171 {
 172     int PrepareTime;   //生产准备时间
 173     int ProduceTime;   //实际生产时间
 174     PrepareTimeProduceTime(int Pre, int Pro) :PrepareTime(Pre), ProduceTime(Pro) {}
 175     PrepareTimeProduceTime() = default;
 176 };
 177
 178 struct ProductionTimeAndPriority
 179 {
 180
 181     map<int, PrepareTimeProduceTime> ProductionTime;   //当前类别的设备在在ProductClassTimePriorityMappingTable中的给定的产品类别对应的生产工序vector中的下标(map关键字)及对应的准备及生产时间(映射值)
 182     int Priority;  //给定类别的产品在当前设备的上的生产优先级
 183 };
 184
 185 struct DeviceCategoryAndNAT
 186 {
 187     string DeviceCategoryCode;    //设备类别编号
 188     int NAT;    //当前类别的产品在该设备上生产完毕后的自然时效时间
 189     DeviceCategoryAndNAT(string De, int N) :DeviceCategoryCode(De), NAT(N) {}
 190 };
 191
 192 class Product
 193 {
 194 public:
 195     string DeviceID;  //产品正在占用或等待的设备编号
 196     int index; //上述DeviceID标识的设备的设备类别在当前产品的类别对应的产品工艺流程vector中的下标
 197     int time;  //产品正在等待DeviceID标识的设备为0,否则为产品已占用DeviceID标识的设备的时间
 198     int flag;  //产品正在占用设备为1,产品正在等待设备为0
 199     static map<string, vector<DeviceCategoryAndNAT>> ProductCategoryWorkingProcedure; //各产品类别及对应的工艺流程的集合
 200     static void initProductCategoryWorkingProcedure();   //初始化ProductCategoryWorkingProcedure
 201     Product(string D, int i, int t, int f) :DeviceID(D), index(i), time(t), flag(f) {}
 202 };
 203
 204 void Product::initProductCategoryWorkingProcedure()   //解析当前目录下ProductCategoryWorkingProcedure.txt从中提取初始化数据初始化ProductCategoryWorkingProcedure
 205 {
 206     ifstream input("ProductCategoryWorkingProcedure.txt");
 207     string temp;
 208     int level;
 209     int flag;
 210     map<string, vector<DeviceCategoryAndNAT>>::iterator p1;
 211     while (input >> temp)
 212     {
 213         if (temp == "#b1" || temp == "#e1")
 214         {
 215             if (temp == "#b1")
 216             {
 217                 level = 1;
 218             }
 219             else
 220             {
 221                 continue;
 222             }
 223         }
 224         else
 225         {
 226             if (temp == "#b2" || temp == "#e2")
 227             {
 228                 if (temp == "#b2")
 229                 {
 230                     level = 2;
 231                     flag = 0;
 232                 }
 233                 else
 234                 {
 235                     continue;
 236                 }
 237             }
 238             else
 239             {
 240                 if (level == 1)
 241                 {
 242                     p1 = ProductCategoryWorkingProcedure.insert(make_pair(temp, vector<DeviceCategoryAndNAT>())).first;
 243                 }
 244                 else
 245                 {
 246                     if (flag == 0)
 247                     {
 248                         p1->second.push_back(DeviceCategoryAndNAT(temp, 0));
 249                         flag = 1;
 250                     }
 251                     else
 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
 264 struct Mouldstate
 265 {
 266     string MouldCode;   //与产品类别对应的模具编号
 267     bool isOn;  //标志模具状态为上线下线的bool变量(T上线F下线)
 268     Mouldstate(string Mo, bool isOn) :MouldCode(Mo), isOn(isOn) {}
 269     Mouldstate() = default;
 270 };
 271
 272 class Device
 273 {
 274 public:
 275     static map<string, map<string, ProductionTimeAndPriority>> DeviceCategorySet; //设备类别和其能够生产的产品类别及对应的生产时间优先级构成的集合,外层关键字映射值为映射表,关键字为产品类别编号,映射值为该类别的设备能够生产的产品类别,及该类别的设备在该产品类别的工艺流程表中对应各下标及生产时间
 276     static void initDeviceCategorySet();  //初始化DeviceCategorySet
 277     Priority_Queue WaitingQueue; //当前设备的等待队列
 278     set<CodePriorityNAT> ProductisProducing;  //设备当前正在生产的零件对应的三元组,set中只能有一个元素
 279     bool isTakenUp;  //标志当前设备是否被占用,T占用(有产品生产或处于调整状态),F闲置
 280     string LatestProductCategory; //该设备最近一次生产完的产品的类别编号
 281     map<string, Mouldstate> MouldOnDevice; //映射表,保存模具能够生产的产品类别(关键字)和模具及模具状态的对应关系
 282 };
 283
 284 void Device::initDeviceCategorySet()   ////解析当前目录下PriorityProducePerpareTime.txt从中提取初始化数据初始化ProductCategoryWorkingProcedure
 285 {
 286     ifstream input("PriorityProducePerpareTime.txt");
 287     string temp;
 288     int level;
 289     int rowinlevel;
 290     map<string, map<string, ProductionTimeAndPriority>>::iterator p1;
 291     map<string, ProductionTimeAndPriority>::iterator p2;
 292     map<int, PrepareTimeProduceTime>::iterator p3;
 293     while (input >> temp)
 294     {
 295         if (temp == "#b1" || temp == "#e1")
 296         {
 297             if (temp == "#b1")
 298             {
 299                 level = 1;
 300                 rowinlevel = 1;
 301             }
 302             else
 303             {
 304                 continue;
 305             }
 306         }
 307         else
 308         {
 309             if (temp == "#b2" || temp == "#e2")
 310             {
 311                 if (temp == "#b2")
 312                 {
 313                     level = 2;
 314                     rowinlevel = 1;
 315                 }
 316                 else
 317                 {
 318                     continue;
 319                 }
 320             }
 321             else
 322             {
 323                 if (temp == "#b3" || temp == "#e3")
 324                 {
 325                     if (temp == "#b3")
 326                     {
 327                         level = 3;
 328                         rowinlevel = 1;
 329                     }
 330                     else
 331                     {
 332                         continue;
 333                     }
 334                 }
 335                 else
 336                 {
 337                     if (level == 1)
 338                     {
 339                         p1 = DeviceCategorySet.insert(make_pair(temp, map<string, ProductionTimeAndPriority>())).first;
 340                     }
 341                     else if (level == 2)
 342                     {
 343                         p2 = p1->second.insert(make_pair(temp, ProductionTimeAndPriority())).first;
 344                     }
 345                     else if (level == 3)
 346                     {
 347                         if (rowinlevel == 1)
 348                         {
 349                             p2->second.Priority = stoi(temp);
 350                         }
 351                         else
 352                         {
 353                             if (rowinlevel % 3 == 2)
 354                             {
 355                                 p3 = p2->second.ProductionTime.insert(make_pair(stoi(temp), PrepareTimeProduceTime())).first;
 356                             }
 357                             else if (rowinlevel % 3 == 0)
 358                             {
 359                                 p3->second.PrepareTime = stoi(temp);
 360
 361                             }
 362                             else
 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
 378 class mould   //模具类 添加关键字对象比较
 379 {
 380 public:
 381     map<string, bool> AsPartComponent;  //映射表,内容为组成模具的零件编号和标志零件是否被占用的标志变量之间的映射关系
 382     string ProductIsProducing; //正在使用该模具生产的产品编号
 383     string DeviceBelongTo;  //模具所属设备编号
 384     bool MouldState; //模具状态,T上线,F下线
 385 };
 386
 387 class Component  //零件类添加关键字对象比较
 388 {
 389 public:
 390     set<string> MouldBelongTo;  //零件所属模具编号的集合
 391     string ComponentCorrespondingMouldClass; //该零件占用的模具能够生产的产品类别编号
 392     string MouldSerialNumber;  //该零件占用的模具序号
 393 };
 394
 395 struct ProductInsertion
 396 {
 397     int InsertTime;   //产品插入时间
 398     set<string> ProductToInsert;   //时间InsertTime下要插入的产品的编号构成的集合
 399     ProductInsertion(int I) :InsertTime(I) {}
 400 };
 401
 402 struct IncompleteOptimization
 403 {
 404     string ProductCode;   //目前已生成的不完全优化方案中对应队列的已加入产品中排在最末端的产品编号
 405     Priority_Queue::iterator It;    //在队列中指向该产品的迭代器
 406     int WaitTime;    //该产品的等待时间
 407     IncompleteOptimization(string P, Priority_Queue::iterator I, int W) :ProductCode(P), It(I), WaitTime(W) {}
 408     IncompleteOptimization() = default;
 409
 410 };
 411
 412 class ProductionCycleComputing
 413 {
 414 public:
 415     int ProductionCycleCalculation();  //计算产品生产周期,返回生产完成时间
 416     ProductionCycleComputing();
 417
 418 private:
 419     static int PositionRelation(const Priority_Queue::iterator &it1, const Priority_Queue::iterator &it2, Priority_Queue &m);  //判断优先级队列中it1是在it2之后还是在之前,在之前返回0,在之后返回1,调用者保证it1,it2不等
 420     int WaitingTimeComputing(Priority_Queue::iterator it, map<string, Device, comparatorless>::iterator m, int InsertScheme);  //计算it所指元素在队列中的等待时间
 421     int TimeIncrementComputing(const Priority_Queue::iterator &BeCalculated, const Priority_Queue::iterator &CauseCalculating, map<string, Device, comparatorless>::iterator m, int InsertScheme); //计算CauseCalculating加入队列后导致修正BeCalculated等待时间所需时间增量
 422     bool NewProductInsert(int &t, const bool &TF);  //处理t时刻新产品插入,TF为true表示在第二轮循环处理否则表示在第一轮循环处理
 423     shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>> ProductInsertHandle(map<string, multiset<CodePriorityNAT>> &ProductInsert, const int &tstart, const int &t);  //tstart时刻将ProductInsert中的产品加入对应设备集的等待队列
 424     void ProductAddToProduction(map<string, map<string, Device, comparatorless>::iterator> &SetofDevice, const int tstart, const int tend, const int &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中的加入关系执行产品加入
 425     void GCDTimeUpdate();  //用于插入产品后更新最大公约数时间GCDTime
 426     static int GCDComputing(const int n, const int m); //计算并返回n,m的最大公约数
 427     void initInsert();
 428     void initMould();
 429     void initComponent();
 430     void initDevice();
 431     void initStateQuery();
 432     void 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;
 439     int GCDTime;  //最大公约数时间(各类设备上生产各类产品的时间,各类产品在各类设备上生产完毕后的自然时效时间,各类产品在各类设备上的生产准备时间,各类设备小修大修时间及时间间隔,以及在运行时要计入的设备产品的剩余生产时间)
 440     struct DeviceState
 441     {
 442         int T2;  //距设备最近一次调整已流逝的时间
 443         int T1;  //设备已调整时间
 444         int sign; //最近一次进行的是大修还是小修,0小修,1大修
 445         bool flag;  //设备是否正在调整,T正在调整,F未在调整
 446     };
 447     map<string, DeviceState> StateQuery;  //设备编号(关键字)和设备状态之间的映射关系
 448     struct RepairTime
 449     {
 450         int T2; //大修时长
 451         int T1; //小修时长
 452         int DeltaT1;  //小修至大修的时间间隔
 453         int DeltaT2;  //大修至小修的时间间隔
 454     };
 455     map<string, RepairTime> RepairTimeOfDevice; //设备类别和维修时间之间的映射关系
 456 };
 457
 458 void ProductionCycleComputing::initRepairTimeOfDevice()
 459 {
 460     ifstream input("RepairTimeOfDevice.txt");
 461     string temp;
 462     int level;
 463     map<string, RepairTime>::iterator p1;
 464     while (input >> temp)
 465     {
 466         if (temp == "#b1" || temp == "#e1")
 467         {
 468             if (temp == "#b1")
 469             {
 470                 level = 1;
 471             }
 472             else
 473             {
 474                 continue;
 475             }
 476         }
 477         else
 478         {
 479             if (temp == "#b2" || temp == "#e2")
 480             {
 481                 if (temp == "#b2")
 482                 {
 483                     level = 2;
 484                 }
 485                 else
 486                 {
 487                     continue;
 488                 }
 489             }
 490             else
 491             {
 492                 if (level == 1)
 493                 {
 494                     p1 = RepairTimeOfDevice.insert(make_pair(temp, RepairTime())).first;
 495                 }
 496                 else
 497                 {
 498                     int flag = 1;
 499                     while (temp != "#e2")
 500                     {
 501                         if (flag == 1)
 502                         {
 503                             p1->second.DeltaT1 = stoi(temp);
 504                             flag = 2;
 505                         }
 506                         else if (flag == 2)
 507                         {
 508                             p1->second.DeltaT2 = stoi(temp);
 509                             flag = 3;
 510                         }
 511                         else if (flag == 3)
 512                         {
 513                             p1->second.T1 = stoi(temp);
 514                             flag = 4;
 515                         }
 516                         else
 517                         {
 518                             p1->second.T2 = stoi(temp);
 519                         }
 520                         input >> temp;
 521                     }
 522                 }
 523             }
 524         }
 525     }
 526 }
 527 void ProductionCycleComputing::initStateQuery()
 528 {
 529     ifstream input("StateQuery.txt");
 530     string temp;
 531     int level;
 532     map<string, DeviceState>::iterator p1;
 533     while (input >> temp)
 534     {
 535         if (temp == "#b1" || temp == "#e1")
 536         {
 537             if (temp == "#b1")
 538             {
 539                 level = 1;
 540             }
 541             else
 542             {
 543                 continue;
 544             }
 545         }
 546         else
 547         {
 548             if (temp == "#b2" || temp == "#e2")
 549             {
 550                 if (temp == "#b2")
 551                 {
 552                     level = 2;
 553                 }
 554                 else
 555                 {
 556                     continue;
 557                 }
 558             }
 559             else
 560             {
 561                 if (level == 1)
 562                 {
 563                     p1 = StateQuery.insert(make_pair(temp, DeviceState())).first;
 564                 }
 565                 else
 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 }
 578 void ProductionCycleComputing::initDevice()
 579 {
 580     ifstream input("Device.txt");
 581     string temp;
 582     int level;
 583     map<string, Device, comparatorless>::iterator p1;
 584     while (input >> temp)
 585     {
 586         if (temp == "#b1" || temp == "#e1")
 587         {
 588             if (temp == "#b1")
 589             {
 590                 level = 1;
 591             }
 592             else
 593             {
 594                 continue;
 595             }
 596         }
 597         else
 598         {
 599             if (temp == "#b2" || temp == "#e2")
 600             {
 601                 if (temp == "#b2")
 602                 {
 603                     level = 2;
 604                 }
 605                 else
 606                 {
 607                     continue;
 608                 }
 609             }
 610             else
 611             {
 612                 if (level == 1)
 613                 {
 614                     p1 = SetofDevice.insert(make_pair(temp, Device())).first;
 615                     p1->second.isTakenUp = false;
 616                     p1->second.LatestProductCategory = "";
 617                 }
 618                 else
 619                 {
 620                     int flag = 1;
 621                     map<string, Mouldstate>::iterator p2;
 622                     while (temp != "#e2")
 623                     {
 624                         if (flag == 1)
 625                         {
 626                             p2 = p1->second.MouldOnDevice.insert(make_pair(temp, Mouldstate())).first;
 627                             flag = 2;
 628                         }
 629                         else if (flag == 2)
 630                         {
 631                             p2->second.MouldCode = temp;
 632                             flag = 3;
 633                         }
 634                         else
 635                         {
 636                             if (temp == "T")
 637                             {
 638                                 p2->second.isOn = true;
 639                             }
 640                             else
 641                             {
 642                                 p2->second.isOn = false;
 643                             }
 644                             flag = 1;
 645                         }
 646                         input >> temp;
 647                     }
 648                 }
 649             }
 650         }
 651     }
 652 }
 653 void ProductionCycleComputing::initComponent()
 654 {
 655     ifstream input("Component.txt");
 656     string temp;
 657     int level;
 658     int flag;
 659     map<string, Component>::iterator p1;
 660     while (input >> temp)
 661     {
 662         if (temp == "#b1" || temp == "#e1")
 663         {
 664             if (temp == "#b1")
 665             {
 666                 level = 1;
 667             }
 668             else
 669             {
 670                 continue;
 671             }
 672         }
 673         else
 674         {
 675             if (temp == "#b2" || temp == "#e2")
 676             {
 677                 if (temp == "#b2")
 678                 {
 679                     level = 2;
 680                     flag = 1;
 681                 }
 682                 else
 683                 {
 684                     continue;
 685                 }
 686             }
 687             else
 688             {
 689                 if (temp == "#b3" || temp == "#e3")
 690                 {
 691                     if (temp == "#b3")
 692                     {
 693                         level = 3;
 694                     }
 695                     else
 696                         continue;
 697                 }
 698                 else
 699                 {
 700                     if (level == 1)
 701                     {
 702                         p1 = SetofComponent.insert(make_pair(temp, Component())).first;
 703                     }
 704                     else
 705                     {
 706                         if (level == 2)
 707                         {
 708                             if (flag == 1)
 709                             {
 710                                 p1->second.ComponentCorrespondingMouldClass = temp;
 711                                 flag = 2;
 712                             }
 713                             else
 714                             {
 715                                 p1->second.MouldSerialNumber = temp;
 716                             }
 717                         }
 718                         else
 719                         {
 720                             while (temp != "#e3")
 721                             {
 722                                 p1->second.MouldBelongTo.insert(temp);
 723                                 input >> temp;
 724                             }
 725                         }
 726                     }
 727                 }
 728             }
 729         }
 730     }
 731 }
 732 void ProductionCycleComputing::initMould()
 733 {
 734     ifstream input("Mould.txt");
 735     string temp;
 736     int level;
 737     int flag;
 738     map<string, mould, comparatorless>::iterator p1;
 739     while (input >> temp)
 740     {
 741         if (temp == "#b1" || temp == "#e1")
 742         {
 743             if (temp == "#b1")
 744             {
 745                 level = 1;
 746             }
 747             else
 748             {
 749                 continue;
 750             }
 751         }
 752         else
 753         {
 754             if (temp == "#b2" || temp == "#e2")
 755             {
 756                 if (temp == "#b2")
 757                 {
 758                     level = 2;
 759                     flag = 1;
 760                 }
 761                 else
 762                 {
 763                     continue;
 764                 }
 765             }
 766             else
 767             {
 768                 if (temp == "#b3" || temp == "#e3")
 769                 {
 770                     if (temp == "#b3")
 771                     {
 772                         level = 3;
 773                         flag = 1;
 774                     }
 775                     else
 776                         continue;
 777                 }
 778                 else
 779                 {
 780                     if (level == 1)
 781                     {
 782                         p1 = SetofMould.insert(make_pair(temp, mould())).first;
 783                     }
 784                     else
 785                     {
 786                         if (level == 2)
 787                         {
 788                             if (flag == 1)
 789                             {
 790                                 p1->second.DeviceBelongTo = temp;
 791                                 flag = 2;
 792                             }
 793                             else
 794                             {
 795                                 if (temp == "T")
 796                                     p1->second.MouldState = true;
 797                                 else
 798                                     p1->second.MouldState = false;
 799                                 p1->second.ProductIsProducing = "";
 800                             }
 801                         }
 802                         else
 803                         {
 804                             map<string, bool>::iterator p2;
 805                             while (temp != "#e3")
 806                             {
 807                                 if (flag == 1)
 808                                 {
 809                                     p2 = p1->second.AsPartComponent.insert(make_pair(temp, false)).first;
 810                                     flag = 2;
 811                                 }
 812                                 else
 813                                 {
 814                                     if (temp == "T")
 815                                         p2->second = true;
 816                                     else
 817                                         p2->second = false;
 818                                     flag = 1;
 819                                 }
 820                                 input >> temp;
 821                             }
 822                         }
 823                     }
 824                 }
 825             }
 826         }
 827     }
 828 }
 829
 830 void ProductionCycleComputing::initInsert()
 831 {
 832     ifstream input("Insert.txt");
 833     vector<int> templist;
 834     string temp;
 835     while (true)
 836     {
 837         input >> temp;
 838         if (temp == "#b")
 839         {
 840             continue;
 841         }
 842
 843         if (temp == "#e")
 844             break;
 845
 846         templist.push_back(stoi(temp));
 847     }
 848
 849     int rowinlevel;
 850     map<string, vector<ProductInsertion>>::iterator p1;
 851     while (input >> temp)
 852     {
 853         if (temp == "#b1" || temp == "#e1")
 854         {
 855             if (temp == "#b1")
 856             {
 857                 rowinlevel = 1;
 858             }
 859             else
 860             {
 861                 continue;
 862             }
 863         }
 864         else
 865         {
 866             if (rowinlevel == 1)
 867             {
 868                 p1 = Insert.insert(make_pair(temp, vector<ProductInsertion>())).first;
 869                 ++rowinlevel;
 870             }
 871             else
 872             {
 873                 p1->second.push_back(ProductInsertion(templist[rowinlevel-2]));
 874                 while (temp != "#e")
 875                 {
 876                     if (temp == "#b")
 877                     {
 878                         input >> temp;
 879                         continue;
 880                     }
 881                     p1->second.back().ProductToInsert.insert(temp);
 882                     input >> temp;
 883                 }
 884                 ++rowinlevel;
 885             }
 886         }
 887     }
 888 }
 889
 890 ProductionCycleComputing::ProductionCycleComputing()
 891 {
 892     initInsert();
 893     initMould();
 894     initComponent();
 895     initDevice();
 896     initStateQuery();
 897     initRepairTimeOfDevice();
 898
 899     //求各类设备上生产各类产品的时间, 各类产品在各类设备上的生产准备时间
 900     //各类产品在各类设备上生产完毕后的自然时效时间
 901     //各类设备小修大修时间及时间间隔的最大公约数更新GCDTime
 902     set<int> temp;
 903     for (map<string, map<string, ProductionTimeAndPriority>>::iterator p = Device::DeviceCategorySet.begin(); p != Device::DeviceCategorySet.end(); ++p)
 904     {
 905         for (map<string, ProductionTimeAndPriority>::iterator q = p->second.begin(); q != p->second.end(); ++q)
 906         {
 907             for (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
 915     for (map<string, vector<DeviceCategoryAndNAT>>::iterator p = Product::ProductCategoryWorkingProcedure.begin(); p != Product::ProductCategoryWorkingProcedure.end(); ++p)
 916     {
 917         for (vector<DeviceCategoryAndNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q)
 918         {
 919             temp.insert(q->NAT);
 920         }
 921     }
 922
 923     for (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
 931     set<int>::iterator p = temp.begin();
 932     set<int>::iterator q = p;
 933     ++q;
 934     if (q != temp.end())
 935     {
 936         GCDTime = GCDComputing(*p, *q);
 937     }
 938     else
 939     {
 940         GCDTime = *p;
 941     }
 942     for (set<int>::iterator s = q; s != temp.end(); ++s)
 943     {
 944         if (s != q)
 945         {
 946             GCDTime = GCDComputing(GCDTime, *s);
 947         }
 948     }
 949
 950 }
 951
 952 int ProductionCycleComputing::GCDComputing(int n, int m)
 953 {
 954     while (m != 0)
 955     {
 956         int temp = m;
 957         m = n % m;
 958         n = temp;
 959     }
 960
 961     return n;
 962 }
 963
 964 void ProductionCycleComputing::GCDTimeUpdate()
 965 {
 966     {
 967         map<string, RepairTime>::iterator p = RepairTimeOfDevice.begin();
 968         for (map<string, DeviceState>::iterator q = StateQuery.begin(); q != StateQuery.end(); )    //用各设备的剩余调整时间和未调整的时间更新最大公约数时间
 969         {
 970             if (q->first.substr(0, q->first.find_first_of(‘+‘)) == p->first)
 971             {
 972                 if (q->second.flag == false)
 973                 {
 974                     if ((q->second.sign == 0 && p->second.DeltaT1 != q->second.T2) || (q->second.sign == 1 && p->second.DeltaT2 != q->second.T2))
 975                     {
 976                         if (q->second.sign == 0)
 977                             GCDTime = GCDComputing(GCDTime, p->second.DeltaT1- q->second.T2);
 978                         else
 979                             GCDTime = GCDComputing(GCDTime, p->second.DeltaT2 - q->second.T2);
 980                     }
 981                 }
 982                 else
 983                 {
 984                     if (q->second.sign == 0)
 985                     {
 986                         GCDTime = GCDComputing(GCDTime, p->second.T1 - q->second.T1);
 987                     }
 988                     else
 989                     {
 990                         GCDTime = GCDComputing(GCDTime, p->second.T2 - q->second.T1);
 991                     }
 992                 }
 993                 ++q;
 994             }
 995             else
 996             {
 997                 ++p;
 998             }
 999         }
1000     }
1001
1002     {
1003         for (map<string, Device, comparatorless>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p)   //用各设备剩余自然时效时间更新最大公约数
1004         {
1005             if (p->second.WaitingQueue.isEmpty() == false)
1006             {
1007                 for (Priority_Queue::iterator q = p->second.WaitingQueue.begin(); q != p->second.WaitingQueue.end(); ++q)
1008                 {
1009                     if (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             }
1013
1014             if (p->second.ProductisProducing.empty() == false)   //用设备正在生产的产品的剩余生产时间更新最大公约数
1015             {
1016                 int t = 0;
1017                 const string &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;
1020                 if (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 }
1028
1029 bool ProductionCycleComputing::NewProductInsert(int &t, const bool &TF)
1030 {
1031     if (Insert.empty() == false)
1032     {
1033         vector<ProductInsertion>::size_type start = SearchPoint;
1034         for ( ; start != Insert.begin()->second.size(); ++start)      //找出[t, GCDTime]时间区间内有产品插入的时间点集合
1035         {
1036             if (t < Insert.begin()->second[start].InsertTime)
1037                 break;
1038         }
1039
1040         vector<ProductInsertion>::size_type end;
1041         if (start == Insert.begin()->second.size())
1042         {
1043             if (Insert.begin()->second[start - 1].InsertTime == t)
1044             {
1045                 if (TF == false)  //不是第二轮循环
1046                 {
1047                     --start;
1048                     end = start;
1049                     SearchPoint = Insert.begin()->second.size();
1050                 }
1051                 else
1052                 {
1053                     return false;
1054                 }
1055             }
1056             else
1057             {
1058                 return false;
1059             }
1060         }
1061         else
1062         {
1063             vector<ProductInsertion>::size_type i = start;
1064             for ( ; i != Insert.begin()->second.size(); ++i)
1065             {
1066                 if (Insert.begin()->second[i].InsertTime >= t + GCDTime)
1067                     break;
1068             }
1069
1070             if (start == 0)
1071             {
1072                 if (i == 0)
1073                 {
1074                     return false;
1075                 }
1076                 else
1077                 {
1078                     SearchPoint = i;
1079                     start = 0;
1080                     end = i - 1;
1081                 }
1082             }
1083             else
1084             {
1085                 if (i == start)
1086                 {
1087                     if (t == Insert.begin()->second[start - 1].InsertTime)
1088                     {
1089                         if (TF == false)
1090                         {
1091                             --start;
1092                             end = start;
1093                             SearchPoint = i;
1094                         }
1095                         else
1096                         {
1097                             SearchPoint = i;  //  SearchPoint = i;可有可无
1098                             return false;
1099                         }
1100                     }
1101                     else
1102                     {
1103                         SearchPoint = i;
1104                         return false;
1105                     }
1106                 }
1107                 else
1108                 {
1109                     if (t == Insert.begin()->second[start - 1].InsertTime)
1110                     {
1111                         if (TF == false)
1112                         {
1113                             --start;
1114                         }
1115                         end = i - 1;
1116                         SearchPoint = i;
1117                     }
1118                     else
1119                     {
1120                         end = i - 1;
1121                         SearchPoint = i;
1122                     }
1123                 }
1124             }
1125         }
1126
1127         if (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关键字设备类别,内层设备编号,映射值为指向设备迭代器
1130
1131             vector<ProductInsertion>::size_type first;
1132             if (Insert.begin()->second[start].InsertTime == t)
1133             {
1134                  first = start + 1;
1135                  map<string, multiset<CodePriorityNAT>> temp;
1136                  for (map<string, vector<ProductInsertion>>::iterator q = Insert.begin(); q != Insert.end(); ++q)
1137                  {
1138                      if (q->second[start].ProductToInsert.empty() == false)
1139                      {
1140                          map<string, multiset<CodePriorityNAT>>::iterator temp1 = temp.insert(make_pair(q->first, multiset<CodePriorityNAT>())).first;
1141                          for (set<string>::iterator p = q->second[start].ProductToInsert.begin(); p != q->second[start].ProductToInsert.end(); ++p)
1142                          {
1143                              int 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             }
1150             else
1151             {
1152                  first = start;
1153             }
1154
1155             map<string, Device, comparatorless>::iterator before = SetofDevice.begin();
1156             map<string, pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator>> NoStartInsert;  //关键字设备类别,值该类别设备的迭代器范围
1157             for (map<string, vector<ProductInsertion>>::iterator p = Insert.begin(); p != Insert.end(); ++p)
1158             {
1159                 vector<ProductInsertion>::size_type q = first;
1160                 for ( ; q != end + 1; ++q)
1161                 {
1162                     if (p->second[q].ProductToInsert.empty() == false)
1163                         break;
1164                 }
1165
1166                 if (q != end + 1)
1167                 {
1168                     DynamicList.insert(make_pair(p->first, map<string, map<string, Device, comparatorless>::iterator>()));
1169
1170                     while (p->first != before->first.substr(0, before->first.find_first_of(‘+‘)))
1171                     {
1172                         ++before;
1173                     }
1174
1175                     map<string, Device, comparatorless>::iterator after = before;
1176                     while (after != SetofDevice.end() && p->first == after->first.substr(0, after->first.find_first_of(‘+‘)))
1177                     {
1178                         ++after;
1179                     }
1180
1181                     NoStartInsert.insert(make_pair(p->first, make_pair(before, after)));
1182                     before = after;
1183                 }
1184             }
1185
1186             //此处执行加入设备活动集和设备状态更新
1187             {
1188                 map<string, RepairTime>::iterator p = RepairTimeOfDevice.begin();
1189                 for (map<string, DeviceState>::iterator q = StateQuery.begin(); q != StateQuery.end(); )
1190                 {
1191                     if (q->first.substr(0, q->first.find_first_of(‘+‘)) == p->first)
1192                     {
1193                         if (q->second.flag == false)
1194                         {
1195                             if ((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                     }
1202                     else
1203                     {
1204                         ++p;
1205                     }
1206                 }
1207             }
1208
1209             map<string, map<string, Device, comparatorless>::iterator> EquipmentToBeAdded;
1210             for (map<string, Device, comparatorless>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p)
1211             {
1212                 EquipmentToBeAdded.insert(make_pair(p->first, p));
1213                 if (p->second.WaitingQueue.isEmpty() == false)
1214                 {
1215                     for (Priority_Queue::iterator q = p->second.WaitingQueue.begin(); q != p->second.WaitingQueue.end(); ++q)
1216                     {
1217                         if (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             }
1222
1223             ProductAddToProduction(EquipmentToBeAdded, t, Insert.begin()->second[first].InsertTime, t, end, DynamicList, NoStartInsert);
1224             for (vector<ProductInsertion>::size_type i = first; i <= end; ++i)
1225             {
1226                 map<string, multiset<CodePriorityNAT>> temp;
1227                 for (map<string, vector<ProductInsertion>>::iterator q = Insert.begin(); q != Insert.end(); ++q)
1228                 {
1229                     if (q->second[i].ProductToInsert.empty() == false)
1230                     {
1231                         map<string, multiset<CodePriorityNAT>>::iterator temp1 = temp.insert(make_pair(q->first, multiset<CodePriorityNAT>())).first;
1232                         for (set<string>::iterator p = q->second[i].ProductToInsert.begin(); p != q->second[i].ProductToInsert.end(); ++p)
1233                         {
1234                             int 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                 }
1239
1240                 if (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();
1246                     while (pa != DynamicList.end() && pb != EquipmentToBeAdded->end())   //通过动态表和有产品加入等待队列的设备集的交集确定可执行产品加入的设备集
1247                     {
1248                         if (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();
1252                             while (pc != pa->second.end() && pd != pb->second.end())
1253                             {
1254                                 if (pc->first == pd->first)
1255                                 {
1256                                     SD.insert(*pc);
1257                                     ++pc;
1258                                     ++pd;
1259                                 }
1260                                 else if (pc->first < pd->first)
1261                                 {
1262                                     ++pc;
1263                                 }
1264                                 else
1265                                 {
1266                                     ++pd;
1267                                 }
1268                             }
1269                             ++pa;
1270                             ++pb;
1271                         }
1272                         else if (pa->first < pb->first)
1273                         {
1274                             ++pa;
1275                         }
1276                         else
1277                         {
1278                             ++pb;
1279                         }
1280                     }
1281                     ProductAddToProduction(SD, Insert.begin()->second[i].InsertTime, Insert.begin()->second[i + 1].InsertTime, t, end, DynamicList, NoStartInsert);
1282                 }
1283                 else
1284                 {
1285                     ProductInsertHandle(temp, Insert.begin()->second[i].InsertTime, Insert.begin()->second[i].InsertTime);
1286                 }
1287             }
1288             //这里更新t和GCDTime
1289             t = Insert.begin()->second[end].InsertTime;
1290             GCDTimeUpdate();
1291         }
1292         else
1293         {
1294             map<string, multiset<CodePriorityNAT>> temp;
1295             for (map<string, vector<ProductInsertion>>::iterator q = Insert.begin(); q != Insert.end(); ++q)
1296             {
1297                 if (q->second[start].ProductToInsert.empty() == false)
1298                 {
1299                     map<string, multiset<CodePriorityNAT>>::iterator temp1 = temp.insert(make_pair(q->first, multiset<CodePriorityNAT>())).first;
1300                     for (set<string>::iterator p = q->second[start].ProductToInsert.begin(); p != q->second[start].ProductToInsert.end(); ++p)
1301                     {
1302                         int 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         }
1309         return true;
1310     }
1311 }
1312
1313 void ProductionCycleComputing::ProductAddToProduction(map<string, map<string, Device, comparatorless>::iterator> &SetofDevice, const int tstart, const int tend, const int &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 {
1315     if (SetofDevice.empty() == false)
1316     {
1317         map<string, multiset<CodePriorityNAT>> TempMap; //设备编号和temp集的映射关系
1318         Priority_Queue TempQueue;  //存放进入设备生产的候选产品的优先级队列
1319         for (map<string, map<string, Device, comparatorless>::iterator>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p)
1320         {
1321             if (!(*p->second).second.ProductisProducing.empty())
1322             {
1323                 if (tstart == t)
1324                 {
1325                     set<CodePriorityNAT>::iterator q = (*p->second).second.ProductisProducing.begin();
1326                     map<string, Product, comparatorless>::iterator m = SetofProduct.find((*q).ProductCode);
1327                     if (NoStartInsert.find((*p->second).first.substr(0, (*p->second).first.find_first_of(‘+‘))) != NoStartInsert.end())
1328                     {
1329                         (*m).second.time += tend - tstart;
1330                     }
1331                     else
1332                     {
1333                         (*m).second.time += Insert.begin()->second[end].InsertTime - t;
1334                     }
1335                 }
1336             }
1337             else
1338             {
1339                 if (t == tstart)
1340                 {
1341                     if ((*p->second).second.isTakenUp == true)
1342                     {
1343                         if (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                         }
1347                         else
1348                         {
1349                             StateQuery[(*p->second).first].T1 += Insert.begin()->second[end].InsertTime - t;
1350                         }
1351                     }
1352                 }
1353
1354                 if (!(*p->second).second.WaitingQueue.isEmpty())
1355                 {
1356                     if ((*p->second).second.isTakenUp == false)
1357                     {
1358                         multiset<CodePriorityNAT> temp;
1359                         CodePriorityNAT temp1;
1360                         bool TF = false;
1361                         while ((*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(‘+‘)));
1366                             if (temp1.NaturalAgingTime != -1 && (*t).second[(*q).second.index - 1].NAT != temp1.NaturalAgingTime)
1367                             {
1368                                 temp.insert(temp1);
1369                             }
1370                             else
1371                             {
1372                                 TF = true;
1373                                 break;
1374                             }
1375                         }
1376
1377                         if ((*p->second).second.WaitingQueue.isEmpty() && TF == false)
1378                         {
1379                             if (tstart == t)
1380                             {
1381                                 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator temp2;
1382                                 if ((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                             }
1387                             for (auto t = temp.begin(); t != temp.end(); t = temp.erase(t))
1388                             {
1389                                 (*p->second).second.WaitingQueue.Insert(*t);
1390                             }
1391                         }
1392                         else
1393                         {
1394                             TempQueue.Insert(temp1);
1395                             TempMap.insert(make_pair(p->second->first, temp));
1396
1397                         }
1398                         continue;
1399                     }
1400                 }
1401                 else
1402                 {
1403                     if (tstart == t)
1404                     {
1405                         if ((*p->second).second.isTakenUp == false)
1406                         {
1407                             if ((*p->second).second.ProductisProducing.empty() == true)
1408                             {
1409                                 map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator temp2;
1410                                 if ((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         }
1420         while (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(‘+‘)));
1427
1428             if (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;
1435                 if (tstart == t)
1436                 {
1437                     if (NoStartInsert.find(p->second->first.substr(0, p->second->first.find_first_of(‘+‘))) != NoStartInsert.end())
1438                     {
1439                         q->second.time = tend - tstart;
1440                     }
1441                     else
1442                     {
1443                         q->second.time = Insert.begin()->second[end].InsertTime - t;
1444                     }
1445                 }
1446                 else
1447                 {
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);
1452                 if (temp1->second.empty() == false)
1453                 {
1454                     for (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             }
1459             else
1460             {
1461                 map<string, mould, comparatorless>::iterator v = SetofMould.find(m->second.MouldCode);
1462                 bool ismeet = true;
1463                 for (map<string, bool>::iterator s = v->second.AsPartComponent.begin(); s != v->second.AsPartComponent.end(); ++s)
1464                 {
1465                     if (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);
1469                         if (w->second.MouldState == true && w->second.ProductIsProducing != "")
1470                         {
1471                             ismeet = false;
1472                             break;
1473                         }
1474                     }
1475                 }
1476                 if (ismeet == true)
1477                 {
1478                     v->second.MouldState = true;
1479                     v->second.ProductIsProducing = temp.ProductCode;
1480                     m->second.isOn = true;
1481                     for (map<string, bool>::iterator s = v->second.AsPartComponent.begin(); s != v->second.AsPartComponent.end(); ++s)
1482                     {
1483                         if (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);
1487                             if (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                             }
1493
1494                             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;
1504
1505                     if (tstart == t)
1506                     {
1507                         if (NoStartInsert.find(p->first.substr(0, p->first.find_first_of(‘+‘))) != NoStartInsert.end())
1508                         {
1509                             q->second.time = tend - tstart;
1510                         }
1511                         else
1512                         {
1513                             q->second.time = Insert.begin()->second[end].InsertTime - t;
1514                         }
1515                     }
1516                     else
1517                     {
1518                         DynamicList[p->first.substr(0, p->first.find_first_of(‘+‘))].erase(p->first);
1519                     }
1520
1521                     map<string, multiset<CodePriorityNAT>>::iterator temp1;
1522
1523                     temp1 = TempMap.find(q->second.DeviceID);
1524                     if (temp1->second.empty() == false)
1525                     {
1526                         for (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                 }
1531                 else
1532                 {
1533                     TempMap[q->second.DeviceID].insert(temp);
1534                     if (!p->second->second.WaitingQueue.isEmpty())
1535                     {
1536                         CodePriorityNAT temp1;
1537                         bool TF = false;
1538                         while ((*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(‘+‘)));
1543                             if (temp1.NaturalAgingTime != -1 && (*v).second[(*s).second.index - 1].NAT != temp1.NaturalAgingTime)
1544                             {
1545                                 TempMap[q->second.DeviceID].insert(temp1);
1546                             }
1547                             else
1548                             {
1549                                 TF = true;
1550                                 break;
1551                             }
1552                         }
1553
1554                         if (!(*p).second->second.WaitingQueue.isEmpty() || TF)
1555                         {
1556                             TempQueue.Insert(temp1);
1557                             continue;
1558                         }
1559                     }
1560
1561                     if (tstart == t)
1562                     {
1563                         map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator temp2;
1564                         if ((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                     }
1569
1570                     for (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         }
1578         for (map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator p = DynamicList.begin(); p != DynamicList.end(); )
1579         {
1580             if (p->second.empty() == true)
1581                 p = DynamicList.erase(p);
1582             else
1583                 ++p;
1584         }
1585     }
1586
1587     if (t != tstart)
1588     {
1589         map<string, DeviceState>::iterator before = StateQuery.begin();
1590         for (map<string, pair<map<string, Device, comparatorless>::iterator, map<string, Device, comparatorless>::iterator>>::iterator p = NoStartInsert.begin(); p != NoStartInsert.end(); ++p)
1591         {
1592             while (p->first != before->first.substr(0, before->first.find_first_of(‘+‘)))
1593             {
1594                 ++before;
1595             }
1596
1597             map<string, DeviceState>::iterator after = before;
1598             while (after != StateQuery.end() && p->first == after->first.substr(0, after->first.find_first_of(‘+‘)))
1599             {
1600                 ++after;
1601             }
1602
1603             map<string, DeviceState>::iterator q = before;
1604             for (map<string, Device, comparatorless>::iterator m = p->second.first; m != p->second.second; ++m, ++q)
1605             {
1606                 if (q->second.flag == true)
1607                 {
1608                     q->second.T1 += tend - tstart;
1609                 }
1610
1611                 if (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 }
1621
1622 shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>> ProductionCycleComputing::ProductInsertHandle(map<string, multiset<CodePriorityNAT>> &ProductInsert, const int &tstart, const int &t)
1623 {
1624     if (ProductInsert.empty() == false)
1625     {
1626         shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>> insertinfo;
1627         if (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         }
1631
1632         for (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);
1635             struct IteratorAndScheme
1636             {
1637                 int 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;
1642             for (map<string, Device, comparatorless>::iterator q = tempIterator.first; q != tempIterator.second; ++q)
1643             {
1644                 if (StateQuery[q->first].flag == true)
1645                 {
1646                     if (StateQuery[q->first].sign == 0)
1647                     {
1648                         if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T1)
1649                         {
1650                             QueueIndex.push_back(IteratorAndScheme(q, 1));
1651                         }
1652                         else
1653                         {
1654                             QueueIndex.push_back(IteratorAndScheme(q, 2));
1655                         }
1656                     }
1657                     else
1658                     {
1659                         if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T2)
1660                         {
1661                             QueueIndex.push_back(IteratorAndScheme(q, 1));
1662                         }
1663                         else
1664                         {
1665                             QueueIndex.push_back(IteratorAndScheme(q, 2));
1666                         }
1667                     }
1668                 }
1669                 else
1670                 {
1671                     QueueIndex.push_back(IteratorAndScheme(q, 1));
1672                 }
1673             }
1674
1675             map<int, IncompleteOptimization> CurrentOptimization;  //关键字队列编号
1676             struct PriorityInsertionTime
1677             {
1678                 vector<int> ProductTime;  //存放给定产品按优先级插入各队列后的等待时间
1679                 vector<IteratorAndScheme>::size_type index; //产品所在的等待时间最小的队列序号
1680                 int tmin; //最小等待时间
1681             };
1682             map<string, PriorityInsertionTime> ProductQueueWaitingTime;//关键字产品编号
1683             int 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();
1689
1690             map<string, map<string, map<string, Device, comparatorless>::iterator>>::iterator currentinsert;
1691             while (p->second.empty() == false)
1692             {
1693                 if (p->second.size() == count)
1694                 {
1695                     vector<IteratorAndScheme>::size_type indextemp;
1696                     Priority_Queue::iterator iteratortemp;
1697                     string codetemp;
1698                     int tmin;
1699                     multiset<CodePriorityNAT>::iterator qmin;
1700                     for (multiset<CodePriorityNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q)
1701                     {
1702                         int 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;
1706                         for (vector<IteratorAndScheme>::size_type i = 0; i < QueueIndex.size(); ++i)
1707                         {
1708                             Priority_Queue::iterator temp = QueueIndex[i].DeviceIterator->second.WaitingQueue.Insert(*q).second;
1709                             int waittime = WaitingTimeComputing(temp, QueueIndex[i].DeviceIterator, QueueIndex[i].InsertScheme);
1710                             if (i == 0)
1711                             {
1712                                 Tmint = waittime;
1713                                 Indext = i;
1714                                 Iteratort = temp;
1715                                 ++Iteratort;
1716                             }
1717                             else
1718                             {
1719                                 if (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;
1732                         if (q == p->second.begin())
1733                         {
1734                             qmin = q;
1735                             tmin = Tmint;
1736                             codetemp = q->ProductCode;
1737                             indextemp = Indext;
1738                             iteratortemp = Iteratort;
1739                         }
1740                         else
1741                         {
1742                             if (Tmint < tmin)
1743                             {
1744                                 qmin = q;
1745                                 tmin = Tmint;
1746                                 codetemp = q->ProductCode;
1747                                 indextemp = Indext;
1748                                 iteratortemp = Iteratort;
1749                             }
1750                         }
1751                     }
1752
1753                     tmax = tmin;
1754                     it = QueueIndex[indextemp].DeviceIterator->second.WaitingQueue.insert(iteratortemp, *qmin);
1755                     cout << t << "时刻产品" << qmin->ProductCode << "加入设备" << QueueIndex[indextemp].DeviceIterator->first << "等待队列等待生产" << endl;
1756                     if (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                 }
1769                 else
1770                 {
1771                     if (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;
1778                         int tmaxop;
1779                         for (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;
1784                             int tmaxt;
1785
1786                             iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second;
1787                             if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1)
1788                             {
1789                                 int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme);
1790                                 ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp;
1791
1792                                 if (ProductQueueWaitingTime[q->ProductCode].index == index)
1793                                 {
1794                                     int mintime;
1795                                     vector<int>::size_type minindex;
1796                                     for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i)
1797                                     {
1798                                         if (i == 0)
1799                                         {
1800                                             mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i];
1801                                             minindex = i;
1802                                         }
1803                                         else
1804                                         {
1805                                             if (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);
1817
1818                             Priority_Queue::iterator tempinsert = QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.Insert(*q).second;
1819                             if (CurrentOptimization.find(ProductQueueWaitingTime[q->ProductCode].index) == CurrentOptimization.end())
1820                             {
1821                                 Priority_Queue::iterator after = tempinsert;
1822                                 ++after;
1823                                 if (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                                 }
1830                                 else
1831                                 {
1832                                     tmaxt = tmax;
1833                                     iteratort = after;
1834                                     iteratormaxt = itmax;
1835                                     indexmaxt = indexmax;
1836                                 }
1837                             }
1838                             else
1839                             {
1840                                 Priority_Queue::iterator after = tempinsert;
1841                                 ++after;
1842                                 Priority_Queue::iterator indexit = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].It;
1843                                 if (PositionRelation(tempinsert, indexit, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue) == 1)
1844                                 {
1845                                     if (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                                     }
1852                                     else
1853                                     {
1854                                         tmaxt = tmax;
1855                                         iteratort = after;
1856                                         iteratormaxt = itmax;
1857                                         indexmaxt = indexmax;
1858                                     }
1859                                 }
1860                                 else
1861                                 {
1862                                     int DeltaTime = TimeIncrementComputing(indexit, tempinsert, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].InsertScheme);
1863                                     int WaitTime = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].WaitTime;
1864                                     if (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                                     }
1872                                     else
1873                                     {
1874                                         tmaxt = tmax;
1875                                         iteratort = after;
1876                                         iteratormaxt = itmax;
1877                                         indexmaxt = indexmax;
1878                                     }
1879                                 }
1880                             }
1881
1882                             if (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                             }
1891                             else
1892                             {
1893                                 if (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                         }
1905
1906                         iteratorop = QueueIndex[indexop].DeviceIterator->second.WaitingQueue.insert(iteratorop, *qop);
1907                         cout << t << "时刻产品" << qop->ProductCode << "加入设备" << QueueIndex[indexop].DeviceIterator->first << "等待队列等待生产" << endl;
1908                         if (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)));
1913                         if (CurrentOptimization.find(indexop) != CurrentOptimization.end())
1914                         {
1915                             if (PositionRelation(iteratorop, CurrentOptimization[indexop].It, QueueIndex[indexop].DeviceIterator->second.WaitingQueue) == 0)
1916                             {
1917                                 int TimeCorrrect = TimeIncrementComputing(CurrentOptimization[indexop].It, iteratorop, QueueIndex[indexop].DeviceIterator, QueueIndex[indexop].InsertScheme);
1918                                 CurrentOptimization[indexop].WaitTime += TimeCorrrect;
1919                             }
1920                             else
1921                             {
1922                                 CurrentOptimization[indexop].ProductCode = qop->ProductCode;
1923                                 CurrentOptimization[indexop].It = iteratorop;
1924                                 CurrentOptimization[indexop].WaitTime = ProductQueueWaitingTime[qop->ProductCode].tmin;
1925                             }
1926                         }
1927                         else
1928                         {
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;
1936
1937                         ProductQueueWaitingTime.erase(qop->ProductCode);
1938                         p->second.erase(qop);
1939                     }
1940                     else
1941                     {
1942                         multiset<CodePriorityNAT>::iterator q = p->second.begin();
1943                             Priority_Queue::iterator iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second;
1944                         if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1)
1945                         {
1946                             int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme);
1947                             ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp;
1948
1949                             if (ProductQueueWaitingTime[q->ProductCode].index == index)
1950                             {
1951                                 int mintime;
1952                                 vector<int>::size_type minindex;
1953                                 for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i)
1954                                 {
1955                                     if (i == 0)
1956                                     {
1957                                         mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i];
1958                                         minindex = i;
1959                                     }
1960                                     else
1961                                     {
1962                                         if (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;
1976                         if (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         }
1986         return insertinfo;
1987     }
1988     return shared_ptr<map<string, map<string, map<string, Device, comparatorless>::iterator>>>(nullptr);
1989 }
1990
1991 int ProductionCycleComputing::ProductionCycleCalculation()
1992 {
1993     clock_t starttime = clock();
1994     int t = 0;
1995     while (true)
1996     {
1997         //此处检测并处理新产品插入
1998         bool TF = false; //第一轮循环
1999         while (NewProductInsert(t, TF) != false)
2000         {
2001             if (TF == false)
2002                 TF = true;
2003         }
2004
2005         map<string, multiset<CodePriorityNAT>> TempMap; //设备编号和temp集的映射关系
2006         Priority_Queue TempQueue;  //存放进入设备生产的候选产品的优先级队列
2007         for (map<string, Device, comparatorless>::iterator p = SetofDevice.begin(); p != SetofDevice.end(); ++p)
2008         {
2009             if (!(*p).second.ProductisProducing.empty())
2010             {
2011                 set<CodePriorityNAT>::iterator q=(*p).second.ProductisProducing.begin();
2012                 map<string, Product, comparatorless>::iterator m = SetofProduct.find((*q).ProductCode);
2013                 (*m).second.time += GCDTime;
2014             }
2015             else
2016             {
2017                 if (!(*p).second.WaitingQueue.isEmpty())
2018                 {
2019                     if ((*p).second.isTakenUp == false)
2020                     {
2021                         multiset<CodePriorityNAT> temp;
2022                         CodePriorityNAT temp1;
2023                         TF = false;
2024                         while ((*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(‘+‘)));
2029                             if (temp1.NaturalAgingTime != -1 && (*t).second[(*q).second.index-1].NAT != temp1.NaturalAgingTime)
2030                             {
2031                                 temp1.NaturalAgingTime += GCDTime;
2032                                 temp.insert(temp1);
2033                             }
2034                             else
2035                             {
2036                                 TF = true;
2037                                 break;
2038                             }
2039                         }
2040
2041                         if ((*p).second.WaitingQueue.isEmpty() && TF == false)
2042                         {
2043                             for (auto t = temp.begin(); t != temp.end(); t=temp.erase(t))
2044                             {
2045                                 (*p).second.WaitingQueue.Insert(*t);
2046                             }
2047                         }
2048                         else
2049                         {
2050                             TempQueue.Insert(temp1);
2051                             TempMap.insert(make_pair(p->first, temp));
2052
2053                         }
2054                         continue;
2055                     }
2056                 }
2057             }
2058             if ((*p).second.WaitingQueue.isEmpty() == false)
2059             {
2060                 for (Priority_Queue::iterator q = p->second.WaitingQueue.begin(); q != p->second.WaitingQueue.end(); ++q)
2061                 {
2062                     if (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             }
2066
2067         }
2068         while (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(‘+‘)));
2075
2076             if (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;
2084
2085                 if (p->second.WaitingQueue.isEmpty() == false)
2086                 {
2087                     for (Priority_Queue::iterator s = p->second.WaitingQueue.begin(); s != p->second.WaitingQueue.end(); ++s)
2088                     {
2089                         if (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                 }
2093
2094                 temp1 = TempMap.find(q->second.DeviceID);
2095                 if (temp1->second.empty() == false)
2096                 {
2097                     for (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             }
2102             else
2103             {
2104                 map<string, mould, comparatorless>::iterator t1 = SetofMould.find(m->second.MouldCode);
2105                 bool ismeet = true;
2106                 for (map<string, bool>::iterator s = t1->second.AsPartComponent.begin(); s != t1->second.AsPartComponent.end(); ++s)
2107                 {
2108                     if (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);
2112                         if (w->second.MouldState == true && w->second.ProductIsProducing != "")
2113                         {
2114                             ismeet = false;
2115                             break;
2116                         }
2117                     }
2118                 }
2119                 if (ismeet == true)
2120                 {
2121                     t1->second.MouldState = true;
2122                     t1->second.ProductIsProducing = temp.ProductCode;
2123                     m->second.isOn = true;
2124                     for (map<string, bool>::iterator s = t1->second.AsPartComponent.begin(); s != t1->second.AsPartComponent.end(); ++s)
2125                     {
2126                         if (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);
2130                             if (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                             }
2136
2137                             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;
2148
2149                     if (p->second.WaitingQueue.isEmpty() == false)
2150                     {
2151                         for (Priority_Queue::iterator s = p->second.WaitingQueue.begin(); s != p->second.WaitingQueue.end(); ++s)
2152                         {
2153                             if (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                     }
2157
2158                     temp1 = TempMap.find(q->second.DeviceID);
2159                     if (temp1->second.empty() == false)
2160                     {
2161                         for (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                 }
2166                 else
2167                 {
2168                     TempMap[q->second.DeviceID].insert(temp);
2169                     if (!p->second.WaitingQueue.isEmpty())
2170                     {
2171                         CodePriorityNAT temp1;
2172                         bool TF = false;
2173                         while ((*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(‘+‘)));
2178                             if (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                             }
2183                             else
2184                             {
2185                                 TF = true;
2186                                 break;
2187                             }
2188                         }
2189
2190                         if (!(*p).second.WaitingQueue.isEmpty() || TF)
2191                         {
2192                             TempQueue.Insert(temp1);
2193                             continue;
2194                         }
2195                     }
2196
2197                     for (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         }
2205
2206         map<string, multiset<CodePriorityNAT>> ProductInsert;  //设备类别和待加入所有该类别设备的等待队列的所有产品的集合的映射关系
2207         for (map<string, Product, comparatorless>::iterator p = SetofProduct.begin(); p != SetofProduct.end(); )
2208         {
2209             map<string, Device, comparatorless>::iterator q = SetofDevice.find(p->second.DeviceID);
2210             if (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);
2215
2216                 /*cout << "[" << t << "," << t + GCDTime << ")时间段产品" << p->first << "于设备" << q->first << "上生产加工";*/
2217                 int temp;
2218                 if (q->second.LatestProductCategory != p->first.substr(0, p->first.find_first_of(‘+‘)))
2219                 {
2220                     temp = m->second.PrepareTime;
2221                 }
2222                 else
2223                 {
2224                     temp = 0;
2225                 }
2226
2227                 if (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 = "";     ////
2233
2234                     cout << t + GCDTime << "时刻产品" << p->first << "在设备" << q->first << "上生产完毕";
2235                     if (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 << ",此时该产品已走完整个加工流程";
2240                         if (v->second.back().NAT == 0)  //p所指产品生产完成,生产完成时间为t+GCDTime
2241                         {
2242                             cout << "且走完加工流程后无自然时效时间,该产品生产完毕" << endl;
2243                             p = SetofProduct.erase(p);
2244                         }
2245                         else
2246                         {
2247                             cout << "且走完加工流程后该产品仍有自然时效时间,该产品开始等待自然时效时间流逝完毕" << endl;
2248                             p->second.flag = 0;     //p所指产品在最后一台设备上生产完毕后有自然时效时间,把产品状态更新为特殊值表示产品处于自然时效阶段
2249                             p->second.time = 0;
2250                             p->second.DeviceID = "";
2251                             ++p;
2252                         }
2253                         continue;
2254                     }
2255                     else
2256                     {
2257                         string 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;
2261                         if (c == ProductInsert.end())
2262                         {
2263                             multiset<CodePriorityNAT> temp1{ temp };
2264                             ProductInsert.insert(make_pair(Cat, temp1));
2265                         }
2266                         else
2267                         {
2268                             c->second.insert(temp);
2269                         }
2270
2271                         p->second.flag = 0;
2272                         p->second.time = 0;
2273                         ++(p->second.index);
2274                         q->second.ProductisProducing.clear();    //该产品在当前设备上生产完成时间为t+GCDTime
2275                     }
2276                     q->second.LatestProductCategory = p->first.substr(0, p->first.find_first_of(‘+‘));
2277                     ++p;
2278                 }
2279                 else
2280                 {
2281                     /*cout << "," << t + GCDTime << "时刻该产品在设备" << q->first << "上尚未生产完毕,继续生产"<<endl*/;
2282                     ++p;
2283                 }
2284
2285             }
2286             else
2287             {
2288                 if (p->second.DeviceID == "")
2289                 {
2290                     /*cout << "[" << t << "," << t + GCDTime << ")时间段产品" << p->first << "之前已经走完整个加工流程,正在等待自然时效时间流逝完毕.";*/
2291                     p->second.time += GCDTime;
2292                     if (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);
2296                         continue;
2297                     }
2298                     else
2299                     {
2300                         /*cout << t + GCDTime << "时刻该产品自然时效时间尚未流逝完毕,继续等待" << endl*/;
2301                     }
2302                 }
2303                 else
2304                 {
2305                     /*cout << "[" << t << "," << t + GCDTime << ")时间段产品" << p->first << "在设备" << q->first << "的生产队列中等待生产" << endl*/;
2306                 }
2307                 ++p;
2308             }
2309         }
2310
2311         if (SetofProduct.empty())
2312         {
2313             //所有产品生产完成,时间为t+GCDTime,结束
2314             cout << "所有产品生产完成" << endl;
2315             clock_t endtime = clock();
2316             cout << "生产时间计算耗费时间为" << (endtime - starttime) << "毫秒" << endl;
2317             return t+GCDTime;
2318         }
2319
2320         //此处利用ProductInsert执行多重优化插入,先判映射表是否为空
2321         if (ProductInsert.empty() == false)
2322         {
2323             for (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);
2326                 struct IteratorAndScheme
2327                 {
2328                     int 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;
2333                 for (map<string, Device, comparatorless>::iterator q = tempIterator.first; q != tempIterator.second; ++q)
2334                 {
2335                     if (StateQuery[q->first].flag == true)
2336                     {
2337                         if (StateQuery[q->first].sign == 0)
2338                         {
2339                             if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T1)
2340                             {
2341                                 QueueIndex.push_back(IteratorAndScheme(q, 1));
2342                             }
2343                             else
2344                             {
2345                                 QueueIndex.push_back(IteratorAndScheme(q, 2));
2346                             }
2347                         }
2348                         else
2349                         {
2350                             if (StateQuery[q->first].T1 + GCDTime == RepairTimeOfDevice[p->first].T2)
2351                             {
2352                                 QueueIndex.push_back(IteratorAndScheme(q, 1));
2353                             }
2354                             else
2355                             {
2356                                 QueueIndex.push_back(IteratorAndScheme(q, 2));
2357                             }
2358                         }
2359                     }
2360                     else
2361                     {
2362                         QueueIndex.push_back(IteratorAndScheme(q, 1));
2363                     }
2364                 }
2365
2366                 map<int, IncompleteOptimization> CurrentOptimization;  //关键字队列编号
2367                 struct PriorityInsertionTime
2368                 {
2369                     vector<int> ProductTime;  //存放给定产品按优先级插入各队列后的等待时间
2370                     vector<IteratorAndScheme>::size_type index; //产品所在的等待时间最小的队列序号
2371                     int tmin; //最小等待时间
2372                 };
2373                 map<string, PriorityInsertionTime> ProductQueueWaitingTime;//关键字产品编号
2374                 int 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();
2380                 while (p->second.empty() == false)
2381                 {
2382                     if (p->second.size() == count)
2383                     {
2384                         vector<IteratorAndScheme>::size_type indextemp;
2385                         Priority_Queue::iterator iteratortemp;
2386                         string codetemp;
2387                         int tmin;
2388                         multiset<CodePriorityNAT>::iterator qmin;
2389                         for (multiset<CodePriorityNAT>::iterator q = p->second.begin(); q != p->second.end(); ++q)
2390                         {
2391                             int 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;
2395                             for (vector<IteratorAndScheme>::size_type i = 0; i < QueueIndex.size(); ++i)
2396                             {
2397                                 Priority_Queue::iterator temp = QueueIndex[i].DeviceIterator->second.WaitingQueue.Insert(*q).second;
2398                                 int waittime = WaitingTimeComputing(temp, QueueIndex[i].DeviceIterator, QueueIndex[i].InsertScheme);
2399                                 if (i == 0)
2400                                 {
2401                                     Tmint = waittime;
2402                                     Indext = i;
2403                                     Iteratort = temp;
2404                                     ++Iteratort;
2405                                 }
2406                                 else
2407                                 {
2408                                     if (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;
2421                             if (q == p->second.begin())
2422                             {
2423                                 qmin = q;
2424                                 tmin = Tmint;
2425                                 codetemp = q->ProductCode;
2426                                 indextemp = Indext;
2427                                 iteratortemp = Iteratort;
2428                             }
2429                             else
2430                             {
2431                                 if (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                     }
2453                     else
2454                     {
2455                         if (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;
2462                             int tmaxop;
2463                             for (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;
2468                                 int tmaxt;
2469
2470                                 iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second;
2471                                 if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1)
2472                                 {
2473                                     int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme);
2474                                     ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp;
2475
2476                                     if (ProductQueueWaitingTime[q->ProductCode].index == index)
2477                                     {
2478                                         int mintime;
2479                                         vector<int>::size_type minindex;
2480                                         for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i)
2481                                         {
2482                                             if (i == 0)
2483                                             {
2484                                                 mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i];
2485                                                 minindex = i;
2486                                             }
2487                                             else
2488                                             {
2489                                                 if (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);
2501
2502                                 Priority_Queue::iterator tempinsert = QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue.Insert(*q).second;
2503                                 if (CurrentOptimization.find(ProductQueueWaitingTime[q->ProductCode].index) == CurrentOptimization.end())
2504                                 {
2505                                     Priority_Queue::iterator after = tempinsert;
2506                                     ++after;
2507                                     if (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                                     }
2514                                     else
2515                                     {
2516                                         tmaxt = tmax;
2517                                         iteratort = after;
2518                                         iteratormaxt = itmax;
2519                                         indexmaxt = indexmax;
2520                                     }
2521                                 }
2522                                 else
2523                                 {
2524                                     Priority_Queue::iterator after = tempinsert;
2525                                     ++after;
2526                                     Priority_Queue::iterator indexit = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].It;
2527                                     if (PositionRelation(tempinsert, indexit, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator->second.WaitingQueue) == 1)
2528                                     {
2529                                         if (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                                         }
2536                                         else
2537                                         {
2538                                             tmaxt = tmax;
2539                                             iteratort = after;
2540                                             iteratormaxt = itmax;
2541                                             indexmaxt = indexmax;
2542                                         }
2543                                     }
2544                                     else
2545                                     {
2546                                         int DeltaTime = TimeIncrementComputing(indexit, tempinsert, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].DeviceIterator, QueueIndex[ProductQueueWaitingTime[q->ProductCode].index].InsertScheme);
2547                                         int WaitTime = CurrentOptimization[ProductQueueWaitingTime[q->ProductCode].index].WaitTime;
2548                                         if (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                                         }
2556                                         else
2557                                         {
2558                                             tmaxt = tmax;
2559                                             iteratort = after;
2560                                             iteratormaxt = itmax;
2561                                             indexmaxt = indexmax;
2562                                         }
2563                                     }
2564                                 }
2565
2566                                 if (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                                 }
2575                                 else
2576                                 {
2577                                     if (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                             }
2589
2590                             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;
2594                             if (CurrentOptimization.find(indexop) != CurrentOptimization.end())
2595                             {
2596                                 if (PositionRelation(iteratorop, CurrentOptimization[indexop].It, QueueIndex[indexop].DeviceIterator->second.WaitingQueue) == 0)
2597                                 {
2598                                     int TimeCorrrect = TimeIncrementComputing(CurrentOptimization[indexop].It, iteratorop, QueueIndex[indexop].DeviceIterator, QueueIndex[indexop].InsertScheme);
2599                                     CurrentOptimization[indexop].WaitTime += TimeCorrrect;
2600                                 }
2601                                 else
2602                                 {
2603                                     CurrentOptimization[indexop].ProductCode = qop->ProductCode;
2604                                     CurrentOptimization[indexop].It = iteratorop;
2605                                     CurrentOptimization[indexop].WaitTime = ProductQueueWaitingTime[qop->ProductCode].tmin;
2606                                 }
2607                             }
2608                             else
2609                             {
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;
2617
2618                             ProductQueueWaitingTime.erase(qop->ProductCode);
2619                             p->second.erase(qop);
2620                         }
2621                         else
2622                         {
2623                             multiset<CodePriorityNAT>::iterator q = p->second.begin();
2624                             Priority_Queue::iterator iteratort = QueueIndex[index].DeviceIterator->second.WaitingQueue.Insert(*q).second;
2625                             if (PositionRelation(iteratort, it, QueueIndex[index].DeviceIterator->second.WaitingQueue) == 1)
2626                             {
2627                                 int temp = TimeIncrementComputing(iteratort, it, QueueIndex[index].DeviceIterator, QueueIndex[index].InsertScheme);
2628                                 ProductQueueWaitingTime[q->ProductCode].ProductTime[index] += temp;
2629
2630                                 if (ProductQueueWaitingTime[q->ProductCode].index == index)
2631                                 {
2632                                     int mintime;
2633                                     vector<int>::size_type minindex;
2634                                     for (vector<int>::size_type i = 0; i != ProductQueueWaitingTime[q->ProductCode].ProductTime.size(); ++i)
2635                                     {
2636                                         if (i == 0)
2637                                         {
2638                                             mintime = ProductQueueWaitingTime[q->ProductCode].ProductTime[i];
2639                                             minindex = i;
2640                                         }
2641                                         else
2642                                         {
2643                                             if (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();
2668         for (map<string, DeviceState>::iterator q = StateQuery.begin(); q != StateQuery.end(); )
2669         {
2670             if (q->first.substr(0, q->first.find_first_of(‘+‘)) == p->first)
2671             {
2672                 if (q->second.flag == true)
2673                 {
2674                     q->second.T1 += GCDTime;
2675                     if (q->second.sign == 0)
2676                     {
2677                         if (p->second.T1 == q->second.T1)
2678                         {
2679                             q->second.T1 = 0;
2680                             q->second.flag = false;
2681                             s->second.isTakenUp = false;
2682                         }
2683                     }
2684                     else
2685                     {
2686                         if (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                 }
2694                 else
2695                 {
2696                     if (q->second.sign == 0)
2697                     {
2698                         if (q->second.T2 == p->second.DeltaT1 || (q->second.T2 += GCDTime, q->second.T2 == p->second.DeltaT1))
2699                         {
2700                             if (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                         }
2708                         else
2709                         {
2710                             if (s->second.ProductisProducing.empty() == true)
2711                                 s->second.isTakenUp = false;
2712                         }
2713                     }
2714                     else
2715                     {
2716                         if (q->second.T2 == p->second.DeltaT2 || (q->second.T2 += GCDTime, q->second.T2 == p->second.DeltaT2))
2717                         {
2718                             if (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                         }
2726                         else
2727                         {
2728                             if (s->second.ProductisProducing.empty() == true)
2729                                 s->second.isTakenUp = false;
2730                         }
2731                     }
2732                 }
2733                 ++q;
2734                 ++s;
2735             }
2736             else
2737             {
2738                 ++p;
2739             }
2740         }
2741         t += GCDTime;
2742     }
2743 }
2744
2745 int 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;
2750     int t = 0;
2751     if (InsertScheme == 1)
2752     {
2753         if (m->second.ProductisProducing.empty() == false)
2754         {
2755             const string &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;
2758             if (temp.substr(0, temp.find_first_of(‘+‘)) != m->second.LatestProductCategory)
2759                 t += temp1.PrepareTime;
2760             t -= SetofProduct.find(temp)->second.time;
2761         }
2762     }
2763     else
2764     {
2765         if (StateQuery[m->first].sign == 0)
2766             t += RepairTimeOfDevice[m->first.substr(0, m->first.find_first_of(‘+‘))].T1;
2767         else
2768             t += RepairTimeOfDevice[m->first.substr(0, m->first.find_first_of(‘+‘))].T2;
2769         t -= StateQuery[m->first].T1;
2770     }
2771
2772     if (before->ProductCode != it->ProductCode)
2773     {
2774         string 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;
2777         if (InsertScheme == 1 && m->second.ProductisProducing.empty() == false)
2778         {
2779             if (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         }
2782         else
2783         {
2784             if (temp.substr(0, temp.find_first_of(‘+‘)) != m->second.LatestProductCategory)
2785                 t += temp1.PrepareTime;
2786         }
2787
2788         if (after->ProductCode != it->ProductCode)
2789         {
2790             while (after != it)
2791             {
2792                 string 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;
2795
2796                 if (temp.substr(0, temp.find_first_of(‘+‘))!=before->ProductCode.substr(0, before->ProductCode.find_first_of(‘+‘)))
2797                     t+=temp1.PrepareTime;
2798
2799                 ++after;
2800                 ++before;
2801             }
2802         }
2803     }
2804     return t;
2805 }
2806
2807 int ProductionCycleComputing::TimeIncrementComputing(const Priority_Queue::iterator &BeCalculated, const Priority_Queue::iterator &CauseCalculating, map<string, Device, comparatorless>::iterator m, int InsertScheme)
2808 {
2809     int t = 0; //修正等待时间的时间增量
2810     string code;
2811     if (CauseCalculating == m->second.WaitingQueue.begin())
2812     {
2813         if (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         }
2818         else
2819         {
2820             code = m->second.LatestProductCategory;
2821         }
2822     }
2823     else
2824     {
2825         Priority_Queue::iterator temp = CauseCalculating;
2826         --temp;
2827         code = temp->ProductCode.substr(0, temp->ProductCode.find_first_of(‘+‘));
2828     }
2829
2830     {
2831         PrepareTimeProduceTime temp1;
2832         if (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         }
2836         else
2837         {
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;
2841         if (CauseCalculating->ProductCode.substr(0, CauseCalculating->ProductCode.find_first_of(‘+‘)) != code)
2842             t += temp1.PrepareTime;
2843     }
2844
2845     Priority_Queue::iterator temp = CauseCalculating;
2846     ++temp;
2847     if (temp != BeCalculated)
2848     {
2849         string Cat1 = temp->ProductCode.substr(0, temp->ProductCode.find_first_of(‘+‘));
2850         string Cat2 = CauseCalculating->ProductCode.substr(0, CauseCalculating->ProductCode.find_first_of(‘+‘));
2851         if ((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];
2854             if (Cat1 != code && Cat1 == Cat2)
2855             {
2856                 t -= temp1.PrepareTime;
2857             }
2858             else
2859             {
2860                 t += temp1.PrepareTime;
2861             }
2862         }
2863     }
2864     return t;
2865 }
2866
2867 int 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;
2871
2872     while (before != it2 && (after != m.end() && after != it2))
2873     {
2874         if (before != m.begin())
2875         {
2876             --before;
2877         }
2878         ++after;
2879     }
2880
2881     if (before == it2)
2882     {
2883         return 1;
2884     }
2885     else
2886     {
2887         if (after == m.end())
2888         {
2889             if (it2 == m.end())
2890                 return 0;
2891             else
2892                 return 1;
2893         }
2894         else
2895         {
2896             return 0;
2897         }
2898     }
2899 }
2900
2901 int main()
2902 {
2903     Product::initProductCategoryWorkingProcedure();
2904     Device::initDeviceCategorySet();
2905     ProductionCycleComputing CalculationExample;
2906     cout << "生产完成时间为" << CalculationExample.ProductionCycleCalculation() << endl;
2907     system("Pause");
2908     return 0;
2909 }

运行结果(输入文件内容即所给样例文件内容) :

原文地址:https://www.cnblogs.com/WSKIT/p/9160601.html

时间: 2024-10-12 08:38:32

C++编程实现对工厂产品生产流程的模拟的相关文章

面向对象编程思想-简单工厂模式

一.引言 简单工厂.工厂方法.抽象工厂模式都属于设计模式中的创建型设计模式,它们帮助我们把对象的实例化部分抽取出来,进而优化系统架构,提高系统的扩展性.本文介绍一个比较容易理解的模式-简单工厂模式. 二.简单工厂模式 定义:"工厂"?!看到这个词语,在现实生活中大家会想到是生产产品的,同样,在简单工厂模式里大家可以理解为工厂其实就是创建对象的一个类.平时我们编程的时候,当使用"new"创建对象时,此类就增加了对该对象的依赖,它们之间的耦合度就增加了,这样当业务需求变

OEM代工厂产品经理个人经历谈

创业不是一件随随便便的事情! 到2007年时,我已经在上海.广州.东莞三地的工厂打工有十来年了.正是这个时间结点,我也即将做父亲了.打了很久的工后,就开始感到疲倦,做来做去,都是给老板做,也就在这时开时思考职业,人生的道路,思考需不需要自己去做点事. 这时,有个机会,我以前的一个客户有一个做手机外壳的订单,量不大,但制成工艺有些复杂,他找了很多OEM厂都不愿意接.辗转找到了我,问我有没有兴趣做,单子虽然不大,但如果做成了还是能挣比小钱的. ?回家后,我和怀孕三个月,在家休养的老婆商量后,觉得虽然

编程思想┊从实例谈面向对象编程(OOP)、工厂模式和重构

有了翅膀才能飞,欠缺灵活的代码就象冻坏了翅膀的鸟儿.不能飞翔,就少了几许灵动的气韵.我们需要给代码带去温暖的阳光,让僵冷的翅膀重新飞起来.结合实例,通过应用OOP.设计模式和重构,你会看到代码是怎样一步一步复活的. 为了更好的理解设计思想,实例尽可能简单化.但随着需求的增加,程序将越来越复杂.此时就有修改设计的必要,重构和设计模式就可以派上用场了.最后当设计渐趋完美后,你会发现,即使需求不断增加,你也可以神清气闲,不用为代码设计而烦恼了. 假定我们要设计一个媒体播放器.该媒体播放器目前只支持音频

面向对象编程-基础(工厂、构造函数、原型、混合、动态等模式)

本文参考:js高级程序设计 第三版这篇文章我啃了大半天写的,是很烦,没有毅力看下去的注定还是不会(1).工厂模式:封装一个函数createPerson,这个函数可以创造一个人对象,包含三个属性和一个方法,然后利用这个函数分别创建了2个对象p1,p2. function createPerson(name,age,job){ var p=new Object(); p.name=name; p.age=age; p.job=job; p.showName=function(){ alert(thi

C#——依赖接口编程与简单工厂

在开发一个项目的时候,有时候会碰到这样一个问题:就是项目开发到一半时,原先对数据库的访问走的是ADO.NET,中途项目经理突然要求改成使用EF实体模型去访问数据库......呃好吧!这样的话就需要去把原有的代码个修改,我们都知道在ADO.Net中,数据访问层DAL与业务逻辑层是耦合在一块的,当数据访问层的代码由原来的使用ADO.NET去访问数据库变为使用EF实体模型去访问数据库时,业务逻辑层BLL的代码也要去相应的改变(这是一件相当痛苦的事情) 1 //private UserEFDAL use

游戏编程精粹学习 - 使用定点颜色插值模拟实时光照

终于有空看点新东西,这一篇在<游戏编程精粹1>的5.3节中,主要讲通过烘焙前后左右4个方向光照并插值,来代替顶点光照的做法. 看了下原文例程的代码,似乎是放在cpu部分处理的顶点色,或可能只是参考用的脚本. 这种烘焙4个方向的做法或许优于顶点光照,但缺点是光线角度较为固定,原文描述早期的足球游戏有使用到. 优点: 优化好比顶点光照更快 精度比光照探针高 可以代替lod2,lod3级别物件的光照 缺点: 只对俯视角.平视角支持比较好 占用一个uv数据 首先写一个简单的兰伯特光照烘焙四个方向: 插

梦工厂实验室 龟兔赛跑预测 模拟

问题 A: 龟兔赛跑预测 时间限制: 1 Sec  内存限制: 512 MB提交: 72  解决: 27[提交][状态][讨论版] 题目描述 输入 输出 样例输入 10 5 5 2 20 样例输出 D 4 枚举时间,依次判断龟兔在当前时间点的状态. #include<stdio.h> int main() { int v1,v2,t,s,l,i; int l1=0,l2=0,f=0; scanf("%d%d%d%d%d",&v1,&v2,&t,&am

编程两道——悼念2016.3.25模拟糟糕的模拟考

很久不做算法题,模拟考一团糟,今天自己实现记录下: 1.生成N位格雷码,e.g.[00,01,10,11] 1 #include <iostream> 2 #include <string> 3 #include <cmath> 4 using namespace std; 5 6 7 const int n = 4; 8 int size = 0; 9 int num = int(pow(2, n)); 10 string* codes = new string[nu

设计模式——工厂模式

常说的工厂模式一般认为有三种:简单工厂.工厂方法模式.抽象工厂模式.其中简单工厂严格上来说不是一种设计模式,而被认为是一种好的编码习惯/风格. 简单工厂 简单工厂的本质就是封装变化的代码,使客户代码将要面临的改变变少.而且被封装的代码也有了更好的复用性,比如服务多个客户端或者被继承/包装等工具来扩展. 下面以肾5和肾6为对象来说明 //define product(iphone) interface public interface IPhone{ public void model(); }