Example #1
0
        private void EM2_RunDecompCountry(string countryShortName)
        {
            string currentAction = "";

            try
            {
                currentAction = "getting " + countryShortName + " config files";
                DataRow row = policyDataTable.Select("Country='" + countryShortName + "'")[0];
                string  sn1 = showFull ? row["System1"].ToString() : countryShortName + "_" + comboBox1.Text;
                string  sn2 = showFull ? row["System2"].ToString() : countryShortName + "_" + comboBox2.Text;
                CountryConfig.SystemRow sr1 = CountryAdministrator.GetCountryConfigFacade(countryShortName).GetSystemRowByName(sn1);
                CountryConfig.SystemRow sr2 = CountryAdministrator.GetCountryConfigFacade(countryShortName).GetSystemRowByName(sn2);

                // make a copy of the country, to be later stored in the temp-folder
                currentAction = "copying " + countryShortName + " for decomposition";
                Country             copiedCountry     = CountryAdministrator.GetCopyOfCountry(countryShortName);
                CountryConfigFacade ccf               = copiedCountry.GetCountryConfigFacade();
                DataConfigFacade    _dataConfigFacade = copiedCountry.GetDataConfigFacade();

                DataConfig.DataBaseRow dbr1 = null;
                DataConfig.DataBaseRow dbr2 = null;
                foreach (DataConfig.DataBaseRow dataSet in _dataConfigFacade.GetDataBaseRows())
                {
                    if (dataSet.Name == row["Data1"].ToString())
                    {
                        dbr1 = dataSet;
                    }
                    if (dataSet.Name == row["Data2"].ToString())
                    {
                        dbr2 = dataSet;
                    }
                }

                // then create all required systems, depending on decomposition and Alpha selection
                currentAction = "getting " + countryShortName + "'s uprate factors";
                Dictionary <string, string> upratingFactors1 = GetUpratingFactors(sr1, dbr1, countryShortName);
                Dictionary <string, string> upratingFactors2 = GetUpratingFactors(sr2, dbr2, countryShortName);
                double alpha = 0;

                List <DecompSystem> allSystems = new List <DecompSystem>();
                bool treatAsMarket             = chkTreatAsMarket.Checked;

                // get the systems of the checked addon (if one was checked)
                string addon    = GetCheckedAddon();
                bool   hasAddon = addon != string.Empty;
                List <AddOnSystemInfo> addonSystems = hasAddon ? AddOnInfoHelper.GetAddOnSystemInfo(addon) : null;

                currentAction = "creating " + countryShortName + "'s decomposed systems";

                const double  ALPHA_CPI = double.MinValue, ALPHA_MII = double.MaxValue; // just any numbers differnt from the alphas in alphaFIX
                List <double> alphas = new List <double>();
                if (checkBoxAlphaCPI.Checked)
                {
                    alphas.Add(ALPHA_CPI);
                }
                if (checkBoxAlphaMII.Checked)
                {
                    alphas.Add(ALPHA_MII);
                }
                if (checkBoxAlphaFIX.Checked)
                {
                    alphas.AddRange(alphaFIXValues);                           // those where gathered in GetAlphaFIX
                }
                foreach (double a in alphas)
                {
                    string systemNameExt = "";
                    // first find Alpha, log text, system name extention etc.
                    if (a == ALPHA_CPI)
                    {
                        Dictionary <string, string> rawIndices = GetRawCPIindices(sr1, countryShortName);
                        if (!(rawIndices.ContainsKey(comboBox1.Text) || rawIndices.ContainsKey(comboBox2.Text)))
                        {
                            throw new Exception("The CPI raw indices ('" + FactorCPI + "') were not found for the selected years!");
                        }
                        double hicp1 = EM_Helpers.SaveConvertToDouble(rawIndices[comboBox1.Text]);
                        double hicp2 = EM_Helpers.SaveConvertToDouble(rawIndices[comboBox2.Text]);
                        alpha = hicp2 / hicp1;
                        AddToLog($"{RunLogger.PetInfo.LOGTAG_ALPHA_CPI} ({countryShortName})", $"{alpha} ({hicp2}/{hicp1})", LOGMODE.EM2);
                        systemNameExt = "_cpi";
                        alphaValues.Add(countryShortName + "_cpi", alpha);
                    }
                    else if (a == ALPHA_MII)
                    {
                        double mii1 = getAlphaFromBaselineFile(sr1.Name);
                        double mii2 = getAlphaFromBaselineFile(sr2.Name);
                        alpha = mii2 / mii1;
                        AddToLog($"{RunLogger.PetInfo.LOGTAG_ALPHA_MII} ({countryShortName})", $"{alpha} ({mii2}/{mii1})", LOGMODE.EM2);
                        systemNameExt = "_mii";
                        alphaValues.Add(countryShortName + "_mii", alpha);
                    }
                    else
                    {
                        alpha         = a;
                        systemNameExt = "_a" + GetAlphaFIXId(a);
                        alphaValues.Add(countryShortName + "_fix" + GetAlphaFIXId(a), alpha);
                    }

                    // Then actually create the required systems
                    if (checkRadioData1.Checked || checkRadioDataBoth.Checked)
                    {
                        DecompSystem ds1 = new DecompSystem();
                        ds1.sr = CountryConfigFacade.CopySystemRow(sr2.Name + "_on_" + dbr1.Name + systemNameExt, ccf.GetSystemRowByID(sr2.ID));
                        copiedCountry.GetDataConfigFacade().CopyDBSystemConfigRows(ccf.GetSystemRowByID(sr2.ID), ds1.sr);
                        ds1.dbr = dbr1;
                        if (hasAddon)
                        {
                            MergeAddOn(addonSystems, sr2.Name, ds1, copiedCountry);
                        }
                        EM2_FixUprating(ds1.sr, upratingFactors1, upratingFactors2, countryShortName, alpha, 1, treatAsMarket);
                        allSystems.Add(ds1);
                        if (checkRadioMonetary.Checked)
                        {
                            DecompSystem ds2 = new DecompSystem();
                            ds2.sr = CountryConfigFacade.CopySystemRow(sr2.Name + "ind_on_" + dbr1.Name + systemNameExt, ccf.GetSystemRowByID(sr2.ID));
                            copiedCountry.GetDataConfigFacade().CopyDBSystemConfigRows(ccf.GetSystemRowByID(sr2.ID), ds2.sr);
                            ds2.dbr = dbr1;
                            if (hasAddon)
                            {
                                MergeAddOn(addonSystems, sr2.Name, ds2, copiedCountry);
                            }
                            EM2_FixUprating(ds2.sr, upratingFactors1, upratingFactors2, countryShortName, alpha, 2, treatAsMarket);
                            allSystems.Add(ds2);
                            DecompSystem ds3 = new DecompSystem();
                            ds3.sr = CountryConfigFacade.CopySystemRow(sr1.Name + "ind" + systemNameExt, ccf.GetSystemRowByID(sr1.ID));
                            copiedCountry.GetDataConfigFacade().CopyDBSystemConfigRows(ccf.GetSystemRowByID(sr1.ID), ds3.sr);
                            ds3.dbr = dbr1;
                            if (hasAddon)
                            {
                                MergeAddOn(addonSystems, sr1.Name, ds3, copiedCountry);
                            }
                            EM2_FixUprating(ds3.sr, upratingFactors1, upratingFactors2, countryShortName, alpha, 3, treatAsMarket);
                            allSystems.Add(ds3);
                        }
                    }
                    if (checkRadioData2.Checked || checkRadioDataBoth.Checked)
                    {
                        DecompSystem ds1 = new DecompSystem();
                        ds1.sr = CountryConfigFacade.CopySystemRow(sr1.Name + "_on_" + dbr2.Name + systemNameExt, ccf.GetSystemRowByID(sr1.ID));
                        copiedCountry.GetDataConfigFacade().CopyDBSystemConfigRows(ccf.GetSystemRowByID(sr1.ID), ds1.sr);
                        ds1.dbr = dbr2;
                        if (hasAddon)
                        {
                            MergeAddOn(addonSystems, sr1.Name, ds1, copiedCountry);
                        }
                        EM2_FixUprating(ds1.sr, upratingFactors2, upratingFactors1, countryShortName, 1 / alpha, 1, treatAsMarket);
                        allSystems.Add(ds1);
                        if (checkRadioMonetary.Checked)
                        {
                            DecompSystem ds2 = new DecompSystem();
                            ds2.sr = CountryConfigFacade.CopySystemRow(sr1.Name + "ind_on_" + dbr2.Name + systemNameExt, ccf.GetSystemRowByID(sr2.ID));
                            copiedCountry.GetDataConfigFacade().CopyDBSystemConfigRows(ccf.GetSystemRowByID(sr1.ID), ds2.sr);
                            ds2.dbr = dbr2;
                            if (hasAddon)
                            {
                                MergeAddOn(addonSystems, sr1.Name, ds2, copiedCountry);
                            }
                            EM2_FixUprating(ds2.sr, upratingFactors2, upratingFactors1, countryShortName, 1 / alpha, 2, treatAsMarket);
                            allSystems.Add(ds2);
                            DecompSystem ds3 = new DecompSystem();
                            ds3.sr = CountryConfigFacade.CopySystemRow(sr2.Name + "ind" + systemNameExt, ccf.GetSystemRowByID(sr2.ID));
                            copiedCountry.GetDataConfigFacade().CopyDBSystemConfigRows(ccf.GetSystemRowByID(sr2.ID), ds3.sr);
                            ds3.dbr = dbr1;
                            if (hasAddon)
                            {
                                MergeAddOn(addonSystems, sr2.Name, ds3, copiedCountry);
                            }
                            EM2_FixUprating(ds3.sr, upratingFactors2, upratingFactors1, countryShortName, 1 / alpha, 3, treatAsMarket);
                            allSystems.Add(ds3);
                        }
                    }
                }

                currentAction = "writting decomposed " + countryShortName + " in the temp folder";

                copiedCountry.WriteXML(EMPath.Folder_Temp(EM_AppContext.FolderEuromodFiles));

                currentAction = "running " + countryShortName + "'s decomposed systems";
                foreach (DecompSystem ds in allSystems)
                {
                    workers.Add(RunSystem(countryShortName, ds.sr.Name, dbr2.Name, EM2_CreateConfig(countryShortName, textBoxOutputPath.Text, ds.dbr, ds.sr)));
                    updateInfoLabel();
                }
            }
            catch (Exception ex)
            {
                em3_petInfo.AddSystemIndependentError($"There was a problem with {currentAction}: {ex.Message}");
            }
        }
Example #2
0
        private void EM2_RunBaselineSystems()
        {
            // get the systems of the checked addon (if one was checked)
            string addon    = GetCheckedAddon();
            bool   hasAddon = addon != string.Empty;
            List <AddOnSystemInfo> addonSystems = hasAddon ? AddOnInfoHelper.GetAddOnSystemInfo(addon) : null;

            foreach (DataRow row in policyDataTable.Rows)
            {
                if (row.Field <bool>("Check"))
                {
                    string                 countryShortName = row["Country"].ToString();
                    Country                copiedCountry    = CountryAdministrator.GetCopyOfCountry(countryShortName);
                    CountryConfigFacade    ccf = copiedCountry.GetCountryConfigFacade();
                    DataConfigFacade       _dataConfigFacade = copiedCountry.GetDataConfigFacade();
                    DataConfig.DataBaseRow dbr1 = null;
                    DataConfig.DataBaseRow dbr2 = null;
                    foreach (DataConfig.DataBaseRow dataSet in _dataConfigFacade.GetDataBaseRows())
                    {
                        if (dataSet.Name == row["Data1"].ToString())
                        {
                            dbr1 = dataSet;
                        }
                        if (dataSet.Name == row["Data2"].ToString())
                        {
                            dbr2 = dataSet;
                        }
                    }

                    string sn1 = showFull ? row["System1"].ToString() : countryShortName + "_" + comboBox1.Text;
                    string sn2 = showFull ? row["System2"].ToString() : countryShortName + "_" + comboBox2.Text;
                    CountryConfig.SystemRow sr1 = ccf.GetSystemRowByName(sn1);
                    CountryConfig.SystemRow sr2 = ccf.GetSystemRowByName(sn2);

                    if (sr1 == null)
                    {
                        throw new Exception("System '" + sn1 + "' does not exist!");
                    }
                    if (sr2 == null)
                    {
                        throw new Exception("System '" + sn2 + "' does not exist!");
                    }

                    if (hasAddon)
                    {
                        if (checkRadioData1.Checked || checkRadioDataBoth.Checked)
                        {
                            MergeAddOn(addonSystems, copiedCountry, ref sr1);
                        }
                        if (checkRadioData2.Checked || checkRadioDataBoth.Checked)
                        {
                            MergeAddOn(addonSystems, copiedCountry, ref sr2);
                        }
                    }

                    copiedCountry.WriteXML(EMPath.Folder_Temp(EM_AppContext.FolderEuromodFiles));

                    if (checkBoxAlphaMII.Checked)
                    {
                        SystemBackgroundWorker w1, w2;
                        w1                = RunSystem(countryShortName, sr1.Name, dbr1.Name, EM2_CreateConfig(countryShortName, textBoxOutputPath.Text, dbr1, sr1, true));
                        w2                = RunSystem(countryShortName, sr2.Name, dbr2.Name, EM2_CreateConfig(countryShortName, textBoxOutputPath.Text, dbr2, sr2, true));
                        w1.isBaseline     = true;
                        w2.isBaseline     = true;
                        w1.secondBaseline = w2;
                        w2.secondBaseline = w1;
                        workers.Add(w1);
                        workers.Add(w2);
                        updateInfoLabel();
                    }
                    else
                    {
                        SystemBackgroundWorker w1 = null, w2 = null;
                        if (checkRadioData1.Checked || checkRadioDataBoth.Checked)
                        {
                            w1            = RunSystem(countryShortName, sr1.Name, dbr1.Name, EM2_CreateConfig(countryShortName, textBoxOutputPath.Text, dbr1, sr1, hasAddon));
                            w1.isBaseline = true;
                        }
                        if (checkRadioData2.Checked || checkRadioDataBoth.Checked)
                        {
                            w2            = RunSystem(countryShortName, sr2.Name, dbr2.Name, EM2_CreateConfig(countryShortName, textBoxOutputPath.Text, dbr2, sr2, hasAddon));
                            w2.isBaseline = true;
                        }

                        if (hasAddon && checkRadioDataBoth.Checked)
                        {
                            w1.secondBaseline = w2;
                            w2.secondBaseline = w1;
                        }
                        if (checkRadioData1.Checked || checkRadioDataBoth.Checked)
                        {
                            workers.Add(w1);
                        }
                        if (checkRadioData2.Checked || checkRadioDataBoth.Checked)
                        {
                            workers.Add(w2);
                        }
                        updateInfoLabel();
                    }
                }
            }
        }