Exemple #1
0
	if nodes == 0:
		division = n
	else:
		division = math.floor(n/nodes)

	# This node's portion of the data
	x_test = x_test[(division*(ID-1)):(division*(ID)),:]

	y_test = y_test[(division*(ID-1)):(division*(ID))]

	return x_train, y_train, x_test, y_test


if __name__ == '__main__':

	m = Messager()
	m.registerCallbackSync()
	m.start()

	import pprint
	pp = pprint.PrettyPrinter()

	# pp.pprint(m.topo)

	while True:
	    requests.delete(SERVER_URL + '/classifier_stream/').text

		# if len(sys.argv) != 5: 
		# 	print("Usage: python svm.py <training_data_file> <test_data_file> <iterations> <target column>")
		# 	# sys.exit(1)
		# if len(sys.argv) == 1:
Exemple #2
0
import random
from clustermessaging.Messager import Messager
from clustermessaging.LED import LED

led = LED()
m = Messager()

m.registerCallbackSync()
m.start()


def setState(state):
    m.lifeState = state
    if state:
        led.setRedOn()
    else:
        led.setRedOff()


setState(random.randint(0, 1) == True)

i = 0
while True:
    i += 1
    for neighbor in m.getNeighbors().keys():
        message = {'state': m.lifeState, 'sync': i}

        m.sendMessage(neighbor, message)

    m.waitForMessageFromAllNeighbors(i)
Exemple #3
0
import random
from clustermessaging.Messager import Messager
from clustermessaging.LED import LED

led = LED()
m = Messager()

m.registerCallbackSync()
m.start()


def setState(state):
    m.lifeState = state
    if state:
        led.setRedOn()
    else:
        led.setRedOff()

setState(random.randint(0, 1) == True)

i = 0
while True:
    i += 1
    for neighbor in m.getNeighbors().keys():
        message = {
            'state': m.lifeState,
            'sync': i
        }

        m.sendMessage(neighbor, message)
Exemple #4
0
# Demo of Messager class.
# run with: DEVICE_ID=1 python sample.py
import os
import time
from clustermessaging.Messager import Messager

m = Messager()

def callback(message, name):
    print('Message Received from %s! %s' % (name, message))

if os.environ['DEVICE_ID'] == '2':
    print('registering callback')
    m.registerCallback(callback)
else:
    print('sending message')
    m.sendMessage('2', 'hello, 2, i am %s' % m.getOwnName())

m.start()
        return (learning_rate * 1.05), new_w
    elif current_error >= last_error:
        return (learning_rate * .5), old_w


if __name__ == '__main__':

    # Parse command line options
    pp = pprint.PrettyPrinter()

    datafile = "data/iris_mod.csv"
    target_column = 4
    iterations = 100

    # Initialize lock and Messager objects
    m = Messager()
    m.registerCallbackSync()
    m.start()
    nodes = len(m.getNeighbors()) + 1

    ID = int(os.environ["DEVICE_ID"])
    w, X, y, num_samples = read_data(datafile, target_column, ID, nodes)
    learning_rate = .5

    import csv
    test = open("test_val/test_{0}.csv".format(ID), "w")
    writer = csv.writer(test)

    for i in range(iterations):
        new_w = w - (learning_rate) * rss_gradient(w, X, y)
        learning_rate, w = update_learning_rate(learning_rate, w, new_w, X, y)
# just sends a value to neighbors three times, to demonstrate synchronized steps
import numpy as np
from clustermessaging.Messager import Messager

m = Messager()

m.mynum = float(np.random.randint(100))
print('My num: %d' % m.mynum)

m.registerCallbackSync()
m.start()

for iter in range(20):
    print('iter %d' % iter)
    for neighbor in m.getNeighbors().keys():
        message = {'num': m.mynum, 'sync': iter}

        print('sending')
        m.sendMessage(neighbor, message)
        print('sent to %s' % neighbor)

    print('Waiting on all neighbors...')
    m.waitForMessageFromAllNeighbors(iter)

    print('Printing all messages')
    for message in m.sync[iter]:
        print(message)

print('done')
	return w

