Esempio n. 1
0
def slave():
    if not linda.connect():
        print "Please start the Linda server first."
        return

    processed, waiting = 0, 1

    while True:
        tup = linda.uts._in((int,) + (int,) * (9 * 9))
        empty, grid = tup[0], tupleToGrid(tup[1:])

        print processed, waiting, empty,
        processed += 1
        waiting -= 1

        if empty == 0:
            linda.uts._out(tup)
        else:
            i = 0
            try:
                x, y = getBlankSquare(grid)
            except TypeError:
                pass
            else:
                for val in getValid(grid, x, y):
                    print (x, y, val),
                    grid[x][y] = val
                    linda.uts._out((empty - 1,) + gridToTuple(grid))
                    i += 1
                print
                waiting += i
Esempio n. 2
0
def slave():
    if not linda.connect():
        print "Please start the Linda server first."
        return

    processed, waiting = 0, 1

    while True:
        tup = linda.uts._in((int, ) + (int, ) * (9 * 9))
        empty, grid = tup[0], tupleToGrid(tup[1:])

        print processed, waiting, empty,
        processed += 1
        waiting -= 1

        if empty == 0:
            linda.uts._out(tup)
        else:
            i = 0
            try:
                x, y = getBlankSquare(grid)
            except TypeError:
                pass
            else:
                for val in getValid(grid, x, y):
                    print(x, y, val),
                    grid[x][y] = val
                    linda.uts._out((empty - 1, ) + gridToTuple(grid))
                    i += 1
                print
                waiting += i
Esempio n. 3
0
    def run(self):
        linda.connect()

        # Returns the person we'd rather marry
        def BestOf(fiance, suitor):
            if fiance is None:
                return suitor
            elif order.index(fiance) < order.index(suitor):
                return fiance
            else:
                return suitor

        # Returns the person we'd rather not marry
        def WorstOf(fiance, suitor):
            if BestOf(fiance, suitor) == fiance:
                return suitor
            else:
                return fiance

        # Get the tuplespace we're working in
        ts = linda.uts._in(("ts", linda.TupleSpace))[1]

        # Randomize the order we want to marry people in
        order = men[:]
        random.shuffle(order)

        fiance = None
        while 1:
            # wait to be proposed to. If this returns None then a deadlock was reached
            p = ts._inp(("propose", str, self.name))
            if p is None:
                break
            else:
                suitor = p[1]

            # Choose who to reject and who to keep
            reject = WorstOf(fiance, suitor)
            fiance = BestOf(fiance, suitor)

            if reject is not None:
                print "%s rejecting %s for %s" % (self.name, reject, fiance)
                ts._out(("reject", reject, self.name))
            else:
                print "%s accepting %s" % (self.name, fiance)

        print "%s ended with %s (%i)" % (self.name, fiance,
                                         order.index(fiance) + 1)
Esempio n. 4
0
    def run(self):
        linda.connect()

        # Returns the person we'd rather marry
        def BestOf(fiance, suitor):
            if fiance is None:
                return suitor
            elif order.index(fiance) < order.index(suitor):
                return fiance
            else:
                return suitor
        # Returns the person we'd rather not marry
        def WorstOf(fiance, suitor):
            if BestOf(fiance, suitor) == fiance:
                return suitor
            else:
                return fiance

        # Get the tuplespace we're working in
        ts = linda.uts._in(("ts", linda.TupleSpace))[1]

        # Randomize the order we want to marry people in
        order = men[:]
        random.shuffle(order)

        fiance = None
        while 1:
            # wait to be proposed to. If this returns None then a deadlock was reached
            p = ts._inp(("propose", str, self.name))
            if p is None:
                break
            else:
                suitor = p[1]

            # Choose who to reject and who to keep
            reject = WorstOf(fiance, suitor)
            fiance = BestOf(fiance, suitor)

            if reject is not None:
                print "%s rejecting %s for %s" % (self.name, reject, fiance)
                ts._out(("reject", reject, self.name))
            else:
                print "%s accepting %s" % (self.name, fiance)

        print "%s ended with %s (%i)" % (self.name, fiance, order.index(fiance) + 1)
Esempio n. 5
0
    def run(self):
        linda.connect()

        # Get the tuplespace we're working in
        ts = linda.uts._in(("ts", linda.TupleSpace))[1]

        # Randomize the order we want to propose to people in
        order = women[:]
        random.shuffle(order)

        # propose to each woman in order
        for w in order:
            print self.name + " proposing to " + w
            ts._out(("propose", self.name, w))

            # wait to see if we get rejected. If a deadlock is reached we've finished and should exit
            r = ts._inp(("reject", self.name, w))
            if r is None:
                break

        print "%s ended with %s (%i)" % (self.name, w, order.index(w) + 1)
Esempio n. 6
0
    def run(self):
        linda.connect()

        # Get the tuplespace we're working in
        ts = linda.uts._in(("ts", linda.TupleSpace))[1]

        # Randomize the order we want to propose to people in
        order = women[:]
        random.shuffle(order)

        # propose to each woman in order
        for w in order:
            print self.name + " proposing to " + w
            ts._out(("propose", self.name, w))

            # wait to see if we get rejected. If a deadlock is reached we've finished and should exit
            r = ts._inp(("reject", self.name, w))
            if r is None:
                break

        print "%s ended with %s (%i)" % (self.name, w, order.index(w) + 1)
