Example #1
0
        public static void Save(SegmentZone segmentZone, BinaryWriter writer)
        {
            writer.Write(segmentZone._parcelSizes.Length);
            writer.Write(segmentZone._zoneWeights.Length);

            writer.Write(segmentZone.Id);
            writer.Write(segmentZone.TotalSize);
            writer.Write(segmentZone.TotalWeight);

            foreach (var item in segmentZone._parcelSizes)
            {
                writer.Write(item.Id);
                writer.Write(item.Value);
            }

            foreach (var index in segmentZone._rankedSizeIndices)
            {
                writer.Write(index);
            }

            foreach (var weight in segmentZone._zoneWeights)
            {
                writer.Write(weight);
            }

            foreach (var index in segmentZone._rankedWeightIndices)
            {
                writer.Write(index);
            }
        }
Example #2
0
        //		public DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segment, int sampleSize, IParcel originParcel, IParcel excludedParcel, IParcel chosenParcel) {
        //			_choiceProbabilityCalculator = choiceProbabilityCalculator;
        //			_segmentZones = Global.SegmentZones[segment];
        //			_sampleSize = sampleSize;
        //
        //			_originParcel = originParcel;
        //			_originSegmentZone = _segmentZones[originParcel.ZoneId];
        //
        //			if (excludedParcel != null) {
        //				_excludedParcel = excludedParcel;
        //				_excludedSegmentZone = _segmentZones[excludedParcel.ZoneId];
        //			}
        //
        //			if (chosenParcel != null) {
        //				_chosenParcel = chosenParcel;
        //				_chosenSegmentZone = _segmentZones[chosenParcel.ZoneId];
        //			}
        //
        //			if (_choiceProbabilityCalculator.ModelIsInEstimationMode && chosenParcel == null) {
        //				throw new ChosenParcelNotSetInEstimationModeException();
        //			}
        //		}

        public DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segment, int sampleSize, IParcel originParcel, IParcel excludedParcel, IParcel usualParcel, IParcel chosenParcel)
        {
            _choiceProbabilityCalculator = choiceProbabilityCalculator;
            _segmentZones = Global.SegmentZones[segment];
            _sampleSize   = sampleSize;

            _originParcel      = originParcel;
            _originSegmentZone = _segmentZones[originParcel.ZoneId];

            if (excludedParcel != null)
            {
                _excludedParcel      = excludedParcel;
                _excludedSegmentZone = _segmentZones[excludedParcel.ZoneId];
            }

            if (usualParcel != null)
            {
                _usualParcel      = usualParcel;
                _usualSegmentZone = _segmentZones[usualParcel.ZoneId];
            }

            if (chosenParcel != null)
            {
                _chosenParcel      = chosenParcel;
                _chosenSegmentZone = _segmentZones[chosenParcel.ZoneId];
            }

            if (_choiceProbabilityCalculator.ModelIsInEstimationMode && chosenParcel == null)
            {
                throw new ChosenParcelNotSetInEstimationModeException();
            }
        }
Example #3
0
        public DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segment, int sampleSize, ITourWrapper tour, ITripWrapper trip, IParcel chosenParcel)
        {
            _choiceProbabilityCalculator = choiceProbabilityCalculator;
            _segmentZones = Global.SegmentZones[segment];
            _sampleSize   = sampleSize;

            _tourOriginParcel      = tour.OriginParcel;
            _tourOriginSegmentZone = _segmentZones[_tourOriginParcel.ZoneId];

            _tripOriginParcel      = trip.OriginParcel;
            _tripOriginSegmentZone = _segmentZones[_tripOriginParcel.ZoneId];

            if (_tourOriginParcel == null || _tripOriginSegmentZone == null)
            {
            }

            if (chosenParcel != null)
            {
                _chosenParcel      = chosenParcel;
                _chosenSegmentZone = _segmentZones[chosenParcel.ZoneId];
            }

            if (_choiceProbabilityCalculator.ModelIsInEstimationMode && chosenParcel == null)
            {
                throw new ChosenParcelNotSetInEstimationModeException();
            }
        }
Example #4
0
        public static SegmentZone[] Deserialize(Stream serializationStream)
        {
            using (var reader = new BinaryReader(serializationStream)) {
                var totalSegmentZones = reader.ReadInt32();
                var arraySize         = reader.ReadInt32();
                var segmentZones      = new SegmentZone[arraySize];

                for (var i = 0; i < totalSegmentZones; i++)
                {
                    var index = reader.ReadInt32();

                    segmentZones[index] = SegmentZone.Load(reader);
                }

                return(segmentZones);
            }
        }
Example #5
0
        public DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segment, int sampleSize, IParcel originParcel, IParcel chosenParcel)
        {
            _choiceProbabilityCalculator = choiceProbabilityCalculator;
            _segmentZones = Global.SegmentZones[segment];
            _sampleSize   = sampleSize;

            _originParcel      = originParcel;
            _originSegmentZone = _segmentZones[originParcel.ZoneId];

            if (chosenParcel != null)
            {
                _chosenParcel      = chosenParcel;
                _chosenSegmentZone = _segmentZones[chosenParcel.ZoneId];
            }

            //JLB 20120329 removed these lines because usual work location model doesn't set a sampled dest to chosen when it is the residence location
            //			if (_choiceProbabilityCalculator.ModelIsInEstimationMode && chosenParcel == null) {
            //				throw new ChosenParcelNotSetInEstimationModeException();
            //			}
        }
Example #6
0
        public static void Serialize(Stream serializationStream, SegmentZone[] segmentZones)
        {
            using (var writer = new BinaryWriter(serializationStream)) {
                var totalSegmentZones = segmentZones.Count(x => x != null);
                var arraySize         = segmentZones.Length;

                writer.Write(totalSegmentZones);
                writer.Write(arraySize);

                for (var index = 0; index < arraySize; index++)
                {
                    if (segmentZones[index] == null)
                    {
                        continue;
                    }

                    writer.Write(index);

                    SegmentZone.Save(segmentZones[index], writer);
                }
            }
        }
Example #7
0
        public static SegmentZone Load(BinaryReader reader)
        {
            var parcelCount = reader.ReadInt32();
            var zoneCount   = reader.ReadInt32();

            var segmentZone = new SegmentZone(parcelCount, zoneCount)
            {
                Id          = reader.ReadInt32(),
                TotalSize   = reader.ReadDouble(),
                TotalWeight = reader.ReadDouble()
            };

            for (var i = 0; i < parcelCount; i++)
            {
                segmentZone._parcelSizes[i] = new ParcelSize(reader.ReadInt32(), reader.ReadDouble());
            }

            segmentZone._rankedSizeIndices = new int[parcelCount];

            for (var i = 0; i < parcelCount; i++)
            {
                segmentZone._rankedSizeIndices[i] = reader.ReadInt32();
            }

            for (var i = 0; i < zoneCount; i++)
            {
                segmentZone._zoneWeights[i] = reader.ReadDouble();
            }

            segmentZone._rankedWeightIndices = new int[zoneCount];

            for (var i = 0; i < zoneCount; i++)
            {
                segmentZone._rankedWeightIndices[i] = reader.ReadInt32();
            }

            return(segmentZone);
        }
Example #8
0
 public abstract void Initialize(DestinationSampler destinationSampler, int destinationParcelId, int destinationParcelSequence, SegmentZone destinationSegmentZone);
Example #9
0
            public override void Initialize(DestinationSampler destinationSampler, int destinationParcelId, int destinationParcelSequence, SegmentZone destinationSegmentZone)
            {
                _tourWeightFromOrigin      = destinationSampler._tourOriginSegmentZone.GetWeight(destinationSegmentZone.Id);
                _totalTourWeightFromOrigin = destinationSampler._tourOriginSegmentZone.TotalWeight;
                _tripWeightFromOrigin      = destinationSampler._tripOriginSegmentZone.GetWeight(destinationSegmentZone.Id);
                _totalTripWeightFromOrigin = destinationSampler._tripOriginSegmentZone.TotalWeight;

                TotalWeightFromDestination = destinationSegmentZone.TotalWeight;

                //if (Global.Configuration.DestinationScale == Constants.DestinationScale.ZONE) {
                //	return;
                //}

                ParcelId                 = destinationParcelId;
                SizeFromDestination      = destinationSegmentZone.GetSize(destinationParcelSequence);
                TotalSizeFromDestination = destinationSegmentZone.TotalSize;
            }
