def proj_noncongested(points,
                      domain,
                      center=None,
                      mass=None,
                      radial_func=RadialFuncInBall(),
                      verbose=None):
    nb_points = len(points)
    assert (nb_points != 0)
    if mass is None:
        mass = np.ones(nb_points) / nb_points
    laguerre = OptimalTransport(positions=points,
                                weights=None,
                                masses=mass,
                                domain=domain,
                                radial_func=radial_func,
                                linear_solver="CuPyx")

    if not center is None:
        initialize_weights(power_diagram=laguerre.pd,
                           center=center,
                           verbose=verbose)

    laguerre.adjust_weights(relax=1)

    if np.linalg.norm(laguerre.pd.integrals() - mass) > 1e-5:
        print("The Newton algorithm did not converge!")
        laguerre.display_vtk("debug_file/bad_Newton.vtk", points=True)
        laguerre.get_domain().display_boundaries_vtk(
            "debug_file/bad_Newton_domain.vtk")
        np.save("debug_file/bad_positions", laguerre.get_positions())
        np.save("debug_file/integrals", laguerre.pd.integrals())
        np.save("debug_file/bad_weights", laguerre.get_weights())
        assert (False)
    return laguerre.pd
Exemplo n.º 2
0
def make_case(name, nb_diracs, dim):
    # default domain
    if name == "random":
        positions = np.random.rand(nb_diracs, dim)
    elif name == "grid":
        positions = make_grid(nb_diracs, dim)
    elif name == "grid_with_rand":
        positions = make_grid(nb_diracs, dim, rand_val=1)
    elif name == "faces":
        # voronoi with 100 points
        pd = PowerDiagram(np.random.rand(5, dim))

        # quantization
        lot = OptimalTransport(positions=make_grid(nb_diracs, dim))
        lot.obj_max_dw = 1e-5
        lot.verbosity = 1
        for ratio in [1 - 0.85**n for n in range(50)]:
            # density
            img_size = 1000
            img_points = []
            items = [range(img_size) for i in range(dim)]
            for i in itertools.product(*items):
                img_points.append(i)
            img = pd.distances_from_boundaries(
                np.array(img_points) / img_size).reshape((img_size, img_size))
            img = (1 - ratio) + ratio * np.exp(-(100 * img)**2)
            lot.set_domain(ScaledImage([0, 0], [1, 1], img / np.mean(img)))

            # opt
            for _ in range(10):
                lot.adjust_weights()
                B = lot.get_centroids()
                lot.set_positions(lot.get_positions() + 0.3 *
                                  (B - lot.get_positions()))

        positions = lot.get_positions()
        plt.plot(positions[:, 0], positions[:, 1], ".")
        plt.show()

    np.save("/data/{}_n{}_d{}_voro.npy".format(name, nb_diracs, dim),
            (positions[:, 0], positions[:, 1]))

    # solve
    if nb_diracs < 32000000:
        ot = OptimalTransport(positions)
        # ot.verbosity = 1

        # solve
        ot.adjust_weights()

        # display
        # ot.display_vtk( "results/pd.vtk" )
        np.save("/data/{}_n{}_d{}.npy".format(name, nb_diracs, dim),
                (positions[:, 0], positions[:, 1], ot.get_weights()))
Exemplo n.º 3
0
        s = 0.5 * target_radius
        g = GradGrid( domain, [ [ limx - 2 * s, 0.5 ] ], s )

        # iterations
        color_values = positions[ :, 1 ]
        color_values = ( color_values - np.min( color_values ) ) / np.ptp( color_values )

        ot = OptimalTransport(domain, RadialFuncInBall())
        ot.set_weights( np.ones( positions.shape[ 0 ] ) * target_radius ** 2 )
        ot.set_masses( np.ones( positions.shape[ 0 ] ) * np.pi * target_radius ** 2 )

        nb_timesteps = int( 20 / target_radius )
        for i in range( nb_timesteps ):
            # change positions
            weights = ot.get_weights()
            for n in range( positions.shape[ 0 ] ):
                c = 1 / ( 10 * weights[ n ] / 7e-4 + 1 )
                positions[ n, : ] += c * 0.5 * target_radius * g.grad( positions[ n, : ] )

            # optimal weights
            ot.set_positions( positions )
            ot.adjust_weights()

            # display
            d = 5
            if i % d == 0:
                ot.display_vtk( directory + "/pd_{:03}.vtk".format( int( i / d ) ) )
        
            # update positions
            positions = ot.get_centroids()
