예제 #1
0
        protected override void Check()
        {
            GenerateStateSpace(new C());

            StateCount.ShouldBe(51);
            TransitionCount.ShouldBe(52);
        }
예제 #2
0
        protected override void Check()
        {
            GenerateStateSpace(new C1());

            StateCount.ShouldBe(3);
            TransitionCount.ShouldBe(6);
            ComputedTransitionCount.ShouldBe(6);

            GenerateStateSpace(new C2());

            StateCount.ShouldBe(1);
            TransitionCount.ShouldBe(2);
            ComputedTransitionCount.ShouldBe(2);

            GenerateStateSpace(new C3());

            StateCount.ShouldBe(1);
            TransitionCount.ShouldBe(2);
            ComputedTransitionCount.ShouldBe(3);

            GenerateStateSpace(new C4());

            StateCount.ShouldBe(2);
            TransitionCount.ShouldBe(4);
            ComputedTransitionCount.ShouldBe(5);
        }
예제 #3
0
        protected override void Check()
        {
            AllowFaultsOnInitialTransitions = true;

            GenerateStateSpace(new C1());

            StateCount.ShouldBe(3);
            TransitionCount.ShouldBe(6);
            ComputedTransitionCount.ShouldBe(6);

            GenerateStateSpace(new C2());

            StateCount.ShouldBe(1);
            TransitionCount.ShouldBe(2);
            ComputedTransitionCount.ShouldBe(2);

            GenerateStateSpace(new C3());

            StateCount.ShouldBe(1);
            TransitionCount.ShouldBe(2);
            ComputedTransitionCount.ShouldBe(3);

            GenerateStateSpace(new C4());

            StateCount.ShouldBe(2);
            TransitionCount.ShouldBe(4);
            ComputedTransitionCount.ShouldBe(5);
        }
예제 #4
0
        protected override void Check()
        {
            GenerateStateSpace(new C());

            StateCount.ShouldBe(6);
            TransitionCount.ShouldBe(26);
            ComputedTransitionCount.ShouldBe(31);
        }
