コード例 #1
0
    def learn(self, n_epoch=10000, sigma=(0.25, 0.01), lrate=(0.5, 0.01)):

        t = np.linspace(0, 1, n_epoch)
        lrate = lrate[0] * (lrate[1] / lrate[0])**t
        sigma = sigma[0] * (sigma[1] / sigma[0])**t
        I = np.random.randint(0, len(self.samples), n_epoch)
        self.samples = self.samples[I]
        pts = Points(self.samples, r=2)
        doc = Text(__doc__)

        pb = ProgressBar(0, n_epoch)
        for i in pb.range():
            pb.print("epochs")
            # Get random sample
            data = self.samples[i]

            # Get index of nearest node (minimum distance)
            winner = np.argmin(((self.codebook - data)**2).sum(axis=-1))

            # Gaussian centered on winner
            G = np.exp(-self.distance[winner]**2 / sigma[i]**2)

            # Move nodes towards sample according to Gaussian
            self.codebook -= lrate[i] * G[...,
                                          np.newaxis] * (self.codebook - data)

            # Draw network
            if i > 500 and not i % 20 or i == n_epoch - 1:
                x, y, z = [self.codebook[:, i].reshape(n, n) for i in range(3)]
                grd = Grid(resx=n - 1,
                           resy=n - 1).wire(False).lw(0.5).bc('lightblue')
                for i in range(n):
                    for j in range(n):
                        grd.setPoint(i * n + j, (x[i, j], y[i, j], z[i, j]))
                show(doc,
                     pts,
                     grd,
                     axes=6,
                     bg='w',
                     azimuth=2,
                     interactive=False)

        return [self.codebook[:, i].reshape(n, n) for i in range(3)]
コード例 #2
0
def demo3d_hanoi(**kwargs):
    nr_disks = kwargs.get("nr_disks", 5)
    interactive = kwargs.get("interactive", 1)

    hanoi = Hanoi(nr_disks)
    tower_states = list([hanoi.towers])
    for _ in hanoi.moves():
        tower_states.append(hanoi.towers)

    vp = Plotter(axes=0, interactive=0, bg="w", size=(800, 600))
    vp.camera.SetPosition([18.5, -20.7, 7.93])
    vp.camera.SetFocalPoint([3.0, 0.0, 2.5])
    vp.camera.SetViewUp([-0.1, +0.17, 0.977])

    cols = makePalette("red", "blue", hanoi.nr_disks + 1, hsv=True)
    disks = {
        hanoi.nr_disks - i: Cylinder(pos=[0, 0, 0],
                                     r=0.2 * (hanoi.nr_disks - i + 1),
                                     c=cols[i])
        for i in range(hanoi.nr_disks)
    }
    for k in disks:
        vp += disks[k]
    vp += Box(pos=(3.0, 0, -.5), length=12.0, width=4.0, height=0.1)
    vp.show(zoom=1.2)

    printc("\n Press q to continue, Esc to exit. ", c="y", invert=1)
    pb = ProgressBar(0, len(tower_states), 1, c="b", ETA=False)
    for t in pb.range():
        pb.print()
        state = tower_states[t]
        for tower_nr in range(3):
            for i, disk in enumerate(state[tower_nr]):
                disks[disk].pos([3 * tower_nr, 0, i + 0.5])
        vp.show(resetcam=0, interactive=interactive, rate=10)
    vp.show(resetcam=0, interactive=1)
コード例 #3
0
ファイル: gas.py プロジェクト: dongbo-BUAA-VR/vtkplotter
        pcmj = p[j]-ptot*mj/mtot
        rrel = norm(pos[j]-pos[i])
        pcmi = pcmi-2*np.dot(pcmi,rrel)*rrel # bounce in cm frame
        pcmj = pcmj-2*np.dot(pcmj,rrel)*rrel
        p[i] = pcmi+ptot*mi/mtot             # transform momenta back to lab frame
        p[j] = pcmj+ptot*mj/mtot
        pos[i] = pos[i]+(p[i]/mi)*deltat     # move forward deltat in time
        pos[j] = pos[j]+(p[j]/mj)*deltat

    # Bounce off the boundary of the torus
    for j in range(Natoms):
        poscircle[j] = norm(pos[j])*RingRadius*[1,1,0]
    outside = np.greater_equal(mag(poscircle-pos),RingThickness-2*Ratom)

    for k in range(len(outside)):
        if outside[k]==1 and np.dot(p[k], pos[k]-poscircle[k])>0:
            p[k] = reflection(p[k],pos[k]-poscircle[k])
            
    # then update positions of display objects
    for i in range(Natoms): Atoms[i].pos(pos[i])                           ### <--
    outside = np.greater_equal(mag(pos), RingRadius+RingThickness)

    vp.render(resetcam=1)                                                  ### <--
    vp.camera.Azimuth(.5)
    vp.camera.Elevation(.1)
    pb.print()

vp.show()


コード例 #4
0
ファイル: spring.py プロジェクト: yordankyosev/vtkplotter
#initial conditions
v = vector(0, 0, 0.2)
x = vector(x0, 0, 0)
xr = vector(l_rest, 0, 0)
sx0 = vector(-0.8, 0, 0)
offx = vector(0, 0.3, 0)

vp.box(pos=(0, -0.1, 0), length=2.0, width=0.02, height=0.5)  #surface
vp.box(pos=(-.82, .15, 0), length=.04, width=0.50, height=0.3)  #wall
block = vp.cube(pos=x, length=0.2, c='t')
spring = vp.helix(sx0, x, r=.06, thickness=.01, texture='metal1')

pb = ProgressBar(0, 500, c='r')
for i in pb.range():
    F = -k * (x - xr) - b * v  # Force and friction
    a = F / m  # acceleration
    v = v + a * dt  # velocity
    x = x + v * dt + 1 / 2 * a * dt**2  # position

    block.pos(x)  # update block position
    spring.stretch(sx0, x)  # stretch helix accordingly
    trace = vp.point(x + offx, c='r/0.5', r=3)  # leave a red trace

    vp.camera.Azimuth(.1)
    vp.camera.Elevation(.1)
    vp.render(trace)  # add trace to the list of actors and render
    pb.print('Fx=' + str(F[0]))

vp.show(interactive=1)
コード例 #5
0
    ynew = y + vnew * dt + 1 / 2 * accel(y, vnew, t) * dt**2
    return ynew, vnew


positions_eu, positions_rk = [], []
y_eu, y_rk = np.array(y), np.array(y)
v_eu, v_rk = np.array(v), np.array(v)
t = 0
pb = ProgressBar(0, Nsteps, c="blue", ETA=0)
for i in pb.range():
    y_eu, v_eu = euler(y_eu, v_eu, t, dt)
    y_rk, v_rk = rk4(y_rk, v_rk, t, dt)
    t += dt
    positions_eu.append(y_eu)  # store result of integration
    positions_rk.append(y_rk)
    pb.print("Integrate: RK-4 and Euler")

####################################################
# Visualize the result
####################################################
vp = Plotter(interactive=0, axes=2)  # choose axes type nr.2
vp.ytitle = "u(x,t)"
vp.ztitle = ""  # will not draw z axis

for i in x:
    vp += Point([i, 0, 0], c="green", r=6)
pts_actors_eu = vp.actors  # save a copy of the actors list
pts_actors_eu[0].legend = "Euler method"

vp.actors = []  # clean up the list
コード例 #6
0
        R12 = Pos[s2]-Pos[s1]
        nR12 = np.linalg.norm(R12)
        d12 = Radius[s1]+Radius[s2] - nR12
        tau = R12/nR12
        DR0 = d12*tau
        x1 = Mass[s1]/(Mass[s1]+Mass[s2])
        x2 = 1-x1                 # x2 = Mass[s2]/(Mass[s1]+Mass[s2])
        Pos[s1] -= x2*DR0
        Pos[s2] += x1*DR0
        DV0 = 2*dot(Vel[s2]-Vel[s1], tau)*tau
        Vel[s1] +=  x2*DV0
        Vel[s2] -=  x1*DV0

    # Update the location of the spheres
    for s in range(Nsp): Spheres[s].pos([Pos[s][0],Pos[s][1],0])
        
    if not int(i)%10:                   # every ten steps:
        rsp = [Pos[0][0],Pos[0][1],0]
        rsv = [Vel[0][0],Vel[0][1],0]
        vp.add(Point(rsp, c='r', r=5, alpha=0.1))  # leave a point trace
        vp.show()                    # render scene   
    pb.print('#actors='+str(len(vp.actors)))
    
vp.show(interactive=1)






コード例 #7
0
ファイル: cell_main.py プロジェクト: zlinzju/vtkplotter
    for colony in colonies:

        newcells = []
        for cell in colony.cells:

            if cell.dieAt(t):
                continue
            if cell.divideAt(t):
                newc = cell.split()  # make daughter cell
                vp += Line(cell.pos, newc.pos, c="k", lw=3, alpha=0.5)
                newcells.append(newc)
            newcells.append(cell)
        colony.cells = newcells

        pts = [c.pos for c in newcells]  # draw all points at once
        vp += Points(pts, c=colony.color, r=5, alpha=0.80)   # nucleus
        vp += Points(pts, c=colony.color, r=15, alpha=0.05)  # halo
        msg += str(len(colony.cells)) + ","

    pb.print(msg + str(int(t)))
    vp.show(resetcam=0)

# draw the oriented ellipsoid that contains 50% of the cells
for colony in colonies:
    pts = [c.pos for c in colony.cells]
    a = pcaEllipsoid(pts, pvalue=0.5, pcaAxes=0)
    a.color(colony.color).alpha(0.3)
    a.legend("1/rate=" + str(colony.cells[0].tdiv) + "h")
    vp += a
vp.show(resetcam=0, interactive=1)
コード例 #8
0
x = vector(x0, 0, 0)
xr = vector(L, 0, 0)
sx0 = vector(-0.8, 0, 0)
offx= vector(0, 0.3, 0)

vp.add(box(pos=(0, -0.1, 0), length=2.0, width=0.02, height=0.5)) #surface
vp.add(box(pos=(-.82,.15,0), length=.04, width=0.50, height=0.3)) #wall
block = cube(pos=x, length=0.2, c='tomato')
block.addTrail(offset=[0,0.2,0], alpha=0.6, lw=2, n=500) 
spring = helix(sx0, x, r=.06, thickness=.01, texture='metal1')
vp.add([block, spring])

pb = ProgressBar(0,300, c='r')
for i in pb.range(): 
    F = -k*(x-xr) - b*v             # Force and friction
    a = F/m                         # acceleration
    v = v + a * dt                  # velocity
    x = x + v*dt + 1/2 * a * dt**2  # position
    
    block.pos(x)                    # update block position and trail
    spring.stretch(sx0, x)          # stretch helix accordingly
    
    vp.camera.Azimuth(0.1)
    vp.camera.Elevation(0.1)
    vp.render() 
    pb.print('Fx='+str(round(F[0], 1))) # print Fx component

vp.show(interactive=1)
 
    
コード例 #9
0
    ynew = y + vnew * dt + 1 / 2 * accel(y, vnew, t) * dt**2
    return ynew, vnew


positions_eu, positions_rk = [], []
y_eu, y_rk = np.array(y), np.array(y)
v_eu, v_rk = np.array(v), np.array(v)
t = 0
pb = ProgressBar(0, Nsteps, c='blue', ETA=0)
for i in pb.range():
    y_eu, v_eu = euler(y_eu, v_eu, t, dt)
    y_rk, v_rk = rk4(y_rk, v_rk, t, dt)
    t += dt
    positions_eu.append(y_eu)  # store result of integration
    positions_rk.append(y_rk)
    pb.print('Integrate: RK-4 and Euler')

####################################################
# Visualize the result
####################################################
vp = Plotter(verbose=0, axes=2)  # choose axes type nr.2
vp.ytitle = 'u(x,t)'
vp.ztitle = ''  # will not draw z axis

for i in x:
    vp.point([i, 0, 0], c='green', r=6)
pts_actors_eu = vp.actors  # save a copy of the actors list
pts_actors_eu[0].legend = 'Euler method'

vp.actors = []  # clean up the list
コード例 #10
0
ファイル: brownian2D.py プロジェクト: zlinzju/vtkplotter
    # Check to see if the spheres are colliding
    for ij in hitlist:
        s1, s2 = divmod(ij, Nsp)  # decode the spheres pair (s1,s2) colliding
        hitlist.remove(s2 * Nsp + s1)  # remove symmetric (s2,s1) pair from list
        R12 = Pos[s2] - Pos[s1]
        nR12 = np.linalg.norm(R12)
        d12 = Radius[s1] + Radius[s2] - nR12
        tau = R12 / nR12
        DR0 = d12 * tau
        x1 = Mass[s1] / (Mass[s1] + Mass[s2])
        x2 = 1 - x1  # x2 = Mass[s2]/(Mass[s1]+Mass[s2])
        Pos[s1] -= x2 * DR0
        Pos[s2] += x1 * DR0
        DV0 = 2 * dot(Vel[s2] - Vel[s1], tau) * tau
        Vel[s1] += x2 * DV0
        Vel[s2] -= x1 * DV0

    # Update the location of the spheres
    for s in range(Nsp):
        Spheres[s].pos([Pos[s][0], Pos[s][1], 0])

    if not int(i) % 10:  # every ten steps:
        rsp = [Pos[0][0], Pos[0][1], 0]
        rsv = [Vel[0][0], Vel[0][1], 0]
        vp += Point(rsp, c="r", r=5, alpha=0.1)  # leave a point trace
        vp.show()  # render scene
    pb.print("#actors=" + str(len(vp.actors)))

vp.show(interactive=1)