Пример #1
0
 def SetObjective(self, cost, ExtraArgs=None):  # callback=None/False ?
     """decorate the cost function with bounds, penalties, monitors, etc"""
     _cost, _raw, _args = self._cost
     # check if need to 'wrap' or can return the stored cost
     if (cost is None or cost is _raw or cost is _cost) and \
        (ExtraArgs is None or ExtraArgs is _args):
         return
     # get cost and args if None was given
     if cost is None: cost = _raw
     args = _args if ExtraArgs is None else ExtraArgs
     args = () if args is None else args
     # quick validation check (so doesn't screw up internals)
     if not isvalid(cost, [0] * self.nDim, *args):
         try:
             name = cost.__name__
         except AttributeError:  # raise new error for non-callables
             cost(*args)
         validate(cost, None, *args)
     #val = len(args) + 1  #XXX: 'klepto.validate' for better error?
     #msg = '%s() invalid number of arguments (%d given)' % (name, val)
     #raise TypeError(msg)
     # hold on to the 'raw' cost function
     self._cost = (None, cost, ExtraArgs)
     self._live = False
     return
Пример #2
0
 def SetObjective(self, cost, ExtraArgs=None):  # callback=None/False ?
     """decorate the cost function with bounds, penalties, monitors, etc"""
     _cost,_raw,_args = self._cost
     # check if need to 'wrap' or can return the stored cost
     if cost in [None, _raw, _cost] and ExtraArgs in [None, _args]:
         return
     # get cost and args if None was given
     if cost is None: cost = _raw
     args = _args if ExtraArgs is None else ExtraArgs
     args = () if args is None else args
     # quick validation check (so doesn't screw up internals)
     if not isvalid(cost, [0]*self.nDim, *args):
         try: name = cost.__name__
         except AttributeError: # raise new error for non-callables
             cost(*args)
         validate(cost, None, *args)
        #val = len(args) + 1  #XXX: 'klepto.validate' for better error?
        #msg = '%s() invalid number of arguments (%d given)' % (name, val)
        #raise TypeError(msg)
     # hold on to the 'raw' cost function
     self._cost = (None, cost, ExtraArgs)
     self._live = False
     return
Пример #3
0
def test_special():
    p = partial(add, 0, x=0)
    p2 = partial(add, z=0)
    p3 = partial(add, 0)

    if IS_PYPY:  # builtins in PYPY are python functions
        if hex(sys.hexversion) < '0x3080cf0':
            base, exp, mod = 'base', 'exponent', 'modulus'
        else:
            base, exp, mod = 'base', 'exp', 'mod'
        assert signature(pow, safe=True) == ((base, exp, mod), {
            mod: None
        }, '', '')
    else:
        assert signature(pow, safe=True) == (None, None, None, None)
    assert signature(p, safe=True) == (None, None, None, None)
    assert signature(p2, safe=True) == (('x', 'y'), {'z': 0}, '', '')
    assert signature(p3, safe=True) == (('y', ), {'!x': 0}, '', '')
    if IS_PYPY:  # PYPY bug in ArgSpec for min, so use pow
        assert isvalid(pow, 0, 1) == True
        assert isvalid(pow, 0) == False
        assert isvalid(pow) == False
    else:  # python >= 3.5 bug in ArgSpec for pow, so use min
        assert isvalid(min, 0, 1) == True
        assert isvalid(min, 0) == False
        assert isvalid(min) == False
    assert isvalid(p, 0, 1) == False
    assert isvalid(p, 0) == False
    assert isvalid(p) == False
    assert isvalid(p2, 0, 1) == False
    assert isvalid(p2, 0) == False
    assert isvalid(p2) == False
    assert isvalid(p3, 0, 1) == False
    assert isvalid(p3, 0) == True
    assert isvalid(p3) == False
    assert _keygen(p3, [], 0) == ((), {'y': 0})
    assert _keygen(p2, [], 0) == ((), {'x': 0, 'z': 0})
    assert _keygen(p, [], 0) == ((0, ), {})
    assert _keygen(min, [], x=0, y=1) == ((), {'y': 1, 'x': 0})
    assert _keygen(min, [], 0, 1) == ((0, 1), {})
    assert _keygen(min, [], 0) == ((0, ), {})
    assert _keygen(min, 'x', 0) == ((0, ), {})
    assert _keygen(min, ['x', 'y'], 0) == ((0, ), {})
    assert _keygen(min, [0, 1], 0) == ((NULL, ), {}) if IS_PYPY else ((0, ),
                                                                      {})
    assert _keygen(min, ['*'], 0) == ((), {}) if IS_PYPY else ((0, ), {})
