private Dictionary <string, string> EM3_CreateConfig(string countryShortName, string outputPath,
                                                             DataConfig.DataBaseRow dbr, CountryConfig.SystemRow sr,
                                                             string parModifications = null)
        {
            Dictionary <string, string> config = new Dictionary <string, string>();

            config.Add(TAGS.CONFIG_PATH_OUTPUT, outputPath);
            config.Add(TAGS.CONFIG_PATH_DATA, EM_AppContext.FolderInput);
            config.Add(TAGS.CONFIG_PATH_EUROMODFILES, EM_AppContext.FolderEuromodFiles);
            config.Add(TAGS.CONFIG_COUNTRY, CountryAdministrator.GetCountryFileName(countryShortName).ToLower().Replace(".xml", ""));
            config.Add(TAGS.CONFIG_ID_DATA, dbr.ID);
            config.Add(TAGS.CONFIG_ID_SYSTEM, sr.ID);
            if (textRunFirstNHH.Text != null && !string.IsNullOrEmpty(textRunFirstNHH.Text) && int.TryParse(textRunFirstNHH.Text, out _))
            {
                config.Add(TAGS.CONFIG_FIRST_N_HH_ONLY, textRunFirstNHH.Text);
            }
            if (parModifications != null)
            {
                config.Add(TAGS.CONFIG_STRING_PAR_MODIFICATIONS, parModifications);
            }

            string addOn = GetCheckedAddon();

            if (!string.IsNullOrEmpty(addOn))
            {
                string addOnSys = null;
                foreach (AddOnSystemInfo aos in AddOnInfoHelper.GetAddOnSystemInfo(addOn))
                {
                    foreach (string sysPattern in aos._supportedSystems)
                    {
                        if (EM_Helpers.DoesValueMatchPattern(sysPattern, sr.Name))
                        {
                            addOnSys = aos._addOnSystemName; break;
                        }
                    }
                }
                if (addOnSys == null)
                {
                    throw new Exception($"No matching Addon found for {sr.Name}");
                }
                config.Add(TAGS.CONFIG_ADDON, $"{addOn}|{addOnSys}");
            }
            return(config);
        }
示例#2
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}");
            }
        }
        // this does not only add the addOn-info, but also collects the country- and addOn-file that need to be transformed to EM3 format
        bool EM3_AddAddOnInfo(string outputPath, Dictionary <ConfigKey, Dictionary <string, string> > contentEMConfigs, RunMainForm runMainForm)
        {
            addOnsToTransform = new List <string>(); countriesToTransform = new List <string>(); // for collecting country- and addon-files which need to be transformed to EM3

            foreach (DataGridViewRow systemRow in runMainForm.dgvRun.Rows)                       // loop over systems
            {
                // assess whether any add-on is selected for run (theoretically more than one add-on can be selected for a system)
                List <AddOnSystemInfo> selectedAddOns = new List <AddOnSystemInfo>();
                foreach (DataGridViewColumn column in runMainForm.dgvRun.Columns)
                {
                    if (column.Name.StartsWith(RunMainForm._colAddOnPrefix) && EM_Helpers.SaveConvertToBoolean(systemRow.Cells[column.Name].Value))
                    {
                        if (!AddOnInfoHelper.GetSupportedSystemInfo(systemRow.Cells[runMainForm.colSystem.Name].Value.ToString(),
                                                                    column.Tag as List <AddOnSystemInfo>, out List <AddOnSystemInfo> supportedAOSystemInfo))
                        {
                            return(false);
                        }
                        selectedAddOns.AddRange(supportedAOSystemInfo);
                    }
                }

                // we need to know whether the system is selected, first to collect the country for transformation (see remark above)
                // and, if it is selected in addition to any add-on(s), we need to copy the configuration file (instead of just adapting it)
                bool   isSystemSelected = EM_Helpers.SaveConvertToBoolean(systemRow.Cells[runMainForm.colRun.Name].Value);
                string countryShortName = systemRow.Cells[runMainForm.colCountry.Name].Value.ToString().ToLower();
                if (isSystemSelected || selectedAddOns.Any())
                {
                    string countryToTransform = countryShortName;
                    if (!countriesToTransform.Contains(countryToTransform))
                    {
                        countriesToTransform.Add(countryToTransform);
                    }
                }

                if (!selectedAddOns.Any())
                {
                    continue;
                }

                // to find the config (prepared in GenerateEMConfigs) we need the dataset
                DataConfig.DBSystemConfigRow dbSystemConfigRow = runMainForm.GetSelectedDBSystemCombination(systemRow); if (dbSystemConfigRow == null)
                {
                    continue;                                                                                                                                    // should not happen
                }
                ConfigKey key = new ConfigKey(countryShortName, dbSystemConfigRow.DataBaseID, dbSystemConfigRow.SystemID);
                if (!contentEMConfigs.ContainsKey(key))
                {
                    continue;                                     // should not happen
                }
                Dictionary <string, string> contentEMConfig = contentEMConfigs[key];

                // if the system itself is selected too, we need to make a copy of the config (i.e. run the plain system and run the add-on)
                if (isSystemSelected)
                {
                    contentEMConfig = new Dictionary <string, string>();
                    foreach (var entry in contentEMConfigs[key])
                    {
                        contentEMConfig.Add(entry.Key, entry.Value);
                    }
                    ConfigKey someKey = new ConfigKey(Guid.NewGuid().ToString(), string.Empty, string.Empty); // the key is irrelevant
                    contentEMConfigs.Add(someKey, contentEMConfig);
                }

                int cnt = 0;
                foreach (AddOnSystemInfo addOnSystemInfo in selectedAddOns)                                       // loop over the add-ons which are selected for run (usually one)
                {
                    contentEMConfig[RunMainForm._labelRunFormInfoText] += addOnSystemInfo._addOnSystemName + " "; // add to text that is displayed in the window showing run status
                    // add: $"ADDON{cnt}", $"{addOn}|{addOnSys}"
                    contentEMConfig.Add(TAGS.EM2CONFIG_ADDON + (cnt++).ToString(), addOnSystemInfo._addOnShortName + "|" + addOnSystemInfo._addOnSystemName);

                    string addOnToTransform = addOnSystemInfo._addOnShortName.ToLower();
                    if (!addOnsToTransform.Contains(addOnToTransform))
                    {
                        EM_AppContext.Instance.WriteXml(addOnSystemInfo._addOnShortName, false, false); // make sure the EM2 version is saved
                        addOnsToTransform.Add(addOnToTransform);
                    }
                }
            }
            return(true);
        }
示例#4
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();
                    }
                }
            }
        }