示例#1
0
def lcp_generic(id, z, w):

    options = sn.SolverOptions(id)
    info = sn.linearComplementarity_driver(lcp, z, w, options)
    print(' iter =', options.iparam[sn.SICONOS_IPARAM_ITER_DONE])
    print(' error=', options.dparam[sn.SICONOS_DPARAM_RESIDU])
    return info
示例#2
0
def lemke_optimizer(eco, payoff_matrix=None, dirac_mode=True):
    A = np.zeros((eco.populations.size, eco.populations.size * eco.layers))
    for k in range(eco.populations.size):
        A[k, k * eco.layers:(k + 1) * eco.layers] = -1

    q = np.zeros(eco.populations.size + eco.populations.size * eco.layers)
    q[eco.populations.size * eco.layers:] = -1
    q = q.reshape(-1, 1)
    if payoff_matrix is None:
        payoff_matrix = total_payoff_matrix_builder(eco)

    H = np.block([[-payoff_matrix, A.T],
                  [-A, np.zeros((A.shape[0], eco.populations.size))]])
    lcp = sn.LCP(H, q)
    ztol = 1e-8

    #solvers = [sn.SICONOS_LCP_PGS, sn.SICONOS_LCP_QP,
    #           sn.SICONOS_LCP_LEMKE, sn.SICONOS_LCP_ENUM]

    z = np.zeros((eco.layers * 2 + eco.populations.size, ), np.float64)
    w = np.zeros_like(z)
    options = sn.SolverOptions(sn.SICONOS_LCP_PIVOT)
    #sn.SICONOS_IPARAM_MAX_ITER = 10000000
    #options.iparam[sn.SICONOS_IPARAM_MAX_ITER] = 1000000
    options.dparam[sn.SICONOS_DPARAM_TOL] = 10**(-5)
    info = sn.linearComplementarity_driver(lcp, z, w, options)
    if sn.lcp_compute_error(lcp, z, w, ztol) > 10**(-5):
        print(sn.lcp_compute_error(lcp, z, w, ztol), "Error")
    #print(info, "Info")
    return z
示例#3
0
def test_lcp_pgs():
    SO = N.SolverOptions(lcp, N.SICONOS_LCP_PGS)
    info = N.lcp_pgs(lcp, z, w, SO)
    print('pgs iter =', SO.iparam[1])
    print('pgs error=', SO.dparam[1])
    assert (np.linalg.norm(z - zsol) <= ztol)
    assert not info
def test_fc3dnsgs():
    N.setNumericsVerbose(2)
    FCP = N.FrictionContactProblem(3,M,q,mu)
    SO=N.SolverOptions(N.SICONOS_FRICTION_3D_NSGS)
    r=N.fc3d_nsgs(FCP, reactions, velocities, SO)
    assert SO.dparam[1] < 1e-10
    assert not r
def test_fc3dlocalac():
    N.setNumericsVerbose(2)
    FCP = N.FrictionContactProblem(3,M,q,mu)
    SO=N.SolverOptions(N.SICONOS_FRICTION_3D_NSN_AC)

    r = N.fc3d_nonsmooth_Newton_AlartCurnier(FCP, reactions, velocities, SO)
    assert SO.dparam[1] < 1e-10
    assert not r
def test_fc3dfischer():
    N.setNumericsVerbose(2)
    FCP = N.FrictionContactProblem(3,M,q,mu)
    SO=N.SolverOptions(N.SICONOS_FRICTION_3D_NSN_FB)

    r = N.fc3d_nonsmooth_Newton_FischerBurmeister(FCP, reactions, velocities, SO)
    assert SO.dparam[1] < 1e-10
    assert not r
示例#7
0
def solve_reduced(fcp, solver_reduced):
    SO_reduced = sn.SolverOptions(solver_reduced)
    SO_reduced.dparam[0] = np.sqrt(fcp.numberOfContacts) * 1e-9
    size_reaction = fcp.numberOfContacts * 3
    reaction_reduced = np.zeros((size_reaction, ))
    velocities_reduced = np.zeros((size_reaction, ))

    return sn.fc3d_driver(fcp, reaction_reduced, velocities_reduced,
                          SO_reduced, no)
示例#8
0
def test_ncp_newton_FBLSA():
    ncp = SN.NCP(2, ncp_function, ncp_Nablafunction)
    z = np.array([0., 0.])
    w = np.array([0., 0.])

    SO = SN.SolverOptions(SN.SICONOS_NCP_NEWTON_FB_FBLSA)
    info = SN.ncp_driver(ncp, z, w, SO)
    assert (np.linalg.norm(z - zsol) <= ztol)
    assert not info
