示例#1
0
 /// <summary>
 /// 概率矩阵其实就是优选取计数器
 /// </summary>
 private void RefreshProbabilityMatrix(ProbabilityMatrix probablityMatrix, List <OptimizedResult> resultList)
 {
     foreach (OptimizedResult res in resultList)
     {
         for (int i = 0; i < res.Tasks.Count; i++)
         {
             probablityMatrix[i, res.Selections[i].Item1]++;
             probablityMatrix[i, res.Selections[i].Item1, res.Selections[i].Item2]++;
         }
     }
 }
示例#2
0
        /// <summary>
        /// 随机生成一个任务序列
        /// </summary>
        private Tuple <int, int>[] GenerateSequence(IList <IList <IList <Task> > > taskMatrix, Task[] tasksArray,
                                                    Random random, ProbabilityMatrix probabilityMatrix,
                                                    double selectDiffElement)
        {
            Tuple <int, int>[] selectionArray = new Tuple <int, int> [tasksArray.Length];
            //Tuple->kind index->source index->count
            int[][] indexSelected = new int[taskMatrix.Count][];
            for (int i = 0; i < taskMatrix.Count; i++)
            {
                indexSelected[i] = new int[taskMatrix[i].Count];
                for (int j = 0; j < taskMatrix[i].Count; j++)
                {
                    indexSelected[i][j] = taskMatrix[i][j].Count;
                }
            }
            int           index     = 0;
            List <int>    probIndex = new List <int>();
            List <double> probList  = new List <double>();

            while (index < tasksArray.Length)
            {
                double sum = 0;
                probList.Clear();
                probIndex.Clear();
                //加入择异因子
                for (int i = 0; i < taskMatrix.Count; i++)
                {
                    bool lastSameHint = false;
                    if (index > 0 && selectionArray[index - 1].Item1 == i)
                    {
                        lastSameHint = true;
                    }
                    if (indexSelected[i].Sum() > 0)
                    {
                        double proValue = lastSameHint ? (probabilityMatrix[index, i] / selectDiffElement) : probabilityMatrix[index, i];
                        sum += proValue;
                        probIndex.Add(i);
                        probList.Add(proValue);
                    }
                }
                int selectKind = RandomSelect(random, probIndex, probList, sum);
                probList.Clear();
                probIndex.Clear();
                sum = 0;
                for (int i = 0; i < taskMatrix[selectKind].Count; i++)
                {
                    if (indexSelected[selectKind][i] > 0)
                    {
                        sum += probabilityMatrix[index, selectKind, i];
                        probIndex.Add(i);
                        probList.Add(probabilityMatrix[index, selectKind, i]);
                    }
                }
                int selectSource = RandomSelect(random, probIndex, probList, sum);
                int taskIndex    = selectKind;
                int sourceIndex  = selectSource;
                int itemIndex    = taskMatrix[selectKind][selectSource].Count - indexSelected[selectKind][selectSource];
                selectionArray[index] = new Tuple <int, int>(taskIndex, sourceIndex);
                tasksArray[index++]   = taskMatrix[taskIndex][sourceIndex][itemIndex];
                indexSelected[selectKind][selectSource]--;
            }
            //设置后置复位任务关键节点
            //for(int i = 0; i < tasksArray.Length; i++)
            //{
            //    tasksArray[i].RecoverJoint = null;
            //    for(int j = i + 1; j < tasksArray.Length; j++)
            //    {
            //        if(selectionArray[i].Item1 == selectionArray[j].Item1)
            //        {
            //            tasksArray[i].RecoverJoint = tasksArray[j].GetUsingRailJoint();
            //            break;
            //        }
            //    }
            //}
            return(selectionArray);
        }
示例#3
0
        /// <summary>
        /// 启发式方式
        /// </summary>
        private OptimizedResult HeuristicMethodOptimize1(List <Task> tasks,
                                                         int batchSize, int refreshSize,
                                                         int maxIterationCount, double errorRatio,
                                                         double eMatrixRatio, double selectDiffElement)
        {
            OptimizedResult result = new OptimizedResult();

            result.TimeCost = Double.MaxValue;
            IList <IList <IList <Task> > > taskMatrix = ParseTasks(tasks);

            Task[] tasksArray = new Task[tasks.Count];
            Random random     = new Random();

            OptimizedResult[]      batchResult       = new OptimizedResult[(int)(batchSize * (1 - eMatrixRatio))];
            OptimizedResult[]      eBatchResult      = new OptimizedResult[(int)(batchSize * eMatrixRatio)];
            List <OptimizedResult> sortedList        = new List <OptimizedResult>(batchSize);
            ProbabilityMatrix      dProbablityMatrix = new ProbabilityMatrix(taskMatrix, tasks.Count);
            ProbabilityMatrix      eProbablityMatrix = new ProbabilityMatrix(taskMatrix, tasks.Count);
            String   output   = "{0}::当前最优:{1:0.00},平权--均值:{2:0.00}, 标准差:{3:0.00}。动态--均值:{4:0.00}, 标准差:{5:0.00}。";
            Debugger debugger = new Debugger();

            int batchIndex = 0;
            int iteration  = 0;

            while (maxIterationCount != -1 && iteration < maxIterationCount)
            {
                //计算一个,先搞平权的batch数量
                for (batchIndex = 0; batchIndex < eBatchResult.Length; batchIndex++)
                {
                    Tuple <int, int>[] selectArray = GenerateSequence(taskMatrix, tasksArray, random, eProbablityMatrix, selectDiffElement);
                    double             timeCost    = CalculateSequence(tasksArray.ToList());
                    iteration++;
                    OptimizedResult res = new OptimizedResult();
                    res.TimeCost             = timeCost;
                    res.Tasks                = tasksArray.ToList();
                    res.Selections           = selectArray;
                    eBatchResult[batchIndex] = res;
                    if (timeCost < result.TimeCost)
                    {
                        result.Counter    = 0;
                        result.Selections = res.Selections;
                        result.Tasks      = res.Tasks;
                        result.TimeCost   = res.TimeCost;
                    }
                    else if (timeCost == result.TimeCost)
                    {
                        result.Counter++;
                    }
                }
                for (batchIndex = 0; batchIndex < batchResult.Length; batchIndex++)
                {
                    Tuple <int, int>[] selectArray = GenerateSequence(taskMatrix, tasksArray, random, dProbablityMatrix, selectDiffElement);
                    double             timeCost    = CalculateSequence(tasksArray.ToList());
                    iteration++;
                    OptimizedResult res = new OptimizedResult();
                    res.TimeCost            = timeCost;
                    res.Tasks               = tasksArray.ToList();
                    res.Selections          = selectArray;
                    batchResult[batchIndex] = res;
                    if (timeCost < result.TimeCost)
                    {
                        result.Counter    = 0;
                        result.Selections = res.Selections;
                        result.Tasks      = res.Tasks;
                        result.TimeCost   = res.TimeCost;
                    }
                    else if (timeCost == result.TimeCost)
                    {
                        result.Counter++;
                    }
                }
                //计算并记录各项指标
                double avg  = batchResult.Length == 0 ? 0 : batchResult.Average(b => b.TimeCost);
                double s2r  = batchResult.Length == 0 ? 0 : Math.Sqrt(batchResult.Sum(b => Math.Pow(b.TimeCost - result.TimeCost, 2)) / batchResult.Length);
                double eAvg = eBatchResult.Length == 0 ? 0 : eBatchResult.Average(b => b.TimeCost);
                double eS2r = eBatchResult.Length == 0 ? 0 : Math.Sqrt(eBatchResult.Sum(b => Math.Pow(b.TimeCost - result.TimeCost, 2)) / eBatchResult.Length);
                debugger.StringAppend(iteration).StringAppend(" ")
                .StringAppend(result.TimeCost).StringAppend(" ")
                .StringAppend(eAvg).StringAppend(" ")
                .StringAppend(eS2r).StringAppend(" ")
                .StringAppend(avg).StringAppend(" ")
                .StringAppend(s2r).StringAppend(" ")
                .StringAppend("\n");
                Debugger.WriteLine(String.Format(output, iteration, result.TimeCost, eAvg, eS2r, avg, s2r));

                sortedList.Clear();
                sortedList.AddRange(eBatchResult);
                sortedList.AddRange(batchResult);
                sortedList.Sort(new OptimizedResultComparator());
                if (eMatrixRatio < 1)
                {
                    //更新概率矩阵
                    RefreshProbabilityMatrix(dProbablityMatrix, sortedList.Take(refreshSize).ToList());
                }
            }
            debugger.WriteTofile();
            return(result);
        }