public StochasticCoordinateGenerator(double[] mean, double[] sigma, int count)
 {
     if(mean.Length != sigma.Length)
         throw new ArgumentException("Mean and Sigma array length must be equal.", "sigma");
     _dimensions = mean.Length;
     _generators = new NormalGenerator[_dimensions];
     for(int i = 0; i < _dimensions; i++)
         _generators[i] = new NormalGenerator(mean[i], sigma[i]);
     _count = count;
 }
        public TrainingConfig(ConfigNode config)
        {
            _config   = config;
            _gaussian = new NormalGenerator(0.0, 0.5);
            _basic    = new BasicConfig(config);

            _autoTrainingEpochs            = new ConfigItem <int>(_config, "AutoTrainingEpochs", 400);
            _autoTrainingAttempts          = new ConfigItem <int>(_config, "AutoTrainingAttempts", 1);
            _autoTrainingPercentSuccessful = new ConfigItem <double>(_config, "AutoTrainingPercentSuccessful", 1.0);

            _shuffleSwapProbability = new ConfigItem <double>(_config, "ShuffleSwapProbability", 0.05);
            _shuffleNoiseSigma      = new ConfigItem <double>(_config, "ShuffleNoiseSigma", 0.5);
            _shuffleEn = new ConfigItem <bool>(_config, "ShuffleEnable", false);
        }
 public StochasticCoordinateGenerator(double[] mean, double[] sigma, int count)
 {
     if (mean.Length != sigma.Length)
     {
         throw new ArgumentException("Mean and Sigma array length must be equal.", "sigma");
     }
     _dimensions = mean.Length;
     _generators = new NormalGenerator[_dimensions];
     for (int i = 0; i < _dimensions; i++)
     {
         _generators[i] = new NormalGenerator(mean[i], sigma[i]);
     }
     _count = count;
 }
        public TrainingConfig(ConfigNode config)
        {
            _config = config;
            _gaussian = new NormalGenerator(0.0, 0.5);
            _basic = new BasicConfig(config);

            _autoTrainingEpochs = new ConfigItem<int>(_config, "AutoTrainingEpochs", 400);
            _autoTrainingAttempts = new ConfigItem<int>(_config, "AutoTrainingAttempts", 1);
            _autoTrainingPercentSuccessful = new ConfigItem<double>(_config, "AutoTrainingPercentSuccessful", 1.0);

            _shuffleSwapProbability = new ConfigItem<double>(_config, "ShuffleSwapProbability", 0.05);
            _shuffleNoiseSigma = new ConfigItem<double>(_config, "ShuffleNoiseSigma", 0.5);
            _shuffleEn = new ConfigItem<bool>(_config, "ShuffleEnable", false);
        }
        [TestMethod] public void normal_average_should_be_0()
        {
            NormalGenerator normal   = new NormalGenerator();
            double          sum      = 0.0;
            double          nbValues = 10e6;

            for (int i = 0; i < nbValues; i++)
            {
                sum += normal.Generate();
            }
            double avg = sum / nbValues;

            Assert.AreEqual(0, avg, 0.1);
        }