示例#9
0
def solve_global(fcp, solver):
    SO = sn.SolverOptions(solver)
    SO.dparam[0] = np.sqrt(fcp.numberOfContacts) * 1e-9

    n, m = fcp.H.shape
    size_reaction = m
    reaction = np.zeros((size_reaction, ))
    velocities = np.zeros((size_reaction, ))
    global_velocities = np.zeros((n, ))
    return sn.gfc3d_driver(fcp, reaction, velocities, global_velocities, SO)
示例#10
0
def test_mcp_newton_FB_FBLSA_2():
    mcp = SN.MCP(n - 5, 5, mcp_function_2, mcp_Nablafunction_2)
    z = np.zeros(n)
    w = np.zeros(n)
    SO = SN.SolverOptions(mcp, SN.SICONOS_MCP_NEWTON_FB_FBLSA)
    info = SN.mcp_newton_FB_FBLSA(mcp, z, w, SO)
    print("z = ", z)
    print("w = ", w)
    #assert (np.linalg.norm(z-zsol) <= ztol)
    assert not info
示例#11
0
def test_mcp_newton_min_FBLSA():
    mcp = sn.MCP(0, 2, mcp_function, mcp_Nablafunction)
    z = np.array([0., 0.])
    w = np.array([0., 0.])

    SO = sn.SolverOptions(sn.SICONOS_MCP_NEWTON_MIN_FBLSA)
    info = sn.mcp_newton_min_FBLSA(mcp, z, w, SO)
    print("z = ", z)
    print("w = ", w)
    assert np.linalg.norm(z - zsol) <= ztol
    assert not info
示例#12
0
def test_mcp_newton_minFBLSA():
    mcp = SN.MixedComplementarityProblem2(0, 2, mcp_function, mcp_Nablafunction)
    z = np.array([0., 0.])
    w = np.array([0., 0.])

    SO = SN.SolverOptions(mcp, SN.SICONOS_MCP_NEWTON_MINFBLSA)
    info = SN.mcp_newton_minFBLSA(mcp, z, w, SO)
    #print("z = ", z)
    #print("w = ", w)
    assert (np.linalg.norm(z-zsol) <= ztol)
    assert not info
示例#13
0
def test_mcp_FB_2():
    mcp = sn.MCP_old(n - 3, 3, mcp_function_2, mcp_Nablafunction_2)
    z = np.zeros(n)
    w = np.zeros(n)

    options = sn.SolverOptions(sn.SICONOS_MCP_OLD_FB)
    sn.mcp_old_driver_init(mcp, options)
    info = sn.mcp_old_FischerBurmeister(mcp, z, w, options)
    sn.mcp_old_driver_reset(mcp, options)
    print("z = ", z)
    print("w = ", w)
    assert not info
示例#14
0
def test_mcp_FB_2():
    mcp = N.MCP_old(n - 3, 3, mcp_function_2, mcp_Nablafunction_2)
    z = np.zeros(n)
    w = np.zeros(n)

    SO = N.SolverOptions(mcp, N.SICONOS_MCP_OLD_FB)
    N.mcp_old_driver_init(mcp, SO)
    info = N.mcp_old_FischerBurmeister(mcp, z, w, SO)
    N.mcp_old_driver_reset(mcp, SO)
    print("z = ", z)
    print("w = ", w)
    assert not info
示例#15
0
def test_mlcp_enum_large():
    z = np.array([0., 0., 0., 0., 0., 0., 0.])
    w = np.array([0., 0., 0., 0., 0., 0., 0.])
    mlcp, zsol = createMLCP_large()
    SO = sn.SolverOptions(sn.SICONOS_MLCP_ENUM)
    sn.mlcp_driver_init(mlcp, SO)
    info = sn.mlcp_enum(mlcp, z, w, SO)
    sn.mlcp_driver_reset(mlcp, SO)
    print("z = ", z)
    print("w = ", w)
    assert (np.linalg.norm(z - zsol) <= ztol)
    assert not info
示例#16
0
def test_mcp_FB():
    mcp = N.MCP_old(1, 1, mcp_function, mcp_Nablafunction)
    z = np.array([0., 0.])
    w = np.array([0., 0.])

    SO = N.SolverOptions(mcp, N.SICONOS_MCP_OLD_FB)
    N.mcp_old_driver_init(mcp, SO)
    info = N.mcp_old_FischerBurmeister(mcp, z, w, SO)
    N.mcp_old_driver_reset(mcp, SO)
    print("z = ", z)
    print("w = ", w)
    assert (np.linalg.norm(z - zsol) <= ztol)
    assert not info