Пример #4
0
assert foo(10,y=2,z=10) == _hash2

#################################################################
# test special cases (builtins) for signature, isvalid, _keygen
def add(x,y):
    return x+y

p = partial(add, 0,x=0)
p2 = partial(add, z=0)
p3 = partial(add, 0)

assert signature(min, safe=True) == (None, None, None, None)
assert signature(p, safe=True) == (None, None, None, None)
assert signature(p2, safe=True) == (('x', 'y'), {'z': 0}, '', '')
assert signature(p3, safe=True) == (('y',), {'!x': 0}, '', '')
assert isvalid(min, 0,1) == True
assert isvalid(min, 0) == False
assert isvalid(min) == False
assert isvalid(p, 0,1) == False
assert isvalid(p, 0) == False
assert isvalid(p) == False
assert isvalid(p2, 0,1) == False
assert isvalid(p2, 0) == False
assert isvalid(p2) == False
assert isvalid(p3, 0,1) == False
assert isvalid(p3, 0) == True
assert isvalid(p3) == False
assert _keygen(p3, [], 0) == ((), {'y': 0})
assert _keygen(p2, [], 0) == ((), {'x': 0, 'z': 0})
assert _keygen(p, [], 0) == ((0,), {})
assert _keygen(min, [], x=0,y=1) == ((), {'y': 1, 'x': 0})
Пример #5
0
def test_special():
    p = partial(add, 0, x=0)
    p2 = partial(add, z=0)
    p3 = partial(add, 0)

    if IS_PYPY:  # builtins in PYPY are python functions
        assert signature(pow, safe=True) == (("base", "exponent", "modulus"), {"modulus": None}, "", "")
    else:
        assert signature(pow, safe=True) == (None, None, None, None)
    assert signature(p, safe=True) == (None, None, None, None)
    assert signature(p2, safe=True) == (("x", "y"), {"z": 0}, "", "")
    assert signature(p3, safe=True) == (("y",), {"!x": 0}, "", "")
    if IS_PYPY:  # PYPY bug in ArgSpec for min, so use pow
        assert isvalid(pow, 0, 1) == True
        assert isvalid(pow, 0) == False
        assert isvalid(pow) == False
    else:  # python >= 3.5 bug in ArgSpec for pow, so use min
        assert isvalid(min, 0, 1) == True
        assert isvalid(min, 0) == False
        assert isvalid(min) == False
    assert isvalid(p, 0, 1) == False
    assert isvalid(p, 0) == False
    assert isvalid(p) == False
    assert isvalid(p2, 0, 1) == False
    assert isvalid(p2, 0) == False
    assert isvalid(p2) == False
    assert isvalid(p3, 0, 1) == False
    assert isvalid(p3, 0) == True
    assert isvalid(p3) == False
    assert _keygen(p3, [], 0) == ((), {"y": 0})
    assert _keygen(p2, [], 0) == ((), {"x": 0, "z": 0})
    assert _keygen(p, [], 0) == ((0,), {})
    assert _keygen(min, [], x=0, y=1) == ((), {"y": 1, "x": 0})
    assert _keygen(min, [], 0, 1) == ((0, 1), {})
    assert _keygen(min, [], 0) == ((0,), {})
    assert _keygen(min, "x", 0) == ((0,), {})
    assert _keygen(min, ["x", "y"], 0) == ((0,), {})
    assert _keygen(min, [0, 1], 0) == ((NULL,), {}) if IS_PYPY else ((0,), {})
    assert _keygen(min, ["*"], 0) == ((), {}) if IS_PYPY else ((0,), {})
