Adding Constraints
AnsweredHi, Please see the code below, I am trying to add additional constraint where I do not want the amount allocated by constraint 'h' to be > 10% of the amount allocated by constraint "r". I have tried a few ways of doing it but couldn't make it work, any help is appreciated.
#Load loans
print('Python - Fwd Allocation Optimizer: Loading loan data...', flush = True)
datasql = cronusHelper.SQLAction(args, proc, 'Loans')
datasql_list = datasql.values.tolist()
del datasql
loans = dict((l, a) for l, a in datasql_list)
del datasql_list
#Load HighBal loans
print('Python - Fwd Allocation Optimizer: Loading HighBal loan data...', flush = True)
datasql = cronusHelper.SQLAction(args, proc, 'HighBalLoans')
datasql_list = datasql.values.tolist()
del datasql
highballoans = dict((l, a) for l, a in datasql_list)
del datasql_list
#Load Fwd Cb
print('Python - Fwd Allocation Optimizer: Loading Fwd Cb data...', flush = True)
datasql = cronusHelper.SQLAction(args, proc, 'FwdCbDtl')
datasql_list = datasql.values.tolist()
del datasql
specname, minfwdamt, maxfwdamt = gp.multidict((s, [mi, mx]) for s, mi, mx in datasql_list)
del datasql_list
#Load Fwd Hb
print('Python - Fwd Allocation Optimizer: Loading Fwd Hb data...', flush = True)
datasql = cronusHelper.SQLAction(args, proc, 'FwdHbDtl')
datasql_list = datasql.values.tolist()
del datasql
spechbname, minhighbalamt, maxhighbalamt = gp.multidict((s, [mi, mx]) for s, mi, mx in datasql_list)
del datasql_list
#Load loan/fwd mapping
print('Python - Fwd Allocation Optimizer: Loading loan/Fwd mapping data...', flush = True)
datasql = cronusHelper.SQLAction(args, proc, 'LoanFwdMapping')
datasql_list = datasql.values.tolist()
del datasql
proceeds = dict(((l, s), p) for l, s, p in datasql_list)
del datasql_list
#Model
print('Python - Fwd Allocation Optimizer: Creating the model...', flush = True)
m = gp.Model("FwdAllocation")
#Parameters
print('Python - Fwd Allocation Optimizer: Setting up model parameters...', flush = True)
m.setParam(GRB.Param.TimeLimit, 1500)
m.setParam(GRB.Param.MIPGap, 1e-6)
m.setParam(GRB.Param.FeasibilityTol, 1e-6)
m.setParam(GRB.Param.Threads, 4)
m.setParam(GRB.Param.NodefileStart, 0.5)
#m.setParam(GRB.Param.MIPFocus,2)
#Decision Variables
print('Python - Fwd Allocation Optimizer: Setting up decision variables...', flush = True)
x = m.addVars (loans.keys(), specname, vtype = GRB.BINARY, obj = proceeds, name="allocate")
m.ModelSense = GRB.MAXIMIZE
#Constraints
print('Python - Fwd Allocation Optimizer: Setting up constraints...', flush = True)
l = m.addConstrs((x.sum(f,'*') <= 1 for f in loans), 'loans')
r = m.addConstrs((gp.quicksum(x[f, c] * loans[f] for f in loans)
== [minfwdamt[c], maxfwdamt[c]] for c in specname), "_")
h = m.addConstrs((gp.quicksum(x[f, c] * highballoans[f] for f in highballoans)
<= maxhighbalamt[c] for c in spechbname), "_")
#Save model
#m.write('Optimizer.lp')
#Optimize
m.optimize()
-
Official comment
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 why not try our AI Gurobot?. -
Hi - Can you send a minimal example and explain more what doesn't work? Thanks.
0 -
Thank you Gwyneth!
I am new to Gurobi and based on all the examples I have gone through so far, my main struggle on the issue here is my ability to formulate that constraint.
I have tried something like this:
hc = m.addConstr(gp.quicksum(x[f,c] * highballoans[f] for f in highballoans for c in spechbname if str.isnumeric(c) == False)
<= 0.10 * gp.quicksum(x[f,c] * loans[f] for f in loans for c in spechbname if str.isnumeric(c) == False))and
hc = m.addConstr(h.sum(c,'*') <= 0.10 * r.sum(c,'*') for c in specname)
but keep running into Gurobi/Syntax errors, thank you
0 -
Can you try breaking the constraints down into smaller pieces and then post a simple example along with the error message that you get? Thanks!
0 -
I was able to add the constraint as following:
hcfn = m.addConstrs((x.prod(highballoanamt,'*', s) <= 0.10 * x.prod(loanamt,'*', s) for s in spechbname
if (str.isnumeric(s) == False
and s.find('FN') != -1
and maxhighbalamt[s] > 0)), "_")
hcfg = m.addConstrs((x.prod(highballoanamt,'*', s) <= 0.12 * x.prod(loanamt,'*', s) for s in spechbname
if (str.isnumeric(s) == False
and s.find('FG') != -1
and maxhighbalamt[s] > 0)), "_")Thank you
0
Post is closed for comments.
Comments
5 comments