Exemple #6
0
        static XGenParser()
        {
            Factories = new Dictionary <string, VariableFactory>
            {
                { "PageViews", VariableFactory.Lambda((segment, token, parser) => segment.VisitVariables.AddOrReplace(Variables.Random("PageViews", new PoissonGenerator(token.Value <double>()).Truncate(1, 20)))) },
                { "VisitCount", VariableFactory.Lambda((segment, token, parser) => segment.VisitorVariables.AddOrReplace(Variables.Random("VisitCount", new PoissonGenerator(token.Value <double>()).Truncate(1, 20)))) },
                { "BounceRate", VariableFactory.Lambda((segment, token, parser) => segment.VisitVariables.AddOrReplace(Variables.Boolean("Bounce", token.Value <double>()))) },
                { "Duration", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var mean = token.Value <double>();
                        segment.RequestVariables.AddOrReplace(Variables.Duration(new SkewNormalGenerator(mean, mean, 3), 1));
                    }) },
                { "StartDate", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.Start = token.Value <DateTime>()) },
                { "EndDate", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.End = token.Value <DateTime>()) },
                { "DayOfWeek", VariableFactory.Lambda((segment, token, parser) => segment.DateGenerator.DayOfWeek(t => t.Clear().Weighted(builder =>
                    {
                        foreach (var kv in (JObject)token)
                        {
                            DayOfWeek day;
                            builder.Add(Enum.TryParse(kv.Key, out day) ? (int)day : int.Parse(kv.Key), kv.Value.Value <double>());
                        }
                    }))) },
                { "YearlyTrend", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        if (token.Value <double>() != 1)
                        {
                            segment.DateGenerator.Year(t => t.Clear().MoveAbsolutePercentage(0).LineAbsolutePercentage(1, token.Value <double>()));
                        }
                        //segment.DateGenerator.YearWeight = 1;
                    }) },
                { "Month", new MonthFactory() },
                { "Identified", VariableFactory.Lambda((segment, token, parser) => segment.VisitorVariables.AddOrReplace(new ContactDataVariable(token.Value <double>()))) },
                { "Campaign", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var campaignPct = token.Value <double?>("Percentage") ?? 1;
                        var campaigns   = parser.ParseWeightedSet <string>(token["Weights"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Campaign", () => Randomness.Random.NextDouble() < campaignPct ? campaigns() : null, true));
                    }) },
                { "Channel", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var channelPct = token.Value <double?>("Percentage") ?? 1;
                        var channels   = parser.ParseWeightedSet <string>(token["Weights"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Channel", () => Randomness.Random.NextDouble() < channelPct ? channels() : null, true));
                    }) },
                { "Referrer", VariableFactory.Lambda((segment, token, parser) => segment.VisitVariables.AddOrReplace(Variables.Random("Referrer", parser.ParseWeightedSet <string>(token), true))) },
                { "Geo", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var regionId = parser.ParseWeightedSet <string>(token["Region"]);
                        segment.VisitorVariables.AddOrReplace(new GeoVariables(() => new GetRandomCityService().GetRandomCity(regionId())));
                    }) },
                { "Devices", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        Func <string> userAgent;
                        if (!token.HasValues)
                        {
                            userAgent = new DeviceRepository().GetAll().Select(d => d.UserAgent).Exponential(.8, 8);
                        }
                        else
                        {
                            var id    = parser.ParseWeightedSet <string>(token);
                            userAgent = () => new DeviceRepository().GetAll().ToDictionary(ga => ga.Id, ga => ga)[id()].UserAgent;
                        }
                        segment.VisitorVariables.AddOrReplace(Variables.Random("UserAgent", userAgent));
                    }) },
                { "Outcomes", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var value = new NormalGenerator(10, 5).Truncate(1);
                        segment.VisitVariables.AddOrReplace(new OutcomeVariable(parser.ParseSet <string>(token), value.Next));
                    }) },

                { "Goal", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var goalPct = token.Value <double?>("Percentage") ?? 0.2;
                        var goals   = parser.ParseWeightedSet <string>(token["Weights"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Goal", () => Randomness.Random.NextDouble() < goalPct ? goals() : null, true));
                    }) },

                { "InternalSearch", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var searchPct = token.Value <double?>("Percentage") ?? 0.2;
                        var keywords  = parser.ParseWeightedSet <string>(token["Keywords"]);
                        segment.VisitVariables.AddOrReplace(Variables.Random("InternalSearch", () => Randomness.Random.NextDouble() < searchPct ? keywords() : null, true));
                    }) },
                { "ExternalSearch", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var searchPct = token.Value <double?>("Percentage") ?? 0.2;
                        var keywords  = parser.ParseWeightedSet <string>(token["Keywords"]);

                        var engineId = parser.ParseWeightedSet <string>(token["Engine"]);

                        segment.VisitVariables.AddOrReplace(new ExternalSearchVariable(() => Randomness.Random.NextDouble() >= searchPct ? null : SearchEngine.SearchEngines.ToDictionary(s => s.Id)[engineId()], () => new[] { keywords() }));
                    }) },
                { "Language", VariableFactory.Lambda((segment, token, parser) =>
                    {
                        var languages = parser.ParseWeightedSet <string>(token);
                        segment.VisitVariables.AddOrReplace(Variables.Random("Language", languages));
                    }) },
                { "LandingPage", new LandingPageFactory() }
            };
        }