Пример #6
0
def test_special():
    p = partial(add, 0,x=0)
    p2 = partial(add, z=0)
    p3 = partial(add, 0)

    
    if IS_PYPY: # builtins in PYPY are python functions
        assert signature(pow, safe=True) == (('base', 'exponent', 'modulus'), {'modulus': None}, '', '')
    else:
        assert signature(pow, safe=True) == (None, None, None, None)
    assert signature(p, safe=True) == (None, None, None, None)
    assert signature(p2, safe=True) == (('x', 'y'), {'z': 0}, '', '')
    assert signature(p3, safe=True) == (('y',), {'!x': 0}, '', '')
    if IS_PYPY: # PYPY bug in ArgSpec for min, so use pow
        assert isvalid(pow, 0,1) == True
        assert isvalid(pow, 0) == False
        assert isvalid(pow) == False
    else: # python >= 3.5 bug in ArgSpec for pow, so use min
        assert isvalid(min, 0,1) == True
        assert isvalid(min, 0) == False
        assert isvalid(min) == False
    assert isvalid(p, 0,1) == False
    assert isvalid(p, 0) == False
    assert isvalid(p) == False
    assert isvalid(p2, 0,1) == False
    assert isvalid(p2, 0) == False
    assert isvalid(p2) == False
    assert isvalid(p3, 0,1) == False
    assert isvalid(p3, 0) == True
    assert isvalid(p3) == False
    assert _keygen(p3, [], 0) == ((), {'y': 0})
    assert _keygen(p2, [], 0) == ((), {'x': 0, 'z': 0})
    assert _keygen(p, [], 0) == ((0,), {})
    assert _keygen(min, [], x=0,y=1) == ((), {'y': 1, 'x': 0})
    assert _keygen(min, [], 0,1) == ((0,1), {})
    assert _keygen(min, [], 0) == ((0,), {})
    assert _keygen(min, 'x', 0) == ((0,), {})
    assert _keygen(min, ['x','y'], 0) == ((0,), {})
    assert _keygen(min, [0,1], 0) == ((NULL,), {}) if IS_PYPY else ((0,), {})
    assert _keygen(min, ['*'], 0) == ((), {}) if IS_PYPY else ((0,), {})
Пример #7
0
#################################################################
# test special cases (builtins) for signature, isvalid, _keygen
def add(x, y):
    return x + y


p = partial(add, 0, x=0)
p2 = partial(add, z=0)
p3 = partial(add, 0)

assert signature(min, safe=True) == (None, None, None, None)
assert signature(p, safe=True) == (None, None, None, None)
assert signature(p2, safe=True) == (('x', 'y'), {'z': 0}, '', '')
assert signature(p3, safe=True) == (('y', ), {'!x': 0}, '', '')
assert isvalid(min, 0, 1) == True
assert isvalid(min, 0) == False
assert isvalid(min) == False
assert isvalid(p, 0, 1) == False
assert isvalid(p, 0) == False
assert isvalid(p) == False
assert isvalid(p2, 0, 1) == False
assert isvalid(p2, 0) == False
assert isvalid(p2) == False
assert isvalid(p3, 0, 1) == False
assert isvalid(p3, 0) == True
assert isvalid(p3) == False
assert _keygen(p3, [], 0) == ((), {'y': 0})
assert _keygen(p2, [], 0) == ((), {'x': 0, 'z': 0})
assert _keygen(p, [], 0) == ((0, ), {})
assert _keygen(min, [], x=0, y=1) == ((), {'y': 1, 'x': 0})