Пример #1
0
        public bool IsValid()
        {
            if (string.IsNullOrWhiteSpace(FileName))
            {
                throw new InvalidOperationException("Pusta nazwa pliku! Można przeprowadzić weryfikacji!");
            }

            var info = GetInfoFromGivenFile();

            var splittedFileName = FileName.Split('_', '.').ToArray();

            string fileNameWithoutExtension = splittedFileName[0];
            int    k = Convert.ToInt32(splittedFileName[1]);
            double h = Convert.ToDouble(splittedFileName[2]) / 10;

            var instanceToVerify = GetInstance(fileNameWithoutExtension, k);

            info.Wait();
            var splittedInfo      = info.Result.Split(' ').Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
            int declaredValue     = Convert.ToInt32(splittedInfo[0]);
            int declaredStartTime = Convert.ToInt32(splittedInfo[1]);

            if (declaredStartTime < 0)
            {
                throw new Exception("Nie można zacząć zadania przed czasem 0!");
            }

            List <int> indexesOrder   = splittedInfo.Skip(2).Select(i => Convert.ToInt32(i)).ToList();
            var        tasksOrder     = PrepareTaskOrderLiest(indexesOrder, instanceToVerify.Tasks);
            var        solvedInstance = new SolvedInstance(instanceToVerify, tasksOrder, h, declaredStartTime);

            return(solvedInstance.Value.Equals(declaredValue));
        }
        //private SolvedInstance ThirdGenerator(SolvedInstance solved, double h, int maxDepth)
        //{
        //    int dueTime = solved.DueTime;

        //    var tasksOrderedByCostForLead = solved.Instance.Tasks.OrderByDescending(t => t.CostForLead).ToList();
        //    var tasksOrderedByCostForDelay = solved.Instance.Tasks.OrderByDescending(t => t.CostForDelay).ToList();

        //    var tasksToDoBeforeDueTime = new List<TaskToSchedule>();
        //    var tasksToDoAfterDueTime = new List<TaskToSchedule>();

        //    while (tasksOrderedByCostForDelay.Any() || tasksOrderedByCostForLead.Any())
        //    {
        //        tasksOrderedByCostForDelay.FirstOrDefault()
        //    }
        //}

        //private SolvedInstance SwapperGenerator(SolvedInstance solved, double h)
        //{
        //    int dueTime = (int)Math.Ceiling(instance.Length * h);

        //    var tasksOrderedByCostForLead = instance.Tasks.OrderByDescending(t => t.CostForLead).ToList();
        //    var tasksOrderedByCostForDelay = instance.Tasks.OrderByDescending(t => t.CostForDelay).ToList();

        //    var tasksToDoBeforeDueTime = new List<TaskToSchedule>();
        //    var tasksToDoAfterDueTime = new List<TaskToSchedule>();

        //    while (tasksOrderedByCostForDelay.Any() || tasksOrderedByCostForLead.Any())
        //    {
        //        tasksOrderedByCostForDelay.FirstOrDefault()
        //    }
        //}

        private SolvedInstance ManipulateStartTime(SolvedInstance instance)
        {
            var firstInstance  = ManipulateStartTimeIncrementing(instance);
            var secondInstance = ManipulateStartTimeDecrementing(instance);

            return(firstInstance.Value < secondInstance.Value ? firstInstance : secondInstance);
        }
Пример #3
0
        public SolvedInstance SolveUsingTabuSearch(SolvedInstance beginningSolution, double h)
        {
            var instanceCreator = new RandomInstanceCreator(TabuListLength);
            var bestSolution    = beginningSolution;
            var currentSolution = bestSolution;

            int stepsWithoutImprovement = 0;
            var start = DateTime.Now;

            while (DateTime.Now.Subtract(start).TotalSeconds < MaxTimeOfProcessingInSeconds)
            {
                currentSolution = instanceCreator.Generate(currentSolution, h);

                if (currentSolution.Value < bestSolution.Value)
                {
                    bestSolution            = currentSolution;
                    stepsWithoutImprovement = 0;
                }
                else
                {
                    stepsWithoutImprovement++;
                }

                if (stepsWithoutImprovement > MaxStepsWithoutImprovement)
                {
                    stepsWithoutImprovement = 0;
                    currentSolution         = bestSolution;
                }
            }

            return(bestSolution);
        }
        public static string GetOutput(SolvedInstance solvedInstance)
        {
            string valuesToOutput = $"{solvedInstance.Value} {solvedInstance.StartTime}";

            StringBuilder sb = new StringBuilder(valuesToOutput);

            solvedInstance.TasksOrder.ForEach(t => sb.AppendFormat(" {0}", t.Index));

            return(sb.ToString());
        }
        public SolvedInstance Generate(SolvedInstance instance, double h)
        {
            var geneatedInstances = new SolvedInstance[Generators.Count];

            Parallel.For(0, Generators.Count, i =>
            {
                geneatedInstances[i] = Generators[i].Invoke(instance, h, MaxDepth);
            });

            return(geneatedInstances.OrderBy(i => i.Value).First());
        }
        public async static void Write(SolvedInstance solvedInstance)
        {
            string fileName = GenerateFileName(solvedInstance.Instance.FileNameWithoutExtension,
                                               solvedInstance.Instance.K, solvedInstance.HParameter);

            string output = GetOutput(solvedInstance);

            using (StreamWriter sr = new StreamWriter(fileName))
            {
                await sr.WriteLineAsync(output);
            }
        }
        private SolvedInstance ManipulateStartTimeDecrementing(SolvedInstance instance)
        {
            if (instance.StartTime > 0)
            {
                var newInstance = new SolvedInstance(instance.Instance, instance.TasksOrder, instance.HParameter, instance.StartTime - 1);
                if (newInstance.Value <= instance.Value)
                {
                    return(ManipulateStartTimeIncrementing(newInstance));
                }
            }

            return(instance);
        }
        private SolvedInstance RandomGenerator(SolvedInstance solved, double h, int maxDepth)
        {
            var newInstance = new SolvedInstance(solved.Instance, solved.Instance.Tasks.OrderBy(t => RandomHelper.GetRandomInt()).ToList(), h, 0);
            var manipulatedStartTimeInstance = ManipulateStartTime(newInstance);

            if (tabuList.IsOnTabuList(manipulatedStartTimeInstance) && maxDepth-- > 0)
            {
                return(RandomGenerator(solved, h, maxDepth));
            }
            else
            {
                return(manipulatedStartTimeInstance);
            }
        }
