Пример #1
0
        public Dictionary <TourSampleItem, int> SampleAndReturnTourDestinations(ISamplingUtilities samplingUtilities)
        {
            TourSampleItem[] sampleItems = new TourSampleItem[_sampleSize];

            for (int i = 0; i < _sampleSize; i++)
            {
                TourSampleItem sampleItem;

                RandomUniform01 randomUniform01 = new RandomUniform01(samplingUtilities.SeedValues[i]);

                int nullDestinationCounter = 0;
                // draw repeatedly until a sample item is drawn
                do
                {
                    sampleItem = GetDestination <TourSampleItem>(randomUniform01, _originParcel, _originSegmentZone, _excludedParcel, _excludedSegmentZone);
                    if (sampleItem != null)
                    {
                        sampleItems[i] = sampleItem;
                    }
                    else
                    {
                        ++nullDestinationCounter;
                        if ((nullDestinationCounter % NULL_DESTINATION_WARNING) == 0)
                        {
                            OutputDebugForFailedDestination
                                (samplingUtilities, nullDestinationCounter);
                            if (nullDestinationCounter >= NULL_DESTINATION_ERROR)
                            {
                                throw new Exception(OutputDebugForFailedDestination
                                                        (samplingUtilities, nullDestinationCounter));
                            }
                        }
                    } //end else got null returned
                } while (sampleItem == null);
            }         //end loop for _sampleSize

            Dictionary <TourSampleItem, int> sample = new Dictionary <TourSampleItem, int>();

            bool skipChoiceProbabilityCalculator = true;

            foreach (TourSampleItem sampleItem in sampleItems)
            {
                // SetAlternative will store the associated alternative for the sample item and calculate the sampling probabilities
                sampleItem.SetAlternative(this, sample, _chosenParcel != null && _chosenParcel.Id == sampleItem.ParcelId, AlternativeType.Default, skipChoiceProbabilityCalculator);
            }

            //JLB 20120329
            //            if (_choiceProbabilityCalculator.ModelIsInEstimationMode) {
            if (_choiceProbabilityCalculator.ModelIsInEstimationMode && _chosenParcel != null)
            {
                // creates a tour sample item that represents the chosen alternative
                if (_usualParcel == null || _usualParcel.Id != _chosenParcel.Id)
                {
                    TourSampleItem chosenSampleItem = new TourSampleItem();
                    chosenSampleItem.Initialize(this, _chosenParcel.Id, _chosenParcel.Sequence, _chosenSegmentZone);
                    chosenSampleItem.SetAlternative(this, sample, true, AlternativeType.Chosen, skipChoiceProbabilityCalculator);
                }
                else if (_usualParcel.Id == _chosenParcel.Id)
                {
                    TourSampleItem chosenSampleItem = new TourSampleItem();

                    chosenSampleItem.Initialize(this, _chosenParcel.Id, _chosenParcel.Sequence, _chosenSegmentZone);
                    chosenSampleItem.SetAlternative(this, sample, true, AlternativeType.Usual, skipChoiceProbabilityCalculator);
                }
                if (_usualParcel != null && _usualParcel.Id != _chosenParcel.Id)
                {
                    TourSampleItem usualSampleItem = new TourSampleItem();

                    usualSampleItem.Initialize(this, _usualParcel.Id, _usualParcel.Sequence, _usualSegmentZone);
                    usualSampleItem.SetAlternative(this, sample, false, AlternativeType.Usual, skipChoiceProbabilityCalculator);
                }
            }
            else if (_usualParcel != null)
            {
                // creates a tour sample item that represents the usual alternative
                TourSampleItem usualSampleItem = new TourSampleItem();

                usualSampleItem.Initialize(this, _usualParcel.Id, _usualParcel.Sequence, _usualSegmentZone);
                usualSampleItem.SetAlternative(this, sample, false, AlternativeType.Usual, skipChoiceProbabilityCalculator);
            }

            foreach (KeyValuePair <TourSampleItem, int> sampleItem in sample)
            {
                // calculates adjustment factor for each tour sample item
                if (sampleItem.Key.Probability >= Constants.EPSILON)
                {
                    sampleItem.Key.AdjustmentFactor = -Math.Log(_sampleSize * sampleItem.Key.Probability / sampleItem.Value);
                }

                //not called in this version
                //samplingUtilities.SetUtilities(sampleItem.Key, sampleItem.Value);
            }

            return(sample);
        }