示例#17
0
def test_vi_1D():
    vi = SN.VI(1, vi_function_1D)
    vi.set_compute_nabla_F(vi_nabla_function_1D)
    x = np.array([0.])
    F = np.array([0.])

    SO = SN.SolverOptions(vi, SN.SICONOS_VI_BOX_QI)
    lb = np.array((-1.0, ))
    ub = np.array((1.0, ))
    vi.set_box_constraints(lb, ub)
    info = SN.variationalInequality_box_newton_QiLSA(vi, x, F, SO)
    print(info)
    print("x = ", x)
    print("F = ", F)
    assert (np.linalg.norm(x - xsol_1D) <= xtol)
    assert not info
示例#18
0
def test_mcp_newton_min_FBLSA_2():
    n = 10
    mcp = sn.MCP(n - 5, 5, mcp_function_2, mcp_Nablafunction_2)
    z = np.zeros(n)
    w = np.zeros(n)
    options = sn.SolverOptions(sn.SICONOS_MCP_NEWTON_MIN_FBLSA)
    options.iparam[sn.SICONOS_IPARAM_STOPPING_CRITERION] = \
        sn.SICONOS_STOPPING_CRITERION_RESIDU

    sn.solver_options_print(options)

    info = sn.mcp_newton_min_FBLSA(mcp, z, w, options)
    print("z = ", z)
    print("w = ", w)
    #assert (np.linalg.norm(z-zsol) <= ztol)
    assert not info
示例#19
0
def test_mcp_newton_min_FBLSA_2():
    mcp = SN.MCP(n - 5, 5, mcp_function_2, mcp_Nablafunction_2)
    z = np.zeros(n)
    w = np.zeros(n)
    SO = SN.SolverOptions(mcp, SN.SICONOS_MCP_NEWTON_MIN_FBLSA)

    SO.internalSolvers.iparam[
        SN.
        SICONOS_IPARAM_STOPPING_CRITERION] = SN.SICONOS_STOPPING_CRITERION_RESIDU

    SN.solver_options_print(SO)

    info = SN.mcp_newton_min_FBLSA(mcp, z, w, SO)
    print("z = ", z)
    print("w = ", w)
    #assert (np.linalg.norm(z-zsol) <= ztol)
    assert not info
示例#20
0
def test_vi_2D():
    vi = SN.VI(2, vi_function_2D)
    vi.set_compute_nabla_F(vi_nabla_function_2D)
    x = np.array((0., 0.))
    F = np.array((0., 0.))

    SO = SN.SolverOptions(vi, SN.SICONOS_VI_BOX_QI)
    lb = np.array((-1.0, -1.0))
    ub = np.array((1.0, 1.0))
    vi.set_box_constraints(lb, ub)
    info = SN.variationalInequality_box_newton_QiLSA(vi, x, F, SO)
    print(info)
    print('number of iteration {:} ; precision {:}'.format(SO.iparam[1], SO.dparam[1]))
    print("x = ", x)
    print("F = ", F)
    assert (np.linalg.norm(x-xsol_2D) <= xtol)
    assert not info
示例#21
0
def test_vi_3D():
    vi = sn.VI(3, vi_function_3D)
    x = np.zeros((3, ))
    F = np.zeros((3, ))

    SO = sn.SolverOptions(sn.SICONOS_VI_BOX_QI)
    vi.set_compute_nabla_F(vi_nabla_function_3D)
    lb = np.array((-1.0, -1.0, -1.0))
    ub = np.array((1.0, 1.0, 1.0))
    vi.set_box_constraints(lb, ub)
    info = sn.variationalInequality_box_newton_QiLSA(vi, x, F, SO)
    print(info)
    print('number of iteration {:} ; precision {:}'.format(
        SO.iparam[sn.SICONOS_IPARAM_ITER_DONE],
        SO.dparam[sn.SICONOS_DPARAM_RESIDU]))
    print("x = ", x)
    print("F = ", F)
    assert (np.linalg.norm(x - xsol_3D) <= xtol)
    assert not info
    assert (np.abs(SO.dparam[sn.SICONOS_DPARAM_RESIDU]) < 1e-10)