예제 #5
0
        public static async Task RunAsync([CosmosDBTrigger(
                                               databaseName: "StoreDatabase",
                                               collectionName: "CartContainerByState",
                                               ConnectionStringSetting = "CosmosDBConnectionString",
                                               CreateLeaseCollectionIfNotExists = true,
                                               LeaseCollectionName = "materializedViewLeases")] IReadOnlyList <Document> input, ILogger log)
        {
            if (input != null && input.Count > 0)
            {
                var stateDict = new Dictionary <string, List <double> >();
                foreach (var doc in input)
                {
                    var action = JsonConvert.DeserializeObject <CartAction>(doc.ToString());

                    if (action.Action != ActionType.Purchased)
                    {
                        continue;
                    }

                    if (stateDict.ContainsKey(action.BuyerState))
                    {
                        stateDict[action.BuyerState].Add(action.Price);
                    }
                    else
                    {
                        stateDict.Add(action.BuyerState, new List <double> {
                            action.Price
                        });
                    }
                }
                var db        = cosmosClient.GetDatabase(_databaseId);
                var container = db.GetContainer(_containerId);

                var tasks = new List <Task>();

                foreach (var key in stateDict.Keys)
                {
                    var query = new QueryDefinition("select * from StateSales s where s.State = @state").WithParameter("@state", key);

                    var resultSet = container.GetItemQueryIterator <StateCount>(query, requestOptions: new QueryRequestOptions()
                    {
                        PartitionKey = new Microsoft.Azure.Cosmos.PartitionKey(key), MaxItemCount = 1
                    });

                    while (resultSet.HasMoreResults)
                    {
                        var stateCount = (await resultSet.ReadNextAsync()).FirstOrDefault();

                        if (stateCount == null)
                        {
                            stateCount = new StateCount
                            {
                                State      = key,
                                TotalSales = stateDict[key].Sum(),
                                Count      = stateDict[key].Count
                            };
                        }
                        else
                        {
                            stateCount.TotalSales += stateDict[key].Sum();
                            stateCount.Count      += stateDict[key].Count;
                        }

                        log.LogInformation("Upserting materialized view document");
                        tasks.Add(container.UpsertItemAsync(stateCount, new Microsoft.Azure.Cosmos.PartitionKey(stateCount.State)));
                    }
                }

                await Task.WhenAll(tasks);
            }
        }
        private void Update(EvaluationContext context)
        {
            var wasUpdate = false;

            var neighbourCount = NeighbourCount.GetValue(context).Clamp(1, 9);
            var stateCount     = StateCount.GetValue(context).Clamp(1, 100);
            var seed           = RandomSeed.GetValue(context);
            var lambda         = Lambda.GetValue(context);
            var isotropic      = Isotropic.GetValue(context);

            if (neighbourCount != _neighbourCount ||
                stateCount != _stateCount ||
                seed != _randomSeed ||
                Math.Abs(lambda - _lambda) > 0.001f ||
                isotropic != _isotropic)
            {
                wasUpdate       = true;
                _neighbourCount = neighbourCount;
                _stateCount     = stateCount;
                _randomSeed     = seed;
                _lambda         = lambda;
                _isotropic      = isotropic;

                _requiredBitCount = (int)Math.Ceiling(Math.Log(_stateCount, 2));
                _ruleTableLength  = 1 << (_requiredBitCount * neighbourCount);

                if (_cellBuffer.Length != _ruleTableLength)
                {
                    _cellBuffer = new Cell[_ruleTableLength];
                }

                var rand = new Random(seed);

                var countPositives = 0f;
                //_lambda = 0.49f;
                for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex++)
                {
                    var choice    = rand.NextDouble();
                    var nextState = (choice > _lambda || ruleIndex == 0)
                                   ? 0
                                   : (rand.Next(stateCount - 1) + 1);

                    if (nextState == 0)
                    {
                        countPositives++;
                    }

                    _cellBuffer[ruleIndex] = new Cell((uint)nextState);
                    _cellBuffer[FlipLookupIndex(ruleIndex)] = new Cell((uint)nextState);
                }

                // Initialize with random
                // for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex++)
                // {
                //     var nextState = rand.Next(stateCount);
                //     _cellBuffer[ruleIndex] =  new Cell((uint)nextState);
                // }

                // Flip to approach lambda
                //var countPositives = 0f;
                // int step = _ruleTableLength/6;
                // for (var ruleIndex = 0; ruleIndex < _ruleTableLength; ruleIndex+= step)
                // {
                //     var currentL = MeasureLambda();
                //     if (currentL < _lambda)
                //     {
                //         _cellBuffer[ruleIndex] =  new Cell((uint)rand.Next(stateCount-1)+1);
                //         _cellBuffer[FlipLookupIndex(ruleIndex)] =  new Cell((uint)rand.Next(stateCount-1)+1);
                //     }
                //     else
                //     {
                //         _cellBuffer[ruleIndex] =   new Cell(0);
                //         _cellBuffer[FlipLookupIndex(ruleIndex)] =  new Cell(0);
                //     }
                //     //countPositives++;
                // }


                Log.Debug($" Lambda:  {MeasureLambda()}");

                const int stride          = 4;
                var       resourceManager = ResourceManager.Instance();
                _bufferWithViews.Buffer = _buffer;
                resourceManager.SetupStructuredBuffer(_cellBuffer, stride * _cellBuffer.Length, stride, ref _buffer);
                resourceManager.CreateStructuredBufferSrv(_buffer, ref _bufferWithViews.Srv);
                resourceManager.CreateStructuredBufferUav(_buffer, UnorderedAccessViewBufferFlags.None, ref _bufferWithViews.Uav);
            }

            OutBuffer.Value   = _bufferWithViews;
            TableLength.Value = _ruleTableLength;
            WasUpdated.Value  = wasUpdate;
        }
        public async Task GetGeoReport(bool isAjax, string keyword, DateTime?start, DateTime?end)
        {
            this.SetFilters(isAjax, keyword, String.Empty, start, end);

            /*
             * Note:  the OpenFDA API has a bug/limitation that it will not support a count on distribution_pattern.exact
             * which would return complete distribution_pattern strings and allow more accurate parsing.  Just counting
             * distribution_pattern splits on individual words, so there is no reliable way to distinguish between e.g.
             * IN for Indiana and the preposition "in". It will also fail to count two-word state names such as "South
             * Dakota" because the words wil be split. Due to the time constraints on this prototype, this view model
             * will accept this limitation of the API and return demonstrative but inaccurate results.
             *
             */

            // create state count list with all states and zero counts
            int cnt = 0;

            this.data = new List <StateCount> ();
            foreach (KeyValuePair <string, string> kvp in StateNames.stateNames)
            {
                cnt++;
                StateCount stateCount = new StateCount(cnt, kvp.Key, kvp.Value);
                this.data.Add(stateCount);
            }

            OpenFDAQuery query = new OpenFDAQuery();

            query.source     = OpenFDAQuery.FDAReportSource.food;
            query.type       = OpenFDAQuery.FDAReportType.enforcement;
            query.queryCount = "distribution_pattern";

            var searchQuery = new List <string>();

            if (!string.IsNullOrWhiteSpace(keyword))
            {
                searchQuery.Add(HttpUtility.UrlEncode(keyword));
            }

            if (start != null && end != null)
            {
                searchQuery.Add(string.Format("recall_initiation_date:[{0:yyyyMMdd}+TO+{1:yyyyMMdd}]", start.Value, end.Value));
            }

            if (searchQuery.Count > 0)
            {
                query.querySearch = string.Join("+AND+", searchQuery);
            }

            bool success = await query.RunQueryAsync();

            if (!success)
            {
                this.ErrorMsg = "An error occurred executing the query.  Please try again.";
            }
            else
            {
                this.ErrorMsg = String.Empty;

                foreach (OpenFDAResult result in query.response.results)
                {
                    var statecount = this.data.Where(s => s.StateAbbr.ToLower().Equals(result.term) || s.StateName.ToLower().Equals(result.term)).FirstOrDefault();
                    if (statecount != null)
                    {
                        int c = 0;
                        Int32.TryParse(result.count, out c);
                        statecount.Count += c;
                    }
                }
            }
        }