コード例 #1
0
def DynT_Constraints(N: dyn_typing.DepSize) -> dyn_typing.DepType:
    nConstraint = dyn_typing.SizeVar()
    return dyn_typing.Tuple((
        dyn_typing.NDArray(numpy.float64, (nConstraint, N)),
        dyn_typing.NDArray(numpy.float64, (nConstraint, )),
        dyn_typing.NDArray(numpy.float64, (N, )),
        dyn_typing.NDArray(numpy.float64, (N, )),
    ))
コード例 #2
0
 def test_NDArray(self) -> None:
     T = dyn_typing.NDArray(numpy.float64, (SizeConst(2), SizeConst(4)))
     assert T.isinstance(numpy.zeros((2, 4)))
     assert not T.isinstance(numpy.zeros((2, 5)))
     assert not T.isinstance(numpy.zeros((2, 4), dtype=numpy.int64))
     assert not T.isinstance(numpy.zeros((2, 4, 5)))
     assert not T.isinstance(1)
     assert T.isinstance(numpy.zeros((2, 4), order="F"))
     n = SizeVar()
     T = dyn_typing.NDArray(numpy.float64, (n, n))
     assert T.isinstance(numpy.zeros((1, 1)))
     assert T.isinstance(numpy.zeros((2, 2)))
     assert T.isinstance(numpy.zeros((3, 3)))
     assert not T.isinstance(numpy.zeros((1, 2)))
     assert not T.isinstance(numpy.zeros((2, 1)))
     assert not T.isinstance(numpy.zeros((3, 2)))
     assert not T.isinstance(numpy.zeros((3, 4)))
コード例 #3
0
def _pcg_output_check(output: Status) -> None:
    pass


N = dyn_typing.SizeVar()

assertNoInfNaN_proj = cast(Callable[[typing.proj_t], None], assertNoInfNaN)


@dyn_typing.dyn_check_4(
    input=(
        dyn_typing.Class(QuadEvaluator),
        typing.DynT_Constraints(N),
        dyn_typing.Float(),
        dyn_typing.NDArray(numpy.float64, (N, N)),
    ),
    output=dyn_typing.Class(Status),
)
@bind_checker.bind_checker_4(
    input=bind_checker.make_checker_4(
        no_check_QPeval,
        constraint_check,
        assertNoInfNaN_float,
        assertNoInfNaN_proj,
    ),
    output=_pcg_output_check,
)
def quad_prog(
    qpval: QuadEvaluator,
    constraints: typing.constraints_t,
コード例 #4
0
def f9(a: Any, b: Any, c: Any, d: Any, e: Any, f: Any, g: Any, h: Any,
       ii: Any) -> None:
    return None


fff = (f0, f1, f2, f3, f4, f5, f6, f7, f8, f9)

MM = SizeVar()
NN = SizeVar()
KK = SizeVar()


@tuplize.tuplize_2
@dyn_typing.dyn_check_2(
    input=(
        dyn_typing.NDArray(numpy.float64, (MM, KK)),
        dyn_typing.NDArray(numpy.float64, (KK, NN)),
    ),
    output=dyn_typing.NDArray(numpy.float64, (MM, NN)),
)
def matmul(a: Any, b: Any) -> Any:
    return a @ b


@tuplize.tuplize_2
@dyn_typing.dyn_check_2(
    input=(
        dyn_typing.NDArray(numpy.float64, (MM, KK)),
        dyn_typing.NDArray(numpy.float64, (KK, NN)),
    ),
    output=dyn_typing.NDArray(numpy.float64, (MM, NN)),
コード例 #5
0
ファイル: linneq.py プロジェクト: Andy-math/optimizer

def _input_checker(
    parameters: Tuple[ndarray, Tuple[ndarray, ndarray, ndarray,
                                     ndarray]]) -> None:
    theta, constraints = parameters
    constraint_check(constraints, theta=theta)


n = dyn_typing.SizeVar()
nConstraint = dyn_typing.SizeVar()


@dyn_typing.dyn_check_2(
    input=(
        dyn_typing.NDArray(numpy.float64, (n, )),
        dyn_typing.Tuple((
            dyn_typing.NDArray(numpy.float64, (nConstraint, n)),
            dyn_typing.NDArray(numpy.float64, (nConstraint, )),
            dyn_typing.NDArray(numpy.float64, (n, )),
            dyn_typing.NDArray(numpy.float64, (n, )),
        )),
    ),
    output=dyn_typing.Bool(),
)
@bind_checker.bind_checker_2(input=_input_checker, output=noCheck)
def check(theta: ndarray, constraints: Tuple[ndarray, ndarray, ndarray,
                                             ndarray]) -> bool:
    A, b, lb, ub = constraints
    """检查参数theta是否满足约束[A @ theta <= b],空约束返回True"""
    result = bool(
コード例 #6
0
import numpy
from optimizer._internals.common import findiff
from overloads import bind_checker, difference, dyn_typing
from overloads.shortcuts import assertNoInfNaN
from overloads.typedefs import ndarray

H = numpy.array([[2, 1], [1, 1]])
A = numpy.array([[1, 1]], dtype=numpy.float64)
b = numpy.array([1], dtype=numpy.float64)
lb = numpy.zeros((2, ))
ub = numpy.full((2, ), numpy.inf)


@dyn_typing.dyn_check_1(
    input=(dyn_typing.NDArray(numpy.float64, (dyn_typing.SizeConst(2), )), ),
    output=dyn_typing.NDArray(numpy.float64, (dyn_typing.SizeConst(2), )),
)
@bind_checker.bind_checker_1(  # force line wrap
    input=bind_checker.make_checker_1(assertNoInfNaN),  # force line wrap
    output=assertNoInfNaN,  # force line wrap
)
def grad(x: ndarray) -> ndarray:
    x1, x2 = x
    assert x1 + x2 <= 1
    assert x1 >= 0
    assert x2 >= 0
    g = (H @ x) * 2.0
    return g  # type: ignore


hessian_GT: ndarray = 2.0 * H