Comments (6)
Hey @loongmxbt , the broadcasting works automatically in linopy, so no need for loops or extra broadcasting. I am not sure if df_units_uc.index
is now a subset of df_units.index
?
Either way, what you wrote in the last section should work if the dimension names are set correctly(!). What I suggest to you is that you explicitly set the names of your axes in the dataframe before defining the variables and constraints. Something like
df_time.index.name = 'time'
df_units.index.name = 'units'
bx = m.add_variables(binary=True, coords=[df_time.index, df_units.index], name='bx' )
unit_power = m.add_variables(lower=0, coords=[df_time.index, df_units.index], name='unit_power' )
m.add_constraints(bx*unit_power >= df_units['p_min'], name='min_power_constraint')
m.add_constraints(unit_power * 1 <= df_units['p_max'], name='max_power_constraint')
(Assuming now, that df_units
equals to df_units_uc
)
Hope that helps!
from linopy.
Thanks for your answer. I modified all "df_units_uc" into "dict_units", also the related data, and the model works fine. Seems an error in my data.
I'll check when I continue to write the model.
from linopy import Model
import pandas as pd
dict_bus_predict = {
't01': { 'B01': 420, 'B02': 530, 'B03': 670 },
't02': { 'B01': 520, 'B02': 580, 'B03': 770 },
't03': { 'B01': 620, 'B02': 570, 'B03': 800 }
}
dict_re_predict = {
't01': { 'PV01': 30, 'WD01': 30, },
't02': { 'PV01': 22, 'WD01': 80, },
't03': { 'PV01': 24, 'WD01': 70, }
}
dict_unit = {
'G01a': { 'is_uc': 1, 'bus': 'B01', 'p_min': 100, 'p_max': 1000, 'init_power':320, 'init_status': -3, 'ramp_limit_up': 260, 'ramp_limit_down': 260, 'cost_a': 0.002, 'cost_b': 0.21, 'cost_c': 21, 'cost_p': 800, 'hot_start_cost': 100, 'cold_start_cost': 150, 'no_load_cost': 100, 'shut_down_cost': 120, 'cold_start_hours': 3 },
'G01b': { 'is_uc': 1, 'bus': 'B01', 'p_min': 100, 'p_max': 1000, 'init_power':320, 'init_status': -6, 'ramp_limit_up': 260, 'ramp_limit_down': 260, 'cost_a': 0.002, 'cost_b': 0.21, 'cost_c': 21, 'cost_p': 800, 'hot_start_cost': 100, 'cold_start_cost': 150, 'no_load_cost': 100, 'shut_down_cost': 120, 'cold_start_hours': 3 },
'G02': { 'is_uc': 1, 'bus': 'B02', 'p_min': 100, 'p_max': 1000, 'init_power':320, 'init_status': 0, 'ramp_limit_up': 260, 'ramp_limit_down': 260, 'cost_a': 0.0015, 'cost_b': 0.2, 'cost_c': 21, 'cost_p': 800, 'hot_start_cost': 100, 'cold_start_cost': 150, 'no_load_cost': 100, 'shut_down_cost': 120, 'cold_start_hours': 3 },
'G03': { 'is_uc': 1, 'bus': 'B03', 'p_min': 100, 'p_max': 1000, 'init_power':320, 'init_status': 0, 'ramp_limit_up': 260, 'ramp_limit_down': 260, 'cost_a': 0.0025, 'cost_b': 0.2, 'cost_c': 21, 'cost_p': 800, 'hot_start_cost': 100, 'cold_start_cost': 150, 'no_load_cost': 100, 'shut_down_cost': 120, 'cold_start_hours': 3 },
}
df_units = pd.DataFrame.from_dict(dict_unit, orient='index')
print(df_units)
df_units_uc = df_units[df_units["is_uc"] == 1]
df_bus_predict = pd.DataFrame.from_dict(dict_bus_predict, orient='index')
print(df_bus_predict)
def main():
m = Model()
unit_power = m.add_variables(lower=0, coords=[df_bus_predict.index, df_units_uc.index], name='unit_power' )
bx = m.add_variables(binary=True, coords=[df_bus_predict.index, df_units_uc.index], name='bx' )
by = m.add_variables(binary=True, coords=[df_bus_predict.index, df_units_uc.index], name='by' )
bz = m.add_variables(binary=True, coords=[df_bus_predict.index, df_units_uc.index], name='bz' )
m.add_constraints(bx * unit_power >= df_units_uc['p_min'], name='min_power_constraint')
m.add_constraints(unit_power * 1 <= df_units_uc['p_max'], name='max_power_constraint')
print(m)
if __name__ == '__main__':
main()
from linopy.
I have a similar problem; I have one pandas dataframe and one pandas series:
#storage dataframe
storage_df = pd.DataFrame( { 'name': [ '422', '435' ], 'pmin': [ 5, 2.5 ], 'pmax': [ 8, 7.5 ], 'Tmin': [ 49, 48.1 ], 'Tmax': [ 59.8, 54.9 ] } )
storage_df.index.name = 'storages'
#time basis vector
dimtime = 24 * 4
vectime = pd.Series( np.arange( dimtime ) )
vectime.index.name = 'time'
I want to create a variable in both dimensions:
m = opt.Model()
#variable
content = m.add_variables( coords = [ storage_df.index, vectime.index ], name = 'content' )
and set constraints in one dimension:
#constraint
m.add_constraints( content >= storage_df[ 'Tmin' ], name = 'content_low' )
I get the following error message (which makes sense):
ValueError: operands could not be broadcast together with shapes (2,) (2,96)
So my question is how to implement such constraints efficiently?
from linopy.
To clarify the imports:
import linopy as opt
import numpy as np
import pandas as pd
from linopy.
Sorry for spaming this issue:
The solution is to multiply the variable with one:
#constraint
m.add_constraints( content * 1 >= storage_df[ 'Tmin' ], name = 'content_low' )
from linopy.
Good that you found a solution. I will update the docs soon on how to work with pandas objects and different approaches to create constraints. But note, always watch out for the names of the dimensions (that the most "tricky" thing)
from linopy.
Related Issues (20)
- MPS IO changes with highspy >= 1.7
- Re-solving a changed model with "direct" api results in wrong model being solved HOT 1
- add_constraints lhs eats constant HOT 2
- Explore PytOptInferface for writing matrix over native C API HOT 9
- Improve workflow for releases HOT 1
- Pass parameters to the Gurobi solver HOT 1
- Docs errata: LinearExpression `expand_dims` HOT 2
- How are "0" coefficient passed to gurobi? HOT 3
- Multiple coordinates for one dimension HOT 2
- groupby sum not allowing multidimensional grouping per default HOT 6
- Creating variable for each linear expression HOT 4
- `xarray` DeprecationWarning: Deleting a single level of a MultiIndex is deprecated. Previously
- masking vs selecting HOT 3
- Slicing selection not aligned on "Python standards" HOT 3
- sol file is not dumped when using gurobi
- 0.3.12 ModuleNotFoundError: No module named 'highspy' HOT 3
- Update xarray package requirements HOT 1
- Overhaul CI and CD workflows HOT 6
- 0.3.3 release onwards have broken Gurobi solves for large models. HOT 5
- Serializing and deserializing linopy.Model HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from linopy.