Commit 4459ec6e authored by Jorn Baayen's avatar Jorn Baayen

Clean up bounds merge logic.

parent 3dc54e74
......@@ -244,41 +244,39 @@ class ModelicaMixin(OptimizationProblem):
# Load additional bounds from model
for v in itertools.chain(self.__pymola_model.states, self.__pymola_model.alg_states, self.__pymola_model.inputs):
sym_name = v.symbol.name()
discrete = self.__discrete.get(sym_name, False)
np_shape = (v.symbol.size1(), v.symbol.size2())
(m, M) = bounds.get(sym_name, (np.full(np_shape, -np.inf), np.full(np_shape, np.inf)))
if self.__discrete.get(sym_name, False):
if np.all(not np.isfinite(m)):
m = np.zeros(np_shape)
if np.all(not np.isfinite(M)):
M = np.ones(np_shape)
try:
(m, M) = bounds[sym_name]
except KeyError:
(m, M) = (np.full(np_shape, -np.inf), np.full(np_shape, np.inf))
m_ = ca.MX(v.min)
if not m_.is_constant():
[m_] = substitute_in_external([m_], self.__mx['parameters'], parameter_values)
if m_.is_constant():
m_ = array_from_mx(m_)
if np.any(m_ > m):
m = m_
else:
if not m_.is_constant():
raise Exception('Could not resolve lower bound for variable {}'.format(sym_name))
else:
m_ = array_from_mx(m_)
if np.any(np.isfinite(m_)) and np.any(m_ > m):
m = m_
m_ = array_from_mx(m_)
M_ = ca.MX(v.max)
if not M_.is_constant():
[M_] = substitute_in_external([M_], self.__mx['parameters'], parameter_values)
if M_.is_constant():
M_ = array_from_mx(M_)
if np.any(M_ < M):
M = M_
else:
if not M_.is_constant():
raise Exception('Could not resolve upper bound for variable {}'.format(sym_name))
else:
M_ = array_from_mx(M_)
if np.any(np.isfinite(M_)) and np.any(M_ < M):
M = M_
M_ = array_from_mx(M_)
# We take the intersection of all provided bounds
for i in range(np_shape[0]):
for j in range(np_shape[1]):
m[i, j] = max(m[i, j], m_[i, j])
M[i, j] = min(M[i, j], M_[i, j])
if discrete:
if not np.isfinite(m[i, j]):
m[i, j] = 0
if not np.isfinite(M[i, j]):
M[i, j] = 1
# Cast to scalar whenever possibe
if m.shape[0] * m.shape[1] == 1:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment