473,418 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

Energy Model code modification to account for year dependent interest rate

The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions interest rate in two functions)
2-ModelSettings.py is in charge of setting the model data, here you specify the structure of the data (mentions interest rate in two identical structures for the transmission lines and the regional parameters)
3-Excel.py reads the ModelSettings.py, writes the excel files and then once they are populated it reads the date and passes them to ModelData.py
4-ModelData.py does some checks and stores the data.
5-ModelVariables.py calculates the variables of the problem using ModelData.py and the functions in Utility.py
6-These are then called by Build.py that has the objective function, then there is Main.py that launches the model and other scripts that i believe are not relevant.
I have modified the ModelSetting.py changing the interest_rate declaration and coping it from investments costs that are defined for every year, every technology and every region as follows:
original:
Expand|Select|Wrap|Line Numbers
  1. if self.mode == ModelMode.Planning:
  2.             connection_parameters_template.update(
  3. ...
  4.                     "interest_rate": {
  5.                         "sheet_name": "Interest_rate",
  6.                         "value": 0.05,
  7.                         "index": pd.Index(
  8.                             ["Interest Rate"], name="Performance Parameter"
  9.                         ),
  10.                         "columns": indexer,
  11.                     },
  12. ...
modified:
Expand|Select|Wrap|Line Numbers
  1. "interest_rate": {
  2.                         "sheet_name": "Interest_rate",
  3.                         "value": 0.05,
  4.                         "index": pd.Index(self.years, name="Years"), #GA modified
  5.                         "columns": indexer,
  6.                     },
This works and gives me the excel file with the correct structure.
Then I have modified ModelVariables.py, again coping from investments:
Original:
Expand|Select|Wrap|Line Numbers
  1. for key in self.new_capacity[reg].keys():
  2.  
  3.                 real_new_capacity_regional[key] = shift_new_cap(
  4.                     self.new_capacity[reg][key], 
  5.                     self.model_data.settings.technologies[reg][key], 
  6.                     self.model_data.regional_parameters[reg]["time_of_construction"].loc[:, key],
  7.                     self.model_data.settings.years)
  8.  
  9.                 (
  10.                     cost_inv_regional[key],
  11.                     cost_inv_tax_regional[key],
  12.                     cost_inv_sub_regional[key],
  13.                 ) = invcosts(
  14.                     self.model_data.regional_parameters[reg]["tech_inv"][key],
  15.                     self.new_capacity[reg][key],
  16.                     self.model_data.regional_parameters[reg]["inv_taxsub"]["Tax"][key],
  17.                     self.model_data.regional_parameters[reg]["inv_taxsub"]["Sub"][key],
  18.                 )
  19.  
  20.                 salvage_inv_regional[key] = cp.multiply(
  21.                     salvage_factor(
  22.                         self.model_data.settings.years,
  23.                         self.model_data.settings.technologies[reg][key],
  24.                         self.model_data.regional_parameters[reg]["tech_lifetime"].loc[:, key],
  25.                         self.model_data.regional_parameters[reg]["interest_rate"].loc[:, key],
  26.                         self.model_data.regional_parameters[reg]["discount_rate"],
  27.                         self.model_data.regional_parameters[reg]["economic_lifetime"].loc[:, key],
  28.                     ),
  29.                     cost_inv_regional[key],
  30.                 )
modified:
Expand|Select|Wrap|Line Numbers
  1. for key in self.new_capacity[reg].keys():
  2.  
  3.                 real_new_capacity_regional[key] = shift_new_cap(
  4.                     self.new_capacity[reg][key], 
  5.                     self.model_data.settings.technologies[reg][key], 
  6.                     self.model_data.regional_parameters[reg]["time_of_construction"].loc[:, key],
  7.                     self.model_data.settings.years)
  8.  
  9.                 (
  10.                     cost_inv_regional[key],
  11.                     cost_inv_tax_regional[key],
  12.                     cost_inv_sub_regional[key],
  13.                 ) = invcosts(
  14.                     self.model_data.regional_parameters[reg]["tech_inv"][key],
  15.                     self.new_capacity[reg][key],
  16.                     self.model_data.regional_parameters[reg]["inv_taxsub"]["Tax"][key],
  17.                     self.model_data.regional_parameters[reg]["inv_taxsub"]["Sub"][key],
  18.                 )
  19.  
  20.                 salvage_inv_regional[key] = cp.multiply(
  21.                     salvage_factor(
  22.                         self.model_data.settings.years,
  23.                         self.model_data.settings.technologies[reg][key],
  24.                         self.model_data.regional_parameters[reg]["tech_lifetime"].loc[:, key],
  25.                         self.model_data.regional_parameters[reg]["interest_rate"][key], #GA modify
  26.                         self.model_data.regional_parameters[reg]["discount_rate"],
  27.                         self.model_data.regional_parameters[reg]["economic_lifetime"].loc[:, key],
  28.                     ),
  29.                     cost_inv_regional[key],
  30.                 )
Now if i do not change the utility function where it uses the interest rate it gives me the error:
Expand|Select|Wrap|Line Numbers
  1. File ~/anaconda3/envs/hypatia/lib/python3.9/site-packages/pandas/core/indexing.py:1941 in _setitem_with_indexer_2d_value
  2.     raise ValueError(
  3.  
  4. ValueError: Must have equal len keys and value when setting with an ndarray
This are the original two functions:
the first one:
Expand|Select|Wrap|Line Numbers
  1. def invcosts_annuity(
  2.     cost_inv_present,
  3.     interest_rate,
  4.     economiclife,
  5.     technologies,
  6.     main_years,
  7.     discount_rate,
  8. ):
  9.  
  10.     """
  11.     Calculates the annuities of the investment costs based on the interest rate
  12.     and economic lifetime of each technology
  13.     """
  14.  
  15.     depreciation = np.divide(
  16.         np.multiply(
  17.             np.power((interest_rate.values + 1), economiclife.values),
  18.             interest_rate.values,
  19.         ),
  20.         (np.power((interest_rate.values + 1), economiclife.values) - 1),
  21.     )
  22.     depreciation = pd.DataFrame(
  23.         depreciation, index=["Depreciation_rate"], columns=technologies
  24.     )
  25.  
  26.     inv_fvalue_total = 0
  27.     for tech_indx, tech in enumerate(technologies):
  28.         inv_fvalue_discounted = 0
  29.         for y_indx, year in enumerate(main_years):
  30.  
  31.             inv_fvalue_annual_discounted = 0
  32.             for future_year in range(
  33.                 y_indx + 1, y_indx + economiclife.loc["Economic Life time", tech] + 1
  34.             ):
  35.  
  36.                 annuity = (
  37.                     cost_inv_present[y_indx, tech_indx]
  38.                     * depreciation.loc["Depreciation_rate", tech]
  39.                 )
  40.  
  41.                 inv_fvalue_annual_discounted += annuity * (
  42.                     1 + discount_rate.loc[year, "Annual Discount Rate"]
  43.                 ) ** (-future_year)
  44.  
  45.             inv_fvalue_discounted += inv_fvalue_annual_discounted
  46.  
  47.         inv_fvalue_total += inv_fvalue_discounted
  48.  
  49.     return inv_fvalue_total
the second one:
Expand|Select|Wrap|Line Numbers
  1. def salvage_factor(
  2.     main_years, technologies, tlft, interest_rate, discount_rate, economiclife
  3. ):
  4.  
  5.     """
  6.     Calculates the salvage factor of the investment cost for the capacities
  7.     that remain after the end of the time horizon to avoid the end of the horizon
  8.     effect
  9.     """
  10.  
  11.     salvage_factor_0 = pd.DataFrame(0, index=main_years, columns=technologies)
  12.  
  13.     rates_factor = pd.DataFrame(0, index=main_years, columns=technologies)
  14.  
  15.     EOH = len(main_years) - 1
  16.  
  17.     for tech in technologies:
  18.  
  19.         technical_factor = (1 - 1 / (1 + interest_rate[tech].values)) / (
  20.             1 - 1 / ((1 + interest_rate[tech].values) ** economiclife[tech].values)
  21.         )
  22.  
  23.         social_factor = (
  24.             1 - 1 / ((1 + discount_rate.values) ** economiclife[tech].values)
  25.         ) / (1 - 1 / (1 + discount_rate.values))
  26.  
  27.         rates_factor.loc[:, tech] = technical_factor * social_factor
  28.  
  29.         for indx, year in enumerate(main_years):
  30.  
  31.             if indx + tlft[tech].values > EOH:
  32.  
  33.                 salvage_factor_0.loc[year, tech] = (
  34.                     (1 + discount_rate.loc[year, :].values)
  35.                     ** (tlft[tech].values - EOH - 1 + indx)
  36.                     - 1
  37.                 ) / ((1 + discount_rate.loc[year, :].values) ** tlft[tech].values - 1)
  38.  
  39.     salvage_factor_mod = pd.DataFrame(
  40.         salvage_factor_0.values * rates_factor.values,
  41.         index=main_years,
  42.         columns=technologies,
  43.     )
  44.  
  45.     return salvage_factor_mod
Please help me to modify these two functions so that they can use the new interest rate.
Oct 10 '23 #1
2 26135
w88indi27
1 Bit
allright, so what is it about?
Oct 17 '23 #2
codiumltd
13 Byte
To modify the two functions, invcosts_annuity and salvage_factor, to use the new interest rate in your Python code, you'll need to make adjustments as follows:

Modify invcosts_annuity function:

You can pass the interest rate for each technology directly to the function instead of relying on the previous structure. Here's how you can modify the function:

python
Copy code
def invcosts_annuity(
cost_inv_present,
interest_rate, # Pass the interest rate for each technology here
economiclife,
technologies,
main_years,
discount_rate,
):
# Rest of the function remains the same
Now, when you call the invcosts_annuity function, make sure to provide the correct interest rate for each technology as an argument.

Modify salvage_factor function:

Similarly, you can pass the interest rate for each technology as an argument to the salvage_factor function and update its usage accordingly. Here's how you can modify the function:

python
Copy code
def salvage_factor(
main_years, technologies, tlft, interest_rate, discount_rate, economiclife
):
# Rest of the function remains the same
In both functions, the interest_rate argument should be a dictionary or a list containing the interest rate for each technology. When calling these functions, provide the correct interest rates for each technology to reflect the changes you made in your code.

This modification allows you to use the new interest rates for specific technologies in these functions while maintaining their original logic.
Oct 18 '23 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: portraitmaker | last post by:
I found some drag and drop code on the web and modified it a little b taking out some of the stuff I didn't need. This sample allows you to drag an image in a table to another positio and swaps...
1
by: Maziar Aflatoun | last post by:
Hi everyone, I have the following HTML code and I like to change the value of REFRESH content from 2 to something else at runtime. Any suggestions? <HTML> <HEAD> <meta name="GENERATOR"...
0
by: Jason | last post by:
I need help designing a query (or two) in MS Access that will calculate a compounded rate of interest. This would be no problem using a simple FV function if the Interest Rate were constant....
2
by: fatimahtaher | last post by:
Hi, I am new to C# programming and my first assignment requires me to calculate total interest paid on a loan. The user will input the loan, the interest rate per month, and the monthly payment....
1
by: lenin42001 | last post by:
Please can you help Have tried various means but cannot get this to work. The saver is prompted for the amount they wish to invest & a time period(in years) If initial investment is over £1000 & if...
4
by: bc070200263 | last post by:
This is an Islamic law that Muslims should pay their Zakat with the rate of 2.5%, means in Rs 100 they should pay Rs 2.5 as Zakat amount. Write a simple program which asks from the user The total...
14
Dököll
by: Dököll | last post by:
Greetings and salutations! Hope it's a good week thus far... Got myself thinking and wanted to see what can be achieved with a certain bit of code: public double getPayment( ) { ...
2
by: hawkswins | last post by:
With the following I get a drop down list of the current year and the next 10 years. I need to set a start year eg 2008.. Can someone assist. DateTime.Now.Year To DateTime.Now.Year + 10
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.