예제 #1
0
        public static void CalculateTankShotPredictions(Point point, PredictionType type, Direction direction, BaseTank tank, int maxDepth = 1, List <Direction> command = null)
        {
            var startShotPoint = point;

            var startIndex = Math.Min(-1 - tank.ShotCountdownLeft, 1);

            //if (!tank.IsShotThisRound)
            //    return;


            for (var i = startIndex; i <= Bullet.DefaultSpeed * maxDepth; i++)
            {
                var shotPoint = BaseMobile.Shift(startShotPoint, direction);
                var shotCell  = Field.GetCell(shotPoint);

                var depth = (int)Math.Ceiling((decimal)i / 2);

                if (i >= -1)
                {
                    var actualDepth = Math.Max(1, tank.ShotCountdownLeft) + depth;
                    shotCell.AddPrediction(actualDepth, type, command);
                }

                if (shotCell.CanShootThrough)
                {
                    startShotPoint = shotPoint;
                }
                else
                {
                    break;
                }
            }
        }
예제 #2
0
        public static BasePrediction Get(PredictionType type)
        {
            switch (type)
            {
            case PredictionType.Bullet:
                return(new BulletPrediction());

            case PredictionType.AiMove:
                return(new AiMovePrediction());

            case PredictionType.AiShot:
                return(new AiShotPrediction());

            case PredictionType.EnemyMove:
                return(new EnemyMovePrediction());

            case PredictionType.EnemyShot:
                return(new EnemyShotPrediction());

            case PredictionType.MyShot:
                return(new MyShotPrediction());

            case PredictionType.MyMove:
                return(new MyMovePrediction());

            case PredictionType.MyKill:
                return(new MyKillPrediction());

            default:
                throw new NotImplementedException();
            }
        }
        private void ConfigurePredicitonType(PredictionType type)
        {
            switch (type)
            {
            case PredictionType.EnglandWales2017:
                pollingDataPath  = Path.Combine(Environment.CurrentDirectory, "Data", "yougov2017.csv");
                containsScotland = false;
                break;

            case PredictionType.Scotland2017:
                pollingDataPath  = Path.Combine(Environment.CurrentDirectory, "Data", "yougov2017Scot.csv");
                containsScotland = true;
                break;

            case PredictionType.EnglandWales2019:
                pollingDataPath  = Path.Combine(Environment.CurrentDirectory, "Data", "yougov2019.csv");
                containsScotland = false;
                break;

            case PredictionType.Scotland2019:
                pollingDataPath  = Path.Combine(Environment.CurrentDirectory, "Data", "yougov2019Scot.csv");
                containsScotland = true;
                break;

            default:
                throw new Exception($"Unhandled prediction type {type.ToString()}");
            }
        }
예제 #4
0
        public static Color?GetBorderColor(PredictionType type)
        {
            switch (type)
            {
            case PredictionType.MyShot:
                return(Color.DeepPink);

            case PredictionType.MyMove:
                return(Color.LawnGreen);

            case PredictionType.AiShot:
                return(Color.Coral);

            case PredictionType.Bullet:
                return(Color.LightCoral);

            case PredictionType.EnemyShot:
                return(Color.DarkOrchid);

            case PredictionType.AiMove:
                return(Color.Aqua);

            case PredictionType.EnemyMove:
                return(Color.Green);

            case PredictionType.MyKill:
                return(Color.Red);
            }

            return(null);
        }
        /// <summary>
        /// Valuta la singola predizione
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <param name="marketValue"></param>
        /// <returns></returns>
        protected int EvaluatePrediction(PredictionType type, decimal value, decimal marketValue)
        {
            switch (type)
            {
            case PredictionType.Unknown:
                return(0);

                break;

            case PredictionType.Equal:
                var tollerance = 0.01M;
                if (value * (1 + tollerance) >= marketValue && value * (1 - tollerance) <= marketValue)
                {
                    return(MAX_POINTS_PER_PREDICTION);
                }
                break;

            case PredictionType.GreatherThan:
                if (marketValue >= value)
                {
                    return(MAX_POINTS_PER_PREDICTION);
                }
                break;

            case PredictionType.LessThan:
                if (marketValue <= value)
                {
                    return(MAX_POINTS_PER_PREDICTION);
                }
                break;
            }

            return(0);
        }
예제 #6
0
 public static void SetVisible(PredictionType type, bool value)
 {
     if (Checkboxes.ContainsKey(type))
     {
         Checkboxes[type].Checked = value;
     }
 }