Example #10
0
        private TSampleItem GetDestination <TSampleItem>(RandomUniform01 randomUniform01, IParcel originParcel, SegmentZone originSegmentZone, IParcel excludedParcel, SegmentZone excludedSegmentZone) where TSampleItem : ISampleItem, new()
        {
            var         destinationZoneId      = 0;
            SegmentZone destinationSegmentZone = null;

            var destinationParcelSequence = 0;
            var destinationParcelId       = 0;
            var destinationParcelIsValid  = false;

            var random = randomUniform01.Uniform01() * originSegmentZone.TotalWeight;

            if (random > .001)
            {
                var total = 0D;

                // draw the zone
                foreach (var weight in originSegmentZone.RankedWeights)
                {
                    total += weight.Value;

                    if (total <= random)
                    {
                        continue;
                    }

                    destinationZoneId      = weight.Id;
                    destinationSegmentZone = _segmentZones[weight.Id];

                    break;
                }
            }

            if (destinationSegmentZone != null && destinationSegmentZone.Key == 0)
            {
            }

            if (destinationSegmentZone == null)
            {
                destinationZoneId      = originParcel.ZoneId;
                destinationSegmentZone = originSegmentZone;
            }

            var excludedSize = 0D;

            if (Global.Settings.DestinationScale == Global.Settings.DestinationScales.Parcel)
            {
                if (destinationZoneId == originParcel.ZoneId)
                {
                    excludedSize += originSegmentZone.GetSize(originParcel.Sequence);
                }

                if (excludedParcel != null && destinationZoneId == excludedParcel.ZoneId)
                {
                    excludedSize += excludedSegmentZone.GetSize(excludedParcel.Sequence);
                }
            }
            if (Global.Settings.DestinationScale == Global.Settings.DestinationScales.Zone)
            {
                if (destinationSegmentZone.Key == 0)
                {
                }
                destinationParcelId       = destinationSegmentZone.Key;
                destinationParcelSequence = 0;
                destinationParcelIsValid  = true;
            }
            else
            {
                //				if (destinationSegmentZone.TotalSize - excludedSize < Constants.EPSILON) {
                //					Console.WriteLine(originSegmentZone.Id);
                //					Console.WriteLine(originSegmentZone.TotalWeight);
                //					Console.WriteLine(originSegmentZone.TotalSize);
                //					Console.WriteLine(destinationSegmentZone.Id);
                //					Console.WriteLine(destinationSegmentZone.TotalWeight);
                //					Console.WriteLine(destinationSegmentZone.TotalSize);
                //					Console.WriteLine(excludedSize);
                //				}

                random = randomUniform01.Uniform01() * (destinationSegmentZone.TotalSize - excludedSize);

                if (random > .001)
                {
                    var total = 0D;

                    // draw the parcel within zone
                    foreach (var size in destinationSegmentZone.RankedSizes)
                    {
                        if (Global.Settings.DestinationScale == Global.Settings.DestinationScales.MicroZone ||
                            (originParcel.Id != size.Id && (excludedParcel == null || excludedParcel.Id != size.Id)))
                        {
                            total += size.Value;
                        }

                        if (total <= random)
                        {
                            continue;
                        }

                        // don't include the drawn parcel if the parcel has no size
                        if (size.Value >= Global.Configuration.MinParcelSize)
                        {
                            destinationParcelId       = size.Id;
                            destinationParcelSequence = size.Sequence;
                            destinationParcelIsValid  = true;
                        }

                        break;
                    }
                }
            }

            if (Global.Settings.DestinationScale != Global.Settings.DestinationScales.Zone && !destinationParcelIsValid)
            {
                return(default(TSampleItem));
            }

            var sampleItem = new TSampleItem();

            sampleItem.Initialize(this, destinationParcelId, destinationParcelSequence, destinationSegmentZone);

            if (destinationParcelIsValid)
            {
                sampleItem.ExcludedSize = excludedSize;
            }

            return(sampleItem);
        }