# Path: misc/profiling.py
# def run_profile(function):
#     pr = cProfile.Profile()  # cProfile.run('simulation()', sort = 'cumtime')
#     pr.enable()
#     results = function()
#     pr.disable()
#     return results, pr
# 
# def str_profile(pr, limit=25):
#     try:
#         from StringIO import StringIO
#     except ImportError:
#         from io import StringIO
#     output = StringIO()
#     ps = pstats.Stats(pr, stream=output)
#     # print(ps.__dict__.keys())
#     directory = os.getcwd()
#     for (f, l, fn), value in ps.stats.items():
#         if directory in f:
#             ps.stats[(f[f.index(directory) + len(directory):], l, fn)] = value
#             del ps.stats[(f, l, fn)]
#     ps.fcn_list = list(ps.stats.keys())
#     ps.sort_stats('tottime')  # .strip_dirs()
# 
#     ps.print_stats(limit)  # ps.print_stats(.1) # ps.reverse_order()
#     so = '\n' + '\n'.join(output.getvalue().split('\n')[::-1]).strip('\n')
#     # print('\n' + output.getvalue().strip('\n') + '\n')
#     output.close()
#     return so
# 
# Path: misc/functions.py
# def randomize(sequence):
#   shuffle(sequence)
#   return sequence
# 
# def elapsed_time(start_time):
#   return time.time() - start_time
# 
# Path: misc/utils.py
# SEPARATOR = '\n'+85*'-'+'\n'
# 
# Path: strips/utils.py
# def default_plan(initial, goal, operators, axioms=[], **kwargs):
#     #return default_search(initial, goal, single_generator(goal, operators, axioms, default_successors))
#     return solve_strips(initial, goal, operators, axioms=axioms, **kwargs)
# 
# SEARCHES = {
#     # TODO: make the key be the function name
#     'a_star': a_star_search,
#     'eager': best_first_search,
#     'lazy': deferred_best_first_search,
#     'hill_climbing': hill_climbing_search,
#     'random_walk': random_walk,
#     'mcts': mcts,
# }
# 
# EVALUATORS = {
#     'bfs': bfs,
#     'uniform': uniform,
#     'astar': astar,
#     'wastar2': wastar2,
#     'wastar3': wastar3,
#     'greedy': greedy,
# }
# 
# HEURISTICS = {
#     'blind': h_blind,
#     'goal': h_goal,
#     'max': h_max,
#     'add': h_add,
#     'ff': h_ff_add,
# }
# 
# SUCCESSORS = {
#     'all': ha_all, # ha_all | ha_applicable
#     'random': ha_random,
#     #'ff': first_goals, # first_operators # TODO: make a wrapper
# }


import argparse
import random
import strips.domains as domains
from time import time
from misc.profiling import run_profile, str_profile
from misc.functions import randomize, elapsed_time
from misc.utils import SEPARATOR
from strips.utils import default_plan, SEARCHES, EVALUATORS, HEURISTICS, SUCCESSORS
    initial, goal, operators = problem()
    operators = randomize(operators)

    #dt = datetime.datetime.now()
    #directory = DIRECTORY + '{}/{}/{}/'.format(problem.__name__,
    # dt.strftime('%Y-%m-%d'), dt.strftime('%H-%M-%S'))
    print('Solving strips problem ' + problem.__name__ + '\n' + SEPARATOR)

    def execute():
        start_time = time()
        try:
            output = default_plan(initial, goal, operators, debug=None, **kwargs) # None | simple_debug
        except KeyboardInterrupt:
            output = None, elapsed_time(start_time)
        #make_dir(directory)
        #print('Created directory:', directory)
        return output

    (plan, state_space), profile_output = run_profile(execute)
    #print('Wrote', directory+'profile')
    print(SEPARATOR)

    data = (str(plan) if (plan is not None) else 'Unable to find a plan') + '\n\n' + str(state_space)
    print(data)
    #write(directory + 'planner_statistics', data)
    #print('Wrote', directory+'planner_statistics')

    if print_profile:
        print(SEPARATOR)
        print(str_profile(profile_output))

