Example #1
0
 public CalculateData()
 {
     try
     {
         dB   = new DB();
         cNTK = new CNTK(dB);
     }
     catch (Exception)
     {
         Console.WriteLine("Connection to DB failed");
     }
 }
Example #2
0
        //for all kind of gas
        public async Task <List <YearWithValue> > TrainAndPredictEmissionsAsync(List <YearWithValue> ListWithCO, List <YearWithValue> Population, int FutureYear)
        {
            MLContext mlContext = new MLContext(seed: 0);

            ListWithCO = ListWithCO.Distinct().ToList();
            int coaid = await dB.GetCountryByNameAsync(ListWithCO.First(x => x.Name != null).Name);      //Inshallah ist in dieser liste nie kein name irgendwo

            int catid = ListWithCO.First(x => x.cat_id != 0).cat_id;
            List <EmissionModel> inputs = new List <EmissionModel>();

            if (!(Population.Count > 0)) //ohje
            {
                Console.WriteLine("Zu diesem Punkt im Programm sollte es eigentlich nie kommen. Ich hab aber keine Zeit, das ordentlich zu fixen. Darum hier diese Pfusch-Lösung mit dieser Ausgabe als Erinnerung, dass ich das gscheid behebe, wenn noch Zeit überbleibt");
                Population = await dB.GetPopulationByCoaIdAsync(coaid);
            }
            // ListWithCO = GetDistinctValues(ListWithCO);
            foreach (var JahrMitCO in ListWithCO)
            {
                float tempyear = JahrMitCO.Year;
                foreach (var JahrMitPopulation in Population)
                {
                    if (JahrMitPopulation.Year == tempyear)
                    {
                        inputs.Add(new EmissionModel()
                        {
                            Year = tempyear, Population = JahrMitPopulation.Value.value, Co2 = JahrMitCO.Value.value
                        });
                    }
                }
            }
            Model modelContainer = TrainEmission(mlContext, inputs);
            //  dB.SaveModelAsParameter(modelContainer, coaid, catid, GetErrorMLNet(modelContainer));
            var    model = modelContainer.trainedModel;
            double j     = inputs.Max(x => x.Year);

            if (j < FutureYear)
            {
                CNTK cNTK = new CNTK(dB);
                j++;
                ListWithCO = await PredictCo2OverYearsAsync(modelContainer, FutureYear, coaid, ListWithCO, cNTK);
            }
            dB.SaveModel(modelContainer, coaid, catid);
            return(ListWithCO);
        }
Example #3
0
        private async Task <List <CategoriesWithYearsAndValues> > GenerateAsync(string country, List <int> kategorienIDs, int from, int futureYear)
        {
            Countrystats countrystats = new Countrystats();                                                                            //Klasse für alle Kategorien und deren Werte per Jahr

            countrystats.Country = new Country(country);                                                                               //Land zu dem die Kategorien mit Werte gehören
            countrystats.ListWithCategoriesWithYearsAndValues = await dB.GetCategoriesWithValuesAndYearsAsync(country, kategorienIDs); //Werte mit Jahren

            categorycount = countrystats.ListWithCategoriesWithYearsAndValues.Count;                                                   //wie viele kategorien an daten für dieses land existieren
            List <CategoriesWithYearsAndValues> CategorysWithFutureValues = new List <CategoriesWithYearsAndValues>();

            Task <List <YearWithValue> >[] liste           = new Task <List <YearWithValue> > [categorycount]; //liste damit jede kategorie in einem task abgearbeitet werden kann
            List <YearWithValue>           PopulationTotal = new List <YearWithValue>();

            //Arbeite jede Kategorie parallel ab
            for (int i = 0; i < categorycount; i++)
            {
                //Erstelle für jede Kategorie einen Liste mit eigenen Datensätzen
                List <YearWithValue> SingleCategoryData = new List <YearWithValue>();

                //Hole einzelne Datensätze für jedes Jahr heraus
                foreach (var YearWithValue in countrystats.ListWithCategoriesWithYearsAndValues[i].YearsWithValues)
                {
                    SingleCategoryData.Add(new YearWithValue(YearWithValue.Year, new Wert(Convert.ToDecimal(YearWithValue.Value.value)), countrystats.Country.name, YearWithValue.cat_id));
                }
                //Wenn ein Wert nicht dokumentiert ist, ist in der Datenbank 0 drin. Das verfälscht den Wert für die Ki
                //entferne deswegen 0
                SingleCategoryData = AI.RemoveZero(SingleCategoryData);
                //Wenn es mindestens ein Jahr einer Kategorie gibt, in der der Wert nicht 0 ist
                if (SingleCategoryData.Count > 1)
                {
                    int coaid = await dB.GetCountryByNameAsync(country);                                                             //numeric of country

                    int categ = await dB.GetCategoryByNameAsync(countrystats.ListWithCategoriesWithYearsAndValues[i].category.name); //numeric of category

                    //Bearbeite eigenen Datensatz
                    int multi = Scale(SingleCategoryData) - 1; //wie viel man die normierten werte mulitplizieren muss damit sie wieder echt sind

                    if (SingleCategoryData.Any(x => x.cat_id == 4))
                    {
                        PopulationTotal = SingleCategoryData;
                    }
                    if (DifferentValuesCount(SingleCategoryData) > 2)
                    {
                        //linear train
                        liste[i] = Task.Run(async() =>
                        {
                            if (await dB.GetMaxYearAsync(coaid, categ) >= futureYear) //if all wanted values are already known
                            {
                                return(SingleCategoryData);
                            }
                            else
                            {
                                if ((SingleCategoryData.Any(x => x.cat_id > 38 && x.cat_id < 46)) || SingleCategoryData.Any(x => x.cat_id == 77)) //if categoy is an emission-type or temp
                                {
                                    ML mL = new ML(dB);
                                    if (dB.CheckModel(coaid, categ)) //check for model
                                    {
                                        List <YearWithValue> yearWithValues = new List <YearWithValue>();
                                        if (categ == 77) //if temp
                                        {
                                            Model model    = dB.LoadModel(0, 77);
                                            yearWithValues = await mL.PredictTempOverYearsAsync(model, futureYear, SingleCategoryData, countrystats.Country);
                                            return(yearWithValues);
                                        }
                                        else
                                        {
                                            Model modelContainer = dB.LoadModel(coaid, categ);
                                            yearWithValues       = await mL.PredictCo2OverYearsAsync(modelContainer, futureYear, coaid, SingleCategoryData, cNTK);
                                        }
                                        return(yearWithValues);
                                    }
                                    else //calculate model
                                    {
                                        if (categ == 77) //if temp
                                        {
                                            Model model            = await mL.TrainTempModelAsync(countrystats.Country);
                                            List <YearWithValue> x = await mL.PredictTempOverYearsAsync(model, futureYear, SingleCategoryData, countrystats.Country);
                                            return(x);
                                        }
                                        else
                                        {
                                            List <YearWithValue> x = await mL.TrainAndPredictEmissionsAsync(SingleCategoryData, PopulationTotal, futureYear);
                                            return(x);
                                        }
                                    }
                                }
                                else //if category is non-emission and no temp
                                {
                                    bool parameterExists = await dB.CheckParametersAsync(coaid, categ); //check if parameter for this country and this category exist
                                    if (parameterExists)
                                    {
                                        Console.WriteLine("Daten werden von Datenbank genommen");
                                        ParameterStorage parStor            = await dB.GetParameterAsync(coaid, categ); //Bekomme Parameter
                                        List <YearWithValue> yearWithValues = new List <YearWithValue>();
                                        foreach (var item in countrystats.ListWithCategoriesWithYearsAndValues[i - 1].YearsWithValues)
                                        {
                                            yearWithValues.Add(new YearWithValue(item.Year, new Wert(Convert.ToDecimal(item.Value.value)), countrystats.Country.name, item.cat_id));
                                        }
                                        yearWithValues = AI.RemoveZero(yearWithValues);
                                        yearWithValues = cNTK.Predict(yearWithValues, from, futureYear, parStor);
                                        return(yearWithValues);
                                    }
                                    else
                                    {
                                        CNTK cNTK = new CNTK(dB);
                                        List <YearWithValue> x = await cNTK.TrainLinearOneOutputAsync(SingleCategoryData, futureYear);
                                        if (SingleCategoryData.Any(a => a.cat_id == 4))
                                        {
                                            PopulationTotal = x;
                                        }
                                        return(PopulationTotal);
                                    }
                                }
                            }


                            //
                        });
                    }
                    else
                    {
                        liste[i] = Task <List <YearWithValue> > .Run(() =>
                        {
                            return(sigmoid.TrainSigmoid(SingleCategoryData, futureYear, multi));
                        });
                    }
                }


                //ohne dieses else gäbe es einige leere Tasks im Array -> Exception
                //ohne if geht die KI datensätze ohne einträge durch -> Verschwendung von Rechenleistung und Zeit
                else
                {
                    liste[i] = Task.Run(() => { return(new List <YearWithValue>()); });
                }
            }

            //Warte parallel bis alle Kategorien gelernt und berechnet wurden
            Task.WaitAll(liste);
            //returne alle Kategorien
            for (int i = 0; i < categorycount; i++)
            {
                CategorysWithFutureValues.Add(new CategoriesWithYearsAndValues(countrystats.ListWithCategoriesWithYearsAndValues[i].category, liste[i].Result));
            }

            return(CategorysWithFutureValues);
        }
Example #4
0
        public async Task <List <YearWithValue> > PredictCo2OverYearsAsync(Model modelContainer, int futureYear, int coa_id, List <YearWithValue> emissions, CNTK cNTK)
        {
            //Get Population till future year
            List <YearWithValue> population = await dB.GetPopulationByCoaIdAsync(coa_id);       //get population that is known

            if (CompareBiggestValueToFutureYear(population, futureYear))                        //check if known population is enough to predict emission
            {
                population = await cNTK.PredictPopulationAsync(coa_id, futureYear, population); //get population to predict emission
            }
            EmissionModel[] populationData = new EmissionModel[population.Count];
            for (int i = 0; i < populationData.Count(); i++)
            {
                populationData[i] = new EmissionModel()
                {
                    Year = population[i].Year, Population = population[i].Value.value
                };
            }
            PredictionEngine <EmissionModel, EmissionPrediction> predictionEngine = modelContainer.mLContext.Model.CreatePredictionEngine <EmissionModel, EmissionPrediction>(modelContainer.trainedModel);
            IDataView inputData   = modelContainer.mLContext.Data.LoadFromEnumerable(populationData);
            IDataView predictions = modelContainer.trainedModel.Transform(inputData);

            float[] scoreColumn = predictions.GetColumn <float>("Score").ToArray();

            for (int i = emissions.Count; i < scoreColumn.Length; i++)
            {
                emissions.Add(new YearWithValue(population[i].Year, new Wert(scoreColumn[i], true)));
            }
            return(emissions);
        }