Пример #2
0
        public Dictionary <TourSampleItem, int> SampleAndReturnTourDestinations(ISamplingUtilities samplingUtilities)
        {
            var sampleItems = new TourSampleItem[_sampleSize];

            for (var i = 0; i < _sampleSize; i++)
            {
                TourSampleItem sampleItem;

                var randomUniform01 = new RandomUniform01(samplingUtilities.SeedValues[i]);

                // draw repeatedly until a sample item is drawn
                do
                {
                    sampleItem = GetDestination <TourSampleItem>(randomUniform01, _originParcel, _originSegmentZone, _excludedParcel, _excludedSegmentZone);
                    if (sampleItem != null)
                    {
                        sampleItems[i] = sampleItem;
                    }
                } while (sampleItem == null);
            }

            var sample = new Dictionary <TourSampleItem, int>();

            bool skipChoiceProbabilityCalculator = true;

            foreach (var sampleItem in sampleItems)
            {
                // SetAlternative will store the associated alternative for the sample item and calculate the sampling probabilities
                sampleItem.SetAlternative(this, sample, _chosenParcel != null && _chosenParcel.Id == sampleItem.ParcelId, AlternativeType.Default, skipChoiceProbabilityCalculator);
            }

            //JLB 20120329
            //			if (_choiceProbabilityCalculator.ModelIsInEstimationMode) {
            if (_choiceProbabilityCalculator.ModelIsInEstimationMode && _chosenParcel != null)
            {
                // creates a tour sample item that represents the chosen alternative
                if (_usualParcel == null || _usualParcel.Id != _chosenParcel.Id)
                {
                    var chosenSampleItem = new TourSampleItem();
                    chosenSampleItem.Initialize(this, _chosenParcel.Id, _chosenParcel.Sequence, _chosenSegmentZone);
                    chosenSampleItem.SetAlternative(this, sample, true, AlternativeType.Chosen, skipChoiceProbabilityCalculator);
                }
                else if (_usualParcel.Id == _chosenParcel.Id)
                {
                    var chosenSampleItem = new TourSampleItem();

                    chosenSampleItem.Initialize(this, _chosenParcel.Id, _chosenParcel.Sequence, _chosenSegmentZone);
                    chosenSampleItem.SetAlternative(this, sample, true, AlternativeType.Usual, skipChoiceProbabilityCalculator);
                }
                if (_usualParcel != null && _usualParcel.Id != _chosenParcel.Id)
                {
                    var usualSampleItem = new TourSampleItem();

                    usualSampleItem.Initialize(this, _usualParcel.Id, _usualParcel.Sequence, _usualSegmentZone);
                    usualSampleItem.SetAlternative(this, sample, false, AlternativeType.Usual, skipChoiceProbabilityCalculator);
                }
            }
            else if (_usualParcel != null)
            {
                // creates a tour sample item that represents the usual alternative
                var usualSampleItem = new TourSampleItem();

                usualSampleItem.Initialize(this, _usualParcel.Id, _usualParcel.Sequence, _usualSegmentZone);
                usualSampleItem.SetAlternative(this, sample, false, AlternativeType.Usual, skipChoiceProbabilityCalculator);
            }

            foreach (var sampleItem in sample)
            {
                // calculates adjustment factor for each tour sample item
                if (sampleItem.Key.Probability >= Constants.EPSILON)
                {
                    sampleItem.Key.AdjustmentFactor = -Math.Log(_sampleSize * sampleItem.Key.Probability / sampleItem.Value);
                }

                //not called in this version
                //samplingUtilities.SetUtilities(sampleItem.Key, sampleItem.Value);
            }

            return(sample);
        }