You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello Pao devs! First of all thank you for your awesome work!
I wanted to ask a question about how to correctly build the following bilevel optimization program. More specifically, I am struggling with the first constraint, as it features variables from both the model and the submodel.
the program executes with no apparent errors, however the solutions provided violate the 1st constraint.
Hope you can help! Leaving a reproducible example below where the first constraint is initialized from the model (case 2).
fromscipy.spatialimportdistanceimportnumpyasnpimportpandasaspdimportpyomo.environaspeimportpyomo.optaspoimportpao"""Initializing dummy variables and useful functions"""defgenerate_2d_coordinate():
returntuple(np.random.uniform(low=0.0, high=100.0, size=2))
defall_unique(lst):
returnlen(lst) ==len(set(lst))
available_taxis_ids= [0,1,2,3,4]
users_ids= [0,1,2,3,4,6,7]
n_assignments=min(len(available_taxis_ids),len(users_ids))
taxi_coordinates= [
generate_2d_coordinate() for_inavailable_taxis_ids
]
destination_coordinates= [
generate_2d_coordinate() for_inusers_ids
]
cost_matrix=distance.cdist(
taxi_coordinates, destination_coordinates, "euclidean"
)
assignment_cost= (
pd.DataFrame(
cost_matrix, index=available_taxis_ids, columns=users_ids
)
.stack()
.to_dict()
)
"""Build and solves the Pyomo model for the MILP problem."""model=pe.ConcreteModel()
model.taxis=pe.Set(initialize=available_taxis_ids)
model.users=pe.Set(initialize=users_ids)
model.z=pe.Var(model.users, domain=pe.NonNegativeReals)
model.submodel=pao.pyomo.SubModel(fixed=[model.z])
model.submodel.taxis=pe.Set(initialize=available_taxis_ids)
model.submodel.users=pe.Set(initialize=users_ids)
model.submodel.taxis=pe.Set(initialize=available_taxis_ids)
model.submodel.users=pe.Set(initialize=users_ids)
model.submodel.assignment_cost=pe.Param(
model.submodel.taxis, model.submodel.users, initialize=assignment_cost
)
model.submodel.y=pe.Var(model.submodel.taxis, model.submodel.users, domain=pe.NonNegativeReals)
expression=sum(
model.submodel.assignment_cost[c, k] *model.submodel.y[c, k]
forcinmodel.submodel.taxisforkinmodel.submodel.users
)
model.obj=pe.Objective(sense=pe.maximize, expr=expression)
model.submodel.obj=pe.Objective(sense=pe.minimize, expr=expression)
defeach_taxi_serves_one_user_max(model, k):
# assign at most one taxi c to every user.constraint=sum(model.submodel.y[c, k] forcinmodel.submodel.taxis) <=model.z[k]
returnconstraintdefeach_user_is_served_by_one_taxi_max(submodel, c):
# a taxi c can only be assign to one given user k.constraint=sum(submodel.y[c, k] forkinsubmodel.users) <=1returnconstraintdefmaximal_injective_assignment(submodel):
# a minimum of min(|Taxis|,|Users|) matches ought to be made.constraint= (
sum(submodel.y[c, k] forkinsubmodel.usersforcinsubmodel.taxis)
==n_assignments
)
returnconstraintmodel.each_taxi_serves_one_user_max=pe.Constraint(
model.users, rule=each_taxi_serves_one_user_max
)
model.submodel.each_user_is_served_by_one_taxi_max=pe.Constraint(
model.submodel.taxis, rule=each_user_is_served_by_one_taxi_max
)
model.submodel.maximal_injective_assignment=pe.Constraint(
rule=maximal_injective_assignment
)
withpao.pyomo.Solver('pao.pyomo.FA') assolver:
solver.solve(model, mip_solver="cbc")
"""Processing Results"""output_df= (
pd.Series(model.submodel.y.extract_values())
.reset_index()
.rename(columns={"level_0": "taxis", "level_1": "users", 0: "y"})
)
solution_is_binary=set(output_df.y.unique()).issubset({0, 1})
assertsolution_is_binarysolution_df=output_df.loc[lambdax: x.y==1, ["users", "taxis"]].reset_index(
drop=True
)
print(solution_df)
assertall_unique(solution_df.users)
userstaxis000121262303474AssertionError
The text was updated successfully, but these errors were encountered:
FranDeLio
changed the title
Help needed making work constraint featuring variables of different levels
Help needed making work rule-based constraint featuring variables of different levels
Apr 14, 2024
FranDeLio
changed the title
Help needed making work rule-based constraint featuring variables of different levels
Implementing rule-based constraints featuring variables of different levels
Apr 14, 2024
FranDeLio
changed the title
Implementing rule-based constraints featuring variables of different levels
Implementing rule-based constraints featuring variables on different levels
Apr 14, 2024
Hello Pao devs! First of all thank you for your awesome work!
I wanted to ask a question about how to correctly build the following bilevel optimization program. More specifically, I am struggling with the first constraint, as it features variables from both the model and the submodel.
If I initialize the constraint within the submodel e.g
then I can not access the variable in the model$z$ and I get the error above, and if I initialize the constraint from the model e.g
the program executes with no apparent errors, however the solutions provided violate the 1st constraint.
Hope you can help! Leaving a reproducible example below where the first constraint is initialized from the model (case 2).
The text was updated successfully, but these errors were encountered: