stack MLinExpr and add constraints
回答済み-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi,
We cannot pass Gurobi MLinExpr() objects to the numpy.vstack() method. This method only accepts numpy arrays. To implement the constraints as you explained, I think the script below should do the job:
import gurobipy as gp
from gurobipy import GRB
import numpy as np
if __name__ == "__main__":
m = gp.Model("LP_model")
expected_cost = m.addMVar(
70, lb=0, ub=float("inf"), vtype="C", name="expected_cost"
)
m.setObjective(expected_cost.sum(), GRB.MINIMIZE)
ST_mat = np.random.random((70, 70))
R_mat = np.random.random((70, 70))
beta = 0.001
EV_myopic = np.random.random((2, 70))
for col in range(70):
m.addConstr(
expected_cost @ ST_mat[:, col] * beta
+ EV_myopic[0, col]
- expected_cost[col]
<= 0
)
for col in range(70):
m.addConstr(
expected_cost @ R_mat[:, col] * beta
+ EV_myopic[1, col]
- expected_cost[col]
<= 0
)Let me know if something is not clear.
Best regards,
Maliheh
0 -
Hi Maliheh, thanks for your support. I follow the code you provided, but it doesn't seem to work now. Please see the code below and help me to go over the problem. Thank you so much for your time and effort
def gurobi_LP(S, p, MF, params, beta=0.75, threshold=1e-6, suppr_output=False):
ST_mat = np.zeros((S, S))R_mat = np.vstack((np.ones((1, S)),np.zeros((S-1, S))))EV_myopic = myopic_costs(S, MF, params, p)
# Create a new modelm = gp.Model('LP_model')
# Create variablesexpected_cost = m.addMVar(S, lb=0, ub=float('inf'), vtype='C', name='expected_cost')
# Set objectivem.setObjective(expected_cost.sum(), GRB.MAXIMIZE)
# Add constraintsfor i in range(S):# m.addConstr(expected_cost[i] <= EV_myopic[:,0][i] + expected_cost @ ST_mat[:,i]) * beta# m.addConstr(expected_cost[i] <= EV_myopic[:,1][i] + expected_cost @ R_mat[:,i]) * betam.addConstr(expected_cost[i] <= EV_myopic[0,i] + expected_cost @ ST_mat[:,i]) * betam.addConstr(expected_cost[i] <= EV_myopic[1,i] + expected_cost @ R_mat[:,i]) * beta
# Run LP modelm.optimize()
# Extract outputsexp_cost = np.array([expected_cost[i].X for i in range(S)])# exp_cost = np.array(exp_cost)
futil_maintanance = np.dot(exp_cost, ST_mat)futil_replace = np.dot(exp_cost, R_mat)futility = np.vstack((futil_maintanance, futil_replace)).Texp_cost_new = EV_myopic + beta*futility
return (exp_cost, choice_prob(exp_cost_new), choice_prob(EV_myopic))0 -
Hi,
I was able to run the snippet that I sent before. Could you run that snippet?
I cannot run your code snippet because the input data is missing. Do you get the error "Invalid argument to Model.addConstr"? If so, the constraints should be implemented exactly as below:m.addConstr(
EV_myopic[0, i] + expected_cost @ ST_mat[:, i] * beta - expected_cost[i]
>= 0
)
m.addConstr(
EV_myopic[1, i] + expected_cost @ R_mat[:, i] * beta - expected_cost[i]
>= 0
)In your implementation, the LHS is a Gurobi Var object and the RHS is a Gurobi MLinExpr object. As shown in the documentation of a Linear Matrix Constraint in the form \(\texttt{expr1 sense expr2}\), either both \(\texttt{expr1}\) and \(\texttt{expr2}\) are MLinExpr objects or one is MLinExpr object and the other one is a constant.
If this explanation does not resolve the issue, please share the code snippet with input data such that I can run it and also include the exact error message you get.
Best regards,
Maliheh
0
投稿コメントは受け付けていません。
コメント
4件のコメント