-
Notifications
You must be signed in to change notification settings - Fork 65
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
How does PyEPO (SPO Loss) package handle 2D decision & constraint variables? #51
Comments
Hi @HishamSalem, When creating your own You need to do as follows: import gurobipy as gp
from gurobipy import GRB
from pyepo.model.grb.grbmodel import optGrbModel
class yourModel(optGrbModel):
def _getModel(self):
"""
A method to build Gurobi model
Returns:
tuple: optimization model and variables
"""
# ceate a model
m = gp.Model("your model")
# varibles
x = m.addVars(need, supply, name="x")
# sense
m.modelSense = GRB.MINIMIZE
# some constraints
pass
return m, x
def setObj(self, c):
"""
A method to set objective function
Args:
c (np.ndarray): cost of objective function
"""
# sum up vector cost
obj = gp.quicksum(c[i,j] * self.x[i,j] for i, j in self.x)
self._model.setObjective(obj)
def solve(self):
"""
A method to solve model
Returns:
tuple: optimal solution (list) and objective value (float)
"""
# update gurobi model
self._model.update()
# solve
self._model.optimize()
# kxk solution map
sol = np.zeros_like(self.x)
# get value
for i,j in self.x:
sol[i,j] = self.x[i,j].x
return sol, self._model.objVal I hope it will be helpful. |
Thank you @LucasBoTang, I get the idea behind this approach, but say if we have 2 targets of demand and supply. If a model does poor demand, the supply allocation would fail as it is a constraint into the optimization. For example we build a pytorch model that predicts both, how would we get the spo class work? What I tried to do is "flatten the outputs from the gurobi model" but I am not sure if this is accurate. Does your suggestion state that we should only represent the supply even if the predictive model would produce other outputs that are represented in the downstream optimization? here is a snipit of the solve part of the model.
how would this work with If you have a multi output then the loss function wouldn't work given the assumption that the inputs in the spo would be flattened. Advice will be very helpful as it will be awesome for me to understand how we can use multiple outputs together for your amazing package. |
I am trying to build a SPO model where the prediction components are supply & Demand.
In our specific case our cost factor is distance traveled for the nodes. However for us to minimize the distance we need to allocate supply to meet the expected demand.
How can we make it so that the loss function can handle the demand which is a constraint in the optimization model as we try to allocate supply?
the shape of inputs of the setobj is (Node,[supply,demand]).
The text was updated successfully, but these errors were encountered: