コード例 #1
0
        /// <summary>
        /// считаем стоимость целевой функции
        /// </summary>
        /// <param name="res"></param>
        /// <returns></returns>
        private double CalcFunctionWeight(Board board, PlacingAndTracingStepResources res, AverageValueDelegate experimentResult)
        {
            double timePrice = 6.25;//6.25$ в час
            double moneyPart = res.server.Price;

            moneyPart += res.workstation.Price;
            TimeSpan time = experimentResult(board, res, 1);

            moneyPart += (time.TotalHours + 1) * timePrice;
            moneyPart  = (int)moneyPart;

            return(moneyPart);
        }
コード例 #2
0
        public PlacingAndTracingStepResources Optimize(Board board, AverageValueDelegate experimentResult, out double cost)
        {
            Random rand = new Random();

            currentConfiguration            = new PlacingAndTracingStepResources();
            currentConfiguration.algorithm  = new MatrixPlacingAlgorithm();
            currentConfiguration.algorithm2 = new WaveTracingAlgorythm();
            currentConfiguration.designer   = new Designer();
            currentConfiguration.designer2  = new Designer();
            currentConfiguration.server     = new Computer()
            {
                CpuFrequency = 3.5e6
            };
            currentConfiguration.workstation = new Computer()
            {
                CpuFrequency = 2.5e6
            };
            currentConfiguration.workstation2 = new Computer()
            {
                CpuFrequency = 2.5e6
            };
            bestConfiguration = currentConfiguration;
            temperature       = StartTemperature;
            currentIteration  = 0;


            while (temperature > EndTemperature)
            {
                PlacingAndTracingStepResources prevConfig;
                prevConfig = currentConfiguration;

                currentConfiguration              = new PlacingAndTracingStepResources();
                currentConfiguration.algorithm    = prevConfig.algorithm;
                currentConfiguration.algorithm2   = prevConfig.algorithm2;
                currentConfiguration.designer     = prevConfig.designer;
                currentConfiguration.designer2    = prevConfig.designer;
                currentConfiguration.server       = prevConfig.server;
                currentConfiguration.workstation  = prevConfig.workstation;
                currentConfiguration.workstation2 = prevConfig.workstation2;


                ///изменение одного параметра
                switch (rand.Next(0, 5))
                {
                case 0:
                    if (currentConfiguration.algorithm is MatrixPlacingAlgorithm)
                    {
                        currentConfiguration.algorithm = new TightPlacingAlgorithm();
                    }
                    else
                    {
                        currentConfiguration.algorithm = new MatrixPlacingAlgorithm();
                    }
                    break;

                case 1:
                    if (currentConfiguration.algorithm2 is WaveTracingAlgorythm)
                    {
                        currentConfiguration.algorithm2 = new TrunkTracingAlgorythm();
                    }
                    else if (currentConfiguration.algorithm2 is TrunkTracingAlgorythm)
                    {
                        currentConfiguration.algorithm2 = new BeamTracingAlgorythm();
                    }
                    else
                    {
                        currentConfiguration.algorithm2 = new WaveTracingAlgorythm();
                    }
                    break;

                case 2:
                    currentConfiguration.server.CpuFrequency = rand.Next(2, 9) * 0.5e6;
                    break;

                case 3:
                    currentConfiguration.workstation.CpuFrequency = rand.Next(2, 9) * 0.5e6;
                    break;

                case 4:
                    currentConfiguration.workstation2.CpuFrequency = rand.Next(2, 9) * 0.5e6;
                    break;
                }

                //выбираем лучший
                if (CalcFunctionWeight(board, bestConfiguration, experimentResult) >= CalcFunctionWeight(board, currentConfiguration, experimentResult))
                {
                    bestConfiguration = currentConfiguration;
                }
                //или рандомно
                else
                {
                    double P = CalculateProbaility(CalcFunctionWeight(board, currentConfiguration, experimentResult) - CalcFunctionWeight(board, bestConfiguration, experimentResult), temperature);
                    if (rand.NextDouble() <= P)
                    {
                        bestConfiguration = currentConfiguration;
                    }
                }
                //запоминаем лучший результат в любом случае
                currentConfiguration = bestConfiguration;

                //Охлаждаем
                temperature -= DeltaTemperature;
                currentIteration++;
            }
            cost = CalcFunctionWeight(board, bestConfiguration, experimentResult);
            return(bestConfiguration);
        }