cmaclell/py_plan

Circular options to act?

Opened this issue · 0 comments

Cool and understandable approach!

I tried to change the book ordering example in a way, that it starts with owning a book 1, selling it for 10 and then it should buy the book 2 nine:

from operator import ge, add
from operator import sub
from functools import partial

from py_search.utils import compare_searches
from py_search.uninformed import depth_first_search

from py_plan.total_order import StateSpacePlanningProblem
from py_plan.base import Operator

buy = Operator('buy',
               [('Book', '?b'),
                ('Cost', '?b', '?c'),
                ('Money', '?m'),
                (ge, '?m', '?c')],
               [('Own', '?b'),
                ('not', ('Money', '?m')),
                ('Money', (sub, '?m', '?c'))], cost=2)

sell = Operator('sell',
               [('Book', '?b'),
                ('Cost', '?b', '?c'),
                ('Own', '?b'),
                ('Money', '?m'),
                ],
               [('Own', '?b'),
                ('Money', (add, '?m', '?c'))], cost=1)

start = [('Money', 0), ('Own', 'book1')]
for i in range(10):
    book = "book%s" % i
    start += [('Book', book), ('Cost', book, 10-i)]

goal = [('Own', 'book2')]

p = StateSpacePlanningProblem(start, goal, [buy, sell])


def progression(problem):
    return partial(depth_first_search, forward=True, backward=False)(problem)


def regression(problem):
    return partial(depth_first_search, forward=False, backward=True)(problem)

def bidirectional(problem):
    return partial(depth_first_search, forward=True, backward=True)(problem)


compare_searches([p], [progression,
                       regression,
                       bidirectional
                       ])
                       
                       
print(next(progression(p)).path())
path = next(regression(p)).path()

print(path[0][0])      

This seems to run from my perspective, but does not stop, maybe it is evaluating all the time selling and buying the same item again and again over and over and there should be a guard to prevent such circles?

Or what is happening now, when the script does not stop?