예제 #1
0
        private static void MapTalkDurationToTime(IList<Talk> talks, Session session, ResultContainer result)
        {
            foreach (var talkDuration in result.ScheduledSizes)
            {
                Talk talk = talks.First(t => t.Duration == talkDuration);

                talk.StartTime = session.Talks.Count <= 0 ? session.StartTime : session.Talks.Last().StartTime.AddMinutes(session.Talks.Last().Duration);

                session.Talks.Add(talk);
                talks.Remove(talk);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="results"></param>
        /// <param name="cancelToken"></param>
        /// <returns></returns>
        public ScheduleResults OrganizeSchedule(ScheduleResults results, CancellationToken cancelToken)
        {
            IList<ResultContainer> containers = new List<ResultContainer>();
            while (_talkSize.Count > 0)
            {
                foreach (var containerSize in _sessionSize)
                {
                    if (cancelToken.IsCancellationRequested) return results;

                    var resultContainer = new ResultContainer { ContainerSize = containerSize };

                    int i;
                    bool firstPass = true;
                    for (i = 0; i < _talkSize.Count; )
                    {
                        if (cancelToken.IsCancellationRequested) return results;

                        int talk = _talkSize[i];
                        int sumScheduleSize = resultContainer.ScheduledSizes == null ? 0 : resultContainer.ScheduledSizes.Sum();

                        if ((resultContainer.ScheduledSizes == null || resultContainer.ScheduledSizes.Count <= 0 || (resultContainer.ScheduledSizes.Last() > talk || !firstPass))
                             && sumScheduleSize + talk <= containerSize)
                        {
                            resultContainer.ScheduledSizes.Add(talk);
                            _packedSchedule.Add(talk);
                            _talkSize.RemoveAt(i);

                            if (resultContainer.ScheduledSizes.Sum(x=>(int)x) == containerSize) i = _talkSize.Count; //int circuit inner for loop
                            continue;
                        }

                        i++;
                        //if we get to the end and we still have room enough for at least the smallest of our packages, then reset and go from
                        //largest to smallest, again.  until our smallest sized parcel remaining is bigger than the container delta.
                        if (i >= _talkSize.Count && sumScheduleSize < containerSize && containerSize - sumScheduleSize >= _talkSize.Last())
                        {
                            firstPass = false;
                            i = 0;
                        }
                    }

                    containers.Add(resultContainer);
                }
            }

            if (IsMoreOptimal(containers, results.CurrentBestResults))
            {
                results.CurrentBestResults = containers;
            }

            return results;
        }