Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
p = np.zeros(len(self.model.getVars())+1)
p[0]=1
p[i+1]=2
for j in optProblem.constraints: #constraint_list:
for k in range(1,len(j.A[0])):
#print(i,p,j)
if len(p) == len(j.A.T[k,:]) and np.equal(p,j.A.T[k,:]).all():
equ = True
break
#TODO: need to also make sure, this constraint only appears if y**2 not in any other constraint
if not equ:
#constraint_list.append(ExprToPoly({Term(y,y):-1.0}, nvars))
optProblem.addCons(str(ExprToPoly({Term(y,y):-1.0}, nvars)) + '>= 0')
else:
if y.getUbLocal() != 1e+20:
boundcons = ExprToPoly(Expr({Term(): y.getUbLocal(), Term(y):-1.0}), nvars)
#constraint_list.append(boundcons)
optProblem.addCons(str(boundcons) + ' >= 0')
if y.getLbLocal != -1e+20: #TODO: do we also need: and y.getLbLocal != 0.0:
boundcons = ExprToPoly(Expr({Term(): -y.getLbLocal(), Term(y):1.0}), nvars)
#constraint_list.append(boundcons)
optProblem.addCons(str(boundcons) + ' >= 0')
#print('cons',len(constraint_list))
#print([(con.A, con.b) for con in constraint_list])
#constraint_list = [str(con) + " >= 0" for con in constraint_list]
#print(constraint_list)
"""Here starts the real computation
first try to use the GP, if that does not work use scipy"""
#---try to solve it using GP, so do not need scipy---
#TODO: sometimes get lower bound > solution, so maybe need to take constant term better into account?
problem = solve_GP(optProblem)
def ExprToPoly(Exp, nvar):
"""turn pyscipopt.scip.Expr into a Polynomial (SCIP -> POEM)
:param: Exp: expression of type pyscipopt.scip.Expr
:param: nvar: number of variables of given problem
"""
nterms = len([key for key in Exp])
#get constant Term
if Term() in Exp:
const = Exp[Term()]
else:
const = 0.0
nterms += 1
#turn Expr into Polynomial
A = np.array([np.zeros(nterms)]*nvar)
b = np.zeros(nterms)
b[0] = const
j = 1
for key in Exp:
if not key == Term():
for el in tuple(key):
i = re.findall(r'x\(?([0-9]+)\)?', str(el))
A[int(i[0])][j] += 1
b[j] = Exp[key]
j += 1
"""
#TODO: currently working with strings, change that to arrays
dictP = p.__dict__()
d = dict()
for key in dictP.keys():
i = 0
t =[]
varstr = [None]*len(var)
for j in range(len(var)):
varstr[j] = str(var[j])
for el in key[1:]:
f = 'x' + str(i)
for _ in range(el):
t.append(var[varstr.index(f)])
i+=1
term = Term()
for el in t:
term += Term(el)
d[term] = dictP[key]
return Expr(d)
if y.getUbLocal() == y.getLbLocal() and y.getUbLocal() == 0:
equ = False
#print(cons)
p = np.zeros(len(self.model.getVars())+1)
p[0]=1
p[i+1]=2
for j in optProblem.constraints: #constraint_list:
for k in range(1,len(j.A[0])):
#print(i,p,j)
if len(p) == len(j.A.T[k,:]) and np.equal(p,j.A.T[k,:]).all():
equ = True
break
#TODO: need to also make sure, this constraint only appears if y**2 not in any other constraint
if not equ:
#constraint_list.append(ExprToPoly({Term(y,y):-1.0}, nvars))
optProblem.addCons(str(ExprToPoly({Term(y,y):-1.0}, nvars)) + '>= 0')
else:
if y.getUbLocal() != 1e+20:
boundcons = ExprToPoly(Expr({Term(): y.getUbLocal(), Term(y):-1.0}), nvars)
#constraint_list.append(boundcons)
optProblem.addCons(str(boundcons) + ' >= 0')
if y.getLbLocal != -1e+20: #TODO: do we also need: and y.getLbLocal != 0.0:
boundcons = ExprToPoly(Expr({Term(): -y.getLbLocal(), Term(y):1.0}), nvars)
#constraint_list.append(boundcons)
optProblem.addCons(str(boundcons) + ' >= 0')
#print('cons',len(constraint_list))
#print([(con.A, con.b) for con in constraint_list])
#constraint_list = [str(con) + " >= 0" for con in constraint_list]
#print(constraint_list)
"""Here starts the real computation
first try to use the GP, if that does not work use scipy"""
dictP = p.__dict__()
d = dict()
for key in dictP.keys():
i = 0
t =[]
varstr = [None]*len(var)
for j in range(len(var)):
varstr[j] = str(var[j])
for el in key[1:]:
f = 'x' + str(i)
for _ in range(el):
t.append(var[varstr.index(f)])
i+=1
term = Term()
for el in t:
term += Term(el)
d[term] = dictP[key]
return Expr(d)