Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
except ValueError:
pass
else:
self.outputs.pop(i)
for new_s in reversed(expanded_symbols):
self.outputs.insert(i, new_s.symbol.name())
else:
new_vars.append(old_var)
setattr(self, l, new_vars)
if len(self.equations) > 0:
self.equations = ca.substitute(self.equations, symbols, values)
self.equations = list(itertools.chain.from_iterable(ca.vertsplit(ca.vec(eq)) for eq in self.equations))
if len(self.initial_equations) > 0:
self.initial_equations = ca.substitute(self.initial_equations, symbols, values)
self.initial_equations = list(itertools.chain.from_iterable(ca.vertsplit(ca.vec(eq)) for eq in self.initial_equations))
if len(self.delay_arguments) > 0:
self.delay_arguments = self._substitute_delay_arguments(self.delay_arguments, symbols, values)
# Make sure that the naming in the main loop and the delay argument loop match
input_names = [v.symbol.name() for v in self.inputs]
assert set(self.delay_states).issubset(input_names)
# Replace values in metadata
self._substitute_metadata(symbols, values)
jj = jacobian(g, var)
jac = horzcat(jac, jj[:, ind])
sym.append(var)
for sym in symvar(jac):
if sym not in self.par_global.values():
return False, None, None
par = struct_symMX(self.par_global_struct)
A, b = jac, -g
for s in sym:
A = substitute(A, s, np.zeros(s.shape))
b = substitute(b, s, np.zeros(s.shape))
dep_b = [s.name() for s in symvar(b)]
dep_A = [s.name() for s in symvar(b)]
for name, sym in self.par_global.items():
if sym.name() in dep_b:
b = substitute(b, sym, par[name])
if sym.name() in dep_A:
A = substitute(A, sym, par[name])
A = Function('A', [par], [A]).expand()
b = Function('b', [par], [b]).expand()
return True, A, b
jac = horzcat(jac, jj[:, ind])
sym.append(var)
for nghb in self.q_ij.keys():
for child, q_ij in self.q_ij[nghb].items():
for name, ind in q_ij.items():
var = self.distr_problem.father.get_variables(child, name, spline=False, symbolic=True, substitute=False)
jj = jacobian(g, var)
jac = horzcat(jac, jj[:, ind])
sym.append(var)
for sym in symvar(jac):
if sym not in self.par_global.values():
return False, None, None
par = struct_symMX(self.par_global_struct)
A, b = jac, -g
for s in sym:
A = substitute(A, s, np.zeros(s.shape))
b = substitute(b, s, np.zeros(s.shape))
dep_b = [s.name() for s in symvar(b)]
dep_A = [s.name() for s in symvar(b)]
for name, sym in self.par_global.items():
if sym.name() in dep_b:
b = substitute(b, sym, par[name])
if sym.name() in dep_A:
A = substitute(A, sym, par[name])
A = Function('A', [par], [A]).expand()
b = Function('b', [par], [b]).expand()
return True, A, b
ind = self.q_i[child][name]
sym2 = MX.zeros(sym.size())
sym2[ind] = v
sym2 = reshape(sym2, sym.shape)
c = substitute(c, sym, sym2)
break
for nghb in self.q_ij.keys():
for label, child in nghb.group.items():
if sym.name() in child.symbol_dict:
name = child.symbol_dict[sym.name()][1]
v = z_ij[nghb.label, label, name]
ind = self.q_ij[nghb][child][name]
sym2 = MX.zeros(sym.size())
sym2[ind] = v
sym2 = reshape(sym2, sym.shape)
c = substitute(c, sym, sym2)
break
for name, s in self.par_global.items():
if s.name() == sym.name():
c = substitute(c, sym, par[name])
lb, ub = con[1], con[2]
self.define_constraint(c, lb, ub)
# construct problem
prob, buildtime = self.father_updx.construct_problem(
self.options, str(self._index), problem)
self.problem_upd_xz = prob
self.father_updx.init_transformations(self.problem.init_primal_transform,
self.problem.init_dual_transform)
self.init_var_dd()
return buildtime
def _substitute_metadata(self, symbols, values):
substitutions = []
for variable in itertools.chain(self.states, self.alg_states, self.inputs, self.parameters, self.constants):
for attribute in CASADI_ATTRIBUTES:
value = getattr(variable, attribute)
if isinstance(value, ca.MX) and not value.is_constant():
substitutions.append((value, variable, attribute))
if len(substitutions) == 0:
return
expressions, variables, attributes = zip(*substitutions)
expressions = ca.substitute(expressions, symbols, values)
for variable, attribute, value in zip(variables, attributes, expressions):
setattr(variable, attribute, value)
def _substitute_symbols(self, expr, variables, parameters):
if isinstance(expr, (int, float)):
return expr
for sym in symvar(expr):
[child, name] = self.symbol_dict[sym.name()]
if name in child._variables:
expr = substitute(expr, sym, variables[child.label, name])
elif name in child._parameters:
expr = substitute(expr, sym, parameters[child.label, name])
return expr
sym.append(var)
for nghb in self.q_ij.keys():
for child, q_ij in self.q_ij[nghb].items():
for name, ind in q_ij.items():
var = self.distr_problem.father.get_variables(child, name, spline=False, symbolic=True, substitute=False)
jj = jacobian(g, var)
jac = horzcat(jac, jj[:, ind])
sym.append(var)
for sym in symvar(jac):
if sym not in self.par_global.values():
return False, None, None
par = struct_symMX(self.par_global_struct)
A, b = jac, -g
for s in sym:
A = substitute(A, s, np.zeros(s.shape))
b = substitute(b, s, np.zeros(s.shape))
dep_b = [s.name() for s in symvar(b)]
dep_A = [s.name() for s in symvar(b)]
for name, sym in self.par_global.items():
if sym.name() in dep_b:
b = substitute(b, sym, par[name])
if sym.name() in dep_A:
A = substitute(A, sym, par[name])
A = Function('A', [par], [A]).expand()
b = Function('b', [par], [b]).expand()
return True, A, b
sym.append(var)
for sym in symvar(jac):
if sym not in self.par_i.values():
return False, None, None
par = struct_symMX(self.par_struct)
A, b = jac, -g
for s in sym:
A = substitute(A, s, np.zeros(s.shape))
b = substitute(b, s, np.zeros(s.shape))
dep_b = [s.name() for s in symvar(b)]
dep_A = [s.name() for s in symvar(b)]
for name, sym in self.par_i.items():
if sym.name() in dep_b:
b = substitute(b, sym, par[name])
if sym.name() in dep_A:
A = substitute(A, sym, par[name])
A = Function('A', [par], [A]).expand()
b = Function('b', [par], [b]).expand()
return True, A, b
all_symbols,
delay_durations_raw).call(
actual_dep_symbols)
# Get rid of remaining hidden dependencies in the delay durations
for i, expr in enumerate(delay_expressions_raw):
if duration_dependencies[i]:
dur = delay_durations_simplified[i]
if len(duration_dependencies[i]) < len(actual_deps):
deps = set(ca.symvar(dur))
actual_deps = {all_symbols[j] for j in duration_dependencies[i]}
false_deps = deps - actual_deps
if false_deps:
[dur] = ca.substitute(
[dur],
list(false_deps),
[np.nan] * len(false_deps))
else:
# Already removed all false dependencies
pass
else:
dur = independent_delay_durations_raw[i]
model.delay_arguments.append(DelayArgument(expr, dur))
# Done
return model