Пример #9
0
 private void AddToTabuList(SolvedInstance instance)
 {
     if (_forbiddenInstances.Count > 0)
     {
         _forbiddenInstances.Insert(0, instance);
         if (_forbiddenInstances.Count > _tabuListMaxLength)
         {
             _forbiddenInstances.RemoveAt(_tabuListMaxLength - 1);
         }
     }
     else
     {
         _forbiddenInstances.Add(instance);
     }
 }
        private SolvedInstance TriangleOrderWithRandomSmallChangeWithOrderByGenerator(SolvedInstance solved, double h, int maxDepth)
        {
            int dueTime = solved.DueTime;

            int smallRandom = RandomHelper.GetRandomInt(MaxSmallChange);
            var tasksToAddToBeforeListDueTime = solved.GetLateTasks().OrderBy(t => RandomHelper.GetRandomInt()).Take(smallRandom);

            var tasksToDoBeforeDueTime = solved.GetEarlyTasks();

            tasksToDoBeforeDueTime.AddRange(tasksToAddToBeforeListDueTime);
            tasksToDoBeforeDueTime = tasksToDoBeforeDueTime.OrderByDescending(t => t.CostForLead).ToList();

            int startTime = Math.Max(0, dueTime - tasksToDoBeforeDueTime.Sum(task => task.Length));

            var newOrder    = new List <TaskToSchedule>();
            int currentTime = startTime;

            while (tasksToDoBeforeDueTime.Any() && (currentTime + tasksToDoBeforeDueTime.First().Length) <= dueTime)
            {
                newOrder.Add(tasksToDoBeforeDueTime.First());
                tasksToDoBeforeDueTime.RemoveAt(0);
            }
            newOrder.Reverse();
            var tasksToDoAfterDueTime = solved.Instance.Tasks.Except(newOrder);

            newOrder.AddRange(tasksToDoAfterDueTime.OrderByDescending(task => task.CostForDelay));

            var newInstance = new SolvedInstance(solved.Instance, newOrder, h, startTime);
            var manipulatedStartTimeInstance = ManipulateStartTime(newInstance);

            if (tabuList.IsOnTabuList(manipulatedStartTimeInstance) && maxDepth-- > 0)
            {
                return(TriangleOrderWithRandomSmallChangeWithOrderByGenerator(solved, h, maxDepth));
            }
            else
            {
                return(manipulatedStartTimeInstance);
            }
        }
Пример #11
0
 public bool IsOnTabuList(SolvedInstance instance)
 {
     if (_forbiddenInstances.Contains(instance))
     {
         return(true);
     }
     else
     {
         lock (_locker)
         {
             if (_forbiddenInstances.Contains(instance))
             {
                 return(true);
             }
             else
             {
                 AddToTabuList(instance);
                 return(false);
             }
         }
     }
 }
        private SolvedInstance TriangleOrderGenerator(SolvedInstance solved, double h, int maxDepth)
        {
            int dueTime = solved.DueTime;

            var threshold = RandomHelper.GetRandomDouble();
            var tasksToDoBeforeDueTime = solved.Instance.Tasks.Where(task => RandomHelper.GetRandomDouble() > threshold).ToList();

            int startTime = Math.Max(0, dueTime - tasksToDoBeforeDueTime.Sum(task => task.Length));

            var newOrder    = new List <TaskToSchedule>();
            int currentTime = startTime;

            while (tasksToDoBeforeDueTime.Any() && (currentTime + tasksToDoBeforeDueTime.First().Length) <= dueTime)
            {
                var taskToAdd = tasksToDoBeforeDueTime.First();
                currentTime += taskToAdd.Length;
                newOrder.Add(taskToAdd);
                tasksToDoBeforeDueTime.RemoveAt(0);
            }
            newOrder = newOrder.OrderBy(task => task.CostForLead).ToList();
            var tasksToDoAfterDueTime = solved.Instance.Tasks.Except(newOrder);

            newOrder.AddRange(tasksToDoAfterDueTime.OrderByDescending(task => task.CostForDelay));

            var newInstance = new SolvedInstance(solved.Instance, newOrder, h, startTime);
            var manipulatedStartTimeInstance = ManipulateStartTime(newInstance);

            if (tabuList.IsOnTabuList(manipulatedStartTimeInstance) && maxDepth-- > 0)
            {
                return(TriangleOrderGenerator(solved, h, maxDepth));
            }
            else
            {
                return(manipulatedStartTimeInstance);
            }
        }