List<List<SubCalendarEvent>> SpreadOutEvents(List<List<SubCalendarEvent>> AlreadyAlignedEvents, Double AverageOccupiedSchedule, List<TimeLine> AllFreeSpots, Dictionary<TimeLine, Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>>> AllPossibleEvents, List<List<SubCalendarEvent>> RestrictedElements) { //Function takes a List Of TimeLines that have been compressed towards the left of a timeLine and Attempts to spread them out List<List<SubCalendarEvent>> retValue= new List<List<SubCalendarEvent>>(); Dictionary<TimeLine, List<mTuple<double, SubCalendarEvent>>> MovableElements_Dict = new Dictionary<TimeLine, List<mTuple<double, SubCalendarEvent>>>(); List<mTuple<double, SubCalendarEvent>> TotalMovableList = new List<mTuple<double,SubCalendarEvent>>(); Dictionary<SubCalendarEvent , Tuple<int,TimeLine>> SubCalEventCurrentlyAssignedTImeLine = new Dictionary<SubCalendarEvent,Tuple<int,TimeLine>>(); Dictionary<TimeLine, mTuple<TimeSpan, TimeSpan>> TImeLine_ToAverageTimeSpan = new Dictionary<TimeLine, mTuple<TimeSpan, TimeSpan>>();//holds the current timeline to mtuple of ideal average timespan and current total active timespan Dictionary<TimeLine, mTuple<int, double>> TimeLineOccupancy = new Dictionary<TimeLine, mTuple<int, double>>(); int j = 0; List<mTuple <TimeLine, int>> LessThanAverage = new List<mTuple<TimeLine,int>>(); List<mTuple <TimeLine, int>> AboveAverage = new List<mTuple<TimeLine,int>>(); foreach (TimeLine eachTimeLine in AllFreeSpots) { TimeSpan TotalActiveSpan =Utility.SumOfActiveDuration(AlreadyAlignedEvents[j]); TimeSpan AverageTimeSpan = new TimeSpan((long)(AverageOccupiedSchedule*(double)eachTimeLine.TimelineSpan.Ticks)); TImeLine_ToAverageTimeSpan.Add(eachTimeLine, new mTuple<TimeSpan, TimeSpan>(AverageTimeSpan, TotalActiveSpan)); double Occupancy = (double)TotalActiveSpan.Ticks / (double)eachTimeLine.TimelineSpan.Ticks;// percentage of active duration relative to the size of the TimeLine Timespan TimeLineOccupancy.Add(eachTimeLine, new mTuple<int,double>(j-1, Occupancy)); if(Occupancy>AverageOccupiedSchedule) { AboveAverage.Add(new mTuple<TimeLine, int>(eachTimeLine, j)); List<SubCalendarEvent> AlreadyAssigned = AlreadyAlignedEvents[j]; List<SubCalendarEvent> RestrictedSubCalevent = RestrictedElements[j]; List<SubCalendarEvent> MovableElements = AlreadyAssigned.ToList(); MovableElements = MovableElements.Where(obj => (!RestrictedSubCalevent.Contains(obj))).ToList(); IEnumerable<KeyValuePair<SubCalendarEvent, Tuple<int, TimeLine>>> ListOFeachKeyValuePair = MovableElements.Select(obj => new KeyValuePair<SubCalendarEvent, Tuple<int, TimeLine>>(obj, new Tuple<int, TimeLine>(j, AllFreeSpots[j]))); foreach (KeyValuePair<SubCalendarEvent, Tuple<int, TimeLine>> eachKeyValuePair in ListOFeachKeyValuePair) { SubCalEventCurrentlyAssignedTImeLine.Add(eachKeyValuePair.Key, eachKeyValuePair.Value); } List<mTuple<double, SubCalendarEvent>> MovableForDict = MovableElements.Select(obj => new mTuple<double, SubCalendarEvent>(DistanceSolver.AverageToAllNodes(obj.myLocation, MovableElements.Where(obj1 => obj1 != obj).ToList().Select(obj2 => obj2.myLocation).ToList()), obj)).ToList(); MovableForDict.Sort(delegate(mTuple<double, SubCalendarEvent> A, mTuple<double, SubCalendarEvent> B) { return A.Item1.CompareTo(B.Item1); /*if(A.Item1==B.Item1) return 0; else{ A.Item1.CompareTo(A.Item1) }*/ }); TotalMovableList.AddRange(MovableForDict); MovableElements_Dict.Add(AllFreeSpots[j], MovableForDict); } else { if(Occupancy<AverageOccupiedSchedule) { double ExcessPercentageSpace = AverageOccupiedSchedule - Occupancy; LessThanAverage.Add(new mTuple<TimeLine, int>(eachTimeLine, j)); Dictionary<TimeLine, Dictionary<string, Dictionary<string, mTuple<bool, SubCalendarEvent>>>> jhgjgjhghj; //IEnumerable<IEnumerable<KeyValuePair<string, mTuple<bool, SubCalendarEvent>>>> DictOfPosSubCals0 = AllPossibleEvents[eachTimeLine].Select(obj=>obj.Value); IEnumerable<KeyValuePair<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>>> DictOfPosSubCals0 = AllPossibleEvents[eachTimeLine].Select(obj => obj); IEnumerable<Dictionary<string, mTuple<bool, SubCalendarEvent>>> DictOfPosSubCals1 = DictOfPosSubCals0.Select(obj => obj.Value); //List<List<KeyValuePair<string, mTuple<bool, SubCalendarEvent>>>> DictOfPosSubCals0List = AllPossibleEvents[eachTimeLine].Select(obj => obj.Value).ToList(); //IEnumerable<KeyValuePair<string, mTuple<bool, SubCalendarEvent>>> DictOfPosSubCals = AllPossibleEvents[eachTimeLine].SelectMany(obj=>obj.Value); IEnumerable< SubCalendarEvent> DictOfPosSubCals = AllPossibleEvents[eachTimeLine].SelectMany(obj=>obj.Value).Select(obj=>obj.Value.Item2); /*from keyValuepair in AllPossibleEvents[eachTimeLine] from pair0 in keyValuepair.Value select pair0.Value.Item2*/ TimeSpan SpaceTogetAverage = new TimeSpan((long)(ExcessPercentageSpace * (double)eachTimeLine.TimelineSpan.Ticks)); List<mTuple<double, SubCalendarEvent>> CompatibleWithTimeLine = PopulateCompatibleList(TotalMovableList, DictOfPosSubCals.ToList(), eachTimeLine, SpaceTogetAverage); List<mTuple<bool, SubCalendarEvent>> restrictedForFuncCall = Utility.SubCalEventsTomTuple(AlreadyAlignedEvents[j], true); CompatibleWithTimeLine.AddRange(Utility.SubCalEventsTomTuple(AlreadyAlignedEvents[j], (double)100)); Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> PossibleEventsForFuncCall = new Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>>(); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> CompatibleWithListForFunCall = new Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>(); Dictionary<SubCalendarEvent, BusyTimeLine> SubCalendarEvent_OldTImeLine = new Dictionary<SubCalendarEvent, BusyTimeLine>();//Dictionary stores the Subcalendar event old TimeLine, just incase the do not get reassigned to the current timeline foreach(mTuple<double, SubCalendarEvent> eachmTuple in CompatibleWithTimeLine) { TimeSpan ActiveTimeSpan = eachmTuple.Item2.ActiveDuration; string subcalStringID = eachmTuple.Item2.ID; SubCalendarEvent_OldTImeLine.Add(eachmTuple.Item2, eachmTuple.Item2.ActiveSlot.CreateCopy()); if(CompatibleWithListForFunCall.ContainsKey(ActiveTimeSpan)) { ++CompatibleWithListForFunCall[ActiveTimeSpan].Item1; ; } else { CompatibleWithListForFunCall.Add(ActiveTimeSpan, new mTuple<int,TimeSpanWithStringID>(1, new TimeSpanWithStringID(eachmTuple.Item2.ActiveDuration,ActiveTimeSpan.Ticks.ToString()))); } if(PossibleEventsForFuncCall.ContainsKey(ActiveTimeSpan)) { PossibleEventsForFuncCall[ActiveTimeSpan].Add(subcalStringID, new mTuple<bool, SubCalendarEvent>(true, eachmTuple.Item2)); } else { PossibleEventsForFuncCall.Add(ActiveTimeSpan, new Dictionary<string, mTuple<bool, SubCalendarEvent>>()); PossibleEventsForFuncCall[ActiveTimeSpan].Add(subcalStringID, new mTuple<bool, SubCalendarEvent>(true, eachmTuple.Item2)); } } List<mTuple<bool, SubCalendarEvent>> UpdatedListForTimeLine= stitchUnRestrictedSubCalendarEvent(eachTimeLine, restrictedForFuncCall, PossibleEventsForFuncCall, CompatibleWithListForFunCall,Occupancy); TimeSpan OccupiedSpace = Utility.SumOfActiveDuration(UpdatedListForTimeLine.Select(obj => obj.Item2).ToList()); TimeSpan ExcessSpace = OccupiedSpace-AverageTimeSpan; if(ExcessSpace.Ticks>0) { IEnumerable<SubCalendarEvent> NewlyAddedElements = (UpdatedListForTimeLine.Where(obj => !AlreadyAlignedEvents[j].Contains(obj.Item2))).Select(obj=>obj.Item2); List<mTuple<double, SubCalendarEvent>> NewlyAddedElementsWithCost = NewlyAddedElements.Select(obj => new mTuple<double, SubCalendarEvent>(DistanceSolver.AverageToAllNodes(obj.myLocation, UpdatedListForTimeLine.Select(obj3=>obj3.Item2).Where(obj1 => obj1 != obj).ToList().Select(obj2 => obj2.myLocation).ToList()), obj)).ToList(); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> CompatibilityListFOrTIghtestForExtraAverga = new Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>(); Dictionary<TimeSpan, List<mTuple<double, SubCalendarEvent>>> CompatibilityListForNewlyAddedElements = new Dictionary<TimeSpan,List<mTuple<double,SubCalendarEvent>>>(); foreach (mTuple<double, SubCalendarEvent> eachmTUple in NewlyAddedElementsWithCost) { if(CompatibilityListForNewlyAddedElements.ContainsKey(eachmTUple.Item2.ActiveDuration)) { CompatibilityListForNewlyAddedElements[eachmTUple.Item2.ActiveDuration].Add(eachmTUple); CompatibilityListForNewlyAddedElements[eachmTUple.Item2.ActiveDuration] = Utility.RandomizeIEnumerable(CompatibilityListForNewlyAddedElements[eachmTUple.Item2.ActiveDuration]); CompatibilityListForNewlyAddedElements[eachmTUple.Item2.ActiveDuration].Sort(delegate(mTuple<double, SubCalendarEvent> A, mTuple<double, SubCalendarEvent> B) { return A.Item1.CompareTo(B.Item1); }); } else { CompatibilityListForNewlyAddedElements.Add(eachmTUple.Item2.ActiveDuration,new List<mTuple<double,SubCalendarEvent>>(){eachmTUple}); } if (CompatibilityListFOrTIghtestForExtraAverga.ContainsKey(eachmTUple.Item2.ActiveDuration)) { ++CompatibilityListFOrTIghtestForExtraAverga[eachmTUple.Item2.ActiveDuration].Item1; } else { CompatibilityListFOrTIghtestForExtraAverga.Add(eachmTUple.Item2.ActiveDuration, new mTuple<int, TimeSpanWithStringID>(1, new TimeSpanWithStringID(eachmTUple.Item2.ActiveDuration, eachmTUple.Item2.ActiveDuration.Ticks.ToString()))); } } TimeSpan Space_NonAverage = TimeSpan.FromTicks((long)((1 - AverageOccupiedSchedule) * eachTimeLine.TimelineSpan.Ticks));//Space derive from subtracting the calculated expected average timespan for this time line from thie timeline //ExcessSpace = Space_NonAverage; SnugArray CompatibilityToBestAverageFit = new SnugArray(CompatibilityListFOrTIghtestForExtraAverga.Values.ToList(), ExcessSpace); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AllPossibleTIghtExcessFits = CompatibilityToBestAverageFit.MySnugPossibleEntries; AllPossibleTIghtExcessFits=SnugArray.SortListSnugPossibilities(AllPossibleTIghtExcessFits); List<mTuple<double, SubCalendarEvent>> removedElements = new List<mTuple<double,SubCalendarEvent>>();//stores element that dont get reassigned to this current timeLine if(AllPossibleTIghtExcessFits.Count>0) { Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> TIghtestFit = AllPossibleTIghtExcessFits[AllPossibleTIghtExcessFits.Count - 1]; foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in TIghtestFit)//Hack alert: Assumes tightest fit is most diverse { while (eachKeyValuePair.Value.Item1>0) { removedElements.Add(CompatibilityListForNewlyAddedElements[eachKeyValuePair.Value.Item2.timeSpan][CompatibilityListForNewlyAddedElements[eachKeyValuePair.Value.Item2.timeSpan].Count-1]); CompatibilityListForNewlyAddedElements[eachKeyValuePair.Value.Item2.timeSpan].RemoveAt(CompatibilityListForNewlyAddedElements[eachKeyValuePair.Value.Item2.timeSpan].Count-1); --eachKeyValuePair.Value.Item1; } } } NewlyAddedElements=CompatibilityListForNewlyAddedElements.SelectMany(obj=>obj.Value).Select(obj=>obj.Item2); UpdatedListForTimeLine.RemoveAll(obj=>removedElements.Select(obj1=>obj1.Item2).Contains(obj.Item2));//use LINQ to remove elements currently in "removedElements" List<SubCalendarEvent> ListOfNewlyAddeedElements = NewlyAddedElements.ToList(); removedElements.ForEach(obj => obj.Item2.shiftEvent(SubCalendarEvent_OldTImeLine[obj.Item2].Start - obj.Item2.ActiveSlot.Start)); for (int i = 0; i < ListOfNewlyAddeedElements.Count; i++)//removes Each reassigned element from its currently attached field { SubCalendarEvent eachSubCalendarEvent = ListOfNewlyAddeedElements[i]; Tuple<int,TimeLine> CurrentMatchingField=SubCalEventCurrentlyAssignedTImeLine[eachSubCalendarEvent]; mTuple<TimeSpan, TimeSpan> AverageTimeSpanAndTotalTimeSpan = TImeLine_ToAverageTimeSpan[CurrentMatchingField.Item2]; if (((AverageTimeSpanAndTotalTimeSpan.Item2 - eachSubCalendarEvent.ActiveDuration) >= AverageTimeSpanAndTotalTimeSpan.Item1)) { AverageTimeSpanAndTotalTimeSpan.Item2 -= eachSubCalendarEvent.ActiveDuration; AlreadyAlignedEvents[CurrentMatchingField.Item1].Remove(eachSubCalendarEvent); SubCalEventCurrentlyAssignedTImeLine[eachSubCalendarEvent] = new Tuple<int, TimeLine>(j, AllFreeSpots[j]); Dictionary<TimeLine, Dictionary<string, Dictionary<string, mTuple<bool, SubCalendarEvent>>>> AllPossibleEvents222222; TotalMovableList.RemoveAll(obj => NewlyAddedElements.Contains(obj.Item2));//removes the newly added element from Total possible movable elements AllPossibleEvents[AllFreeSpots[j]][eachSubCalendarEvent.ActiveDuration].Remove(eachSubCalendarEvent.ID); if (AllPossibleEvents[AllFreeSpots[j]][eachSubCalendarEvent.ActiveDuration].Count < 1) { AllPossibleEvents[AllFreeSpots[j]].Remove(eachSubCalendarEvent.ActiveDuration); } } else { ListOfNewlyAddeedElements.Remove(eachSubCalendarEvent); eachSubCalendarEvent.shiftEvent(SubCalendarEvent_OldTImeLine[eachSubCalendarEvent].Start - eachSubCalendarEvent.ActiveSlot.Start); --i; } } NewlyAddedElements = ListOfNewlyAddeedElements; AlreadyAlignedEvents[j].AddRange(NewlyAddedElements); TotalActiveSpan = Utility.SumOfActiveDuration(AlreadyAlignedEvents[j]); AverageTimeSpan = new TimeSpan((long)(AverageOccupiedSchedule * (double)eachTimeLine.TimelineSpan.Ticks)); Occupancy = (double)TotalActiveSpan.Ticks / (double)eachTimeLine.TimelineSpan.Ticks;// percentage of active duration relative to the size of the TimeLine Timespan } } } j++; } Dictionary<TimeLine, List<SubCalendarEvent>> CompatibleList = new Dictionary<TimeLine, List<SubCalendarEvent>>();//this Dictionary stores keyValuepair of a TimeLine and Subcalevents that can work within said timeLine that are not part of the currently assigned set; retValue = AlreadyAlignedEvents.ToList(); return retValue; }
List<SubCalendarEvent> OptimizeArrangeOfSubCalEvent(TimeLine PertinentFreeSpot, Tuple<SubCalendarEvent, SubCalendarEvent> BoundaryCalendarEvent, List<mTuple<int, TimeSpanWithStringID>> CompatibleWithList, Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> PossibleEntries_Cpy, double occupancy=0) { CompatibleWithList.Clear(); Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> PossibleSubCalEvents = removeSubCalEventsThatCantWorkWithTimeLine(PertinentFreeSpot, PossibleEntries_Cpy,true); foreach (KeyValuePair<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> eachKeyValuePair in PossibleSubCalEvents)//populates PossibleEntries_Cpy. I need a copy to maintain all references to PossibleEntries { CompatibleWithList.Add(new mTuple<int, TimeSpanWithStringID>(eachKeyValuePair.Value.Count, new TimeSpanWithStringID(eachKeyValuePair.Value.ToList()[0].Value.Item2.ActiveDuration, eachKeyValuePair.Key.Ticks.ToString()))); } SnugArray BestFit_beforeBreak = new SnugArray(CompatibleWithList, PertinentFreeSpot.TimelineSpan); TimeSpan AverageTimeSpan= new TimeSpan((long)(occupancy*(double)PertinentFreeSpot.TimelineSpan.Ticks)); List<SubCalendarEvent> LowestCostArrangement = new System.Collections.Generic.List<SubCalendarEvent>(); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AllPossibleBestFit_beforeBreak = BestFit_beforeBreak.MySnugPossibleEntries; AllPossibleBestFit_beforeBreak = SnugArray.SortListSnugPossibilities(AllPossibleBestFit_beforeBreak, new TimeSpanWithStringID(AverageTimeSpan,AverageTimeSpan.Ticks.ToString())); AllPossibleBestFit_beforeBreak.Reverse(); List<List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>> var3_beforeBreak = new System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>>(); if (AllPossibleBestFit_beforeBreak.Count > 0) { var3_beforeBreak.Add(AllPossibleBestFit_beforeBreak); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AveragedBestFit = getAveragedOutTIimeLine(var3_beforeBreak, 0); Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> removedImpossibleValue = removeSubCalEventsThatCantWorkWithTimeLine(PertinentFreeSpot, PossibleEntries_Cpy); List<List<SubCalendarEvent>> PossibleSubCaleventsCobination = generateCombinationForDifferentEntries(AveragedBestFit[0], removedImpossibleValue); PossibleSubCaleventsCobination = Utility.RandomizeIEnumerable(PossibleSubCaleventsCobination); LowestCostArrangement = getArrangementWithLowestDistanceCost(PossibleSubCaleventsCobination, BoundaryCalendarEvent); TimeLine FreeSpotUpdated; } return LowestCostArrangement; }
List<List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>> generateTreeCallsToSnugArray(Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> AvailableSubCalendarEvents, List<TimeLine> AllTimeLines, int TimeLineIndex, List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar, Dictionary<TimeLine, Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> DictionaryOfTimelineAndSubcalendarEvents, Dictionary<TimeLine, Tuple<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>, Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>> DictionaryOfTimelineAndConstrainedElements)//, List<SubCalendarEvent> usedSubCalendarEvensts) { /* * Name: jerome Biotidara * Description: This function is responsible for generating the snug possibilities for a list containing all timelines it takes 5 elements as its parameters. -AvailableSubCalendarEvents ->The remaining List of subcalendar events. -AllTimeLines->A list of the Timelines that needto be populated with the desired snug possibility -TimeLineIndex->An Index keeping track of what timeline the funciton is current working on -FullTimeLineWithSnugListOfSubcalendarEvents->is a list containing A list of subcalendarEvents for each TimeLine * -PertainingSnugPossibilityForTimieline-> is a list of snugpossibilities that apply to a TimeLine in a TimeLineIndex * -DictionaryOfTimelineAndSubcalendarEvents-> A dictionary of TimeLine and interferring subcalendarevents */ //if (ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack.Count >= maxHackConstant) { //return ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack; } if (AvailableSubCalendarEvents.Count > MaxNumberOfInterferringSubcalEvents) { MaxNumberOfInterferringSubcalEvents = AvailableSubCalendarEvents.Count; } if (TimeLineIndex > LargestTimeIndex) { LargestTimeIndex = TimeLineIndex; } int indexChecker = 14; /*if (TimeLineIndex == indexChecker) { mTuple<bool, List<TimeSpanWithStringID>> statusOfError=Utility.isViolatingTimeSpanString(InterferringTimeSpanWithStringID_Cpy, SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar); if (statusOfError.Item1 ) { ; } }*/ List<List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>> ListOfAllSnugPossibilitiesInRespectiveTImeLines = new List<List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>>(); if (AllTimeLines.Count < 1) { return null; } if ((TimeLineIndex >= AllTimeLines.Count) && (AvailableSubCalendarEvents.Count > 0)) { LogInfo += ("Couldnt Find For this timeLine timeLine\n"); } if ((TimeLineIndex >= AllTimeLines.Count) || (AvailableSubCalendarEvents.Count < 1)) { if (TimeLineIndex < AllTimeLines.Count) { LogInfo += ("Weird Exit\n"); } if (AvailableSubCalendarEvents.Count < 1) { ; } ListOfAllSnugPossibilitiesInRespectiveTImeLines.Add(SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar); return ListOfAllSnugPossibilitiesInRespectiveTImeLines; } int TOtalEvents = 0; if ((TimeLineIndex == 13) || (TimeLineIndex == 13)) { //foreach (Dictionary<string, mTuple<int, TimeSpanWithStringID>> eachDictionary in SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar) { foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in AvailableSubCalendarEvents) { TOtalEvents += eachKeyValuePair.Value.Item1; } } if (TOtalEvents == 1) { ; } } Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> MyPertinentSubcalendarEvents; Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> CantBeUsedInCurrentTimeLine = new Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>(); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> RestrictedToThisTimeLineElements = DictionaryOfTimelineAndConstrainedElements[AllTimeLines[TimeLineIndex]].Item1; TimeSpan TotalTimeUsedUpByConstrained = new TimeSpan(0); foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in RestrictedToThisTimeLineElements)//HACK THIS does not take into account two clashing restricted events { int CantBeUsedCount = DictionaryOfTimelineAndConstrainedElements[AllTimeLines[TimeLineIndex]].Item2[eachKeyValuePair.Key].Item1 - RestrictedToThisTimeLineElements[eachKeyValuePair.Key].Item1; CantBeUsedInCurrentTimeLine.Add(eachKeyValuePair.Key, new mTuple<int, TimeSpanWithStringID>(CantBeUsedCount, eachKeyValuePair.Value.Item2)); AvailableSubCalendarEvents[eachKeyValuePair.Key].Item1 -= CantBeUsedCount; //AvailableSubCalendarEvents[eachKeyValuePair.Key].Item1 -= RestrictedToThisTimeLineElements[eachKeyValuePair.Key].Item1; //DictionaryOfTimelineAndSubcalendarEvents[AllTimeLines[TimeLineIndex]][eachKeyValuePair.Key].Item1 -= RestrictedToThisTimeLineElements[eachKeyValuePair.Key].Item1; //AvailableSubCalendarEvents[eachKeyValuePair.Key].Item1 -= AvailableSubCalendarEventsCountReduction; ; } MyPertinentSubcalendarEvents = Utility.ListIntersection(AvailableSubCalendarEvents, DictionaryOfTimelineAndSubcalendarEvents[AllTimeLines[TimeLineIndex]]); foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in RestrictedToThisTimeLineElements) { MyPertinentSubcalendarEvents[eachKeyValuePair.Key].Item1 -= eachKeyValuePair.Value.Item1; TotalTimeUsedUpByConstrained += (TimeSpan.FromTicks(eachKeyValuePair.Value.Item2.timeSpan.Ticks * eachKeyValuePair.Value.Item1)); } List<mTuple<int, TimeSpanWithStringID>> ListOfTimeSpanWithID_WithCounts = MyPertinentSubcalendarEvents.Values.ToList(); SnugArray MySnugArray = new SnugArray(ListOfTimeSpanWithID_WithCounts, AllTimeLines[TimeLineIndex].TimelineSpan - TotalTimeUsedUpByConstrained); //SnugArray MySnugArray = new SnugArray(ConstrainedMySubcalendarEventTimespans, MySubcalendarEventTimespans, AllTimeLines[TimeLineIndex].TimelineSpan); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AllSnugPossibilities = MySnugArray.MySnugPossibleEntries; AllSnugPossibilities = SnugArray.SortListSnugPossibilities(AllSnugPossibilities); AllSnugPossibilities.Reverse(); if (AllSnugPossibilities.Count > 1) { AllSnugPossibilities.RemoveRange(1, (AllSnugPossibilities.Count - 1)); } foreach (Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachDictionary in AllSnugPossibilities) { /*if ((TimeLineIndex ==5)||(TimeLineIndex ==9)) { if (eachDictionary.ContainsKey("90000000000")) { if (eachDictionary["90000000000"].Item1 >= 2) { ; } } }*/ foreach (TimeSpan eachTimeSpan in RestrictedToThisTimeLineElements.Keys) { if (eachDictionary.ContainsKey(eachTimeSpan)) { eachDictionary[eachTimeSpan].Item1 += RestrictedToThisTimeLineElements[eachTimeSpan].Item1; } else { eachDictionary.Add(eachTimeSpan, new mTuple<int, TimeSpanWithStringID>(RestrictedToThisTimeLineElements[eachTimeSpan].Item1, RestrictedToThisTimeLineElements[eachTimeSpan].Item2)); } } } foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in CantBeUsedInCurrentTimeLine)//restores restricted for other timeLines into AvailableSubCalendarEvents { AvailableSubCalendarEvents[eachKeyValuePair.Key].Item1 += eachKeyValuePair.Value.Item1; } List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> SerialIzedListOfSubCalendarEvents = AllSnugPossibilities; Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> AvailableSubCalendarEventsAfterRemovingAssingedSnugElements = new Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>(); if (SerialIzedListOfSubCalendarEvents.Count > 0) { foreach (Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> AlreadyAssignedSubCalendarEvent in AllSnugPossibilities) { AvailableSubCalendarEventsAfterRemovingAssingedSnugElements = Utility.NotInList_NoEffect(AvailableSubCalendarEvents, AlreadyAssignedSubCalendarEvent); SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar[TimeLineIndex] = AlreadyAssignedSubCalendarEvent; /*if (checkSumOfTimeEvent(SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar) >= MaxNumberOfInterferringSubcalEvents) { ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack.Add(SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar); //if (ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack.Count >= maxHackConstant) { //return ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack; } }*/ ListOfAllSnugPossibilitiesInRespectiveTImeLines.AddRange(generateTreeCallsToSnugArray(AvailableSubCalendarEventsAfterRemovingAssingedSnugElements, AllTimeLines, TimeLineIndex + 1, SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar.ToList(), DictionaryOfTimelineAndSubcalendarEvents, DictionaryOfTimelineAndConstrainedElements)); //ListOfAllSnugPossibilitiesInRespectiveTImeLines = MyHolder.ToList(); } } else { AvailableSubCalendarEventsAfterRemovingAssingedSnugElements = Utility.NotInList_NoEffect(AvailableSubCalendarEvents, new Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>());//Hack this can be optimized... the whole "notinlist" call can be optimized as a call to AvailableSubCalendarEvents. Review to see if references to func are affected. ListOfAllSnugPossibilitiesInRespectiveTImeLines.AddRange(generateTreeCallsToSnugArray(AvailableSubCalendarEventsAfterRemovingAssingedSnugElements, AllTimeLines, TimeLineIndex + 1, SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar.ToList(), DictionaryOfTimelineAndSubcalendarEvents, DictionaryOfTimelineAndConstrainedElements)); } return ListOfAllSnugPossibilitiesInRespectiveTImeLines; }
List<mTuple<bool, SubCalendarEvent>> FurtherFillTimeLineWithSubCalEvents(List<mTuple<bool, SubCalendarEvent>> AllReadyAssignedSubCalEvents, TimeLine ReferenceTimeLine, Dictionary<TimeLine, Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AllCompatibleWithList, TimeLine PreceedingTimeLine, Dictionary<TimeLine, Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>>> PossibleEntries) { /* * CompatibleWithList has whats left after stitchUnRestrictedSubCalendarEvent has removed all possible fittable Events * Hack Alert: The current implementation does not optimize for restricted values */ List<SubCalendarEvent> AssignedSubCalendarEvents = new System.Collections.Generic.List<SubCalendarEvent>(); foreach (mTuple<bool, SubCalendarEvent> eachmTuple in AllReadyAssignedSubCalEvents) { AssignedSubCalendarEvents.Add(eachmTuple.Item2); } List<TimeSpanWithStringID> LeftOvers = new System.Collections.Generic.List<TimeSpanWithStringID>(); foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in AllCompatibleWithList[PreceedingTimeLine]) { if (eachKeyValuePair.Value.Item1 > 0) { LeftOvers.Add(eachKeyValuePair.Value.Item2); } } PreceedingTimeLine.Empty(); TimeLine UpdatedTImeLine = Utility.AddSubCaleventsToTimeLine(PreceedingTimeLine, AssignedSubCalendarEvents); PreceedingTimeLine.AddBusySlots(UpdatedTImeLine.OccupiedSlots); List<TimeLine> AllFreeSpots = PreceedingTimeLine.getAllFreeSlots().ToList(); Dictionary<TimeLine, List<mTuple<bool, SubCalendarEvent>>> matchingValidSubcalendarEvents = new System.Collections.Generic.Dictionary<TimeLine, System.Collections.Generic.List<mTuple<bool, SubCalendarEvent>>>();//Dictionary contains a match up of the free within already assigned variables and possible fillers Dictionary<TimeLine, Dictionary<string, mTuple<int, TimeSpanWithStringID>>> ForSnugArray = new System.Collections.Generic.Dictionary<TimeLine, System.Collections.Generic.Dictionary<string, mTuple<int, TimeSpanWithStringID>>>(); Dictionary<TimeLine, SnugArray> FreeSpotSnugArrays = new System.Collections.Generic.Dictionary<TimeLine, SnugArray>(); Dictionary<TimeLine, List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>> FreeSpotSnugPossibiilities = new System.Collections.Generic.Dictionary<TimeLine, System.Collections.Generic.List<System.Collections.Generic.Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>>(); //Dictionary<TimeLine, List<TimeSpanWithStringID>> FreeSpotSnugPossibilities = new System.Collections.Generic.Dictionary<TimeLine, SnugArray>(); Dictionary<string, SubCalendarEvent> AllMovableSubCalEvents = new System.Collections.Generic.Dictionary<string, SubCalendarEvent>(); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AllMoveOvrSet = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>(); // Dictionary<string, Dictionary<SubCalendarEvent, List<TimeLine>>> SubEventToMatchingTimeLinePossible = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<SubCalendarEvent, System.Collections.Generic.List<TimeLine>>>(); Dictionary<TimeLine, Dictionary<TimeSpan, List<mTuple<bool, SubCalendarEvent>>>> TimeLine_WithMathChingSubCalevents = new Dictionary<TimeLine, Dictionary<TimeSpan, List<mTuple<bool, SubCalendarEvent>>>>();// string in second dictionary is the String of the duration of the SubCalendarEvent Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> TotalPossibleTimeSpanWithStrings = new System.Collections.Generic.Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>(); foreach (TimeLine eachTimeLine in AllFreeSpots) { List<mTuple<bool, SubCalendarEvent>> PossibleFillers = removeSubCalEventsThatCantWorkWithTimeLine(eachTimeLine, PossibleEntries[PreceedingTimeLine].ToList(), true);//hack we need to ensure cases of partial fit ForSnugArray.Add(eachTimeLine, new System.Collections.Generic.Dictionary<string, mTuple<int, TimeSpanWithStringID>>()); TimeLine_WithMathChingSubCalevents.Add(eachTimeLine, new System.Collections.Generic.Dictionary<TimeSpan, System.Collections.Generic.List<mTuple<bool, SubCalendarEvent>>>()); foreach (mTuple<bool, SubCalendarEvent> eachmTuple in PossibleFillers) { if (ForSnugArray[eachTimeLine].ContainsKey(eachmTuple.Item2.ActiveDuration.ToString())) { ++ForSnugArray[eachTimeLine][eachmTuple.Item2.ActiveDuration.ToString()].Item1; } else { ForSnugArray[eachTimeLine].Add(eachmTuple.Item2.ActiveDuration.ToString(), new mTuple<int, TimeSpanWithStringID>(1, new TimeSpanWithStringID(eachmTuple.Item2.ActiveDuration, eachmTuple.Item2.ActiveDuration.ToString()))); } if (!AllMovableSubCalEvents.ContainsKey(eachmTuple.Item2.ID))//populates all movable SubCalendarEVents { AllMovableSubCalEvents.Add(eachmTuple.Item2.ID, eachmTuple.Item2); if (TotalPossibleTimeSpanWithStrings.ContainsKey(eachmTuple.Item2.ActiveDuration)) { ++TotalPossibleTimeSpanWithStrings[eachmTuple.Item2.ActiveDuration].Item1; } else { TotalPossibleTimeSpanWithStrings.Add(eachmTuple.Item2.ActiveDuration, new mTuple<int, TimeSpanWithStringID>(1, new TimeSpanWithStringID(eachmTuple.Item2.ActiveDuration, eachmTuple.Item2.ActiveDuration.ToString()))); } } TimeSpan IdTimeSpan = eachmTuple.Item2.ActiveDuration; if (TimeLine_WithMathChingSubCalevents[eachTimeLine].ContainsKey(IdTimeSpan)) { TimeLine_WithMathChingSubCalevents[eachTimeLine][IdTimeSpan].Add(eachmTuple); } else { TimeLine_WithMathChingSubCalevents[eachTimeLine].Add(IdTimeSpan, new System.Collections.Generic.List<mTuple<bool, SubCalendarEvent>>() { eachmTuple }); } /*if (SubEventToMatchingTimeLinePossible.ContainsKey(eachmTuple.Item2.ActiveDuration.ToString()))// builds a dictionary of the TimeSpan String ID and a Dictionary of SubCalendar EVents with a List of feasible TimeLine { if (SubEventToMatchingTimeLinePossible[eachmTuple.Item2.ActiveDuration.ToString()].ContainsKey(eachmTuple.Item2)) { SubEventToMatchingTimeLinePossible[eachmTuple.Item2.ActiveDuration.ToString()][eachmTuple.Item2].Add(eachTimeLine); } else { SubEventToMatchingTimeLinePossible[eachmTuple.Item2.ActiveDuration.ToString()].Add(eachmTuple.Item2, new System.Collections.Generic.List<TimeLine>() { eachTimeLine }); } } else { SubEventToMatchingTimeLinePossible.Add(eachmTuple.Item2.ActiveDuration.ToString(), new System.Collections.Generic.Dictionary<SubCalendarEvent, System.Collections.Generic.List<TimeLine>>()); SubEventToMatchingTimeLinePossible[eachmTuple.Item2.ActiveDuration.ToString()].Add(eachmTuple.Item2, new System.Collections.Generic.List<TimeLine>() { eachTimeLine }); }*/ } matchingValidSubcalendarEvents.Add(eachTimeLine, PossibleFillers); } foreach (TimeLine eachTimeLine in AllFreeSpots) { FreeSpotSnugArrays.Add(eachTimeLine, new SnugArray(ForSnugArray[eachTimeLine].Values.ToList(), eachTimeLine.TimelineSpan)); } foreach (TimeLine eachTimeLine in AllFreeSpots) { FreeSpotSnugPossibiilities.Add(eachTimeLine, FreeSpotSnugArrays[eachTimeLine].MySnugPossibleEntries); } foreach (List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> allFreeSpotSnugPossibiilities in FreeSpotSnugPossibiilities.Values) { AllMoveOvrSet.AddRange(allFreeSpotSnugPossibiilities); } TimeSpanWithStringID UnAssignedTimeSpanWithString = new TimeSpanWithStringID(ReferenceTimeLine.TimelineSpan, ReferenceTimeLine.TimelineSpan.ToString()); /*if (LeftOvers.Count > 0) { UnAssignedTimeSpanWithString = LeftOvers[0]; }*/ TimeSpan TotalMovedSofar = new TimeSpan(0); Dictionary<TimeLine, List<mTuple<bool, SubCalendarEvent>>> FreeSpots_MatchedEvents = new System.Collections.Generic.Dictionary<TimeLine, System.Collections.Generic.List<mTuple<bool, SubCalendarEvent>>>(); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> CompatibleListForReferenceTimeLine = AllCompatibleWithList[ReferenceTimeLine]; foreach (TimeLine eachTimeLine in AllFreeSpots) { FreeSpots_MatchedEvents.Add(eachTimeLine, new System.Collections.Generic.List<mTuple<bool, SubCalendarEvent>>()); Tuple<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>, Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> UpdatedCompatibleList_BestSnugPossibility = UpdateCompatibleListOfTimeLine(CompatibleListForReferenceTimeLine, FreeSpotSnugPossibiilities[eachTimeLine], ReferenceTimeLine, UnAssignedTimeSpanWithString, TotalPossibleTimeSpanWithStrings); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> UpdatedCompatibleList = UpdatedCompatibleList_BestSnugPossibility.Item1; Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> BestSnugAvailability = UpdatedCompatibleList_BestSnugPossibility.Item2; TotalMovedSofar += SnugArray.TotalTimeSpanOfSnugPossibility(BestSnugAvailability); Dictionary<TimeSpan, List<mTuple<bool, SubCalendarEvent>>> TimeLineMatchingSubEvents = TimeLine_WithMathChingSubCalevents[eachTimeLine]; foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in BestSnugAvailability) { if (TimeLineMatchingSubEvents[eachKeyValuePair.Key].Count > 0) { FreeSpots_MatchedEvents[eachTimeLine].Add(TimeLineMatchingSubEvents[eachKeyValuePair.Key][0]); TimeLineMatchingSubEvents[eachKeyValuePair.Key].RemoveAt(0); --TotalPossibleTimeSpanWithStrings[eachKeyValuePair.Key].Item1; if (TotalPossibleTimeSpanWithStrings[eachKeyValuePair.Key].Item1 == 0) { TotalPossibleTimeSpanWithStrings.Remove(eachKeyValuePair.Key); } } else { ; } } //KeyValuePair<string, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair List<TimeSpan> AllKeys = CompatibleListForReferenceTimeLine.Keys.ToList(); foreach (TimeSpan eachTimeSpan in AllKeys)//Updates the Compatible List of the Reference timelin, in order to reflect movement in data { if (UpdatedCompatibleList.ContainsKey(eachTimeSpan))//checks if reference timeLine has updated keys { CompatibleListForReferenceTimeLine[eachTimeSpan] = UpdatedCompatibleList[eachTimeSpan]; } else { CompatibleListForReferenceTimeLine.Remove(eachTimeSpan); } } } TimeSpan FreeSpaceAfterMovement = ReferenceTimeLine.TimelineSpan - SnugArray.TotalTimeSpanOfSnugPossibility(CompatibleListForReferenceTimeLine); if (LeftOvers.Count > 0)//checks if there are move overs from preceeding compatible TimeLine { SnugArray BestFitIntoCurrentTimeLine = new SnugArray(AllCompatibleWithList[PreceedingTimeLine].Values.ToList(), FreeSpaceAfterMovement);//Tries to construct tightest fit on newly created free space in reference timelin....hack alert, it does not prioritize restricted elements List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> SortedData = BestFitIntoCurrentTimeLine.MySnugPossibleEntries;//sort the snug possibility based on mst filling if (SortedData.Count > 0) { Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> selectedDictionary = SortedData[SortedData.Count - 1]; foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair in selectedDictionary) { if (CompatibleListForReferenceTimeLine.ContainsKey(eachKeyValuePair.Key)) { CompatibleListForReferenceTimeLine[eachKeyValuePair.Key].Item1 += eachKeyValuePair.Value.Item1; AllCompatibleWithList[PreceedingTimeLine][eachKeyValuePair.Key].Item1 -= eachKeyValuePair.Value.Item1; } else { CompatibleListForReferenceTimeLine.Add(eachKeyValuePair.Key, eachKeyValuePair.Value); AllCompatibleWithList[PreceedingTimeLine][eachKeyValuePair.Key].Item1 -= eachKeyValuePair.Value.Item1; } if (AllCompatibleWithList[PreceedingTimeLine][eachKeyValuePair.Key].Item1 < 0) { ; } } } } List<mTuple<bool, SubCalendarEvent>> retValue = new System.Collections.Generic.List<mTuple<bool, SubCalendarEvent>>(AllReadyAssignedSubCalEvents); foreach (TimeLine eachTimeLine in AllFreeSpots)//Snaps each Subcalevent to the beginning of a free TimeLine { List<SubCalendarEvent> ListOfSubCalEvents = Utility.mTupleToSubCalEvents(FreeSpots_MatchedEvents[eachTimeLine]); if (Utility.PinSubEventsToStart(ListOfSubCalEvents, eachTimeLine)) { retValue.AddRange(FreeSpots_MatchedEvents[eachTimeLine]); } else { ; } } return retValue; }
Tuple<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>, Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> UpdateCompatibleListOfTimeLine(Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> CurrentCompatibleList, List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> MovedOverSet, TimeLine ReferenceTimeLine, TimeSpanWithStringID LeftOuts, Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> TotalOfMovedVariables) { //Hack alert: You need to create a situation that enforces a restricted event as being assigned first TimeSpan CurrentTotalOfSnugVariables = new TimeSpan(0); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> retValue_MovedVariables = new Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>(); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> retValue_CurrentCompatibleList = new Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>(); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> CurrentCompatibleList_Cpy = SnugArray.CreateCopyOFSnuPossibilities(CurrentCompatibleList); //TimeSpan SumOfLeftOuts= new TimeSpan(0); TimeSpan RemainderTimeSpan = new TimeSpan(0); Dictionary<string, mTuple<int, TimeSpanWithStringID>> MovedOverListUpdate = new System.Collections.Generic.Dictionary<string, mTuple<int, TimeSpanWithStringID>>(); if (LeftOuts.timeSpan > ReferenceTimeLine.TimelineSpan) { return null; } CurrentTotalOfSnugVariables = SnugArray.TotalTimeSpanOfSnugPossibility(CurrentCompatibleList_Cpy); RemainderTimeSpan = ReferenceTimeLine.TimelineSpan - CurrentTotalOfSnugVariables; TimeSpan RemainderOfLeftOverChunk = LeftOuts.timeSpan - RemainderTimeSpan; List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> MovedOverSet_Cpy = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>(); foreach (Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachDictionary in MovedOverSet) { MovedOverSet_Cpy.Add(compareMovedOverSetWithTotalPossibleEntries(eachDictionary, TotalOfMovedVariables)); } while (RemainderOfLeftOverChunk.Ticks < 0) { ReferenceTimeLine = new TimeLine(ReferenceTimeLine.Start, ReferenceTimeLine.End - LeftOuts.timeSpan); RemainderOfLeftOverChunk = LeftOuts.timeSpan - RemainderTimeSpan; } SnugArray FitsInChunkOfRemainder_SnugArray = new SnugArray(CurrentCompatibleList_Cpy.Values.ToList(), RemainderOfLeftOverChunk); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> SnugPossibilities = FitsInChunkOfRemainder_SnugArray.MySnugPossibleEntries; Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> Viable_SnugPossibilities = getPlausibleEntriesFromMovedOverSet(MovedOverSet_Cpy, SnugPossibilities); Dictionary<Dictionary<string, mTuple<int, TimeSpanWithStringID>>, List<Dictionary<string, mTuple<int, TimeSpanWithStringID>>>> LeftAfterRemovalSnugPossibilities = new System.Collections.Generic.Dictionary<System.Collections.Generic.Dictionary<string, mTuple<int, TimeSpanWithStringID>>, System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, mTuple<int, TimeSpanWithStringID>>>>(); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> BestSnugPossibility = Viable_SnugPossibilities; retValue_MovedVariables = SnugArray.AddToSnugPossibilityList(retValue_MovedVariables, BestSnugPossibility); Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> CurrentCompatibleList_Cpy_updated = SnugArray.RemoveSnugPossibilityFromAnother(CurrentCompatibleList_Cpy, BestSnugPossibility); retValue_CurrentCompatibleList = SnugArray.RemoveSnugPossibilityFromAnother(CurrentCompatibleList, retValue_MovedVariables); //retValue_CurrentCompatibleList = SnugArray.AddToSnugPossibilityList(CurrentCompatibleList, MovedOverListUpdate); Tuple<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>, Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> retValue = new Tuple<Dictionary<TimeSpan,mTuple<int,TimeSpanWithStringID>>,Dictionary<TimeSpan,mTuple<int,TimeSpanWithStringID>>>(retValue_CurrentCompatibleList, retValue_MovedVariables); //item1 is Updated CurrentCompatible List //item2 is Best Snug change to timeLine return retValue; }
public SnugArray(List<mTuple<int, TimeSpanWithStringID>> EntryArrayOfStuff, TimeSpan MaxValue) { //EntryArrayOfStuff=EntryArrayOfStuff.OrderBy(obj=>obj.TimeSpanID.ToString()).ToList(); /*ParentID_TimeSpanID = new Dictionary<string,List<TimeSpanWithID>>(); foreach (TimeSpanWithID MyTimeSpanWithID in EntryArrayOfStuff) { string ParentCharString=MyTimeSpanWithID.TimeSpanID.getCalendarEventID(); if(ParentID_TimeSpanID.ContainsKey(ParentCharString )) { ParentID_TimeSpanID[MyTimeSpanWithID.TimeSpanID.getCalendarEventID()].Add(MyTimeSpanWithID); } else { ParentID_TimeSpanID.Add(ParentCharString,new List<TimeSpanWithID>()); ParentID_TimeSpanID[ParentCharString].Add(MyTimeSpanWithID); } }*/ Dictionary<TimeSpan, ParentIDCount> TimeSpanBucket = generateBuckets(EntryArrayOfStuff); //SnugArray MyThis = new SnugArray(ConstrainedElements, EntryArrayOfStuff, MaxValue, new Dictionary<string, int>(), new List<Dictionary<string, int>>()); SnugArray MyThis = new SnugArray(TimeSpanBucket, MaxValue, new List<Dictionary<string, int>>()); TopElements=MyThis.TopElements; SubSnugArrayElements=MyThis.SubSnugArrayElements; ListOfDictionaryOfID_Count=MyThis.ListOfDictionaryOfID_Count;//This is a List of Dictionaries with a string of ID and a count of the IDs found AlreadyFoundPermutation=MyThis.AlreadyFoundPermutation; //ConstainedElements=MyThis.ConstainedElements; MyDict = MyThis.MyDict; maxTimeSpan = MaxValue; ArrayOfStuff = MyThis.ArrayOfStuff; }
private SnugArray(Dictionary<TimeSpan, ParentIDCount> EntryArrayOfStuff, TimeSpan MaxValue, List<Dictionary<string, int>> MyListOfDicts) //private SnugArray(List<TimeSpanWithID> ConstrainedElements,List<TimeSpanWithID> EntryArrayOfStuff, TimeSpan MaxValue,List<Dictionary<string, int>> MyListOfDicts) //private SnugArray(List<TimeSpanWithID> ConstrainedElements,List<TimeSpanWithID> EntryArrayOfStuff, TimeSpan MaxValue,Dictionary<string, int> MyDict,List<Dictionary<string, int>> MyListOfDicts) { EntryArrayOfStuff = new Dictionary<TimeSpan, ParentIDCount>(EntryArrayOfStuff); ArrayOfStuff = EntryArrayOfStuff; int i=0; maxTimeSpan = MaxValue; List<TimeSpan> EventKeys = EntryArrayOfStuff.Keys.ToList(); List<TimeSpan> SmallerElements = new List<TimeSpan>(); i = 0; int MyArrayCount = EventKeys.Count; ListOfDictionaryOfID_Count = MyListOfDicts; for (; i < MyArrayCount; i++)//loop gets element only smaller or equal to max size { ParentIDCount MyEntry = EntryArrayOfStuff[EventKeys[i]]; if ((MyEntry.Item2 <= MaxValue)&&(MyEntry.Item1 >0))//checks if smaller than max value { SmallerElements.Add(EventKeys[i]); } } int IncludeOrDontIncludeLastElement = 0; if (SmallerElements.Count > 1) { int counter = SmallerElements.Count; TimeSpan TotalSumOfTimeSoFar = new TimeSpan(0); for (; counter > 0;counter-- )//including the index 0 in for loop means index is small enough to include all indexes { TimeSpan KeyToLastElement = SmallerElements[counter - 1]; ParentIDCount CurrentParentIDCOunt = EntryArrayOfStuff[KeyToLastElement]; TotalSumOfTimeSoFar += TimeSpan.FromTicks(CurrentParentIDCOunt.Item2.Ticks * CurrentParentIDCOunt.Item1); TimeSpan TimeSpanDiff = MaxValue - TotalSumOfTimeSoFar; if (TimeSpanDiff >= EntryArrayOfStuff[SmallerElements[0]].Item2) { ++IncludeOrDontIncludeLastElement; } else { break; } } } TopElements = SmallerElements.ToArray(); //int LengthOfSubElmentArray = TopElements.Length; i = 0; TimeSpan[] MyFittableElemtsHolder = new TimeSpan[SmallerElements.Count];//Array holds the current array of subelements for future reasons List<TimeSpan> MyFittableElementsHolderList = new List<TimeSpan>();//This is also holds a curent array of sub elements. SmallerElements.CopyTo(MyFittableElemtsHolder, 0); MyFittableElementsHolderList = MyFittableElemtsHolder.ToList(); MyArrayCount = SmallerElements.Count - IncludeOrDontIncludeLastElement; SubSnugArrayElements = new SnugArray[MyArrayCount]; Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>>[] PreppedDataArray = new Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>>[MyArrayCount]; MyDict = EntryArrayOfStuff; Dictionary<TimeSpan, ParentIDCount> EntryArrayOfStuff_Cpy = new Dictionary<TimeSpan, ParentIDCount>(EntryArrayOfStuff); #if enableMultithreading int j = 0; for (; j < MyArrayCount; j++)//prepares data for multithreading breaks dependent references { ParentIDCount DictEntry = EntryArrayOfStuff_Cpy[TopElements[j]]; DictEntry = DictEntry.CreateCopy; --(DictEntry.Item1); EntryArrayOfStuff_Cpy[TopElements[j]] = DictEntry; Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>> PreppedData = new Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>>(EntryArrayOfStuff_Cpy, (MaxValue - DictEntry.Item2), ListOfDictionaryOfID_Count);//creates new snug array with elements that are smaller PreppedDataArray[j] = PreppedData; EntryArrayOfStuff_Cpy.Remove(TopElements[j]); } Parallel.For(0, MyArrayCount, k =>//makes parallel calls { if (k > 0) { EntryArrayOfStuff_Cpy.Remove(TopElements[j - 1]); } Tuple<Dictionary<TimeSpan, ParentIDCount>, TimeSpan, List<Dictionary<string, int>>> PreppedData0 = PreppedDataArray[k]; SubSnugArrayElements[k] = new SnugArray(PreppedData0.Item1, PreppedData0.Item2, PreppedData0.Item3);//creates graph on current node } ); #else for (; i < MyArrayCount; i++) { ParentIDCount DictEntry = EntryArrayOfStuff_Cpy[TopElements[i]]; DictEntry = DictEntry.CreateCopy; --(DictEntry.Item1); EntryArrayOfStuff_Cpy[TopElements[i]] = DictEntry; SubSnugArrayElements[i] = new SnugArray(EntryArrayOfStuff_Cpy, (MaxValue - DictEntry.Item2), ListOfDictionaryOfID_Count);//creates new graph for current node EntryArrayOfStuff_Cpy.Remove(TopElements[i]);//removes current node from graph } #endif }
//, List<SubCalendarEvent> usedSubCalendarEvensts) List<List<List<SubCalendarEvent>>> generateTreeCallsToSnugArray(List<SubCalendarEvent> AvailableSubCalendarEvents, List<TimeLine> AllTimeLines, int TimeLineIndex, List<List<SubCalendarEvent>> SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar, Dictionary<TimeLine, List<SubCalendarEvent>> DictionaryOfTimelineAndSubcalendarEvents) { /* * Name: jerome Biotidara * Description: This function is responsible for generating the snug possibilities for a list containing all timelines it takes 5 elements as its parameters. -AvailableSubCalendarEvents ->The remaining List of subcalendar events. -AllTimeLines->A list of the Timelines that needto be populated with the desired snug possibility -TimeLineIndex->An Index keeping track of what timeline the funciton is current working on -FullTimeLineWithSnugListOfSubcalendarEvents->is a list containing A list of subcalendarEvents for each TimeLine * -PertainingSnugPossibilityForTimieline-> is a list of snugpossibilities that apply to a TimeLine in a TimeLineIndex * -DictionaryOfTimelineAndSubcalendarEvents-> A dictionary of TimeLine and interferring subcalendarevents */ //if (ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack.Count >= maxHackConstant) { //return ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack; } if (AvailableSubCalendarEvents.Count > MaxNumberOfInterferringSubcalEvents) { MaxNumberOfInterferringSubcalEvents = AvailableSubCalendarEvents.Count; } if (TimeLineIndex > LargestTimeIndex) { LargestTimeIndex = TimeLineIndex; } if (TimeLineIndex == 8) { int Haha = 1; Haha += 1; } List<List<List<SubCalendarEvent>>> ListOfAllSnugPossibilitiesInRespectiveTImeLines = new System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.List<SubCalendarEvent>>>(); if (AllTimeLines.Count < 1) { return null; } if ((TimeLineIndex >= AllTimeLines.Count) && (AvailableSubCalendarEvents.Count > 0)) { LogInfo += ("Couldnt Find For this timeLine timeLine\n"); } if ((TimeLineIndex >= AllTimeLines.Count) || (AvailableSubCalendarEvents.Count < 1)) { if (TimeLineIndex < AllTimeLines.Count) { LogInfo += ("Weird Exit\n"); } ListOfAllSnugPossibilitiesInRespectiveTImeLines.Add(SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar); return ListOfAllSnugPossibilitiesInRespectiveTImeLines; } List<TimeSpanWithID> MySubcalendarEventTimespans = new System.Collections.Generic.List<TimeSpanWithID>(); List<SubCalendarEvent> MyPertinentSubcalendarEvents = Utility.ListPartOfList(AvailableSubCalendarEvents, DictionaryOfTimelineAndSubcalendarEvents[AllTimeLines[TimeLineIndex]]); List<SubCalendarEvent> ConstrainedToList = ConstrainedList(AllTimeLines[TimeLineIndex], MyPertinentSubcalendarEvents); MyPertinentSubcalendarEvents = Utility.NotInList_NoEffect(MyPertinentSubcalendarEvents, ConstrainedToList);//this removes the constrained Items from the Subcalendar list foreach (SubCalendarEvent MySubcalendarevent in MyPertinentSubcalendarEvents) { MySubcalendarEventTimespans.Add(new TimeSpanWithID(MySubcalendarevent.ActiveDuration, new EventID(MySubcalendarevent.ID))); } List<TimeSpanWithID> ConstrainedMySubcalendarEventTimespans = new System.Collections.Generic.List<TimeSpanWithID>(); foreach (SubCalendarEvent MyConstrainedSubcalendarevent in ConstrainedToList) { ConstrainedMySubcalendarEventTimespans.Add(new TimeSpanWithID(MyConstrainedSubcalendarevent.ActiveDuration, new EventID(MyConstrainedSubcalendarevent.ID))); } TimeSpan TotalimeOccupieByConstrained = SubCalendarEvent.TotalActiveDuration(ConstrainedToList); if(TimeLineIndex==2) { ; } Stopwatch sw = new Stopwatch(); sw.Start(); // ... SnugArray MySnugArray = new SnugArray(new List<TimeSpanWithID>(), MySubcalendarEventTimespans, AllTimeLines[TimeLineIndex].TimelineSpan - TotalimeOccupieByConstrained); //SnugArray MySnugArray = new SnugArray(ConstrainedMySubcalendarEventTimespans, MySubcalendarEventTimespans, AllTimeLines[TimeLineIndex].TimelineSpan); List<List<TimeSpanWithID>> AllSnugPossibilities = MySnugArray.MySnugPossibleEntries; /* AllSnugPossibilities looks like this * {{43,23,54},{43,54,23},{23,43,54}...} where each digit is a time span and remember it generates every permutation. */ List<List<SubCalendarEvent>> SubcalendarMyList = BuildListMatchingTimelineAndSubCalendarEvent(AllSnugPossibilities, MyPertinentSubcalendarEvents, ConstrainedToList); sw.Stop(); /* the function BuildListMatchingTimelineAndSubCalendarEvent builds a list of Subcalendar events for each timespan. * assuming subCal0 has TimeSpan =43 * assuming subCal1 has TimeSpan =23 * assuming subCal2 has TimeSpan =54 * assuming subCal3 has TimeSpan =43 * assuming subCal4 has TimeSpan =23 * assuming subCal5 has TimeSpan =23 * SubcalendarMyList will have the array {{{SubCal0,Subcal3},{subCal1,subCal4,subCal5},{subCal2}},{{SubCal0,Subcal3},{subCal2},{subCal1,subCal4,subCal5}},{{subCal1,subCal4,subCal5},{SubCal0,Subcal3},{subCal2}}...} */ //List<List<SubCalendarEvent>> SerialIzedListOfSubCalendarEvents = SerializeListOfMatchingSubcalendarEvents(SubcalendarMyList); List<List<SubCalendarEvent>> SerialIzedListOfSubCalendarEvents = SubcalendarMyList; List<SubCalendarEvent> AvailableSubCalendarEventsAfterRemovingAssingedSnugElements = new List<SubCalendarEvent>(); if (SerialIzedListOfSubCalendarEvents.Count > 0) { foreach (List<SubCalendarEvent> AlreadyAssignedSubCalendarEvent in SerialIzedListOfSubCalendarEvents) { AvailableSubCalendarEventsAfterRemovingAssingedSnugElements = Utility.NotInList(AvailableSubCalendarEvents.ToList(), AlreadyAssignedSubCalendarEvent); SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar[TimeLineIndex] = AlreadyAssignedSubCalendarEvent; if (checkSumOfTimeEvent(SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar) >= MaxNumberOfInterferringSubcalEvents) { ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack.Add(SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar); //if (ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack.Count >= maxHackConstant) { //return ListOfAllSnugPossibilitiesInRespectiveTImeLines_hack; } } ListOfAllSnugPossibilitiesInRespectiveTImeLines.AddRange(generateTreeCallsToSnugArray(AvailableSubCalendarEventsAfterRemovingAssingedSnugElements, AllTimeLines, TimeLineIndex + 1, SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar.ToList(), DictionaryOfTimelineAndSubcalendarEvents)); //ListOfAllSnugPossibilitiesInRespectiveTImeLines = MyHolder.ToList(); } } else { AvailableSubCalendarEventsAfterRemovingAssingedSnugElements = Utility.NotInList(AvailableSubCalendarEvents.ToList(), new List<SubCalendarEvent>());//Hack this can be optimized... the whole "notinlist" call can be optimized as a call to AvailableSubCalendarEvents. Review to see if references to func are affected. ListOfAllSnugPossibilitiesInRespectiveTImeLines.AddRange(generateTreeCallsToSnugArray(AvailableSubCalendarEventsAfterRemovingAssingedSnugElements, AllTimeLines, TimeLineIndex + 1, SubCalendarEventsPopulatedIntoTimeLineIndex_SoFar.ToList(), DictionaryOfTimelineAndSubcalendarEvents)); } if (AvailableSubCalendarEventsAfterRemovingAssingedSnugElements.Count < 1) { ; } return ListOfAllSnugPossibilitiesInRespectiveTImeLines; }
List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> OptimizeForDeadLine(IEnumerable<KeyValuePair<DateTime, Dictionary<TimeSpan, int>>> DeadLinePreference, TimeSpan CurrentFreeSpace, bool Aggressive = true) { List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> retValue = new List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>(); HashSet<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> qualifiesForNextStage = new HashSet<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>(); HashSet<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AggressiveSet = new HashSet<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>(); IEnumerable<KeyValuePair<DateTime, Dictionary<TimeSpan, int>>> AllBestFitOptions_IEnu = DeadLinePreference; IEnumerable<KeyValuePair<DateTime, Dictionary<TimeSpan, int>>> DeadLinePreference_Qualified; HashSet<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> forNextLevel = new HashSet<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>(); AllBestFitOptions_IEnu = AllBestFitOptions_IEnu.OrderBy(obj => obj.Key); //AllBestFitOptions_IEnu.Reverse(); bool iniAggressive = Aggressive; foreach (KeyValuePair<DateTime, Dictionary<TimeSpan, int>> eachKeyValuePair in AllBestFitOptions_IEnu) { //IEnumerable<KeyValuePair<TimeSpan, int>> AllTimeSpan = eachKeyValuePair.Value.OrderBy(obj => obj); IEnumerable<mTuple<int, TimeSpanWithStringID>> AllTimeSpan = eachKeyValuePair.Value.Select(obj => new mTuple<int, TimeSpanWithStringID>(obj.Value, new TimeSpanWithStringID(obj.Key, obj.Key.Ticks.ToString()))); SnugArray BestFit_OfDeadline = new SnugArray(AllTimeSpan.ToList(), CurrentFreeSpace); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AllPossibleBestFit_beforeBreak = SnugArray.SortListSnugPossibilities_basedOnTimeSpan(BestFit_OfDeadline.MySnugPossibleEntries); AllPossibleBestFit_beforeBreak.Reverse(); if (AllPossibleBestFit_beforeBreak.Count > 0) { Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> TightestConfiguration = AllPossibleBestFit_beforeBreak[0]; retValue.Add(TightestConfiguration); TimeSpan UsedUpSpace = SnugArray.TotalTimeSpanOfSnugPossibility(TightestConfiguration); CurrentFreeSpace -= UsedUpSpace; DeadLinePreference_Qualified = AllBestFitOptions_IEnu.Where(obj => obj.Value != eachKeyValuePair.Value); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> furtherCalls = OptimizeForDeadLine(DeadLinePreference_Qualified, CurrentFreeSpace, Aggressive); if (furtherCalls.Count > 0) { foreach (Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachDictionary in furtherCalls) { foreach (KeyValuePair<TimeSpan, mTuple<int, TimeSpanWithStringID>> eachKeyValuePair0 in TightestConfiguration) { if (eachDictionary.ContainsKey(eachKeyValuePair0.Key)) { eachDictionary[eachKeyValuePair0.Key].Item1 += eachKeyValuePair0.Value.Item1; } else { eachDictionary.Add(eachKeyValuePair0.Key, eachKeyValuePair0.Value); } } } } else { furtherCalls.Add(TightestConfiguration); } return furtherCalls; } } return retValue; }
List<SubCalendarEvent> OptimizeArrangeOfSubCalEvent(TimeLine PertinentFreeSpot, Tuple<SubCalendarEvent, SubCalendarEvent> BoundaryCalendarEvent, List<mTuple<int, TimeSpanWithStringID>> CompatibleWithList, Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> PossibleEntries_Cpy, double occupancy = 0, bool Aggressive = true) { CompatibleWithList.Clear(); Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> PossibleSubCalEvents = removeSubCalEventsThatCantWorkWithTimeLine(PertinentFreeSpot, PossibleEntries_Cpy, true); Dictionary<DateTime, Dictionary<TimeSpan, int>> DeadLineTODuration = new Dictionary<DateTime, Dictionary<TimeSpan, int>>(); foreach (KeyValuePair<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> eachKeyValuePair in PossibleSubCalEvents)//populates PossibleEntries_Cpy. I need a copy to maintain all references to PossibleEntries { CompatibleWithList.Add(new mTuple<int, TimeSpanWithStringID>(eachKeyValuePair.Value.Count, new TimeSpanWithStringID(eachKeyValuePair.Value.ToList()[0].Value.Item2.ActiveDuration, eachKeyValuePair.Key.Ticks.ToString()))); foreach (SubCalendarEvent eachSubcalevent in eachKeyValuePair.Value.Values.Select(obj => obj.Item2)) { DateTime endTime = eachSubcalevent.getCalendarEventRange.End; if (DeadLineTODuration.ContainsKey(endTime)) { if (DeadLineTODuration[endTime].ContainsKey(eachSubcalevent.ActiveDuration)) { ++DeadLineTODuration[endTime][eachSubcalevent.ActiveDuration]; } else { DeadLineTODuration[endTime].Add(eachSubcalevent.ActiveDuration, 1); } } else { DeadLineTODuration.Add(endTime, new Dictionary<TimeSpan, int>()); DeadLineTODuration[endTime].Add(eachSubcalevent.ActiveDuration, 1); } DeadLineTODuration[endTime].OrderBy(obj => obj); } } SnugArray BestFit_beforeBreak = new SnugArray(CompatibleWithList, PertinentFreeSpot.TimelineSpan); TimeSpan AverageTimeSpan = new TimeSpan((long)(occupancy * (double)PertinentFreeSpot.TimelineSpan.Ticks)); List<SubCalendarEvent> LowestCostArrangement = new System.Collections.Generic.List<SubCalendarEvent>(); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AllPossibleBestFit_beforeBreak = BestFit_beforeBreak.MySnugPossibleEntries; AllPossibleBestFit_beforeBreak = SnugArray.SortListSnugPossibilities_basedOnTimeSpan(AllPossibleBestFit_beforeBreak, new TimeSpanWithStringID(AverageTimeSpan, AverageTimeSpan.Ticks.ToString())); AllPossibleBestFit_beforeBreak.Reverse(); List<List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>> var3_beforeBreak = new System.Collections.Generic.List<System.Collections.Generic.List<System.Collections.Generic.Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>>>(); if (AllPossibleBestFit_beforeBreak.Count > 0) { var3_beforeBreak.Add(AllPossibleBestFit_beforeBreak); if (AllPossibleBestFit_beforeBreak.Count > 1) { ; } //List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AveragedBestFit = getAveragedOutTIimeLine(var3_beforeBreak, 0); List<Dictionary<TimeSpan, mTuple<int, TimeSpanWithStringID>>> AveragedBestFit = OptimizeForDeadLine(DeadLineTODuration, PertinentFreeSpot.TimelineSpan); Dictionary<TimeSpan, Dictionary<string, mTuple<bool, SubCalendarEvent>>> removedImpossibleValue = removeSubCalEventsThatCantWorkWithTimeLine(PertinentFreeSpot, PossibleEntries_Cpy); List<List<SubCalendarEvent>> PossibleSubCaleventsCobination = generateCombinationForDifferentEntries(AveragedBestFit[0], removedImpossibleValue); if (Aggressive) { if (PossibleSubCaleventsCobination.Count > 1) { PossibleSubCaleventsCobination.OrderByDescending(obj => obj.Count); PossibleSubCaleventsCobination = PossibleSubCaleventsCobination.GetRange(0, 1); } } if (PossibleSubCaleventsCobination.Count>1) { ; } PossibleSubCaleventsCobination = Utility.RandomizeIEnumerable(PossibleSubCaleventsCobination); LowestCostArrangement = getArrangementWithLowestDistanceCost(PossibleSubCaleventsCobination, BoundaryCalendarEvent); //TimeLine FreeSpotUpdated; } return LowestCostArrangement; }