// Update is called once per frame void FixedUpdate() { trueCars = new List <Car>(); trueTasks = new List <Task>(); //重新分配任务,先清空已经分配的任务 foreach (Car car in GlobalVaribles.car_list) { CarController cc = GameObject.Find(car.carName).gameObject.GetComponent <CarController>(); if (cc.carMessage.workState == WorkState.Empty || cc.carMessage.workState == WorkState.WayToLoad) { cc.task = null; } } if (GlobalVaribles.allocableTasks.Count > 0 && GlobalVaribles.allocableCars.Count > 0) { Dictionary <Car, Task> minMatch = new Dictionary <Car, Task>(); //matchList中的最小匹配minMatch //Task>=Car, 从任务队列中取出小车数目的任务,再进行全排列 if (GlobalVaribles.allocableTasks.Count >= GlobalVaribles.allocableCars.Count) { List <Dictionary <Car, Task> > matchList = new List <Dictionary <Car, Task> >(); //匹配Dic for (int i = 0; i < GlobalVaribles.allocableCars.Count; i++) { trueTasks.Add(GlobalVaribles.allocableTasks.ElementAt(i)); } trueCars = GlobalVaribles.allocableCars; //得到对应的匹配组合(car和task一样多) getMatchList(ref matchList, trueCars.ToArray(), trueTasks, 0, trueCars.Count); //matchList的每一项都应该包括unAllocableMatch foreach (Dictionary <Car, Task> matchDic in matchList) { foreach (KeyValuePair <Car, Task> kvp in GlobalVaribles.UnAllocableMatch) { matchDic.Add(kvp.Key, kvp.Value);//进行深拷贝 } } float minTime = getMinMatch(matchList, ref minMatch); } else//Task<Car,会有空闲的小车,此时需要进行(多选少),再进行全排列 { //所有任务都要分配 foreach (Task t in GlobalVaribles.allocableTasks) { trueTasks.Add(t); } List <Car[]> carLists = MathTool <Car> .GetCombination(GlobalVaribles.allocableCars.ToArray(), trueTasks.Count); float minTime = GlobalVaribles.INFINITY; foreach (Car[] carList in carLists) { List <Dictionary <Car, Task> > aMatchList = new List <Dictionary <Car, Task> >(); getMatchList(ref aMatchList, carList, trueTasks, 0, carList.Length); //matchList的每一项都应该包括unAllocableMatch foreach (Dictionary <Car, Task> matchDic in aMatchList) { foreach (KeyValuePair <Car, Task> kvp in GlobalVaribles.UnAllocableMatch) { matchDic.Add(kvp.Key, kvp.Value);//进行深拷贝 } } //计算matchList中的最小匹配minMatch Dictionary <Car, Task> aMinMatch = new Dictionary <Car, Task>(); float aMinTime = getMinMatch(aMatchList, ref aMinMatch); //aMinTime只是局部最小,还要比较多个matchList的最小值 if (aMinTime < minTime) { minTime = aMinTime; minMatch = aMinMatch; } } } //给各个小车分发任务 DistributeTasks(minMatch); } }