示例#1
0
def init():
  if Settings.get('helper_tasks', True):
    import helper_tasks
    helper_tasks.main()


  def worker(line):
    try:
      result = execCommand(split(line), True)
      print '' if result is None else '%s\n' % str(result)

    except HandledException as e:
      err('%s\n' % e)

  if stdin.isatty(): # Enter interactive shell

    prompt = Settings.get('prompt', '>')

    while True:
      try:
        line = raw_input(prompt)

        if line:
          worker(line)

      except EOFError: # ^z (null character) was passed.
        exit()

  else:

    for line in stdin.readlines():
      worker(line.strip('\r\n ')) # #Pending: As of now, stdin should provide whole commands. Commands and argoments couldn't be separated into multiple lines (like with the interactive mode). Fix this,
示例#2
0
def aStar(initialState, goalState):
    closed = set()
    openedIPQ = IndexedPriorityQueue.IndexedPriorityQueue()

    currentIPQ = puzzle.Puzzle(arguments, initialState, goalState, 0)
    currentIPQ.compute()
    openedIPQ.append(currentIPQ)

    while hasAtLeastOneElem(openedIPQ.opened):
        currentIPQ = openedIPQ.pop()
        if currentIPQ.puzzle == goalState:
            traceRoute(currentIPQ, openedIPQ.allTimeOpened,
                       openedIPQ.maxSameTimeOpened)
        for n in currentIPQ.getNeighbours():
            neighbour = puzzle.Puzzle(arguments, n, goalState,
                                      currentIPQ.g + 1)
            neighbour.compute()
            neighbour.parent = currentIPQ

            if hashPuzzle(
                    neighbour.puzzle, neighbour.f
            ) in closed or openedIPQ.gotOpenedWithLowerCost(neighbour):
                continue
            openedIPQ.append(neighbour)

        closed.add(hashPuzzle(currentIPQ.puzzle, currentIPQ.f))

    helpers.exit("no solution found")
示例#3
0
def idaStar(initialState, goalState):
    current = puzzle.Puzzle(arguments, initialState, goalState, 0)
    current.compute()
    threshold = current.f

    while True:
        res = idaSearch(current, goalState, 0, threshold)
        if res == 0:
            helpers.exit("")
        if res > sys.maxsize:
            helpers.exit("no solution found")
        threshold = res
示例#4
0
def main(args):
    puzzleSize = parser.getPuzzleSize()
    puzzle = parser.getPuzzle(puzzleSize)

    print("Puzzle size: " + str(puzzleSize))
    print("Initial state:")

    helpers.debugPuzzle(puzzle)

    goal = solver.getFinalPuzzle(puzzleSize, args.goal)
    print("Goal state:")
    helpers.debugPuzzle(goal)

    if solver.isSolvable(puzzle, goal, args.goal) == False:
        helpers.exit("Puzzle is not solvable")

    solver.solve(args, puzzle, goal)
示例#5
0
def init():
  if Settings.get('helper_tasks', True):
    import helper_tasks
    helper_tasks.main()

  while True:
    try:
      line = raw_input('>')

      if line:
        result = execCommand(split(line), True)
        print '' if result is None else '%s\n' % str(result)

    except HandledException as e:
      err('%s\n' % e)

    except EOFError: # ^z (null character) was passed
      exit()
示例#6
0
    puzzleSize = parser.getPuzzleSize()
    puzzle = parser.getPuzzle(puzzleSize)

    print("Puzzle size: " + str(puzzleSize))
    print("Initial state:")

    helpers.debugPuzzle(puzzle)

    goal = solver.getFinalPuzzle(puzzleSize, args.goal)
    print("Goal state:")
    helpers.debugPuzzle(goal)

    if solver.isSolvable(puzzle, goal, args.goal) == False:
        helpers.exit("Puzzle is not solvable")

    solver.solve(args, puzzle, goal)

if __name__ == "__main__":
    argsParser = argparse.ArgumentParser(description='N-puzzle solver using A* or IDA algorithm')

    argsParser.add_argument('-a', '--algorithm', action='store', choices=['a', 'ida'], help='Specify the algorithm to solve the puzzle between A* and IDA*')
    argsParser.add_argument('--heuristic', action='store', choices=['misplacedTiles', 'manhattan', 'linearConflicts'], help='Specify the heuristic used')
    argsParser.add_argument('-s', '--search', action='store', choices=['heuristic', 'greedy', 'uniform'], help='Specify the searching logic')
    argsParser.add_argument('-g', '--goal', action='store', choices=['fill', 'snake'], help='Specify the goal state')

    args = argsParser.parse_args()

    if args.algorithm == 'ida' and args.search == 'greedy':
        helpers.exit('IDA* algorithm is not suported with greedy search')

    main(args)