Esempio n. 7
0
def master():
    grid = loadFile(sys.argv[1])

    print "Solving..."
    print gridString(grid)

    empty = sum([len([x for x in row if x is None]) for row in grid])

    if not linda.connect():
        print "Please start the Linda server first."
        return

    start = time.time()
    linda.uts._out((empty, ) + gridToTup(grid))

    tup = linda.uts._in((0, ) + (int, ) * (9*9))

    print "Solved Grid in %f seconds." % (time.time() - start)
    print gridString(tupToGrid(tup[1:]))
Esempio n. 8
0
def master():
    grid = loadFile(sys.argv[1])

    print "Solving..."
    print gridString(grid)

    empty = sum([len([x for x in row if x is None]) for row in grid])

    if not linda.connect():
        print "Please start the Linda server first."
        return

    start = time.time()
    linda.uts._out((empty, ) + gridToTup(grid))

    tup = linda.uts._in((0, ) + (int, ) * (9 * 9))

    print "Solved Grid in %f seconds." % (time.time() - start)
    print gridString(tupToGrid(tup[1:]))
Esempio n. 9
0
#!/usr/bin/python

import linda

linda.connect()

t = linda.Type("a :: int;")
v = linda.Value(1, t)

linda.uts._out((v, ))

print linda.uts._in((t,))
Esempio n. 10
0
#!/usr/bin/python

import sys
import time

import linda

if not linda.connect():
    print "Please start the Linda server first."
    sys.exit(1)

class Grid:
    def __init__(self):
        self.grid = tuple([tuple([None for _ in range(9)]) for _ in range(9)])

def gridToValue(grid, memo):
    rows = []
    for row in grid.grid:
        cols = []
        for col in row:
            if col is None:
                v = linda.Nil()
                v.sum_pos = 1
            else:
                v = linda.Value(col)
                v.sum_pos = 0
            cols.append(v)
        rows.append(tuple(cols))
    return linda.Value(tuple(rows))

def valueToGrid(val, memo):
Esempio n. 11
0
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation; either version 2.1 of the License, or
#    (at your option) any later version.
#
#    PyLinda is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with PyLinda; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import linda

linda.connect()

import random
import time

number = 5


def Philosopher(i):
    while 1:
        think(i)
        if random.random() < 0.3:  # have they eaten enough?
            break
        ts._in(("room ticket", ))
        ts._in(("chopstick", i))
        ts._in(("chopstick", (i + 1) % number))
Esempio n. 12
0
#
# Process command line options
#
from optparse import OptionParser

parser = OptionParser(version="%prog 1.0")
parser.add_option("-p",
                  "--connect-port",
                  type="int",
                  dest="connectport",
                  default=2102,
                  help="The port to connect to.")

(options, args) = parser.parse_args()

linda.connect(options.connectport)
#
#
#

# Lists of men's and women's names
men = [
    "AIDAN", "JADEN", "CADEN", "ETHAN", "CALEB", "DYLAN", "JACOB", "JORDAN",
    "LOGAN", "HAYDEN", "CONNOR", "RYAN", "MORGAN", "CAMERON", "ANDREW",
    "JOSHUA", "NOAH", "MATTHEW", "ADDISON", "ASHTON"
]
women = [
    "MADISON", "EMMA", "ABIGAIL", "RILEY", "CHLOE", "HANNAH", "ALEXIS",
    "ISABELLA", "MACKENZIE", "TAYLOR", "OLIVIA", "HAILEY", "PAIGE", "EMILY",
    "GRACE", "AVA", "AALIYAH", "ALYSSA", "FAITH", "BRIANNA"
]
Esempio n. 13
0
import linda
import random
import threading

#
# Process command line options
#
from optparse import OptionParser

parser = OptionParser(version="%prog 1.0")
parser.add_option("-p", "--connect-port", type="int", dest="connectport", default=2102,
                  help="The port to connect to.")

(options, args) = parser.parse_args()

linda.connect(options.connectport)
#
#
#

# Lists of men's and women's names
men = ["AIDAN", "JADEN", "CADEN", "ETHAN", "CALEB", "DYLAN", "JACOB", "JORDAN", "LOGAN", "HAYDEN", "CONNOR", "RYAN", "MORGAN", "CAMERON", "ANDREW", "JOSHUA", "NOAH", "MATTHEW", "ADDISON", "ASHTON"]
women = ["MADISON", "EMMA", "ABIGAIL", "RILEY", "CHLOE", "HANNAH", "ALEXIS", "ISABELLA", "MACKENZIE", "TAYLOR", "OLIVIA", "HAILEY", "PAIGE", "EMILY", "GRACE", "AVA", "AALIYAH", "ALYSSA", "FAITH", "BRIANNA"]

ts = None

# Function that represents a Man
class Man(threading.Thread):
    def __init__(self, name):
        threading.Thread.__init__(self)
        self.name = name