Using an aggregated value as a lookup index value
The following code is trying to maximize variable "var". One of the constraints requires sum("var") grouped by index y. This is then divided by the total var to get the grouped y%. Each y % needs to be grouped by z to get sum("var") by z.
Unfortunately, the constraint requires the sum("var") by z values to be converted using a large lookup table. The line in bold below is where I would ideally like to do the lookup but it doesn't seem possible. Any advice on how to accomplish this would be extremely helpful
dfg = (
pd.DataFrame({'x':[1,2,3,4], 'y': [1,1,2,2],'z': ["a","a","b","b"]}).set_index(["x","y","z"])
.gppd.add_vars(m,vtype=GRB.INTEGER, name="var"))
m.setObjective(dfg["var"].sum(), GRB.MAXIMIZE)
def avg_y(group):
group = group.sort_index()
return gppd.add_vars(m,
group.iloc[1:]/250,
vtype=GRB.INTEGER,
ub = 1,
name="avg_yn"
)
avg_ydf = dfg.groupby("y").apply(avg_y).droplevel(0)
def agg_z(group):
group = group.sort_index()
return gppd.add_vars(
m,
group.iloc[1:], # I need to take this value as a key and look up the value that I need
vtype=GRB.INTEGER,
name="agg_z"
)
zdf = avg_ydf.groupby("z").apply(agg_z).droplevel(0)
This is the lookup code that I would like to use..
DS.iloc[DS.index.get_indexer([agg_z], method="nearest")[0]]
0
Please sign in to leave a comment.
Comments
0 comments