Exemple #7
0
        public virtual IEnumerable <VisitorSegment> ParseContacts(JToken definition, JobType type)
        {
            var segments = new List <VisitorSegment>();

            if (JobType.Contacts != type)
            {
                return(segments);
            }

            foreach (var contact in (JArray)definition)
            {
                if (contact["interactions"] == null)
                {
                    continue;
                }
                foreach (var interactionJObject in contact["interactions"])
                {
                    var segment     = new VisitorSegment(contact.Value <string>("email"));
                    var interaction = interactionJObject.ToObject <Interaction>();

                    //set city
                    if (interaction.GeoData != null)
                    {
                        var city = _geoDataRepository.Cities.FirstOrDefault(x => x.GeoNameId == interaction.GeoData.GeoNameId);
                        segment.VisitorVariables.Add(new GeoVariables(() => city));
                    }

                    //set contact
                    segment.VisitVariables.Add(ExtractContact(contact));

                    //set channel (can be overriden below)
                    segment.VisitVariables.Add(new SingleVisitorVariable <string>("Channel", visit => interaction.ChannelId));


                    //set search options
                    if (!string.IsNullOrEmpty(interaction.SearchEngine))
                    {
                        var searchEngine = SearchEngine.SearchEngines.First(s => s.Id.Equals(interaction.SearchEngine, StringComparison.InvariantCultureIgnoreCase));
                        if (!string.IsNullOrEmpty(interaction.SearchKeyword))
                        {
                            var searchKeywords = interaction.SearchKeyword.Split(' ');
                            segment.VisitVariables.AddOrReplace(new ExternalSearchVariable(() => searchEngine, () => searchKeywords));
                        }
                    }

                    var pageItemInfos = interaction.Pages?.ToArray() ?? Enumerable.Empty <PageItemInfo>();
                    var pages         = new List <PageDefinition>();

                    foreach (var page in pageItemInfos)
                    {
                        var pageDefinition = new PageDefinition
                        {
                            Path = page.Path
                        };

                        //set goals
                        if (page.Goals != null)
                        {
                            pageDefinition.RequestVariables.Add("TriggerEvents", page.Goals.Select(x => new TriggerEventData
                            {
                                Id     = x.Id,
                                Name   = x.DisplayName,
                                IsGoal = true
                            }).ToList());
                        }
                        pages.Add(pageDefinition);
                    }


                    //set userAgent
                    SetUserAgent(segment);

                    //set datetime
                    segment.DateGenerator.Start = DateTime.Today.AddHours(12).AddMinutes(Randomness.Random.Next(-240, 240)).AddSeconds(Randomness.Random.Next(-240, 240)).Add(TimeSpan.Parse(interaction.Recency));


                    //set outcomes
                    if (interaction.Outcomes != null)
                    {
                        var outcomes = interaction.Outcomes.Select(x => x.Id.ToString());
                        var value    = new NormalGenerator(10, 5).Truncate(1);
                        segment.VisitVariables.AddOrReplace(new OutcomeVariable(() => new HashSet <string>(outcomes), value.Next));
                    }

                    //set campaign (can be overriden below)
                    if (!string.IsNullOrEmpty(interaction.CampaignId) && pageItemInfos.Any())
                    {
                        var pageItemInfo = pageItemInfos.First();
                        pageItemInfo.Path = pageItemInfo.Path + "?sc_camp=" + interaction.CampaignId;
                    }



                    var visitorBehavior = new StrictWalk(_sitecoreRoot, pages);
                    segment.Behavior = () => visitorBehavior;
                    segments.Add(segment);
                }
            }
            return(segments);
        }