if __name__ == "__main__":

	# Parse command line options
	# usage_string = 'Usage: python avg_sync.py <assignment file> <iterations>'

	# if len(sys.argv) != 4:
	# 	print(usage_string)
	# 	sys.exit(1)
	# else:
	# 	assignment = json.load(open(sys.argv[1]))
	# 	iterations = sys.argv[2]

	# Initialize lock and Messager objects
	m = Messager()
	m.registerCallbackSync()
	m.start()

	# Get necessary values
	ID = int(os.environ["DEVICE_ID"])
	my_val = assignment[str(ID)]
	w = get_weights(m.topo,ID)

	# Create x vector
	nodes = len(assignment)
	x = np.zeros(nodes,dtype=float)
	x[ID-1] = my_val # Insert own value

	iterations = 10
	for i in range(iterations):
# Async averaging (gossip)
import time
import numpy as np
from threading import Lock
import random
from clustermessaging.Messager import Messager

m = Messager()
lock = Lock()

m.mynum = float(np.random.randint(100))
print('My num: %d' % m.mynum)

def callback(message, name):
    print('Got message from %s: %s' % (name, message))
    lock.acquire()

    if message['rsvp']:
        print('Replying...')
        reply = {
            'num': m.mynum,
            'rsvp': False
        }
        m.sendMessage(name, reply)

    print('old avg: %f' % m.mynum)
    m.mynum = (m.mynum + float(message['num'])) / 2
    print('new avg: %f' % m.mynum)

    lock.release()
# just sends a value to neighbors three times, to demonstrate synchronized steps
import numpy as np
from clustermessaging.Messager import Messager

m = Messager()

m.mynum = float(np.random.randint(100))
print("My num: %d" % m.mynum)

m.registerCallbackSync()
m.start()

for iter in range(20):
    print("iter %d" % iter)
    for neighbor in m.getNeighbors().keys():
        message = {"num": m.mynum, "sync": iter}

        print("sending")
        m.sendMessage(neighbor, message)
        print("sent to %s" % neighbor)

    print("Waiting on all neighbors...")
    m.waitForMessageFromAllNeighbors(iter)

    print("Printing all messages")
    for message in m.sync[iter]:
        print(message)

print("done")
Exemple #10
0
# Greedy Geo-Routing (async)
import math
import time
import random
from clustermessaging.LED import LED
from clustermessaging.Messager import Messager
import kazoo.recipe.watchers
from kazoo.protocol.states import EventType

led = LED()
m = Messager()


def forwardMessage(path=None):
    if not path:
        path = []

    def distanceToTarget(coords):
        target = m.getTarget()
        return math.sqrt((target[0] - coords[0])**2 +
                         (target[1] - coords[1])**2)

    distances = [(distanceToTarget(m.getLocation(id)), id)
                 for id in m.getNeighbors().keys()]
    distances.append((distanceToTarget(m.getOwnLocation()), m.getOwnName()))
    mindist = min(distances)[0]

    closest_neighbor = random.choice(
        [neighbor for (dist, neighbor) in distances if dist == mindist])

    path.append(m.getOwnName())
Exemple #11
0
# Demo of Messager class.
# run with: DEVICE_ID=1 python sample.py
import os
import time
from clustermessaging.Messager import Messager

m = Messager()


def callback(message, name):
    print('Message Received from %s! %s' % (name, message))


if os.environ['DEVICE_ID'] == '2':
    print('registering callback')
    m.registerCallback(callback)
else:
    print('sending message')
    m.sendMessage('2', 'hello, 2, i am %s' % m.getOwnName())

m.start()
Exemple #12
0
# Async averaging (gossip)
import time
import numpy as np
from threading import Lock
import random
from clustermessaging.Messager import Messager

m = Messager()
lock = Lock()

m.mynum = float(np.random.randint(100))
print('My num: %d' % m.mynum)


def callback(message, name):
    print('Got message from %s: %s' % (name, message))
    lock.acquire()

    if message['rsvp']:
        print('Replying...')
        reply = {'num': m.mynum, 'rsvp': False}
        m.sendMessage(name, reply)

    print('old avg: %f' % m.mynum)
    m.mynum = (m.mynum + float(message['num'])) / 2
    print('new avg: %f' % m.mynum)

    lock.release()


m.registerCallback(callback)