예제 #7
0
        private IEnumerable <BasePrediction> GetPredictionsByType(PredictionType type)
        {
            switch (type)
            {
            case PredictionType.DangerCell:
                return(DangerCellPredictions.Select(x => (BasePrediction)x));

            case PredictionType.MyKill:
                return(MyKillPredictions.Select(x => (BasePrediction)x));

            case PredictionType.MyShot:
                return(MyShotPredictions.Select(x => (BasePrediction)x));

            case PredictionType.MyMove:
                return(MyMovePredictions.Select(x => (BasePrediction)x));

            case PredictionType.AiShot:
                return(AiShotPredictions.Select(x => (BasePrediction)x));

            case PredictionType.Bullet:
                return(BulletPredictions.Select(x => (BasePrediction)x));

            case PredictionType.AiMove:
                return(AiMovePredictions.Select(x => (BasePrediction)x));

            case PredictionType.EnemyShot:
                return(EnemyShotPredictions.Select(x => (BasePrediction)x));

            case PredictionType.EnemyMove:
                return(EnemyMovePredictions.Select(x => (BasePrediction)x));

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
예제 #8
0
        public static Brush GetTextColor(PredictionType type)
        {
            switch (type)
            {
            case PredictionType.MyShot:
                return(Brushes.DeepPink);

            case PredictionType.MyMove:
                return(Brushes.LawnGreen);

            case PredictionType.AiShot:
                return(Brushes.Coral);

            case PredictionType.Bullet:
                return(Brushes.LightCoral);

            case PredictionType.EnemyShot:
                return(Brushes.DarkOrchid);

            case PredictionType.AiMove:
                return(Brushes.Aqua);

            case PredictionType.EnemyMove:
                return(Brushes.Green);

            case PredictionType.MyKill:
                return(Brushes.Red);
            }

            return(null);
        }
예제 #9
0
파일: Prediction.cs 프로젝트: Mej0/a
        public static Vector3 PredictPosition(Unit target, int time, bool noTurning = false,
                                              PredictionType type = PredictionType.GridNav)
        {
            var maxDistance = time / 1000.0f * target.MovementSpeed + target.HullRadius;

            if (noTurning && IsRotating(target)) // max distance checking? -> todo: testing
            {
                return(Vector3.Zero);
            }

            if (!target.IsMoving)
            {
                return(target.NetworkPosition);
            }

            var inFront = Ensage.Common.Prediction.InFront(target, maxDistance);

            inFront.Z = target.NetworkPosition.Z;

            if (time <= 400 || true)
            {
                Log.Debug($"using infront {inFront}");
                return(inFront);
            }
            // TODO: better -> fix it
            bool completed;
            var  path = Pathfinding.CalculateStaticLongPath(
                target.NetworkPosition,
                inFront * 1.5f,
                target.MovementSpeed * time * 4,
                true,
                out completed).ToList();

            if (!completed)
            {
                Log.Debug($"using gridnav not completed {inFront}");
                return(inFront);
            }

            var distance = 0.0f;
            var lastNode = Vector3.Zero;

            for (var i = 0; i < path.Count; ++i)
            {
                var len = i == 0 ? (path[i] - target.NetworkPosition).Length() : (path[i] - path[i - 1]).Length();
                lastNode = path[i];
                if (maxDistance < len + distance)
                {
                    break;
                }

                distance += len;
            }

            var dir = lastNode.Normalized();

            dir *= maxDistance - distance;
            Log.Debug($"using gridnav  {lastNode + dir}");
            return(lastNode + dir);
        }
예제 #10
0
        private double[] ConvertToValidInputType(object inputData)
        {
            if (inputData.GetType() == PredictionType)
            {
                return((double[])inputData);
            }

            // if not exact same type, try parsing as double
            if (!AI.Utils.Types.IsList(inputData))
            {
                throw new ArgumentException("Type cannot be converted");
            }
            var inputList  = (IList)inputData;
            var outputList = new double[inputList.Count];

            for (int i = 0; i < inputList.Count; i++)
            {
                double parsed;
                if (!double.TryParse(inputList[i].ToString(), out parsed))
                {
                    throw new Exception(
                              "Input data type is not valid and conversion failed." + Environment.NewLine +
                              "Supplied : " + inputData.GetType().ToString() + Environment.NewLine +
                              "Expected : " + PredictionType.ToString());
                }
                else
                {
                    outputList[i] = parsed;
                }
            }
            return(outputList);
        }
예제 #11
0
파일: Cell.cs 프로젝트: led318/tank-bot
        public BasePrediction AddPrediction(int depth, PredictionType type, List <Direction> command = null, BaseItem item = null)
        {
            var addedPrediction = Predictions.Add(type, depth, Point, command, item);

            IsDirty = true;

            return(addedPrediction);
        }
예제 #12
0
        public static void CalculateStuckPosition(BaseMobile mobileItem, PredictionType type, int maxDepth)
        {
            var cell = Field.GetCell(mobileItem.Point);

            for (var depth = 1; depth <= maxDepth; depth++)
            {
                cell.AddPrediction(depth, type, item: mobileItem);
            }
        }
예제 #13
0
        public static bool GetVisible(PredictionType type)
        {
            if (Checkboxes.ContainsKey(type))
            {
                return(Checkboxes[type].Checked);
            }

            return(false);
        }
 private void LoadProbabilities(PredictionType type)
 {
     ConfigurePredicitonType(type);
     LoadPreviousVoteProbabilities();
     LoadAgeProbabilities();
     LoadGenderProbabilities();
     LoadReferendumProbabilities();
     LoadRegionProbabilities();
     LoadSocialGradeProbabilies();
 }
예제 #15
0
 internal WeaponBarrelsBase(BinaryReader binaryReader)
 {
     this.flags                       = (Flags)binaryReader.ReadInt32();
     this.roundsPerSecond             = binaryReader.ReadRange();
     this.accelerationTimeSeconds     = binaryReader.ReadSingle();
     this.decelerationTimeSeconds     = binaryReader.ReadSingle();
     this.barrelSpinScale             = binaryReader.ReadSingle();
     this.blurredRateOfFire           = binaryReader.ReadSingle();
     this.shotsPerFire                = binaryReader.ReadInt32();
     this.fireRecoveryTimeSeconds     = binaryReader.ReadSingle();
     this.softRecoveryFraction        = binaryReader.ReadSingle();
     this.magazine                    = binaryReader.ReadShortBlockIndex1();
     this.roundsPerShot               = binaryReader.ReadInt16();
     this.minimumRoundsLoaded         = binaryReader.ReadInt16();
     this.roundsBetweenTracers        = binaryReader.ReadInt16();
     this.optionalBarrelMarkerName    = binaryReader.ReadStringID();
     this.predictionType              = (PredictionType)binaryReader.ReadInt16();
     this.firingNoise                 = (FiringNoiseHowLoudThisWeaponAppearsToTheAI)binaryReader.ReadInt16();
     this.accelerationTimeSeconds0    = binaryReader.ReadSingle();
     this.decelerationTimeSeconds0    = binaryReader.ReadSingle();
     this.damageError                 = binaryReader.ReadRange();
     this.accelerationTimeSeconds1    = binaryReader.ReadSingle();
     this.decelerationTimeSeconds1    = binaryReader.ReadSingle();
     this.invalidName_                = binaryReader.ReadBytes(8);
     this.minimumErrorDegrees         = binaryReader.ReadSingle();
     this.errorAngleDegrees           = binaryReader.ReadRange();
     this.dualWieldDamageScale        = binaryReader.ReadSingle();
     this.distributionFunction        = (DistributionFunction)binaryReader.ReadInt16();
     this.projectilesPerShot          = binaryReader.ReadInt16();
     this.distributionAngleDegrees    = binaryReader.ReadSingle();
     this.minimumErrorDegrees0        = binaryReader.ReadSingle();
     this.errorAngleDegrees0          = binaryReader.ReadRange();
     this.firstPersonOffsetWorldUnits = binaryReader.ReadVector3();
     this.damageEffectReportingType   = (DamageEffectReportingType)binaryReader.ReadByte();
     this.invalidName_0               = binaryReader.ReadBytes(3);
     this.projectile                  = binaryReader.ReadTagReference();
     this.eh = new WeaponBarrelDamageEffectStructBlock(binaryReader);
     this.ejectionPortRecoveryTime = binaryReader.ReadSingle();
     this.illuminationRecoveryTime = binaryReader.ReadSingle();
     this.heatGeneratedPerRound01  = binaryReader.ReadSingle();
     this.ageGeneratedPerRound01   = binaryReader.ReadSingle();
     this.overloadTimeSeconds      = binaryReader.ReadSingle();
     this.angleChangePerShot       = binaryReader.ReadRange();
     this.accelerationTimeSeconds2 = binaryReader.ReadSingle();
     this.decelerationTimeSeconds2 = binaryReader.ReadSingle();
     this.angleChangeFunction      = (AngleChangeFunctionFunctionUsedToScaleBetweenInitialAndFinalAngleChangePerShot)binaryReader.ReadInt16();
     this.invalidName_1            = binaryReader.ReadBytes(2);
     this.invalidName_2            = binaryReader.ReadBytes(8);
     this.invalidName_3            = binaryReader.ReadBytes(24);
     this.firingEffects            = ReadBarrelFiringEffectBlockArray(binaryReader);
 }
예제 #16
0
        public static BasePrediction Get(PredictionType type, int depth, Point point, List <Direction> command, BaseItem item)
        {
            var prediction = Get(type);

            prediction.Depth = depth;
            prediction.Point = point;
            prediction.Item  = item;

            if (command != null)
            {
                prediction.Commands.AddRange(command);
            }

            return(prediction);
        }
예제 #17
0
        public BasePrediction Add(PredictionType type, int depth, Point point, List <Command> commands = null, BaseItem item = null)
        {
            var prediction = PredictionFactory.Get(type, depth, point, commands, item);

            switch (type)
            {
            case PredictionType.AiMove:
                AiMovePredictions.Add((AiMovePrediction)prediction);
                break;

            case PredictionType.AiShot:
                AiShotPredictions.Add((AiShotPrediction)prediction);
                break;

            case PredictionType.Bullet:
                BulletPredictions.Add((BulletPrediction)prediction);
                break;

            case PredictionType.EnemyMove:
                EnemyMovePredictions.Add((EnemyMovePrediction)prediction);
                break;

            case PredictionType.EnemyShot:
                EnemyShotPredictions.Add((EnemyShotPrediction)prediction);
                break;

            case PredictionType.MyMove:
                MyMovePredictions.Add((MyMovePrediction)prediction);
                break;

            case PredictionType.MyShot:
                MyShotPredictions.Add((MyShotPrediction)prediction);
                break;

            case PredictionType.MyKill:
                MyKillPredictions.Add((MyKillPrediction)prediction);
                break;

            case PredictionType.DangerCell:
                DangerCellPredictions.Add((DangerCellPrediction)prediction);
                break;

            default:
                throw new NotImplementedException();
            }

            return(prediction);
        }
예제 #18
0
        public static Vector3 PredictPosition(Unit target, int time, PredictionType type = PredictionType.GridNav)
        {
            var maxDistance = (time / 1000.0f) * target.MovementSpeed;
            var inFront     = Ensage.Common.Prediction.InFront(target, maxDistance);

            if (time <= 500)
            {
                return(inFront);
            }

            bool completed;
            var  path = Pathfinding.CalculateStaticLongPath(
                target.NetworkPosition,
                inFront * 1.5f,
                target.MovementSpeed * time * 4,
                true,
                out completed).ToList();

            if (!completed)
            {
                return(inFront);
            }

            var distance = 0.0f;
            var lastNode = Vector3.Zero;

            for (var i = 0; i < path.Count; ++i)
            {
                var len = i == 0 ? (path[i] - target.NetworkPosition).Length() : (path[i] - path[i - 1]).Length();
                lastNode = path[i];
                if (maxDistance < len + distance)
                {
                    break;
                }

                distance += len;
            }

            var dir = lastNode.Normalized();

            dir *= maxDistance - distance;
            return(lastNode + dir);
        }
예제 #19
0
        private (DateTime, DateTime) GetDateRange(PredictionType predictionType)
        {
            var today         = DateTime.Today.ToUniversalTime().Date;
            var predictionEnd = today;

            switch (predictionType)
            {
            case PredictionType.Weekly:
                predictionEnd = today.AddDays(7);
                break;

            case PredictionType.Monthly:
                predictionEnd = today.AddDays(30);
                break;

            case PredictionType.Yearly:
                predictionEnd = today.AddDays(365);
                break;
            }

            return(today, predictionEnd);
        }
        private double ConvertToValidInputType(object inputData)
        {
            if (inputData.GetType() == PredictionType)
            {
                return((double)inputData);
            }

            // if not exact same type, try parsing as double
            var parsed = new double();

            if (!double.TryParse(inputData.ToString(), out parsed))
            {
                throw new Exception(
                          "Input data type is not valid and conversion failed." + Environment.NewLine +
                          "Supplied : " + inputData.GetType().ToString() + Environment.NewLine +
                          "Expected : " + PredictionType.ToString());
            }
            else
            {
                return(parsed);
            }
        }
예제 #21
0
        private static void GetPrediction()
        {
            switch (PredictionType.GetValue <StringList>().SelectedIndex)
            {
            case 0:
                if (Target.UnitState.HasFlag(UnitState.Hexed))
                {
                    Mysticflare.UseAbility(Prediction.InFront(Target, 87));
                    break;
                }
                Mysticflare.UseAbility(Prediction.InFront(Target, 100));
                break;

            case 1:
                if (Target.UnitState.HasFlag(UnitState.Hexed))
                {
                    Mysticflare.UseAbility(Prediction.PredictedXYZ(Target, 210 / Target.MovementSpeed * 1000));
                    break;
                }
                Mysticflare.UseAbility(Prediction.PredictedXYZ(Target, 230 / Target.MovementSpeed * 1000));
                break;
            }
        }
예제 #22
0
 public void Validate()
 {
     if (this.GetEncoderModeIndex() < 0)
     {
         throw new Exception("unsupported encoder mode");
     }
     this.SetDefaultValuesForMode();
     if (Padding < 0)
     {
         throw new Exception("unsupported padding value " + Padding.ToString());
     }
     if (BlockSize != 0 && (BlockSize < 256 || BlockSize >= FlakeConstants.MAX_BLOCKSIZE))
     {
         throw new Exception("unsupported block size " + BlockSize.ToString());
     }
     if (MinLPCOrder > MaxLPCOrder || MaxLPCOrder > lpc.MAX_LPC_ORDER)
     {
         throw new Exception("invalid MaxLPCOrder " + MaxLPCOrder.ToString());
     }
     if (MinFixedOrder < 0 || MinFixedOrder > 4)
     {
         throw new Exception("invalid MinFixedOrder " + MinFixedOrder.ToString());
     }
     if (MaxFixedOrder < 0 || MaxFixedOrder > 4)
     {
         throw new Exception("invalid MaxFixedOrder " + MaxFixedOrder.ToString());
     }
     if (MinPartitionOrder < 0)
     {
         throw new Exception("invalid MinPartitionOrder " + MinPartitionOrder.ToString());
     }
     if (MinPartitionOrder > MaxPartitionOrder || MaxPartitionOrder > 8)
     {
         throw new Exception("invalid MaxPartitionOrder " + MaxPartitionOrder.ToString());
     }
     if (PredictionType == PredictionType.None)
     {
         throw new Exception("invalid PredictionType " + PredictionType.ToString());
     }
     if (PredictionType != PredictionType.Fixed)
     {
         if (WindowMethod == WindowMethod.Invalid)
         {
             throw new InvalidOperationException("invalid WindowMethod " + WindowMethod.ToString());
         }
         if (WindowFunctions == WindowFunction.None)
         {
             throw new InvalidOperationException("invalid WindowFunctions " + WindowFunctions.ToString());
         }
         if (EstimationDepth > 32 || EstimationDepth < 1)
         {
             throw new InvalidOperationException("invalid EstimationDepth " + EstimationDepth.ToString());
         }
         if (MinPrecisionSearch < 0 || MinPrecisionSearch >= lpc.MAX_LPC_PRECISIONS)
         {
             throw new Exception("unsupported MinPrecisionSearch value");
         }
         if (MaxPrecisionSearch < 0 || MaxPrecisionSearch >= lpc.MAX_LPC_PRECISIONS)
         {
             throw new Exception("unsupported MaxPrecisionSearch value");
         }
         if (MaxPrecisionSearch < MinPrecisionSearch)
         {
             throw new Exception("unsupported MaxPrecisionSearch value");
         }
     }
     if (!AllowNonSubset && !IsSubset())
     {
         throw new Exception("the encoding parameters specified do not conform to the FLAC Subset");
     }
 }
예제 #23
0
        public int flake_set_defaults(FlakeWriterSettings settings)
        {
            // default to level 7 params
            window_function = WindowFunction.Flattop | WindowFunction.Tukey;
            order_method = OrderMethod.Akaike;
            stereo_method = StereoMethod.Evaluate;
            window_method = WindowMethod.Evaluate;
            block_time_ms = 105;			
            prediction_type = PredictionType.Search;
            estimation_depth = 1;
            variable_block_size = 0;
            lpc_min_precision_search = 1;
            lpc_max_precision_search = 1;
            do_seektable = true;
            development_mode = -1;

            // differences from level 7
            switch (settings.EncoderModeIndex)
            {
                case 0:
                    block_time_ms = 53;
                    prediction_type = PredictionType.Fixed;
                    stereo_method = StereoMethod.Independent;
                    break;
                case 1:
                    prediction_type = PredictionType.Levinson;
                    stereo_method = StereoMethod.Independent;
                    window_function = WindowFunction.Bartlett;
                    break;
                case 2:
                    stereo_method = StereoMethod.Independent;
                    window_function = WindowFunction.Bartlett;
                    break;
                case 3:
                    stereo_method = StereoMethod.Estimate;
                    window_function = WindowFunction.Bartlett;
                    break;
                case 4:
                    stereo_method = StereoMethod.Estimate;
                    window_function = WindowFunction.Bartlett;
                    break;
                case 5:
                    stereo_method = StereoMethod.Estimate;
                    window_method = WindowMethod.Estimate;
                    break;
                case 6:
                    stereo_method = StereoMethod.Estimate;
                    break;
                case 7:
                    break;
                case 8:
                    estimation_depth = 2;
                    lpc_min_precision_search = 0;
                    break;
                case 9:
                    window_function = WindowFunction.Bartlett;
                    break;
                case 10:
                    //lpc_max_precision_search = 2;
                    break;
                case 11:
                    estimation_depth = 5;
                    //lpc_max_precision_search = 2;
                    variable_block_size = 4;
                    break;
            }

            return 0;
        }
예제 #24
0
 public static void CalculateTanksShotPredictions(IEnumerable <BaseTank> tanks, PredictionType type, int maxDepth = 1)
 {
     foreach (var tank in tanks)
     {
         if (tank.CurrentDirection.HasValue)
         {
             CalculateTankShotPredictions(tank.Point, type, tank.CurrentDirection.Value, tank, maxDepth);
         }
     }
 }
예제 #25
0
		public int flake_set_defaults(int lvl)
		{
			compression = lvl;

			if ((lvl < 0 || lvl > 12) && (lvl != 99))
			{
				return -1;
			}

			// default to level 5 params
			window_function = WindowFunction.Flattop | WindowFunction.Tukey;
			order_method = OrderMethod.Akaike;
			stereo_method = StereoMethod.Evaluate;
			window_method = WindowMethod.Evaluate;
			block_size = 0;
			block_time_ms = 105;			
			prediction_type = PredictionType.Search;
			min_prediction_order = 1;
			max_prediction_order = 12;
			estimation_depth = 1;
			min_fixed_order = 2;
			max_fixed_order = 2;
			min_partition_order = 0;
			max_partition_order = 8;
			variable_block_size = 0;
			lpc_min_precision_search = 1;
			lpc_max_precision_search = 1;
			do_seektable = true; 

			// differences from level 7
			switch (lvl)
			{
				case 0:
					block_time_ms = 53;
					prediction_type = PredictionType.Fixed;
					stereo_method = StereoMethod.Independent;
					max_partition_order = 6;
					break;
				case 1:
					prediction_type = PredictionType.Levinson;
					stereo_method = StereoMethod.Independent;
					window_function = WindowFunction.Bartlett;
					max_prediction_order = 8;
					max_partition_order = 6;
					break;
				case 2:
					stereo_method = StereoMethod.Independent;
					window_function = WindowFunction.Bartlett;
					max_partition_order = 6;
					break;
				case 3:
					stereo_method = StereoMethod.Estimate;
					window_function = WindowFunction.Bartlett;
					max_prediction_order = 8;
					break;
				case 4:
					stereo_method = StereoMethod.Estimate;
					window_function = WindowFunction.Bartlett;
					break;
				case 5:
					stereo_method = StereoMethod.Estimate;
					window_method = WindowMethod.Estimate;
					break;
				case 6:
					stereo_method = StereoMethod.Estimate;
					break;
				case 7:
					break;
				case 8:
					estimation_depth = 2;
					min_fixed_order = 0;
					lpc_min_precision_search = 0;
					break;
				case 9:
					window_function = WindowFunction.Bartlett;
					max_prediction_order = 32;
					break;
				case 10:
					min_fixed_order = 0;
					max_fixed_order = 4;
					max_prediction_order = 32;
					//lpc_max_precision_search = 2;
					break;
				case 11:
					min_fixed_order = 0;
					max_fixed_order = 4;
					max_prediction_order = 32;
					estimation_depth = 5;
					//lpc_max_precision_search = 2;
					variable_block_size = 4;
					break;
			}

			return 0;
		}
예제 #26
0
        private async Task <List <Data> > LatestPredictions(
            PredictionType type,
            int deleteAfterMinutes,                   // if data is older than X minutes delete
            int lookaheadMinutes,                     // when querying request data X minutes in the future
            int cacheInvalidationMinutes,             // invalidate the cache after X minutes
            int pullMoreMinutes,                      // request more data if the newest data is older X minutes old
            Func <DateTime, DateTime, Task <List <string> > > retrieveAdditional
            )
        {
            PredictionDetails details = null;

            lock (AllData)
            {
                // get prediction
                if (!AllData.TryGetValue(type, out details))
                {
                    details = new PredictionDetails();
                    AllData.Add(type, details);
                }
            }

            // init
            var now            = DateTime.UtcNow;
            var past           = now.AddMinutes(-1 * Math.Abs(deleteAfterMinutes));
            var future         = now.AddMinutes(lookaheadMinutes);
            var datetimewindow = now.AddMinutes(pullMoreMinutes);

            // check if we have a cahce for this prediction
            lock (details)
            {
                if (details.Cache != null && details.Cache.Count > 0 && details.Timer.IsRunning && details.Timer.Elapsed.TotalMinutes < cacheInvalidationMinutes)
                {
                    // return this early
                    return(details.Cache);
                }
            }

            // ensure we have x hours worth of data
            var tries   = 3;
            var results = new List <Data>();

            do
            {
                List <string> remove = null;
                var           latest = default(DateTime);

                // iterate through results
                //  1. identify results that can be removed
                //  2. get the latest datetime stamp
                //  3. capture results that will ve returned
                lock (details)
                {
                    foreach (var kvp in details.Data)
                    {
                        // 1. too old
                        if (kvp.Value.Date < past)
                        {
                            if (remove == null)
                            {
                                remove = new List <string>();
                            }
                            remove.Add(kvp.Key);
                        }
                        // 2. capture latest datetimestamp
                        if (kvp.Value.Date > latest)
                        {
                            latest = kvp.Value.Date;
                        }
                        // 3. results
                        if (kvp.Value.Date >= past)
                        {
                            results.Add(kvp.Value);
                        }
                    }
                }

                // query for results
                if (latest == default(DateTime) || results.Count == 0 || datetimewindow > latest)
                {
                    // this call likely involves a network call, which may fail
                    // in order to be resilent to network failures, in that case
                    // no data is returned
                    List <string> latestjson = null;
                    try
                    {
                        latestjson = await retrieveAdditional(past, future);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Catastrophic failure during get - {e}");
                        return(new List <Data>());
                    }

                    // query for more results
                    foreach (var json in latestjson)
                    {
                        try
                        {
                            var data = ParseJson(json, type);

                            lock (details)
                            {
                                foreach (var p in data)
                                {
                                    // add it to the set of predictions
                                    var key = $"{p.Date:yyyyMMdd HH mm ss fff}";
                                    if (!details.Data.ContainsKey(key))
                                    {
                                        details.Data.Add(key, p);
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine($"Failure during parse - {e}");
                        }
                    }

                    // clear so that we capture again
                    results.Clear();
                }

                // remove the items that are too old
                if (remove != null && remove.Count > 0)
                {
                    lock (details)
                    {
                        foreach (var key in remove)
                        {
                            details.Data.Remove(key);
                        }
                    }
                }

                // we failed to successfully get data, return none
                if (--tries == 0)
                {
                    return(new List <Data>());
                }
            }while (results.Count == 0);

            // update the details with the cache
            lock (details)
            {
                details.Cache = results;
                details.Timer.Restart();

                return(details.Cache);
            }
        }
예제 #27
0
        private List <Data> ParseJson(string json, PredictionType type)
        {
            if (string.IsNullOrWhiteSpace(json))
            {
                throw new Exception("Failed to get json");
            }

            // parse json
            var bytes  = System.Text.Encoding.UTF8.GetBytes(json);
            var reader = new Utf8JsonReader(bytes, true, default(JsonReaderState));

            var sunriseseen          = false;
            var sunsetseen           = false;
            var tseen                = false;
            var vseen                = false;
            var typeseen             = false;
            var forecasthourlyseen   = false;
            var endtimeseen          = false;
            var temperatureseen      = false;
            var windspeedseen        = false;
            var winddirectionseen    = false;
            var shortforecastseen    = false;
            var temperaturetrendseen = false;
            var utcdateseen          = false;
            var signalseen           = false;
            var lowbatteryseen       = false;
            var raintotalseen        = false;
            var outtemperatureseen   = false;
            var outhumidityseen      = false;
            var pressureseen         = false;
            var intemperatureseen    = false;
            var collectweather       = false;
            var pressuretrendseen    = false;
            var raintotaltrendseen   = false;
            var results              = new List <Data>();
            var p              = default(Data);
            var min            = default(Data);
            var max            = default(Data);
            var pressuretrend  = new List <float>();
            var raintotaltrend = new List <float>();
            var count          = 0;

            while (reader.Read())
            {
                //
                // noah data
                //
                if (type == PredictionType.Extremes || type == PredictionType.Tides)
                {
                    tseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_T));
                    if (tseen && reader.TokenType == JsonTokenType.String)
                    {
                        // add it if there is already a prediction in flight
                        if (count > 0)
                        {
                            results.Add(p);
                        }
                        count++;

                        var t = System.Text.Encoding.UTF8.GetString(reader.ValueSpan);

                        // incoming datetimes are GMT/UTC
                        var date = DateTime.Parse(t, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
                        p = new Data()
                        {
                            Date = date, Type = ""
                        };

                        tseen = false;
                    }

                    vseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_V));
                    if (vseen && reader.TokenType == JsonTokenType.String)
                    {
                        p.Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan));
                        vseen   = false;
                    }

                    typeseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_Type));
                    if (typeseen && reader.TokenType == JsonTokenType.String)
                    {
                        p.Type   = System.Text.Encoding.UTF8.GetString(reader.ValueSpan);
                        typeseen = false;
                    }
                }

                //
                // sunrise/sunset data
                //
                if (type == PredictionType.Suns)
                {
                    sunriseseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_Sunrise));
                    if (sunriseseen && reader.TokenType == JsonTokenType.String)
                    {
                        count++;
                        var t = System.Text.Encoding.UTF8.GetString(reader.ValueSpan);

                        // incoming datetimes are GMT/UTC
                        var date = DateTime.Parse(t, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
                        results.Add(new Data()
                        {
                            Date = date, Type = "sunrise"
                        });

                        sunriseseen = false;
                    }
                    sunsetseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_Sunset));
                    if (sunsetseen && reader.TokenType == JsonTokenType.String)
                    {
                        count++;
                        var t = System.Text.Encoding.UTF8.GetString(reader.ValueSpan);

                        // incoming datetimes are GMT/UTC
                        var date = DateTime.Parse(t, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal);
                        results.Add(new Data()
                        {
                            Date = date, Type = "sunset"
                        });

                        sunsetseen = false;
                    }
                }

                //
                // weather
                //
                if (type == PredictionType.WeatherInfo)
                {
                    forecasthourlyseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_ForecastHourly));
                    if (forecasthourlyseen && reader.TokenType == JsonTokenType.String)
                    {
                        results.Add(new Data()
                        {
                            StrValue = System.Text.Encoding.UTF8.GetString(reader.ValueSpan)
                        });
                        forecasthourlyseen = false;
                        break;
                    }
                }

                if (type == PredictionType.Weather)
                {
                    endtimeseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_EndTime));
                    if (endtimeseen && reader.TokenType == JsonTokenType.String)
                    {
                        // only capture the current weather
                        if (p.Date != default(DateTime) || min.Date != default(DateTime) || max.Date != default(DateTime))
                        {
                            collectweather = false;
                            continue;
                        }

                        var t = System.Text.Encoding.UTF8.GetString(reader.ValueSpan);

                        // incoming datetimes are local
                        var date = DateTime.Parse(t, CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal);

                        // check if the end date is in the future
                        if (DateTime.UtcNow < date)
                        {
                            p = new Data()
                            {
                                Date = date, Type = "weather"
                            };
                            min = new Data()
                            {
                                Date = date.AddSeconds(6), Type = "temperaturelow", Value = Single.MaxValue
                            };
                            max = new Data()
                            {
                                Date = date.AddSeconds(7), Type = "temperaturehigh", Value = Single.MinValue
                            };
                            collectweather = true;
                        }

                        endtimeseen = false;
                    }

                    temperatureseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_Temperature));
                    if (temperatureseen && reader.TokenType == JsonTokenType.Number)
                    {
                        var temp = 0f;
                        reader.TryGetSingle(out temp);
                        if (collectweather && p.Date != default(DateTime))
                        {
                            results.Add(new Data()
                            {
                                Date = p.Date.AddSeconds(1), Value = temp, Type = "temperature"
                            });
                        }
                        if (min.Date != default(DateTime) && min.Date.AddHours(24) >= p.Date)
                        {
                            min.Value = Math.Min(min.Value, temp);
                        }
                        if (max.Date != default(DateTime) && max.Date.AddHours(24) >= p.Date)
                        {
                            max.Value = Math.Max(max.Value, temp);
                        }
                        temperatureseen = false;
                    }

                    temperaturetrendseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_TemperatureTrend));
                    if (temperaturetrendseen && (reader.TokenType == JsonTokenType.String || reader.TokenType == JsonTokenType.Null))
                    {
                        if (collectweather && p.Date != default(DateTime) && reader.TokenType != JsonTokenType.Null)
                        {
                            results.Add(new Data()
                            {
                                Date = p.Date.AddSeconds(2), StrValue = System.Text.Encoding.UTF8.GetString(reader.ValueSpan), Type = "temperaturetrend"
                            });
                        }
                        temperaturetrendseen = false;
                    }

                    windspeedseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_WindSpeed));
                    if (windspeedseen && (reader.TokenType == JsonTokenType.String || reader.TokenType == JsonTokenType.Null))
                    {
                        if (collectweather && p.Date != default(DateTime) && reader.TokenType != JsonTokenType.Null)
                        {
                            results.Add(new Data()
                            {
                                Date = p.Date.AddSeconds(3), StrValue = System.Text.Encoding.UTF8.GetString(reader.ValueSpan), Type = "windspeed"
                            });
                        }
                        windspeedseen = false;
                    }

                    winddirectionseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_WindDirection));
                    if (winddirectionseen && (reader.TokenType == JsonTokenType.String || reader.TokenType == JsonTokenType.Null))
                    {
                        if (collectweather && p.Date != default(DateTime) && reader.TokenType != JsonTokenType.Null)
                        {
                            results.Add(new Data()
                            {
                                Date = p.Date.AddSeconds(4), StrValue = System.Text.Encoding.UTF8.GetString(reader.ValueSpan), Type = "winddirection"
                            });
                        }
                        winddirectionseen = false;
                    }

                    shortforecastseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_ShortForecast));
                    if (shortforecastseen && (reader.TokenType == JsonTokenType.String || reader.TokenType == JsonTokenType.Null))
                    {
                        if (collectweather && p.Date != default(DateTime) && reader.TokenType != JsonTokenType.Null)
                        {
                            results.Add(new Data()
                            {
                                Date = p.Date.AddSeconds(5), StrValue = System.Text.Encoding.UTF8.GetString(reader.ValueSpan), Type = "shortforecast"
                            });
                        }
                        shortforecastseen = false;
                    }
                }

                //
                // station
                //
                if (type == PredictionType.Station)
                {
                    utcdateseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_UtcDate));
                    if (utcdateseen && reader.TokenType == JsonTokenType.String)
                    {
                        var t = System.Text.Encoding.UTF8.GetString(reader.ValueSpan);

                        // incoming datetimes are GMT/UTC
                        var date = DateTime.Parse(t, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
                        p = new Data()
                        {
                            Date = date, Type = "station"
                        };

                        utcdateseen = false;
                    }

                    signalseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_Signal));
                    if (signalseen && reader.TokenType == JsonTokenType.Null)
                    {
                        signalseen = false;
                    }
                    if (signalseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "signal"
                        });
                        signalseen = false;
                    }

                    lowbatteryseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_Lowbattery));
                    if (lowbatteryseen && reader.TokenType == JsonTokenType.Null)
                    {
                        lowbatteryseen = false;
                    }
                    if (lowbatteryseen && reader.TokenType != JsonTokenType.PropertyName)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = reader.TokenType == JsonTokenType.True ? 1f : 0f, Type = "lowbattery"
                        });
                        lowbatteryseen = false;
                    }

                    windspeedseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_WindSpeed));
                    if (windspeedseen && reader.TokenType == JsonTokenType.Null)
                    {
                        windspeedseen = false;
                    }
                    if (windspeedseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "windspeed"
                        });
                        windspeedseen = false;
                    }

                    winddirectionseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_WindDirection));
                    if (winddirectionseen && reader.TokenType == JsonTokenType.Null)
                    {
                        winddirectionseen = false;
                    }
                    if (winddirectionseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "winddirection"
                        });
                        winddirectionseen = false;
                    }

                    raintotalseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_RaintTotal));
                    if (raintotalseen && reader.TokenType == JsonTokenType.Null)
                    {
                        raintotalseen = false;
                    }
                    if (raintotalseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "raintotal"
                        });
                        raintotalseen = false;
                    }

                    outtemperatureseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_OutTemperature));
                    if (outtemperatureseen && reader.TokenType == JsonTokenType.Null)
                    {
                        outtemperatureseen = false;
                    }
                    if (outtemperatureseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "outtemperature"
                        });
                        outtemperatureseen = false;
                    }

                    intemperatureseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_InTemperature));
                    if (intemperatureseen && reader.TokenType == JsonTokenType.Null)
                    {
                        intemperatureseen = false;
                    }
                    if (intemperatureseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "intemperature"
                        });
                        intemperatureseen = false;
                    }

                    outhumidityseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_OutHumidity));
                    if (outhumidityseen && reader.TokenType == JsonTokenType.Null)
                    {
                        outhumidityseen = false;
                    }
                    if (outhumidityseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "outhumidity"
                        });
                        outhumidityseen = false;
                    }

                    pressureseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_Pressure));
                    if (pressureseen && reader.TokenType == JsonTokenType.Null)
                    {
                        pressureseen = false;
                    }
                    if (pressureseen && reader.TokenType == JsonTokenType.Number)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Value = Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)), Type = "pressure"
                        });
                        pressureseen = false;
                    }

                    pressuretrendseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_PressureTrend));
                    if (pressuretrendseen && reader.TokenType == JsonTokenType.Null)
                    {
                        pressuretrendseen = false;
                    }
                    if (pressuretrendseen && reader.TokenType == JsonTokenType.StartArray)
                    {
                        pressuretrend.Clear();
                    }
                    if (pressuretrendseen && reader.TokenType == JsonTokenType.Number)
                    {
                        pressuretrend.Add(Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)));
                    }
                    if (pressuretrendseen && reader.TokenType == JsonTokenType.EndArray)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Values = pressuretrend.ToArray(), Type = "pressuretrend"
                        });
                        pressuretrendseen = false;
                        pressuretrend.Clear();
                    }

                    raintotaltrendseen |= (reader.TokenType == JsonTokenType.PropertyName && reader.ValueSpan.SequenceEqual(s_RainTotalTrend));
                    if (raintotaltrendseen && reader.TokenType == JsonTokenType.Null)
                    {
                        raintotaltrendseen = false;
                    }
                    if (raintotaltrendseen && reader.TokenType == JsonTokenType.StartArray)
                    {
                        raintotaltrend.Clear();
                    }
                    if (raintotaltrendseen && reader.TokenType == JsonTokenType.Number)
                    {
                        raintotaltrend.Add(Convert.ToSingle(System.Text.Encoding.UTF8.GetString(reader.ValueSpan)));
                    }
                    if (raintotaltrendseen && reader.TokenType == JsonTokenType.EndArray)
                    {
                        p.Date = p.Date.AddMilliseconds(10);
                        results.Add(new Data()
                        {
                            Date = p.Date, Values = raintotaltrend.ToArray(), Type = "raintotaltrend"
                        });
                        raintotaltrendseen = false;
                        raintotaltrend.Clear();
                    }
                }         // (type == PredictionType.Station)
            }             // while(reader.Read())

            // add the last prediction
            if (count > 0)
            {
                results.Add(p);
            }
            if (min.Date != default(DateTime) && min.Value > Single.MinValue)
            {
                results.Add(min);
            }
            if (max.Date != default(DateTime) && max.Value < Single.MaxValue)
            {
                results.Add(max);
            }

            return(results);
        }
 public VoterManager(PredictionType type)
 {
     scotlandOnly = type == PredictionType.Scotland2017 || type == PredictionType.Scotland2019;
 }
예제 #29
0
 public static void CalculateStuckPosition(IEnumerable <BaseMobile> mobileItems, PredictionType type, int maxDepth)
 {
     foreach (var mobileItem in mobileItems)
     {
         CalculateStuckPosition(mobileItem, type, maxDepth);
     }
 }
예제 #30
0
        public static void CalculateStuckPosition(IEnumerable <BaseMobile> mobileItems, PredictionType type, int maxDepth)
        {
            foreach (var mobileItem in mobileItems)
            {
                var cell = Field.GetCell(mobileItem.Point);

                for (var depth = 1; depth <= maxDepth; depth++)
                {
                    cell.AddPrediction(depth, type, item: mobileItem);
                }
            }
        }
예제 #31
0
		unsafe void encode_residual(FlacFrame frame, int ch, PredictionType predict, OrderMethod omethod, int pass, int best_window)
		{
			int* smp = frame.subframes[ch].samples;
			int i, n = frame.blocksize;
			// save best.window, because we can overwrite it later with fixed frame

			// CONSTANT
			for (i = 1; i < n; i++)
			{
				if (smp[i] != smp[0]) break;
			}
			if (i == n)
			{
				frame.subframes[ch].best.type = SubframeType.Constant;
				frame.subframes[ch].best.residual[0] = smp[0];
				frame.subframes[ch].best.size = (uint)frame.subframes[ch].obits;
				return;
			}

			// VERBATIM
			frame.current.type = SubframeType.Verbatim;
			frame.current.size = (uint)(frame.subframes[ch].obits * frame.blocksize);
			frame.ChooseBestSubframe(ch);

			if (n < 5 || predict == PredictionType.None)
				return;

			// FIXED
			if (predict == PredictionType.Fixed ||
				(predict == PredictionType.Search && pass != 1) ||
				//predict == PredictionType.Search ||
				//(pass == 2 && frame.subframes[ch].best.type == SubframeType.Fixed) ||
				n <= eparams.max_prediction_order)
			{
				int max_fixed_order = Math.Min(eparams.max_fixed_order, 4);
				int min_fixed_order = Math.Min(eparams.min_fixed_order, max_fixed_order);

				for (i = min_fixed_order; i <= max_fixed_order; i++)
					encode_residual_fixed_sub(frame, i, ch);
			}

			// LPC
			if (n > eparams.max_prediction_order &&
			   (predict == PredictionType.Levinson ||
				predict == PredictionType.Search)
				//predict == PredictionType.Search ||
				//(pass == 2 && frame.subframes[ch].best.type == SubframeType.LPC))
				)
			{
				float* lpcs = stackalloc float[lpc.MAX_LPC_ORDER * lpc.MAX_LPC_ORDER];
				int min_order = eparams.min_prediction_order;
				int max_order = eparams.max_prediction_order;

				for (int iWindow = 0; iWindow < _windowcount; iWindow++)
				{
					if (best_window != -1 && iWindow != best_window)
						continue;

					LpcContext lpc_ctx = frame.subframes[ch].lpc_ctx[iWindow];

					lpc_ctx.GetReflection(max_order, smp, n, frame.window_buffer + iWindow * Flake.MAX_BLOCKSIZE * 2);
					lpc_ctx.ComputeLPC(lpcs);

					//int frameSize = n;
					//float* F = stackalloc float[frameSize];
					//float* B = stackalloc float[frameSize];
					//float* PE = stackalloc float[max_order + 1];
					//float* arp = stackalloc float[max_order];
					//float* rc = stackalloc float[max_order];

					//for (int j = 0; j < frameSize; j++)
					//    F[j] = B[j] = smp[j];

					//for (int K = 1; K <= max_order; K++)
					//{
					//    // BURG:
					//    float denominator = 0.0f;
					//    //float denominator = F[K - 1] * F[K - 1] + B[frameSize - K] * B[frameSize - K];
					//    for (int j = 0; j < frameSize - K; j++)
					//        denominator += F[j + K] * F[j + K] + B[j] * B[j];
					//    denominator /= 2;

					//    // Estimate error
					//    PE[K - 1] = denominator / (frameSize - K);

					//    float reflectionCoeff = 0.0f;
					//    for (int j = 0; j < frameSize - K; j++)
					//        reflectionCoeff += F[j + K] * B[j];
					//    reflectionCoeff /= denominator;
					//    rc[K - 1] = arp[K - 1] = reflectionCoeff;

					//    // Levinson-Durbin
					//    for (int j = 0; j < (K - 1) >> 1; j++)
					//    {
					//        float arptmp = arp[j];
					//        arp[j] -= reflectionCoeff * arp[K - 2 - j];
					//        arp[K - 2 - j] -= reflectionCoeff * arptmp;
					//    }
					//    if (((K - 1) & 1) != 0)
					//        arp[(K - 1) >> 1] -= reflectionCoeff * arp[(K - 1) >> 1];

					//    for (int j = 0; j < frameSize - K; j++)
					//    {
					//        float f = F[j + K];
					//        float b = B[j];
					//        F[j + K] = f - reflectionCoeff * b;
					//        B[j] = b - reflectionCoeff * f;
					//    }

					//    for (int j = 0; j < K; j++)
					//        lpcs[(K - 1) * lpc.MAX_LPC_ORDER + j] = (float)arp[j];
					//}

					switch (omethod)
					{
						case OrderMethod.Akaike:
							//lpc_ctx.SortOrdersAkaike(frame.blocksize, eparams.estimation_depth, max_order, 7.1, 0.0);
							lpc_ctx.SortOrdersAkaike(frame.blocksize, eparams.estimation_depth, max_order, 4.5, 0.0);
							break;
						default:
							throw new Exception("unknown order method");
					}

					for (i = 0; i < eparams.estimation_depth && i < max_order; i++)
						encode_residual_lpc_sub(frame, lpcs, iWindow, lpc_ctx.best_orders[i], ch);
				}
			}
		}
