Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the ecological analysis method #86

Open
mzy2240 opened this issue Sep 9, 2022 · 2 comments
Open

Improve the ecological analysis method #86

mzy2240 opened this issue Sep 9, 2022 · 2 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@mzy2240
Copy link
Owner

mzy2240 commented Sep 9, 2022

  1. Reduce the number of conditions by using formatted string

e.g.

if target == 'MW':
    flow = gen.loc[i, 'GenMW']

can be optimized into

flow = gen.loc[i, f"Gen{target}"]
  1. When computing the EFM, compute the line flow and the loss induced EFM together

  2. Use loc instead of column + index to access cells for better readiness and performance

@mzy2240
Copy link
Owner Author

mzy2240 commented Sep 11, 2022

@faoh

  1. Please consider using vectorization to replace iterating a dataframe. If you have to do that, consider using df.iterrows() instead of the vanilla loop.

  2. There is no need to have two separate loops for the following code:

# feed line flow to EFM
for i in range(num_branch):
    frombus = branch_df['findex'][i]
    tobus = branch_df['tindex'][i]
    flow = branch_df.loc[i, f"Line{target}"]
    if flow > 0:
        EFM[1+frombus+num_gen][1+tobus+num_gen] += abs(flow)
    else:
        EFM[1+tobus+num_gen][1+frombus+num_gen] += abs(flow)

# feed loss
# this loss aggregates the line loss to from bus
# can be further updated based on your consideration.
# however, the loss is/should be very small, thus it may not induce any change
for i in range(num_branch):
    frombus = branch_df['findex'][i]
    tobus = branch_df['tindex'][i]
    flow = branch_df.loc[i, f"LineLoss{target}"]
    # flow=branch_df.LineLossMW[i]
    EFM[1+num_gen+frombus][2+num_bus+num_gen] += abs(flow)
  1. Use loc instead of column + index to access cells for better readiness and performance
frombus = branch_df['findex'][i]
tobus = branch_df['tindex'][i]

faoh added a commit that referenced this issue Sep 11, 2022
faoh added a commit that referenced this issue Sep 11, 2022
@mzy2240
Copy link
Owner Author

mzy2240 commented Sep 12, 2022

@faoh Good job! Now you could consider vectorizing most of the loops, e.g.

for i in range(num_gen):
    flow = gen.loc[i, f"Gen{target}"]
    EFM[0][i+1] += flow  # [row][col]

The above code could be easily converted into the following code:

EFM[0][1:1+num_gen] = gen[f"Gen{target}"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants