Ejemplo n.º 1
0
        private static void GetColorValueFromAllOriginalValues(List <List <Countrystats> > allAllValues, int year, List <Response> responses)
        {
            //Find all values for year
            List <YearWithValue> valuesForYear = new List <YearWithValue>();

            foreach (var valuesForOnecountry in allAllValues)
            {
                if (valuesForOnecountry.Any(x => x.ListWithCategoriesWithYearsAndValues.Count > 1))
                {
                    Console.WriteLine("sucks");
                }
                if (valuesForOnecountry.First().doesContainAnyValues())
                {
                    Countrystats country = valuesForOnecountry.First();
                    CategoriesWithYearsAndValues categoriesWithYearsAndValues = country.ListWithCategoriesWithYearsAndValues.Find(z => z.YearsWithValues.Any(b => b.Year == year));
                    if (categoriesWithYearsAndValues != null)
                    {
                        var yearWithValues = categoriesWithYearsAndValues.YearsWithValues;
                        var valueForYear   = yearWithValues.Find(x => x.Year == year);
                        //  var valueForYear = valuesForOnecountry.First(y => y == y).ListWithCategoriesWithYearsAndValues.Find(z => z.YearsWithValues.Any(b => b.Year == year)).YearsWithValues.First(a => a.Year == year);
                        valuesForYear.Add(valueForYear);
                        //Find max of all values in year
                        float maxValue = valuesForYear.Where(x => x == x).Max(y => y.Value.value);
                        //Berechne color-Value für alles und weise es den korrekten responses zu
                        //calc color-value
                        float m = 2 * 255 / maxValue;
                        foreach (var item in valuesForYear)
                        {
                            try
                            {
                                int index = responses.FindIndex(x => x.valuePairList.Any(y => y.value == item.Value.value)); //Get first value-year list where any value is the same as one of the values in the list with values for year
                                var temp  = responses[index];
                                temp.colorVal = m * temp.valuePairList.First(x => x.year == year).value;
                                if (temp.colorVal < 0)
                                {
                                    temp.errorMessage = "colorValue below 0, ich fix es später";
                                    temp.colorVal     = 1;
                                }
                                responses[index] = temp;
                            }
                            catch (InvalidOperationException)
                            {
                                Console.WriteLine("Meier, kann nicht besser nach Kategorien filtern wo keine Werte drin sind");
                            }
                        }
                    }
                }
            }

            var countriesWithNoValuesInYear = responses.Where(x => x.colorVal == 0).ToArray();

            for (int i = 0; i < countriesWithNoValuesInYear.Count(); i++)
            {
                countriesWithNoValuesInYear[i].errorMessage = "Country has no value in the specific year";
            }
        }
Ejemplo n.º 2
0
        public async Task <Model> TrainTempModelAsync(Country country)
        {
            List <Countrystats> countrystats = new List <Countrystats>(); //list with countries and temp values
            //create list with all countries
            List <Country> countries = await dB.GetAllCountriesAsync();

            //get temp and connect to country
            for (int i = 0; i < countries.Count; i++)
            {
                Countrystats stat = new Countrystats();
                stat.Country = countries[i];
                stat.ListWithCategoriesWithYearsAndValues = await dB.GetCategoriesWithValuesAndYearsAsync(countries[i].name, new List <int>() { 77 });

                countrystats.Add(stat);
            }
            //Create Model
            MLContext        mLContext = new MLContext(seed: 0);
            List <TempModel> temps     = new List <TempModel>();

            for (int i = 0; i < countrystats.Count; i++)
            {
                var catlist = countrystats[i].ListWithCategoriesWithYearsAndValues;
                for (int j = 0; j < catlist.Count; j++) //catlist.count should be 1 anyway
                {
                    var valuelist = catlist[j].YearsWithValues;
                    for (int k = 1; k < valuelist.Count; k++)
                    {
                        temps.Add(new TempModel()
                        {
                            latitude = countrystats[i].Country.latitude, longitude = countrystats[i].Country.longitude,
                            temp     = valuelist[k].Value.value, lastYearValue = valuelist[k - 1].Value.value, year = valuelist[k].Year
                        });
                    }
                }
            }
            //Speichere Modell
            Model modelContainer = TrainTemp(mLContext, temps);

            dB.SaveModel(modelContainer, 0, 77);
            //Verwende Modell um

            return(modelContainer);
        }
Ejemplo n.º 3
0
        public async Task <List <Countrystats> > GenerateForEachCountryAsync(List <int> laenderIDs, List <int> kategorienIDs, int from, int futureYear)
        {
            List <string> laender = await dB.GetCountriesToCategoriesAsync(laenderIDs); //Liste mit allen Ländern

            List <Countrystats> countrystats = new List <Countrystats>();

            for (int i = 0; i < laender.Count; i++)
            {
                await Task.Run(async() =>
                {
                    Countrystats c = new Countrystats();
                    c.Country      = new Country(laender[i]);
                    c.ListWithCategoriesWithYearsAndValues = await GenerateAsync(laender[i], kategorienIDs, from, futureYear);
                    countrystats.Add(c);
                });

                Console.WriteLine("Land {0} wurde berechnet", laender[i]);
            }
            return(countrystats);
        }
Ejemplo n.º 4
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);
        }