sympy.log: lambda x: core.log(x), sympy.sin: lambda x: core.sin(x), sympy.asin: lambda x: core.asin(x), sympy.sinh: lambda x: core.sinh(x), sympy.asinh: lambda x: core.asinh(x), sympy.cos: lambda x: core.cos(x), sympy.acos: lambda x: core.acos(x), sympy.cosh: lambda x: core.cosh(x), sympy.acosh: lambda x: core.acosh(x), sympy.tan: lambda x: core.tan(x), sympy.atan: lambda x: core.atan(x), sympy.tanh: lambda x: core.tanh(x), sympy.atanh: lambda x: core.atanh(x), sympy.ceiling: lambda x: core.ceil(x), sympy.floor: lambda x: core.floor(x), sympy.sqrt: lambda x: core.sqrt(x), sympy.Derivative: _nondifferentiable, sympy.Tuple: lambda *x: x, } _pyomo_operator_map = { EXPR.SumExpression: sympy.Add, EXPR.ProductExpression: sympy.Mul, EXPR.NPV_ProductExpression: sympy.Mul, EXPR.MonomialTermExpression: sympy.Mul, } _functionMap = { 'exp': sympy.exp, 'log': sympy.log, 'log10': lambda x: sympy.log(x) / sympy.log(10),
def criticalityCheck(self, x, y, z, rom_params, worstcase=False, M=[0.0]): model = self.model self.setVarValue(x=x, y=y, z=z) self.setBound(x, y, z, 1e10) self.deactiveExtraConObj() self.activateRomCons(x, rom_params) optGJH = SolverFactory('contrib.gjh') optGJH.solve(model, tee=False, symbolic_solver_labels=True) g, J, varlist, conlist = model._gjh_info l = ConcreteModel() l.v = Var(varlist, domain=Reals) for i in varlist: #dummy = model.find_component(i) l.v[i] = 0.0 l.v[i].setlb(-1.0) l.v[i].setub(1.0) if worstcase: if M.all() == 0.0: print( 'WARNING: worstcase criticality was requested but Jacobian error bound is zero' ) l.t = Var(range(0, self.ly), domain=Reals) for i in range(0, self.ly): l.t[i].setlb(-M[i]) l.t[i].setub(M[i]) def linConMaker(l, i): # i should be range(len(conlist) - 1) # because last element of conlist is the objective con_i = model.find_component(conlist[i]) isEquality = con_i.equality isROM = False if conlist[i][:7] == '.' + self.TRF.name + '.rom': isROM = True romIndex = int(filter(str.isdigit, conlist[i])) # This is very inefficient # Fix this later if these problems get big # This is the ith row of J times v Jv = sum(x[2] * l.v[varlist[x[1]]] for x in J if x[0] == i) if isEquality: if worstcase and isROM: return Jv + l.t[romIndex] == 0 else: return Jv == 0 else: lo = con_i.lower up = con_i.upper if lo is not None: level = lo.value - con_i.lslack() if up is not None: return (lo.value <= level + Jv <= up.value) else: return (lo.value <= level + Jv) elif up is not None: level = up.value - con_i.uslack() return (level + Jv <= up.value) else: raise Exception( "This constraint seems to be neither equality or inequality: " + conlist(i)) l.lincons = Constraint(range(len(conlist) - 1), rule=linConMaker) l.obj = Objective(expr=sum(x[1] * l.v[varlist[x[0]]] for x in g)) # Calculate gradient norm for scaling purposes gfnorm = sqrt(sum(x[1]**2 for x in g)) opt = SolverFactory(self.config.solver) opt.options.update(self.config.solver_options) #opt.options['halt_on_ampl_error'] = 'yes' #opt.options['max_iter'] = 5000 results = opt.solve(l, keepfiles=self.keepfiles, tee=self.stream_solver) if ((results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal)): l.solutions.load_from(results) if gfnorm > 1: return True, abs(l.obj()) / gfnorm else: return True, abs(l.obj()) else: print("Waring: Crticality check fails with solver Status: " + str(results.solver.status)) print("And Termination Conditions: " + str(results.solver.termination_condition)) return False, infinity
def criticalityCheck(self, x, y, z, rom_params, worstcase=False, M=[0.0]): model = self.model self.setVarValue(x=x,y=y,z=z) self.setBound(x, y, z, 1e10) self.deactiveExtraConObj() self.activateRomCons(x, rom_params) optGJH = SolverFactory('contrib.gjh') optGJH.solve(model, tee=False, symbolic_solver_labels=True) g, J, varlist, conlist = model._gjh_info l = ConcreteModel() l.v = Var(varlist, domain=Reals) for i in varlist: #dummy = model.find_component(i) l.v[i] = 0.0 l.v[i].setlb(-1.0) l.v[i].setub(1.0) if worstcase: if M.all() == 0.0: print('WARNING: worstcase criticality was requested but Jacobian error bound is zero') l.t = Var(range(0, self.ly), domain=Reals) for i in range(0, self.ly): l.t[i].setlb(-M[i]) l.t[i].setub(M[i]) def linConMaker(l, i): # i should be range(len(conlist) - 1) # because last element of conlist is the objective con_i = model.find_component(conlist[i]) isEquality = con_i.equality isROM = False if conlist[i][:7] == '.' + self.TRF.name + '.rom': isROM = True romIndex = int(filter(str.isdigit, conlist[i])) # This is very inefficient # Fix this later if these problems get big # This is the ith row of J times v Jv = sum(x[2] * l.v[varlist[x[1]]] for x in J if x[0] == i) if isEquality: if worstcase and isROM: return Jv + l.t[romIndex] == 0 else: return Jv == 0 else: lo = con_i.lower up = con_i.upper if lo is not None: level = lo.value - con_i.lslack() if up is not None: return (lo.value <= level + Jv <= up.value) else: return (lo.value <= level + Jv) elif up is not None: level = up.value - con_i.uslack() return (level + Jv <= up.value) else: raise Exception( "This constraint seems to be neither equality or inequality: " + conlist(i)) l.lincons = Constraint(range(len(conlist)-1), rule=linConMaker) l.obj = Objective(expr=sum(x[1] * l.v[varlist[x[0]]] for x in g)) # Calculate gradient norm for scaling purposes gfnorm = sqrt(sum(x[1]**2 for x in g)) opt = SolverFactory(self.config.solver) opt.options.update(self.config.solver_options) #opt.options['halt_on_ampl_error'] = 'yes' #opt.options['max_iter'] = 5000 results = opt.solve( l, keepfiles=self.keepfiles, tee=self.stream_solver) if ((results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal)): l.solutions.load_from(results) if gfnorm > 1: return True, abs(l.obj())/gfnorm else: return True, abs(l.obj()) else: print("Waring: Crticality check fails with solver Status: " + str(results.solver.status)) print("And Termination Conditions: " + str(results.solver.termination_condition)) return False, infinity