Giter VIP home page Giter VIP logo

Comments (6)

FabianHofmann avatar FabianHofmann commented on August 25, 2024

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.

loongmxbt avatar loongmxbt commented on August 25, 2024

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.

joga4er avatar joga4er commented on August 25, 2024

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.

joga4er avatar joga4er commented on August 25, 2024

To clarify the imports:

import linopy as opt
import numpy as np
import pandas as pd

from linopy.

joga4er avatar joga4er commented on August 25, 2024

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.

FabianHofmann avatar FabianHofmann commented on August 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.