Example #1
0
        public void Train(IList<InputOutput> trainingSet, INeuralNetwork nn)
        {
            Validate();

            if (nn == null)
                throw new ArgumentNullException(nameof(nn));

            var rand = RandomProvider.GetRandom(Seed);

            if (ShouldInitializeWeights)
                InitializeWeights(nn, rand);

            var validationSetFraction = GetValidationSetFraction();

            IList<InputOutput> trainingSubSet;
            IList<InputOutput> validationSubSet = null;

            if (validationSetFraction > 0)
            {
                var split = trainingSet.Shuffle(rand).Split(validationSetFraction);
                validationSubSet = split.First;
                trainingSubSet = split.Second;
            }
            else
            {
                trainingSubSet = trainingSet;
            }

            Train(trainingSubSet, validationSubSet, rand, nn);
        }
        /// <summary>
        /// Returns a list of matches where each team from list1 plays against a team from teams2.
        /// All teams from both lists play one match. Whether a team plays home or away is determined randomly.
        /// The total number of teams for both lists must be an even number and both lists must contain an equal number of teams.
        /// </summary>
        public List<Match> GetMatches(IList<Team> teams1, IList<Team> teams2)
        {
            // Total number of teams must be even and both lists same number of teams.
             bool isValid = (teams1.Count + teams2.Count) % 2 == 0
                        && teams1.Count == teams2.Count;
             if (!isValid)
             {
            throw new Exception("Even number of teams expected and both lists same number of teams");
             }

             teams1.Shuffle();
             teams2.Shuffle();

             var matches = new List<Match>();
             for (int i = 0; i < teams1.Count; i++)
             {
            // Optionally swith home/away
            var homeTeam = teams1[i];
            var awayTeam = teams2[i];
            bool switchHomeAndAway = _randomizer.GetRandomBoolean();
            if (switchHomeAndAway)
            {
               homeTeam = teams2[i];
               awayTeam = teams1[i];
            }

            var match = MatchFactory.CreateMatch(homeTeam, awayTeam);

            matches.Add(match);
             }

             return matches;
        }
Example #3
0
        /// <summary>Online evaluation for rankings of items</summary>
        /// <remarks>
        /// The evaluation protocol works as follows:
        /// For every test user, evaluate on the test items, and then add the those test items to the training set and perform an incremental update.
        /// The sequence of users is random.
        /// </remarks>
        /// <param name="recommender">the item recommender to be evaluated</param>
        /// <param name="test">test cases</param>
        /// <param name="training">training data (must be connected to the recommender's training data)</param>
        /// <param name="test_users">a list of all test user IDs</param>
        /// <param name="candidate_items">a list of all candidate item IDs</param>
        /// <param name="candidate_item_mode">the mode used to determine the candidate items</param>
        /// <returns>a dictionary containing the evaluation results (averaged by user)</returns>
        public static ItemRecommendationEvaluationResults EvaluateOnline(
			this IRecommender recommender,
			IPosOnlyFeedback test, IPosOnlyFeedback training,
			IList<int> test_users, IList<int> candidate_items,
			CandidateItems candidate_item_mode)
        {
            var incremental_recommender = recommender as IIncrementalItemRecommender;
            if (incremental_recommender == null)
                throw new ArgumentException("recommender must be of type IIncrementalItemRecommender");

            // prepare candidate items once to avoid recreating them
            switch (candidate_item_mode)
            {
                case CandidateItems.TRAINING: candidate_items = training.AllItems; break;
                case CandidateItems.TEST:     candidate_items = test.AllItems; break;
                case CandidateItems.OVERLAP:  candidate_items = new List<int>(test.AllItems.Intersect(training.AllItems)); break;
                case CandidateItems.UNION:    candidate_items = new List<int>(test.AllItems.Union(training.AllItems)); break;
            }

            test_users.Shuffle();
            var results_by_user = new Dictionary<int, ItemRecommendationEvaluationResults>();
            foreach (int user_id in test_users)
            {
                if (candidate_items.Intersect(test.ByUser[user_id]).Count() == 0)
                    continue;

                // prepare data
                var current_test_data = new PosOnlyFeedback<SparseBooleanMatrix>();
                foreach (int index in test.ByUser[user_id])
                    current_test_data.Add(user_id, test.Items[index]);
                // evaluate user
                var current_result = Items.Evaluate(recommender, current_test_data, training, current_test_data.AllUsers, candidate_items, CandidateItems.EXPLICIT);
                results_by_user[user_id] = current_result;

                // update recommender
                var tuples = new List<Tuple<int, int>>();
                foreach (int index in test.ByUser[user_id])
                    tuples.Add(Tuple.Create(user_id, test.Items[index]));
                incremental_recommender.AddFeedback(tuples);
            }

            var results = new ItemRecommendationEvaluationResults();

            foreach (int u in results_by_user.Keys)
                foreach (string measure in Items.Measures)
                    results[measure] += results_by_user[u][measure];

            foreach (string measure in Items.Measures)
                results[measure] /= results_by_user.Count;

            results["num_users"] = results_by_user.Count;
            results["num_items"] = candidate_items.Count;
            results["num_lists"] = results_by_user.Count;

            return results;
        }
 public InfectionDeck(IList<InfectionCard> infectionCards)
 {
     infectionCards.Shuffle();
     foreach (InfectionCard card in infectionCards)
     {
         card.Discarded += CardDiscarded;
     }
     cardPile = new Stack<InfectionCard>(infectionCards);
     discardPile = new Stack<InfectionCard>();
 }