Exemplo n.º 4
0
from pysdot.domain_types import ConvexPolyhedraAssembly
from pysdot.radial_funcs import RadialFuncInBall
from pysdot import OptimalTransport
import numpy as np

domain = ConvexPolyhedraAssembly()
domain.add_box([0, 0], [1, 10])

R = 0.5

ot = OptimalTransport(domain, RadialFuncInBall())
ot.set_masses(np.array([np.pi * R**2 / 2]))
ot.set_weights(np.array([R**2]))

cpt = 0
for y in np.linspace(0, -20, 100):
    ot.set_positions(np.array([[0.5, y]]))
    ot.adjust_weights()

    r = ot.get_weights()[0]**0.5
    a = np.arcsin(R / r)

    e = 0.5 * r * (np.pi / 2 * np.sin(a) - a / np.sin(a) + np.cos(a))
    f = 0.5 * (np.cos(a) + a / np.sin(a))
    yp = y + r * f

    print(y, yp)

    ot.display_vtk("lc_{}.vtk".format(cpt))
    cpt += 1
Exemplo n.º 5
0
from pysdot import OptimalTransport
from matplotlib import pyplot
import numpy as np

positions = []
ss = 1e-3
for x in np.linspace(0, 1 - ss, 20):
    positions.append([x, 0.5])
    positions.append([x + ss, 0.5])

ot = OptimalTransport(np.array(positions))
ot.verbosity = 1
ot.adjust_weights()

pyplot.plot(ot.get_positions()[:, 0], ot.get_weights(), '+')
pyplot.show()
Exemplo n.º 6
0
for dist_name in ["uniform"]:  # , "voro_50"
    for dim in [3]:
        for nb_diracs in map(
                int, [1e5 / 8, 1e5 / 4, 1e5 / 2]):  # 1e5, 2e5, 4e5, 8e5, 16e5
            positions = make_positions(dist_name, nb_diracs, dim)

            # diracs
            ot = OptimalTransport(positions=positions)
            ot.verbosity = 1

            # solve
            print(dim, nb_diracs)
            ot.adjust_weights()

            # display, save
            if nb_diracs <= 1e6:
                ot.display_vtk("vtk/pd.vtk", points=True)

            pw = np.zeros([dim + 1, nb_diracs])
            for d in range(dim):
                pw[d, :] = positions[:, d]
            np.save(
                "/data/sdot/{}_{}_{}D_equalw.npy".format(
                    dist_name, nb_diracs, dim), pw)

            pw[dim, :] = ot.get_weights()
            np.save(
                "/data/sdot/{}_{}_{}D_solved.npy".format(
                    dist_name, nb_diracs, dim), pw)
Exemplo n.º 7
0
        # optimal weights
        ot.set_positions(positions)
        ot.adjust_weights()

        # display
        d = 2 * na
        if i % d == 0:
            ot.pd.display_asy(directory + "/pd_{:03}.asy".format(int(i / d)),
                              values=color_values,
                              linewidth=0.005,
                              dotwidth=target_radius * 0,
                              closing=domain_asy,
                              avoid_bounds=True)
            ot.display_vtk(directory + "/pd_{:03}.vtk".format(int(i / d)))
            h_positions.append(positions)
            h_weights.append(ot.get_weights())

        # update positions
        positions = ot.get_centroids()

        for n in range(positions.shape[0]):
            if timeout[n] == 0 and positions[n, 0] > 4:
                timeout[n] = i + 1

    # output with timeout information
    timeout = (timeout - np.min(timeout)) / np.ptp(timeout)
    for i in range(len(h_weights)):
        ot.set_positions(h_positions[i])
        ot.set_weights(h_weights[i])
        ot.pd.display_asy(directory + "/pd_timeout_{:03}.asy".format(i),
                          "in_ball(weight**0.5)",