예제 #32
0
        public static void CalculateMobilePredictions(IEnumerable <BaseMobile> mobileItems, PredictionType type, Func <Cell, bool> breakCondition, bool includeFirstObstacle = false, int?depthRestriction = null)
        {
            var maxDepth = depthRestriction ?? AppSettings.PredictionDepth;

            foreach (var mobileItem in mobileItems)
            {
                var predictionStartPoint = mobileItem.Point;
                var canNotMoveFurther    = false;

                for (var depth = 1; depth <= maxDepth; depth++)
                {
                    var nextPoints = mobileItem.GetNextPoints(predictionStartPoint);

                    foreach (var nextPoint in nextPoints)
                    {
                        var nextCell = Field.GetCell(nextPoint);

                        if (breakCondition(nextCell))
                        {
                            nextCell.AddPrediction(depth, type, item: mobileItem);
                            predictionStartPoint = nextPoint;
                        }
                        else
                        {
                            if (includeFirstObstacle)
                            {
                                nextCell.AddPrediction(depth, type, item: mobileItem);
                            }

                            canNotMoveFurther = true;
                            break;
                        }
                    }

                    if (canNotMoveFurther)
                    {
                        break;
                    }
                }
            }
        }
        private void GenerateProcess(int backwardWindow, int forwardWindow, PredictionType prediction,
                                     String predictField)
        {
            _script.Properties.SetProperty(
                ScriptProperties.PROCESS_CONFIG_BACKWARD_SIZE, backwardWindow);
            _script.Properties.SetProperty(
                ScriptProperties.PROCESS_CONFIG_FORWARD_SIZE, forwardWindow);

            IList<ProcessField> fields = _script.Process.Fields;
            fields.Clear();
            foreach (DataField df in _script.Fields)
            {
                if (string.Compare(df.Name, "prediction", true) == 0)
                {
                    continue;
                }

                var command = new StringBuilder();

                if (string.Compare(df.Name, "time", true) == 0)
                {
                    command.Append("cint(field(\"");
                    command.Append(df.Name);
                    command.Append("\",0");
                    command.Append("))");
                    fields.Add(new ProcessField(df.Name, command.ToString()));
                }
                else
                {
                    command.Append("cfloat(field(\"");
                    command.Append(df.Name);
                    command.Append("\",0");
                    command.Append("))");
                    fields.Add(new ProcessField(df.Name, command.ToString()));
                }
            }

            var c = new StringBuilder();

            switch (prediction)
            {
                case PredictionType.fieldmax:
                    c.Append("fieldmax(\"");
                    c.Append(predictField);
                    c.Append("\",");
                    c.Append(-forwardWindow);
                    c.Append(",");
                    c.Append(-1);
                    c.Append(")");
                    break;
                case PredictionType.fieldmaxpip:
                    c.Append("fieldmaxpip(\"");
                    c.Append(predictField);
                    c.Append("\",");
                    c.Append(-forwardWindow);
                    c.Append(",");
                    c.Append(-1);
                    c.Append(")");
                    break;
            }

            fields.Add(new ProcessField("prediction", c.ToString()));
        }
        public void WizardRealTime(IList<SourceElement> sourceData, FileInfo csvFile,
                                   int backwardWindow, int forwardWindow, PredictionType prediction,
                                   String predictField)
        {
            Preprocess = true;
            _script.BasePath = csvFile.DirectoryName;
            _script.Properties.SetProperty(
                ScriptProperties.HeaderDatasourceSourceHeaders, true);
            _script.Properties.SetProperty(
                ScriptProperties.HeaderDatasourceRawFile, csvFile);
            _script.Properties.SetProperty(
                ScriptProperties.SetupConfigInputHeaders, true);

            LagWindowSize = backwardWindow;
            LeadWindowSize = 1;
            _timeSeries = true;
            _format = AnalystFileFormat.DecpntComma;
            MethodType = WizardMethodType.FeedForward;
            _includeTargetField = false;
            TargetFieldName = "prediction";
            Missing = new DiscardMissing();

            Goal = AnalystGoal.Regression;
            Range = NormalizeRange.NegOne2One;
            TaskNormalize = true;
            TaskRandomize = false;
            TaskSegregate = true;
            TaskBalance = false;
            TaskCluster = false;
            MaxError = 0.05;
            CodeEmbedData = true;

            DetermineClassification();
            GenerateFilenames(csvFile);
            GenerateSettings();
            GenerateSourceData(sourceData);
            GenerateNormalizedFields();

            // if there is a time field, then ignore it
            AnalystField timeField = _script.FindAnalystField("time");
            if (timeField != null)
            {
                timeField.Action = NormalizationAction.Ignore;
            }

            GenerateSegregate();
            GenerateGenerate();
            GenerateProcess(backwardWindow, forwardWindow, prediction, predictField);
            GenerateCode();

            // override raw_file to be the processed file
            _script.Properties.SetProperty(
                ScriptProperties.HeaderDatasourceRawFile,
                FilePre);

            GenerateTasks();
            if (_timeSeries && (_lagWindowSize > 0)
                && (_leadWindowSize > 0))
            {
                ExpandTimeSlices();
            }
        }
예제 #35
0
 public static void Init(PredictionType type, CheckBox checkbox)
 {
     Checkboxes[type] = checkbox;
 }