Exemple #8
0
        public virtual IEnumerable <VisitorSegment> ParseContacts(JToken definition, JobType type)
        {
            var segments = new List <VisitorSegment>();

            if (JobType.Contacts != type)
            {
                return(segments);
            }

            foreach (var contact in (JArray)definition)
            {
                if (contact["interactions"] == null)
                {
                    continue;
                }
                foreach (var interactionJObject in contact["interactions"])
                {
                    var segment     = new VisitorSegment(contact.Value <string>("email"));
                    var interaction = interactionJObject.ToObject <Interaction>();

                    //set city
                    if (interaction.GeoData != null)
                    {
                        var city = _geoDataRepository.Cities.FirstOrDefault(x => x.GeoNameId == interaction.GeoData.GeoNameId);
                        segment.VisitorVariables.Add(new GeoVariables(() => city));
                    }

                    //set contact
                    segment.VisitVariables.Add(ExtractContact(contact));

                    //set channel (can be overriden below)
                    segment.VisitVariables.Add(new SingleVisitorVariable <string>("Channel", visit => interaction.ChannelId));


                    //set search options
                    if (!string.IsNullOrEmpty(interaction.SearchEngine))
                    {
                        var searchEngine = SearchEngine.SearchEngines.First(s => s.Id.Equals(interaction.SearchEngine, StringComparison.InvariantCultureIgnoreCase));
                        if (!string.IsNullOrEmpty(interaction.SearchKeyword))
                        {
                            var searchKeywords = interaction.SearchKeyword.Split(' ');
                            segment.VisitVariables.AddOrReplace(new ExternalSearchVariable(() => searchEngine, () => searchKeywords));
                        }
                    }
                    //set userAgent
                    SetUserAgent(segment);

                    //set datetime

                    //Visits while happen in this interval adjusted for the interaction's local time.
                    const double minHour = 6, maxHour = 23;

                    //Does the interaction have a time zone? If not use UTC.
                    var tz = interaction.GeoData?.TimeZoneInfo ?? TimeZoneInfo.Utc;

                    //Pick a random time between min and max hour. DateTimeKind.Unspecified is used because we are converting to local time next.
                    var startDate = DateTime.SpecifyKind(DateTime.Today.AddHours(minHour + Randomness.Random.NextDouble() * (maxHour - minHour)), DateTimeKind.Unspecified);

                    startDate = TimeZoneInfo.ConvertTimeToUtc(startDate, tz); //Convert from local time to UTC.

                    //"Recency" indicates how many days ago the visit happened. Hence substract this number of days.
                    int parseRecency;
                    var success = Int32.TryParse(interaction.Recency, out parseRecency);
                    if (!success)
                    {
                        throw new Exception("Error parsing interaction recency (days since interaction) to integer value");
                    }
                    var ensurePositiveRecency = Math.Abs(parseRecency);
                    segment.DateGenerator.Start = startDate.Add(-TimeSpan.Parse(ensurePositiveRecency.ToString()));

                    //set outcomes
                    if (interaction.Outcomes != null)
                    {
                        var outcomes = interaction.Outcomes.Select(x => x.Id.ToString());
                        var value    = new NormalGenerator(10, 5).Truncate(1);
                        segment.VisitVariables.AddOrReplace(new OutcomeVariable(() => new HashSet <string>(outcomes), value.Next));
                    }

                    var pageItemInfos = interaction.Pages?.ToArray() ?? Enumerable.Empty <PageItemInfo>();
                    var pages         = new List <PageDefinition>();

                    //set campaign (can be overriden below)
                    if (!string.IsNullOrEmpty(interaction.CampaignId) && pageItemInfos.Any())
                    {
                        var pageItemInfo = pageItemInfos.First();
                        pageItemInfo.Path = pageItemInfo.Path + "?sc_camp=" + interaction.CampaignId;
                    }

                    foreach (var page in pageItemInfos)
                    {
                        var pageDefinition = new PageDefinition
                        {
                            Path = page.Path
                        };

                        //set goals
                        if (page.Goals != null)
                        {
                            pageDefinition.RequestVariables.Add("TriggerEvents", page.Goals.Select(x => new TriggerEventData
                            {
                                Id     = x.Id,
                                Name   = x.DisplayName,
                                IsGoal = true
                            }).ToList());
                        }
                        pages.Add(pageDefinition);
                    }

                    var visitorBehavior = new StrictWalk(_sitecoreRoot, pages);
                    segment.Behavior = () => visitorBehavior;
                    segments.Add(segment);
                }
            }
            return(segments);
        }