示例#22
0
def test_ncp_path():
    ncp = SN.NCP(2, ncp_function, ncp_Nablafunction)
    z = np.array([0., 0.])
    w = np.array([0., 0.])

    SO = SN.SolverOptions(SN.SICONOS_NCP_PATH)
    info = SN.ncp_driver(ncp, z, w, SO)

    if siconos.WITH_PATHFERRIS:
        assert (np.linalg.norm(z - zsol) <= ztol)
        assert not info
        return
    else:
        assert info != 0

        try:
            SN.ncp_path(ncp, z, w, SO)
        except RuntimeError:
            pass
        except:
            assert 0
示例#23
0
 def __init__(self, name=None, gnuplot_name=None, API=None, TAG=None, iparam_iter=None,
              dparam_err=None, maxiter=None, precision=None, with_guess=None):
     self._name = name
     if (gnuplot_name==None):
         self._gnuplot_name = self._name
     else:
         self._gnuplot_name = gnuplot_name
     self._API = API
     self._TAG = TAG
     self._iparam_iter = iparam_iter
     self._dparam_err = dparam_err
     self._SO = N.SolverOptions(TAG)  # set default solver options
     if (maxiter==None):
         raise RuntimeError ("SiconosSolver() maxiter have to specified.")
     else :
         self._SO.iparam[0] = maxiter
     if (precision==None):
         raise RuntimeError ("SiconosSolver() precision have to specified.")
     else:
         self._SO.dparam[0] = precision
     if (with_guess==None):
         raise RuntimeError ("SiconosSolver() with_guess have to specified.")
     else:
         self._with_guess = with_guess
示例#24
0
def test_lcp_lexicolemke():
    SO = N.SolverOptions(lcp, N.SICONOS_LCP_LEMKE)
    info = N.lcp_lexicolemke(lcp, z, w, SO)
    print('lexicolemke iter =', SO.iparam[1])
    assert (np.linalg.norm(z - zsol) <= ztol)
    assert not info
示例#25
0
def test_vi_C_interface():
    try:
        from cffi import FFI
        cffi_is_present = True
    except:
        cffi_is_present = False
        return

    if cffi_is_present:
        h = 1e-5
        T = 1.0
        t = 0.0

        theta = 1.0
        gamma = 1.0
        g = 9.81
        kappa = 0.4

        xk = np.array((1., 10.))
        ffi = FFI()
        ffi.cdef('void set_cstruct(uintptr_t p_env, void* p_struct);')
        ffi.cdef('''typedef struct
                 {
                 int id;
                 double* xk;
                 double h;
                 double theta;
                 double gamma;
                 double g;
                 double kappa;
                 unsigned int f_eval;
                 unsigned int nabla_eval;
                  } data;
                 ''')

        data_struct = ffi.new('data*')
        data_struct.id = -1  # to avoid freeing the data in the destructor
        data_struct.xk = ffi.cast('double *', xk.ctypes.data)
        data_struct.h = h
        data_struct.theta = theta
        data_struct.gamma = gamma
        data_struct.g = g
        data_struct.kappa = kappa

        vi = SN.VI(2)
        import siconos
        D = ffi.dlopen(siconos.__path__[0] + '/_numerics.so')
        D.set_cstruct(vi.get_env_as_long(), ffi.cast('void*', data_struct))
        vi.set_compute_F_and_nabla_F_as_C_functions('ZhuravlevIvanov.so',
                                                    'compute_F',
                                                    'compute_nabla_F')

        lambda_ = np.zeros((2, ))
        xkp1 = np.zeros((2, ))

        SO = SN.SolverOptions(vi, SN.SICONOS_VI_BOX_QI)
        lb = np.array((-1.0, -1.0))
        ub = np.array((1.0, 1.0))
        vi.set_box_constraints(lb, ub)

        N = int(T / h + 10)
        print(N)
        SO.dparam[0] = 1e-24
        SO.iparam[0] = 100
        SO.iparam[2] = 1
        SO.iparam[3] = 0
        SO.iparam[4] = 5

        signs = np.empty((N, 2))
        sol = np.empty((N, 2))
        sol[0, :] = xk

        k = 0
        #SN.numerics_set_verbose(3)

        while t <= T:
            k += 1
            info = SN.variationalInequality_box_newton_QiLSA(
                vi, lambda_, xkp1, SO)
            #print('iter {:} ; solver iter = {:} ; prec = {:}'.format(k, SO.iparam[1], SO.dparam[1]))
            if info > 0:
                print(lambda_)
                #            vi_function(2, signs[k-1, :], xkp1)
                lambda_[0] = -np.sign(xkp1[0])
                lambda_[1] = -np.sign(xkp1[1])
                if np.abs(xk[0]) < 1e-10:
                    lambda_[0] = 0.01
                if np.abs(xk[1]) < 1e-10:
                    lambda_[1] = 0.01
                    print('ok lambda')
                    print(lambda_)
                info = SN.variationalInequality_box_newton_QiLSA(
                    vi, lambda_, xkp1, SO)
                print('iter {:} ; solver iter = {:} ; prec = {:}'.format(
                    k, SO.iparam[1], SO.dparam[1]))
                if info > 0:
                    print('VI solver failed ! info = {:}'.format(info))
                    print(xk)
                    print(lambda_)
                    print(xkp1)
                    kaboom()
    #        else:
    #            print('iter {:} ; solver iter = {:} ; prec = {:}'.format(k, SO.iparam[1], SO.dparam[1]))

    #       vi_function(2, lambda_, xkp1)
            sol[k, 0:2] = xkp1
            np.copyto(xk, xkp1, casting='no')
            signs[k, 0:2] = lambda_
            t = k * h
