Skip to content

Commit 7f0747f

Browse files
committed
Simplify the code in check_bounds_conflict.
1 parent 0d35b61 commit 7f0747f

1 file changed

Lines changed: 19 additions & 48 deletions

File tree

opty/direct_collocation.py

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -338,63 +338,34 @@ def check_bounds_conflict(self, free):
338338
a ValueError is raised.
339339
340340
"""
341-
errors1 = []
342-
errors2 = []
341+
eom_rev_errors = []
342+
var_rev_errors = []
343343
if self.eom_bounds is not None:
344344
# check for reversed bounds
345345
for key in self.eom_bounds.keys():
346346
if self.eom_bounds[key][0] > self.eom_bounds[key][1]:
347-
errors1.append(key)
348-
349-
if self.bounds is not None:
350-
# check for reversed bounds
351-
for key in self.bounds.keys():
352-
if np.any(self.bounds[key][0] > self.bounds[key][1]):
353-
errors2.append(key)
354-
355-
errors = errors1 + errors2
356-
if len(errors) > 0:
357-
msg = (f'The lower bound(s) for {errors} is (are) greater than'
358-
f' the upper bound(s).')
359-
raise ValueError(msg)
347+
eom_rev_errors.append(key)
360348

361349
if self.bounds is not None:
362350
violating_variables = []
363-
364-
if self.collocator._variable_duration:
365-
local_ts = self.collocator.time_interval_symbol
366-
if local_ts in self.bounds.keys():
367-
if (free[-1] < self.bounds[local_ts][0]
368-
or free[-1] > self.bounds[local_ts][1]):
369-
violating_variables.append(local_ts)
370-
371-
symbole = (self.collocator.state_symbols +
372-
self.collocator.unknown_input_trajectories)
373-
for symb in symbole:
374-
if symb in self.bounds.keys():
375-
idx = symbole.index(symb)
376-
feld = free[idx*self.collocator.num_collocation_nodes:
377-
(idx+1)*self.collocator.num_collocation_nodes]
378-
if (np.any(feld < self.bounds[symb][0])
379-
or np.any(feld > self.bounds[symb][1])):
380-
violating_variables.append(symb)
381-
382-
# check that initial guesses for unknown parameters are within
383-
startidx = len(symbole) * self.collocator.num_collocation_nodes
384-
for symb in self.collocator.unknown_parameters:
385-
if symb in self.bounds.keys():
386-
idx = self.collocator.unknown_parameters.index(symb)
387-
if (free[startidx+idx] < self.bounds[symb][0]
388-
or free[startidx+idx] > self.bounds[symb][1]):
389-
violating_variables.append(symb)
390-
391-
if len(violating_variables) > 0:
392-
msg = (f'The initial guesses for {violating_variables} are in '
393-
f'conflict with their bounds.')
351+
for sym, (low, high) in self.bounds.items():
352+
# check for reversed bounds
353+
if np.any(low > high):
354+
var_rev_errors.append(sym)
355+
vals = self.extract_values(free, sym)
356+
if np.any(vals < low) or np.any(vals > high):
357+
violating_variables.append(sym)
358+
359+
if violating_variables:
360+
msg = (f'The initial guesses for {violating_variables} '
361+
'are in conflict with their bounds.')
394362
raise ValueError(msg)
395363

396-
else:
397-
pass
364+
errors = eom_rev_errors + var_rev_errors
365+
if len(errors) > 0:
366+
msg = (f'The lower bound(s) for {errors} is (are) '
367+
'greater than the upper bound(s).')
368+
raise ValueError(msg)
398369

399370
def _generate_constraint_bound_arrays(self):
400371

0 commit comments

Comments
 (0)