Пример #1
0
def bcbp_secondpass(groups):

    print len(groups)
    while len(groups) > 0:
        
        failures = []
        bal_groups = []

        for group in groups:

            random_group = [random_perm(triad) for triad in group]
            first_node = GroupNode(random_group)
#            print first_node.group, first_node.distrib

            fringe = PriorityQueue(min, lambda node: node.score)
            fringe.append(first_node)

            if sum([sum(x) for x in first_node.distrib]) / len(first_node.distrib) == 3:
                goal_score = 0
            else:
                goal_score = len(first_node.distrib)

            visited = []
            while True:

                if len(fringe) == 0:
                    print 'No solutions'
                    break
                node = fringe.pop()
                visited.append(hash(tuple(node.group)))

                if len(visited) >= 10000:
#                    print 'failure!', len(visited)
                    failures.append(node.group)
                    break

                if node.score == goal_score:
#                    print
#                    print
#                    print 'success!', len(visited)
                    print node.group
                    bal_groups.append(node.group)
                    print
                    break

                succs = node.successors()
                new_succs = [x for x in succs if hash(tuple(x.group)) not in visited]

                fringe.extend(new_succs)
                gc.collect()

        groups = failures
        print 'looping', len(failures)
        return bal_groups
Пример #2
0
def main():

    if len(sys.argv) < 2:
        print 'Usage: python %s <number of pieces> <group size>' % (
            sys.argv[0])
        return
    if len(sys.argv) == 2:
        numpieces = int(sys.argv[1])
        gs = groupsizes(numpieces)
        if len(gs) == 1:
            numgroups = gs[0]
        else:
            print 'Usage: python %s %d [ %s ]' % (sys.argv[0], numpieces, \
                ' | '.join([str(i) for i in gs]))
            return

    if len(sys.argv) == 3:
        numpieces = int(sys.argv[1])
        numgroups = int(sys.argv[2])

    fringe = PriorityQueue(min, lambda node: node.score)

    node = firstNode(numpieces, numgroups)
    fringe.append(node)

    while True:
        if len(fringe) == 0:
            print 'No solutions'
            break
        node = fringe.pop()
        print node.score
        if node.score == 0:  # goal state
            break
        fringe.extend(node.successors())
        if len(fringe.A) > 100:
            fringe.A = fringe.A[:100]
        gc.collect()

    print node.score
    print node.groups
    print node.distrib

    bal_groups = bcbp_func.bcbp_secondpass(node.groups)

    print "-------------final result-------------"
    if len(bal_groups) == 0:
        print "failed"
    else:
        bcbp_func.write_stimlist(bal_groups)

        print "-------------SUCESSFUL !-------------"
Пример #3
0
def uniformCostSearch(problem):
    "** Search the node of least total cost first. **"   
    fringe = PriorityQueue()
    
    closed = {} # Bookeeping for visisted nodes
    fringe.push(Node(problem.initial))
    while fringe:
        node = fringe.pop()  # Choose a node to expand   
        if problem.goal_test(node.state):   # Check goal state
            return node
        if node.state not in closed:
            closed[node.state] = True       # Add state node to visited tree
            fringe.extend(node.expand(problem))  # Expanding node  
    return None
def cost_limited_astar_search(problem, limit, f):
    frontier = PriorityQueue("min", lambda node: f(node))
    frontier.append(Node(problem.initial))
    res = limit  # Limit as a placeholder value
    cutoff = False
    explored = set()
    while frontier:
        n = frontier.pop()
        if problem.goal_test(n.state):
            return 1, n
        if not cutoff and n not in explored:
            frontier.extend(n.expand(problem))
            explored.add(n)
        if f(n) > limit:
            cutoff = True
            res = f(n)
    return 0, res