Пример #1
0
def random_search_2(solution):
    """ Random search to a solution. More informed than random_search.
    While not solved, the method chooses an unsatisfied vertex, with
    probability proportional to its demand. It then clears a route
    from the supply center to that vertex, and repeats.
    """
    vs = solution.vertices
    es = solution.edges
    while not solution.satisfied():
        #pick an unsatisfied node, and go there
        todo = list(solution.unsatisfied_vertices)
        pri = [t.demand for t in todo]
        if len(todo) != 0:
            target = choice_prob(todo, pri)
            source = choice(solution.supplies)
            [e.clear() for e in solution.decisions]
            path = min_path(vs, es, source, target)
            [e.restore() for e in solution.decisions]
            path = filter(lambda x: x not in solution.decisions, path)
            solution = solution.step_many(path)
        else:
            print 'defaulting to greedy'
            return greedy_search(solution)
    return solution
Пример #2
0
from network import min_path
from random import choice

v,e,r = read('cambridge')
supplies = [vv for vv in v if vv.supply > 0]
s = Solution(v, e, r, supplies)

s = greedy_search(s)
s.plot(out='nudge1.png')

d = s.decisions
truncate = 40
begin = d[:truncate]
satisfied = set([vv for ee in begin for vv in [ee.v1, ee.v2]])
unsatisfied = list(set(v).difference(satisfied))
target = choice(unsatisfied)

[ee.clear() for ee in begin]
middle = min_path(v, e, supplies[1], target)
[ee.restore() for ee in begin]
end = d[truncate:]

s2 = Solution(v, e, r, supplies, decisions = begin)
s2.plot(out='nudge2.png')

s3 = Solution(v, e, r, supplies, decisions = begin + middle)
s3.plot(out='nudge3.png')

s4 = Solution(v, e, r, supplies, decisions = begin + middle + end)
s4.plot(out='nudge4.png')