Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def consprop(self, constraints, nusefulconss, nmarkedconss, proptiming): # I have no idea what to return, documentation?
result = SCIP_RESULT.DIDNOTFIND
for cons in constraints:
prop_result = self.propagate_cons(cons)
if prop_result == SCIP_RESULT.CUTOFF:
result = prop_result
break
if prop_result == SCIP_RESULT.REDUCEDDOM:
result = prop_result
return {"result": result}
#print(" -> gomory cut for <%s>: rhs=%f, eff=%f"%(cols[c].getVar() if c >= 0 else rows[-c-1].getName(),
#cutrhs, scip.getCutEfficacy(None, cut)))
#SCIPdebugMessage(" -> found gomory cut <%s>: act=%f, rhs=%f, norm=%f, eff=%f, min=%f, max=%f (range=%f)\n",
# cutname, SCIPgetRowLPActivity(scip, cut), SCIProwGetRhs(cut), SCIProwGetNorm(cut),
# SCIPgetCutEfficacy(scip, NULL, cut),
# SCIPgetRowMinCoef(scip, cut), SCIPgetRowMaxCoef(scip, cut),
# SCIPgetRowMaxCoef(scip, cut)/SCIPgetRowMinCoef(scip, cut))
# flush all changes before adding the cut
scip.flushRowExtensions(cut)
infeasible = scip.addCut(cut, forcecut=True)
if infeasible:
result = SCIP_RESULT.CUTOFF
else:
result = SCIP_RESULT.SEPARATED
scip.releaseRow(cut)
return {"result": result}
# so that then one can ask stuff about it, like its efficacy, etc. This function would receive all coefficients
# and basically do what we do here: cacheRowExtension etc
cut = scip.createEmptyRowSepa(self, "gmi%d_x%d"%(scip.getNLPs(),c if c >= 0 else -c-1), lhs = None, rhs = cutrhs)
scip.cacheRowExtensions(cut)
for j in range(len(cutcoefs)):
if scip.isZero(cutcoefs[j]): # maybe here we need isFeasZero
continue
#print("var : ", cols[j].getVar(), " coef: ", cutcoefs[j])
#print("cut.lhs : ", cut.getLhs(), " cut.rhs: ", cut.getRhs())
scip.addVarToRow(cut, cols[j].getVar(), cutcoefs[j])
if cut.getNNonz() == 0:
assert scip.isFeasNegative(cutrhs)
#print("Gomory cut is infeasible: 0 <= ", cutrhs)
return {"result": SCIP_RESULT.CUTOFF}
# Only take efficacious cuts, except for cuts with one non-zero coefficient (= bound changes)
# the latter cuts will be handeled internally in sepastore.
if cut.getNNonz() == 1 or scip.isCutEfficacious(cut):
#print(" -> gomory cut for <%s>: rhs=%f, eff=%f"%(cols[c].getVar() if c >= 0 else rows[-c-1].getName(),
#cutrhs, scip.getCutEfficacy(None, cut)))
#SCIPdebugMessage(" -> found gomory cut <%s>: act=%f, rhs=%f, norm=%f, eff=%f, min=%f, max=%f (range=%f)\n",
# cutname, SCIPgetRowLPActivity(scip, cut), SCIProwGetRhs(cut), SCIProwGetNorm(cut),
# SCIPgetCutEfficacy(scip, NULL, cut),
# SCIPgetRowMinCoef(scip, cut), SCIPgetRowMaxCoef(scip, cut),
# SCIPgetRowMaxCoef(scip, cut)/SCIPgetRowMinCoef(scip, cut))
# flush all changes before adding the cut
scip.flushRowExtensions(cut)
# We would need an event handler to let us know whenever a variable of our constraint changed its domain
# Currently we can't write event handlers in python.
G, vals = self.build_value_graph(vars, domains)
try:
M = networkx.bipartite.maximum_matching(G) # returns dict between nodes in matching
except:
top_nodes = {n for n, d in G.nodes(data=True) if d['bipartite'] == 0}
bottom_nodes = set(G) - top_nodes
M = networkx.bipartite.maximum_matching(G, top_nodes) # returns dict between nodes in matching
if( len(M)/2 < len(vars) ):
#print("it is infeasible: max matching of card ", len(M), " M: ", M)
#print("Its value graph:\nV = ", G.nodes(), "\nE = ", G.edges())
plot_graph(G)
return SCIP_RESULT.CUTOFF
# build auxiliary directed graph: direct var -> val if [var, val] is in matching, otherwise var <- val
# note that all vars are matched
D = networkx.DiGraph()
D.add_nodes_from(G) ## this seems to work
for var in vars:
D.add_edge(var.name, M[var.name])
for val in domains[var.ptr()]:
if val != M[var.name]:
D.add_edge(val, var.name)
# find arcs that *do not* need to be removed and *remove* them from G. All remaining edges of G
# should be use to remove values from the domain of variables
# get all free vertices
V = set(G.nodes())
V_matched = set(M)
# so that then one can ask stuff about it, like its efficacy, etc. This function would receive all coefficients
# and basically do what we do here: cacheRowExtension etc up to releaseRow
cut = scip.createEmptyRowSepa(self, "gmi%d_x%d"%(self.ncuts,c if c >= 0 else -c-1), lhs = None, rhs = cutrhs)
scip.cacheRowExtensions(cut)
for j in range(len(cutcoefs)):
if scip.isZero(cutcoefs[j]): # maybe here we need isFeasZero
continue
#print("var : ", cols[j].getVar(), " coef: ", cutcoefs[j])
#print("cut.lhs : ", cut.getLhs(), " cut.rhs: ", cut.getRhs())
scip.addVarToRow(cut, cols[j].getVar(), cutcoefs[j])
if cut.getNNonz() == 0:
assert scip.isFeasNegative(cutrhs)
#print("Gomory cut is infeasible: 0 <= ", cutrhs)
return {"result": SCIP_RESULT.CUTOFF}
# Only take efficacious cuts, except for cuts with one non-zero coefficient (= bound changes)
# the latter cuts will be handeled internally in sepastore.
if cut.getNNonz() == 1 or scip.isCutEfficacious(cut):
#print(" -> gomory cut for <%s>: rhs=%f, eff=%f"%(cols[c].getVar() if c >= 0 else rows[-c-1].getName(),
# cutrhs, scip.getCutEfficacy(cut)))
#SCIPdebugMessage(" -> found gomory cut <%s>: act=%f, rhs=%f, norm=%f, eff=%f, min=%f, max=%f (range=%f)\n",
# cutname, SCIPgetRowLPActivity(scip, cut), SCIProwGetRhs(cut), SCIProwGetNorm(cut),
# SCIPgetCutEfficacy(scip, NULL, cut),
# SCIPgetRowMinCoef(scip, cut), SCIPgetRowMaxCoef(scip, cut),
# SCIPgetRowMaxCoef(scip, cut)/SCIPgetRowMinCoef(scip, cut))
# flush all changes before adding the cut
scip.flushRowExtensions(cut)
# cutrhs, scip.getCutEfficacy(cut)))
#SCIPdebugMessage(" -> found gomory cut <%s>: act=%f, rhs=%f, norm=%f, eff=%f, min=%f, max=%f (range=%f)\n",
# cutname, SCIPgetRowLPActivity(scip, cut), SCIProwGetRhs(cut), SCIProwGetNorm(cut),
# SCIPgetCutEfficacy(scip, NULL, cut),
# SCIPgetRowMinCoef(scip, cut), SCIPgetRowMaxCoef(scip, cut),
# SCIPgetRowMaxCoef(scip, cut)/SCIPgetRowMinCoef(scip, cut))
# flush all changes before adding the cut
scip.flushRowExtensions(cut)
infeasible = scip.addCut(cut, forcecut=True)
self.ncuts += 1
if infeasible:
result = SCIP_RESULT.CUTOFF
else:
result = SCIP_RESULT.SEPARATED
scip.releaseRow(cut)
return {"result": result}
# feature normalization
candidate_states = (candidate_states - self.feat_shift) / self.feat_scale
candidate_scores = self.policy.predict(candidate_states)
best_var = candidate_vars[candidate_scores.argmax()]
else:
raise NotImplementedError
self.model.branchVar(best_var)
result = scip.SCIP_RESULT.BRANCHED
# fair node counting
if result == scip.SCIP_RESULT.REDUCEDDOM:
self.ndomchgs += 1
elif result == scip.SCIP_RESULT.CUTOFF:
self.ncutoffs += 1
return {'result': result}