Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        //PCA from JLB
        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();
            }
        }
Exemplo n.º 3
0
        public DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segmentZonesIndex, int sampleSize, IParcel chosenParcel, IParcel originParcel) : this(choiceProbabilityCalculator, segmentZonesIndex, sampleSize, chosenParcel) {
            _originParcel = originParcel;
            _originSegmentZone = _segmentZones[originParcel.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();
            //            }
        }
Exemplo n.º 4
0
        private const int NULL_DESTINATION_ERROR = NULL_DESTINATION_WARNING * 10;   //MUST BE A MULTIPLE OF NULL_DESTINATION_WARNING

        private DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segmentZonesIndex, int sampleSize, IParcel chosenParcel) {
            _choiceProbabilityCalculator = choiceProbabilityCalculator;
            _segmentZonesIndex = segmentZonesIndex;
            _segmentZones = Global.SegmentZones[_segmentZonesIndex];
            _sampleSize = sampleSize;

            if (chosenParcel != null) {
                _chosenParcel = chosenParcel;
                _chosenSegmentZone = _segmentZones[chosenParcel.ZoneId];
            }
        }
Exemplo n.º 5
0
        public DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segmentZonesIndex, int sampleSize, IParcel chosenParcel, IParcel originParcel, IParcel excludedParcel, IParcel usualParcel) : this(choiceProbabilityCalculator, segmentZonesIndex, sampleSize, chosenParcel, originParcel) {

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

            if (usualParcel != null) {
                _usualParcel = usualParcel;
                _usualSegmentZone = _segmentZones[usualParcel.ZoneId];
            }
            if (_choiceProbabilityCalculator.ModelIsInEstimationMode && chosenParcel == null) {
                throw new ChosenParcelNotSetInEstimationModeException();
            }
        }
Exemplo n.º 6
0
        public DestinationSampler(ChoiceProbabilityCalculator choiceProbabilityCalculator, int segmentZonesIndex, int sampleSize, IParcel chosenParcel, ITourWrapper tour, ITripWrapper trip) : this(choiceProbabilityCalculator, segmentZonesIndex, sampleSize, chosenParcel) {
            _tourOriginParcel = tour.OriginParcel;
            _tourOriginSegmentZone = _segmentZones[_tourOriginParcel.ZoneId];

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

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

            }

            if (_choiceProbabilityCalculator.ModelIsInEstimationMode && chosenParcel == null) {
                throw new ChosenParcelNotSetInEstimationModeException();
            }
        }
Exemplo n.º 7
0
        public static SegmentZone[] Deserialize(Stream serializationStream)
        {
            using (BinaryReader reader = new BinaryReader(serializationStream)) {
                int           totalSegmentZones = reader.ReadInt32();
                int           arraySize         = reader.ReadInt32();
                SegmentZone[] segmentZones      = new SegmentZone[arraySize];

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

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

                return(segmentZones);
            }
        }
Exemplo n.º 8
0
        public static void Serialize(Stream serializationStream, SegmentZone[] segmentZones)
        {
            using (BinaryWriter writer = new BinaryWriter(serializationStream)) {
                int totalSegmentZones = segmentZones.Count(x => x != null);
                int arraySize         = segmentZones.Length;

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

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

                    writer.Write(index);

                    SegmentZone.Save(segmentZones[index], writer);
                }
            }
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
 public abstract void Initialize(DestinationSampler destinationSampler, int destinationParcelId, int destinationParcelSequence, SegmentZone destinationSegmentZone);
Exemplo n.º 11
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;
            }
Exemplo n.º 12
0
        private TSampleItem GetDestination <TSampleItem>(RandomUniform01 randomUniform01, IParcel originParcel, SegmentZone originSegmentZone, IParcel excludedParcel, SegmentZone excludedSegmentZone) where TSampleItem : ISampleItem, new()
        {
            int         destinationZoneId      = 0;
            SegmentZone destinationSegmentZone = null;

            int  destinationParcelSequence = 0;
            int  destinationParcelId       = 0;
            bool destinationParcelIsValid  = false;

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

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

                // draw the zone
                foreach (WeightSegmentItem 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;
            }

            double excludedSize = 0D;

            if (Global.Configuration.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.Configuration.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)
                {
                    double total = 0D;

                    // draw the parcel within zone
                    foreach (SizeSegmentItem size in destinationSegmentZone.RankedSizes)
                    {
                        if (Global.Configuration.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.Configuration.DestinationScale != Global.Settings.DestinationScales.Zone && !destinationParcelIsValid)
            {
                return(default(TSampleItem));
            }

            TSampleItem sampleItem = new TSampleItem();

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

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

            return(sampleItem);
        }