예제 #1
0
        /// <summary>
        /// The predict.
        /// </summary>
        /// <param name="scenarioId">
        /// The scenario id.
        /// </param>
        /// <param name="inputSetId">
        /// The input set id.
        /// </param>
        /// <returns>
        /// The <see cref="PredictionSet"/>.
        /// </returns>
        public PredictionSet Predict(string scenarioId, string inputSetId)
        {
            var predictions = new PredictionSet {
                Predictions = new List <Prediction>()
            };
            ScenarioTrainings trainings;

            if (!trainingByScenario.TryGetValue(scenarioId, out trainings))
            {
                trainings = this.Train(scenarioId);
            }

            if (trainings == null)
            {
                return(predictions);
            }

            using (var dbContext = new OpenAIEntities1())
            {
                List <Feature> features =
                    dbContext.Features.Where(f => f.ScenarioId == scenarioId).OrderBy(f => f.Position).ToList();
                Dictionary <string, Input> inputsById =
                    dbContext.Inputs.Where(i => i.ScenarioId == scenarioId && i.InputSetId == inputSetId)
                    .ToDictionary(i => i.FeatureId);

                var sortedInputs = new List <Input>();
                foreach (Feature feature in features)
                {
                    Input input;
                    if (inputsById.TryGetValue(feature.FeatureId, out input))
                    {
                        sortedInputs.Add(input);
                    }
                    else
                    {
                        sortedInputs.Add(new Input {
                            FeatureId = feature.FeatureId
                        });
                    }
                }


                List <string> unset =
                    features.Select(f => f.FeatureId).Except(inputsById.Values.Select(i => i.FeatureId)).ToList();
                foreach (string inputId in unset)
                {
                    TrainerHelper training;
                    if (!trainings.TrainingByFeatureId.TryGetValue(inputId, out training))
                    {
                        continue;
                    }

                    var inputsMinusFeature = sortedInputs.Where(i => i.FeatureId != inputId).Select(i => i.Value).ToArray();
                    KeyValuePair <string, double> valueAndConfidence = trainer.Decide(training, inputsMinusFeature, inputId);

                    var prediction = new Prediction
                    {
                        InputId    = inputId,
                        Confidence = valueAndConfidence.Value,
                        Value      = valueAndConfidence.Key
                    };
                    predictions.Predictions.Add(prediction);
                }
            }

            return(predictions);
        }