Example #5
0
        /// <summary>
        /// Used by the producer to send a metadata request since it has access to the ProducerConfig
        /// </summary>
        /// <param name="topics">The topics for which the metadata needs to be fetched</param>
        /// <param name="brokers">The brokers in the cluster as configured on the client</param>
        /// <param name="producerConfig">The producer's config</param>
        /// <param name="correlationId">topic metadata response</param>
        /// <returns></returns>
        public static TopicMetadataResponse FetchTopicMetadata(
            ISet<string> topics, IList<Broker> brokers, ProducerConfig producerConfig, int correlationId)
        {
            var fetchMetaDataSucceeded = false;
            var i = 0;
            var topicMetadataRequest = new TopicMetadataRequest(
                TopicMetadataRequest.CurrentVersion, correlationId, producerConfig.ClientId, topics.ToList());

            TopicMetadataResponse topicMetadataResponse = null;
            Exception t = null;

            // shuffle the list of brokers before sending metadata requests so that most requests don't get routed to the same broker
            var shuffledBrokers = brokers.Shuffle();
            while (i < shuffledBrokers.Count() && !fetchMetaDataSucceeded)
            {
                var producer = ProducerPool.CreateSyncProducer(producerConfig, shuffledBrokers[i]);
                Logger.InfoFormat("Fetching metadata from broker {0} with correlation id {1} for {2} topic(s) {3}", shuffledBrokers[i], correlationId, topics.Count, string.Join(",", topics));
                try
                {
                    topicMetadataResponse = producer.Send(topicMetadataRequest);
                    fetchMetaDataSucceeded = true;
                }
                catch (Exception e)
                {
                    Logger.Warn(string.Format("Fetching topic metadata with correlation id {0} for topic [{1}] from broker [{2}] failed", correlationId, topics, shuffledBrokers[i]), e);
                    t = e;
                }
                finally
                {
                    i++;
                    producer.Dispose();
                }
            }

            if (!fetchMetaDataSucceeded)
            {
                throw new KafkaException(
                    string.Format(
                        "fetching topic metadata for topics [{0}] from broker [{1}] failed", string.Join(",", topics), string.Join(", ", shuffledBrokers)),
                    t);
            }

            Logger.DebugFormat("Successfully fetched metadata for {0} topic(s) {1}", topics.Count(), string.Join(",",  topics));
            return topicMetadataResponse;
        }
        /// <summary>
        /// SIDE EFFECT: Sets the Victim property if needed
        /// </summary>
        /// <param name="zoo"></param>
        /// <param name="killer"></param>
        /// <returns>the night activity text</returns>
        public IList<string> GetNightActivity(IList<string> zoo, string killer)
        {
            if (!zoo.Contains(killer))
            {
                return new List<string>();
            }

            zoo.Shuffle();
            Victim = GetVictim(zoo, killer);

            IList<string> nightCopy = new List<string>(GameResources.TwoPersonNightActivities);
            nightCopy.Shuffle();

            IList<string> activities = new List<string>();
            for (int i = 0; i < zoo.Count - 1; i++)
            {
                activities.Add(String.Format(nightCopy[i], zoo[i], zoo[i + 1]));
            }
            activities.Add(String.Format(nightCopy[zoo.Count], zoo[zoo.Count - 1], zoo[0]));
            activities.Shuffle();

            return activities;
        }
        ///
        protected override float[] FoldIn(IList<Tuple<int, float>> rated_items)
        {
            SetupLoss();

            // initialize user parameters
            float user_bias = 0;
            var factors = new float[NumFactors];
            factors.InitNormal(InitMean, InitStdDev);

            float reg_weight = FrequencyRegularization ? (float) (RegU / Math.Sqrt(rated_items.Count)) : RegU;

            // perform training
            rated_items.Shuffle();
            for (uint it = 0; it < NumIter; it++)
                for (int index = 0; index < rated_items.Count; index++)
                {
                    int item_id = rated_items[index].Item1;

                    // compute rating and error
                    double score = global_bias + user_bias + item_bias[item_id] + DataType.MatrixExtensions.RowScalarProduct(item_factors, item_id, factors);
                    double sig_score = 1 / (1 + Math.Exp(-score));
                    double prediction = min_rating + sig_score * rating_range_size;
                    double err = rated_items[index].Item2 - prediction;

                    float gradient_common = compute_gradient_common(sig_score, err);

                    // adjust bias
                    user_bias += BiasLearnRate * LearnRate * (gradient_common - BiasReg * reg_weight * user_bias);

                    // adjust factors
                    for (int f = 0; f < NumFactors; f++)
                    {
                        float u_f = factors[f];
                        float i_f = item_factors[item_id, f];

                        double delta_u = gradient_common * i_f - reg_weight * u_f;
                        factors[f] += (float) (LearnRate * delta_u);
                    }
                }

            var user_vector = new float[NumFactors + 1];
            user_vector[FOLD_IN_BIAS_INDEX] = user_bias;
            Array.Copy(factors, 0, user_vector, FOLD_IN_FACTORS_START, NumFactors);

            return user_vector;
        }
        ///
        protected override float[] FoldIn(IList<Tuple<int, float>> rated_items)
        {
            var user_p = new float[NumFactors];
            user_p.InitNormal(InitMean, InitStdDev);
            float user_bias = 0;

            var items = (from pair in rated_items select pair.Item1).ToArray();
            float user_reg_weight = FrequencyRegularization ? (float) (Regularization / Math.Sqrt(items.Length)) : Regularization;

            // compute stuff that will not change
            var y_sum_vector = y.SumOfRows(items);
            double norm_denominator = Math.Sqrt(items.Length);
            for (int f = 0; f < y_sum_vector.Count; f++)
                y_sum_vector[f] = (float) (y_sum_vector[f] / norm_denominator);

            rated_items.Shuffle();
            for (uint it = 0; it < NumIter; it++)
            {
                for (int index = 0; index < rated_items.Count; index++)
                {
                    int item_id = rated_items[index].Item1;

                    double prediction = global_bias + user_bias + item_bias[item_id];
                    prediction += DataType.MatrixExtensions.RowScalarProduct(item_factors, item_id, y_sum_vector);
                    prediction += DataType.MatrixExtensions.RowScalarProduct(item_factors, item_id, user_p);

                    float err = (float) (rated_items[index].Item2 - prediction);

                    // adjust bias
                    user_bias += BiasLearnRate * LearnRate * ((float) err - BiasReg * user_reg_weight * user_bias);

                    // adjust factors
                    for (int f = 0; f < NumFactors; f++)
                    {
                        float u_f = user_p[f];
                        float i_f = item_factors[item_id, f];

                        double delta_u = err * i_f - user_reg_weight * u_f;
                        user_p[f] += (float) (LearnRate * delta_u);
                    }
                }
            }

            // assign final parameter values to return vector
            var user_vector = new float[NumFactors + 1];
            user_vector[0] = user_bias;
            for (int f = 0; f < NumFactors; f++)
                user_vector[f + 1] = (float) y_sum_vector[f] + user_p[f];

            return user_vector;
        }
        ///
        protected override float[] FoldIn(IList<Tuple<int, float>> rated_items)
        {
            SetupLoss();

            float user_bias = 0;

            var items = (from pair in rated_items select pair.Item1).ToArray();
            float user_reg_weight = FrequencyRegularization ? (float) (Regularization / Math.Sqrt(items.Length)) : Regularization;

            // compute stuff that will not change
            var y_sum_vector = y.SumOfRows(items);
            double norm_denominator = Math.Sqrt(items.Length);
            for (int f = 0; f < y_sum_vector.Count; f++)
                y_sum_vector[f] = (float) (y_sum_vector[f] / norm_denominator);

            rated_items.Shuffle();
            for (uint it = 0; it < NumIter; it++)
            {
                for (int index = 0; index < rated_items.Count; index++)
                {
                    int item_id = rated_items[index].Item1;

                    double score = global_bias + user_bias + item_bias[item_id];
                    score += item_factors.RowScalarProduct(item_id, y_sum_vector);
                    double sig_score = 1 / (1 + Math.Exp(-score));
                    double prediction = min_rating + sig_score * rating_range_size;
                    float err = (float) (rated_items[index].Item2 - prediction);
                    float gradient_common = compute_gradient_common(sig_score, err);

                    // adjust bias
                    user_bias += BiasLearnRate * LearnRate * ((float) gradient_common - BiasReg * user_reg_weight * user_bias);
                }
            }

            // assign final parameter values to return vector
            var user_vector = new float[NumFactors + 1];
            user_vector[0] = user_bias;
            for (int f = 0; f < NumFactors; f++)
                user_vector[f + 1] = (float) y_sum_vector[f];

            return user_vector;
        }
        /// <summary>Compute parameters (latent factors) for a user represented by ratings</summary>
        /// <returns>a vector of latent factors</returns>
        /// <param name='rated_items'>a list of (item ID, rating value) pairs</param>
        protected virtual float[] FoldIn(IList<Pair<int, float>> rated_items)
        {
            var user_vector = new float[NumFactors];
            user_vector.InitNormal(InitMean, InitStdDev);
            rated_items.Shuffle();
            for (uint it = 0; it < NumIter; it++)
            {
                for (int index = 0; index < rated_items.Count; index++)
                {
                    int item_id = rated_items[index].First;
                    float err = rated_items[index].Second - Predict(user_vector, item_id, false);

                    // adjust factors
                    for (int f = 0; f < NumFactors; f++)
                    {
                        float u_f = user_vector[f];
                        float i_f = item_factors[item_id, f];

                        double delta_u = err * i_f - Regularization * u_f;
                        user_vector[f] += (float) (LearnRate * delta_u);
                    }
                }
            }
            return user_vector;
        }
Example #11
0
		void StartRound() {
			int round = rounds.Count + 1;

			var previousRound = currentRound;
			if (currentRound != null)
				rounds.Add(currentRound);
			currentRound = new List<Attempt>();

			items = previousRound != null 
				? new List<PracticeItem>(from x in previousRound where !x.Correct select x.Item) 
				: Owner.GetAllItems();

			items.Shuffle(rng);

			index = 0;
			score = 0;

			state = items.Count > 0 ? State.Guessing : State.GameOverview;
		}
Example #12
0
 public async Task BonusGenerateAsync(int count)
 {
     var connect = new SQLiteAsyncConnection(DataBaseFile);
     _bonuses = await connect.Table<BonusTable>().Take(count).ToListAsync();
     _bonuses.Shuffle();
 }
        /// <summary>
        /// Returns a list of matches where all teams play one match.
        /// Whether a team plays home or away is determined randomly.
        /// The given team list must contain an even number of teams.
        /// </summary>
        public List<Match> GetMatches(IList<Team> teams)
        {
            // Even number of teams expected.
             bool isValid = teams.Count % 2 == 0;
             if (!isValid)
             {
            throw new Exception("Even number of teams expected");
             }

             teams.Shuffle();

             var matches = new List<Match>();
             for (int i = 0; i < teams.Count; i += 2)
             {
            var match = MatchFactory.CreateMatch(teams[i], teams[i + 1]);
            matches.Add(match);
             }

             return matches;
        }