Skip to content

Commit 204597f

Browse files
committed
create branch
1 parent e004aba commit 204597f

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

src/hiopbbpy/opt/boalgorithm.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,17 @@ def minimizer_callback(self, x0s):
351351
xopt = y.x
352352
yopt = y.fun
353353
elif self.method == "trust-constr":
354-
nonlinear_constraint = NonlinearConstraint(self.constraints['cons'], self.constraints['cl'], self.constraints['cu'], jac=self.constraints['jac'])
355-
y = minimize(self.fun['obj'], x0, method=self.method, bounds=self.bounds, constraints=[nonlinear_constraint], options=self.solver_options)
354+
constraints = []
355+
if self.constraints: # non-empty dict → constrained problem
356+
nonlinear_constraint = NonlinearConstraint(
357+
self.constraints['cons'],
358+
self.constraints['cl'],
359+
self.constraints['cu'],
360+
jac=self.constraints.get('jac', None)
361+
)
362+
constraints.append(nonlinear_constraint)
363+
364+
y = minimize(self.fun['obj'], x0, method=self.method, bounds=self.bounds, constraints=constraints, options=self.solver_options)
356365
success = y.success
357366
if not success:
358367
msg = y.message

src/hiopbbpy/opt/optproblem.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,21 @@ def __init__(self, objective, gradient, constraint:Union[Dict, List[Dict]], xbou
3535
self.eval_g = gradient
3636
self.xl = [b[0] for b in xbounds]
3737
self.xu = [b[1] for b in xbounds]
38-
self.cl = []
39-
self.cu = []
4038
self.nvar = len(xbounds)
4139

4240
self.ipopt_options = solver_options
4341
self.ipopt_options['sb'] = 'yes'
4442

45-
if isinstance(self.cons, list):
43+
unconstrained = (
44+
constraint is None
45+
or constraint == {}
46+
or constraint == []
47+
)
48+
if unconstrained:
49+
self.cl = []
50+
self.cu = []
51+
self.ncon = 0
52+
elif isinstance(self.cons, list):
4653
# constraints is provided as a list of dict, supported by SLSQP and Ipopt
4754
for con in self.cons:
4855
check_required_keys(con,['type', 'fun'])
@@ -60,7 +67,7 @@ def __init__(self, objective, gradient, constraint:Union[Dict, List[Dict]], xbou
6067
self.cl = constraint['cl']
6168
self.cu = constraint['cu']
6269
else:
63-
raise ValueError("constraints must be provided as a dict of a list of dict.")
70+
raise ValueError("constraints must be None, {}, [], a dict, or a list of dict.")
6471
self.ncon = len(self.cl)
6572

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

8289
def constraints(self, x):
90+
if self.ncon == 0:
91+
return np.zeros((0,), dtype=float)
92+
8393
if isinstance(self.cons, list):
8494
return np.array([con['fun'](x) for con in self.cons])
8595
else:
86-
return self.cons['cons'](x)
96+
return np.asarray(self.cons['cons'](x), dtype=float)
8797

8898
def jacobian(self, x):
8999
if isinstance(self.cons, list):

src/hiopbbpy/utils/util.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ def __init__(self, name='hiopbbpy'):
120120

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

124125
# Create a console output handler
125-
ch = logging.StreamHandler()
126+
if not self._logger.handlers:
127+
ch = logging.StreamHandler()
126128

127-
# Define the output format: logger name, and message
128-
formatter = logging.Formatter('%(name)s %(message)s')
129+
# Define the output format: logger name, and message
130+
formatter = logging.Formatter('%(name)s %(message)s')
129131

130-
# Add the handle
131-
ch.setFormatter(formatter)
132-
self._logger.addHandler(ch)
132+
# Add the handle
133+
ch.setFormatter(formatter)
134+
self._logger.addHandler(ch)
133135

134136
def setlevel(self, level_str):
135137
level = getattr(logging, str(level_str).upper(), logging.INFO)

0 commit comments

Comments
 (0)