示例#26
0
    data_struct.h = h
    data_struct.theta = theta
    data_struct.gamma = gamma
    data_struct.g = g
    data_struct.kappa = kappa

    vi = SN.VI(2)
    D = ffi.dlopen(SN._numerics.__file__)
    D.set_cstruct(vi.get_env_as_long(), ffi.cast('void*', data_struct))
    vi.set_compute_F_and_nabla_F_as_C_functions('ZhuravlevIvanovVI.so',
                                                'compute_F', 'compute_nabla_F')

    lambda_ = np.zeros((2, ))
    xkp1 = np.zeros((2, ))

    SO = SN.SolverOptions(vi, SN.SICONOS_VI_BOX_QI)
    lb = np.array((-1.0, -1.0))
    ub = np.array((1.0, 1.0))
    vi.set_box_constraints(lb, ub)

    N = int(T / h + 10)
    print(N)
    SO.dparam[0] = 1e-12
    SO.iparam[0] = 50
    SO.iparam[2] = 1
    SO.iparam[3] = 0
    SO.iparam[4] = 10

    signs = np.empty((N, 2))
    sol = np.empty((N, 2))
    sol[0, :] = xk
    xk = np.array((1., 10.))

    T = 4.0
    t = 0.0
    z = np.zeros((4, ))
    w = np.empty((4, ))

    kappa = 0.41
    g = 9.81
    theta = 1.0
    gamma = 1.0

    zi_syst = ZI(h, xk, theta, gamma, kappa, g)
    mcp = SN.MixedComplementarityProblem2(0, 4, zi_syst)

    SO = SN.SolverOptions(mcp, SN.SICONOS_MCP_NEWTON_FBLSA)
    SO.dparam[0] = 1e-24
    SO.iparam[0] = 150
    SO.iparam[3] = 2
    SO.iparam[4] = 10

    N = int(T / h + 10)
    print(N)
    lambdaPM = np.empty((N, 4))
    signs = np.empty((N, 2))
    sol = np.empty((N, 2))
    sol[0, :] = xk

    k = 0

    while t <= T:
示例#28
0
q[...] = [-1, 1, 3, -1, 1, 3, -1, 1, 3]

# Friction coeff
mu = [0.1, 0.1, 0.1]

fc3d = sn.FrictionContactProblem(3, nc, W, q, mu)


# Case 2 : use fclib-library, read hdf5 file




# --- Set solver options ---

# Check Friction_cst.h for a list of solvers ids.
solver_options = sn.SolverOptions(sn.SICONOS_FRICTION_3D_NSGS)#sn.SICONOS_FRICTION_3D_FPP)

eps = np.finfo(np.float64).eps
solver_options.dparam[0] = 100 * eps

# --- Set unknowns ---
velocity = np.zeros(3 * nc, dtype=np.float64)
reaction = np.zeros_like(velocity)

# --- Call solver driver with predefined options ---

sn.fc3d_driver(fc3d, reaction, velocity, solver_options)


示例#29
0
def test_lcp_enum():
    SO = N.SolverOptions(lcp, N.SICONOS_LCP_ENUM)
    info = N.lcp_enum(lcp, z, w, SO)
    assert (np.linalg.norm(z - zsol) <= ztol)
    assert not info
示例#30
0
def test_fc3dfischer():
    """Non-smooth Newton, Fischer-Burmeister.
    """
    SO = sn.SolverOptions(sn.SICONOS_FRICTION_3D_NSN_FB)
    solve(FCP, sn.fc3d_nonsmooth_Newton_FischerBurmeister, SO)