/// <summary>
        /// Creates no-branches way over allowed points from given start
        /// </summary>
        private TravelFragment tryBuildFragmentFrom(TravelCard startPoint)
        {
            if (startPoint == null)
            {
                return(null);
            }

            TravelFragment result         = new TravelFragment(startPoint);
            TravelCard     lastFoundPoint = startPoint;

            // while next step is only one possible - keep moving
            while (_travelPointsDic.ContainsKey(lastFoundPoint.To)
                   // only one possible move
                   && _travelPointsDic[lastFoundPoint.To].Count == 1
                   // no fragments as next possible move (could became unresolvable)
                   && _travelFragments.All(item => item.StartPoint != lastFoundPoint.To))
            {
                // single point - add and continue
                TravelCard tempPoint = _travelPointsDic[lastFoundPoint.To].First();
                // remove item from possible points
                _travelPointsDic.Remove(lastFoundPoint.To);
                // add to fragment
                result.Add(tempPoint);

                lastFoundPoint = tempPoint;
            }

            return(result);
        }
        /// <summary>
        /// This method tries each variant of possible ambiguous fragment joins. On the first success it returns the variant found.
        /// </summary>
        private List <TravelFragment> tryMultivariantCombining()
        {
            HashSet <TravelFragment> results = new HashSet <TravelFragment>();

            foreach (TravelFragment sourceFragment in _travelFragments)
            {
                // search possible next stop
                IEnumerable <TravelFragment> matchedFragments = _travelFragments.Except(new[] { sourceFragment })
                                                                .Where(item => sourceFragment.EndPoint == item.StartPoint);

                foreach (var possibleNextStop in matchedFragments)
                {
                    // create copies of data
                    // join possible branch and try next level of merging

                    List <TravelFragment> fragmentsCopy        = _travelFragments.Except(new[] { sourceFragment, possibleNextStop }).Select(item => item.Clone() as TravelFragment).ToList();
                    TravelFragment        sourceCopy           = sourceFragment.Clone() as TravelFragment;
                    TravelFragment        possibleNextStopCopy = possibleNextStop.Clone() as TravelFragment;
                    sourceCopy.AddFragmentData(possibleNextStopCopy);

                    fragmentsCopy.Add(sourceCopy);

                    TravelSolution tsVariant = new TravelSolution(fragmentsCopy);
                    tsVariant.Solve();
                    if (tsVariant.Result != null)
                    {
                        results.UnionWith(tsVariant.AllResults);
                    }
                }
            }
            return(results.ToList());
        }
        private void combineFragments()
        {
            // combine all simple variants
            bool isDataCombined = false;
            int  processingIdx  = 0;

            do
            {
                isDataCombined = false;
                processingIdx  = 0;

                // cycle over fragments
                while (processingIdx < _travelFragments.Count())
                {
                    TravelFragment sourceFragment = _travelFragments[processingIdx];
                    // search possible next stop
                    List <TravelFragment> matchedFragments = _travelFragments.Except(new[] { sourceFragment })
                                                             .Where(item => sourceFragment.EndPoint == item.StartPoint).ToList();

                    // decise which fragment to add
                    TravelFragment fragmentToAdd = null;

                    // single fragment found - take it
                    if (matchedFragments.Count == 1)
                    {
                        fragmentToAdd = matchedFragments.First();
                    }
                    // different options - take any circle fragment if exists
                    else
                    {
                        fragmentToAdd = matchedFragments.FirstOrDefault(item => item.StartPoint == item.EndPoint);
                    }

                    if (fragmentToAdd != null)
                    {
                        sourceFragment.AddFragmentData(matchedFragments.First());
                        _travelFragments.Remove(matchedFragments.First());
                        isDataCombined = true;
                    }

                    processingIdx++;
                }
            }while (isDataCombined && _travelFragments.Count() > 1);
        }
Beispiel #4
0
        /// <summary>
        /// compare element by element with another instance Data collection
        /// equal content = equal collections = equal objects
        /// </summary>
        public bool Equals(TravelFragment other)
        {
            if (other == null)
            {
                return(false);
            }


            var otherData = other.GetDataCopy();

            if (_data.Count != otherData.Count)
            {
                return(false);
            }

            bool equals = true;

            for (int idx = 0; idx < _data.Count; idx++)
            {
                equals &= _data[idx].Equals(otherData[idx]);
            }
            return(equals);
        }
Beispiel #5
0
 public void AddFragmentData(TravelFragment addFragment)
 {
     _data.AddRange(addFragment.GetDataCopy());
 }