Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/hiopbbpy/opt/boalgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,17 @@ def minimizer_callback(self, x0s):
xopt = y.x
yopt = y.fun
elif self.method == "trust-constr":
nonlinear_constraint = NonlinearConstraint(self.constraints['cons'], self.constraints['cl'], self.constraints['cu'], jac=self.constraints['jac'])
y = minimize(self.fun['obj'], x0, method=self.method, bounds=self.bounds, constraints=[nonlinear_constraint], options=self.solver_options)
constraints = []
if self.constraints: # non-empty dict → constrained problem
nonlinear_constraint = NonlinearConstraint(
self.constraints['cons'],
self.constraints['cl'],
self.constraints['cu'],
jac=self.constraints.get('jac', None)
)
constraints.append(nonlinear_constraint)

y = minimize(self.fun['obj'], x0, method=self.method, bounds=self.bounds, constraints=constraints, options=self.solver_options)
success = y.success
if not success:
msg = y.message
Expand Down
20 changes: 15 additions & 5 deletions src/hiopbbpy/opt/optproblem.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ def __init__(self, objective, gradient, constraint:Union[Dict, List[Dict]], xbou
self.eval_g = gradient
self.xl = [b[0] for b in xbounds]
self.xu = [b[1] for b in xbounds]
self.cl = []
self.cu = []
self.nvar = len(xbounds)

self.ipopt_options = solver_options
self.ipopt_options['sb'] = 'yes'

if isinstance(self.cons, list):
unconstrained = (
constraint is None
or constraint == {}
or constraint == []
)
if unconstrained:
self.cl = []
self.cu = []
self.ncon = 0
elif isinstance(self.cons, list):
# constraints is provided as a list of dict, supported by SLSQP and Ipopt
for con in self.cons:
check_required_keys(con,['type', 'fun'])
Expand All @@ -60,7 +67,7 @@ def __init__(self, objective, gradient, constraint:Union[Dict, List[Dict]], xbou
self.cl = constraint['cl']
self.cu = constraint['cu']
else:
raise ValueError("constraints must be provided as a dict of a list of dict.")
raise ValueError("constraints must be None, {}, [], a dict, or a list of dict.")
self.ncon = len(self.cl)

cyipopt = _require_cyipopt()
Expand All @@ -80,10 +87,13 @@ def gradient(self, x):
return self.eval_g(x)

def constraints(self, x):
if self.ncon == 0:
return np.zeros((0,), dtype=float)

if isinstance(self.cons, list):
return np.array([con['fun'](x) for con in self.cons])
else:
return self.cons['cons'](x)
return np.asarray(self.cons['cons'](x), dtype=float)

def jacobian(self, x):
if isinstance(self.cons, list):
Expand Down
14 changes: 8 additions & 6 deletions src/hiopbbpy/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,18 @@ def __init__(self, name='hiopbbpy'):

# Create a logger instance with a given name
self._logger = logging.getLogger(name)
self._logger.propagate = False # prevent double logging

# Create a console output handler
ch = logging.StreamHandler()
if not self._logger.handlers:
ch = logging.StreamHandler()

# Define the output format: logger name, and message
formatter = logging.Formatter('%(name)s %(message)s')
# Define the output format: logger name, and message
formatter = logging.Formatter('%(name)s %(message)s')

# Add the handle
ch.setFormatter(formatter)
self._logger.addHandler(ch)
# Add the handle
ch.setFormatter(formatter)
self._logger.addHandler(ch)

def setlevel(self, level_str):
level = getattr(logging, str(level_str).upper(), logging.INFO)
Expand Down
Loading