@@ -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 ):
0 commit comments