# Path: misc/profiling.py
# def run_profile(function):
#     pr = cProfile.Profile()  # cProfile.run('simulation()', sort = 'cumtime')
#     pr.enable()
#     results = function()
#     pr.disable()
#     return results, pr
# 
# def str_profile(pr, limit=25):
#     try:
#         from StringIO import StringIO
#     except ImportError:
#         from io import StringIO
#     output = StringIO()
#     ps = pstats.Stats(pr, stream=output)
#     # print(ps.__dict__.keys())
#     directory = os.getcwd()
#     for (f, l, fn), value in ps.stats.items():
#         if directory in f:
#             ps.stats[(f[f.index(directory) + len(directory):], l, fn)] = value
#             del ps.stats[(f, l, fn)]
#     ps.fcn_list = list(ps.stats.keys())
#     ps.sort_stats('tottime')  # .strip_dirs()
# 
#     ps.print_stats(limit)  # ps.print_stats(.1) # ps.reverse_order()
#     so = '\n' + '\n'.join(output.getvalue().split('\n')[::-1]).strip('\n')
#     # print('\n' + output.getvalue().strip('\n') + '\n')
#     output.close()
#     return so
# 
# Path: misc/functions.py
# def randomize(sequence):
#   shuffle(sequence)
#   return sequence
# 
# def elapsed_time(start_time):
#   return time.time() - start_time
# 
# Path: misc/utils.py
# SEPARATOR = '\n'+85*'-'+'\n'
# 
# Path: strips/utils.py
# def default_plan(initial, goal, operators, axioms=[], **kwargs):
#     #return default_search(initial, goal, single_generator(goal, operators, axioms, default_successors))
#     return solve_strips(initial, goal, operators, axioms=axioms, **kwargs)
# 
# SEARCHES = {
#     # TODO: make the key be the function name
#     'a_star': a_star_search,
#     'eager': best_first_search,
#     'lazy': deferred_best_first_search,
#     'hill_climbing': hill_climbing_search,
#     'random_walk': random_walk,
#     'mcts': mcts,
# }
# 
# EVALUATORS = {
#     'bfs': bfs,
#     'uniform': uniform,
#     'astar': astar,
#     'wastar2': wastar2,
#     'wastar3': wastar3,
#     'greedy': greedy,
# }
# 
# HEURISTICS = {
#     'blind': h_blind,
#     'goal': h_goal,
#     'max': h_max,
#     'add': h_add,
#     'ff': h_ff_add,
# }
# 
# SUCCESSORS = {
#     'all': ha_all, # ha_all | ha_applicable
#     'random': ha_random,
#     #'ff': first_goals, # first_operators # TODO: make a wrapper
# }


# Path: solve_strips.py
import argparse
import random
import strips.domains as domains
from time import time
from misc.profiling import run_profile, str_profile
from misc.functions import randomize, elapsed_time
from misc.utils import SEPARATOR
from strips.utils import default_plan, SEARCHES, EVALUATORS, HEURISTICS, SUCCESSORS

    initial, goal, operators = problem()
    operators = randomize(operators)

    #dt = datetime.datetime.now()
    #directory = DIRECTORY + '{}/{}/{}/'.format(problem.__name__,
    # dt.strftime('%Y-%m-%d'), dt.strftime('%H-%M-%S'))
    print('Solving strips problem ' + problem.__name__ + '\n' + SEPARATOR)

    def execute():
        start_time = time()
        try:
            output = default_plan(initial, goal, operators, debug=None, **kwargs) # None | simple_debug
        except KeyboardInterrupt:
            output = None, elapsed_time(start_time)
        #make_dir(directory)
        #print('Created directory:', directory)
        return output

    (plan, state_space), profile_output = run_profile(execute)
    #print('Wrote', directory+'profile')
    print(SEPARATOR)

    data = (str(plan) if (plan is not None) else 'Unable to find a plan') + '\n\n' + str(state_space)
    print(data)
    #write(directory + 'planner_statistics', data)
    #print('Wrote', directory+'planner_statistics')

    if print_profile:
        print(SEPARATOR)
        print(str_profile(profile_output))

    print(SEPARATOR)