#! /usr/bin/env python

"""
Simple experiment to run fMM on a single problem with different fraction parameters.
"""

import os
import os.path
import platform

from lab.environments import LocalEnvironment, BaselSlurmEnvironment

from downward.experiment import FastDownwardExperiment
from downward import suites
from downward.reports.absolute import AbsoluteReport
from downward.reports.summary import SummaryReport
from downward.reports.plotFMM import FmmPlotReport


# Suites
SUITE_OPTIMAL = [ # needs: BENCHMARKS_DIR
    'blocks', 'depot', 'driverlog', 'elevators-opt08-strips', 'elevators-opt11-strips',
    'floortile-opt11-strips', 'ged-opt14-strips', 'gripper', 'hiking-opt14-strips', 
    'logistics00', 'miconic', 'nomystery-opt11-strips', 'openstacks-opt08-strips', 
    'openstacks-strips', 'pegsol-08-strips', 'pegsol-opt11-strips', 'psr-small', 'rovers',
    'satellite', 'scanalyzer-08-strips', 'scanalyzer-opt11-strips', 'storage', 
    'termes-opt18-strips','tpp', 'transport-opt08-strips', 'transport-opt11-strips', 
    'transport-opt14-strips', 'trucks-strips', 'zenotravel']
SUITE_PROMISING_SINGLE_GOAL = [ # needs: BENCHMARK_DIR
    'blocks', 'custom', 'depot','driverlog','elevators-opt11-strips',
    'ged-opt14-strips', 'gripper', 'hiking-opt14-strips',
    'maintenance-opt14-adl', 'miconic',   
    'rovers'
]
SUITE_SINGLE_GOAL = [ # needs: BENCHMARK_DIR
    'gripper', 'depot','driverlog','elevators-opt11-strips',
    'ged-opt14-strips', 'hiking-opt14-strips',
    'maintenance-opt14-adl', 
    'miconic', 'custom', 'rovers', 'tile'
]
SUITE_SHORT = [ # needs: BENCHMARK_DIR
    'driverlog'
]
SUITE_TEST = [
    'driverlog:p01.pddl'
]




NODE = platform.node()
REMOTE = NODE.endswith(".scicore.unibas.ch") or NODE.endswith(".cluster.bc2.ch")
BENCHMARKS_DIR = os.environ["DOWNWARD_BENCHMARKS"]
BENCHMARK_DIR = os.environ["MASTER_BENCHMARK"]
REPO = os.environ['DOWNWARD_REPO']
REV = 'tip' # Branch here: defaut, tip, specific_revision, tag.
if REMOTE:
    ENV = BaselSlurmEnvironment(email="marvin.buff@unibas.ch")
    SUITE = SUITE_SINGLE_GOAL
    DIR = BENCHMARK_DIR
else:
    ENV = LocalEnvironment(processes=4)
    SUITE = SUITE_TEST
    DIR = BENCHMARKS_DIR

ATTRIBUTES_STANDARD = [
    'error', 'expanded', 'evaluated', 'cost', 'memory', # Standard 1
    'plan_length', 'planner_time', 'search_time', # Standard 2
    'ratio_fb_actions', 'ratio_fb_branching', 'meeting_point', # Combined forward/backward
    'ratio_jump_expanded', 'ratio_jump_evaluated', # after last jump
    'ratio_jump_generated', 'jump_expanded'] 
ATTRIBUTES_DIRECTIONAL = [ # forward/ backward 
    'f_expanded', 'b_expanded', 
    'f_evaluated', 'b_evaluated', 
    'f_generated', 'b_generated',
    'f_jump_expanded', 'b_jump_expanded']
ATTRIBUTES_BACKWARD = [ # specific backward
    'b_pruned', 'b_initial_goals']
ATTIRBUTES_EXTENDED = [ # unused but implemented
    'dead_ends', 'reopened', # Standard
    'frontier_meetings' ,'f_ex_before_meeting', 'b_ex_before_meeting' # Meetings
]

ATTRIBUTES = ATTRIBUTES_STANDARD + ATTRIBUTES_DIRECTIONAL + ATTRIBUTES_BACKWARD

# Create a new experiment.
exp = FastDownwardExperiment(environment=ENV)
# Add custom parser.
exp.add_parser(exp.EXITCODE_PARSER)
exp.add_parser(exp.TRANSLATOR_PARSER)
exp.add_parser(exp.SINGLE_SEARCH_PARSER)
exp.add_parser(exp.PLANNER_PARSER)
exp.add_parser('parsers/simple-parser.py')

DRIVER_OPTIONS = ['--overall-time-limit', '30m', '--overall-memory-limit', '3584M']

algo = 'hmax'

algo_front = "fmm("+algo+", b_eval="+algo+"(transform=backward), p="
algo_middle = [x * 0.1 for x in range(0,11)]
algo_end = ")"
algos = [algo_front+str(middle)+algo_end for middle in algo_middle]
algos_name = ['fmm('+algo+',p='+('%.2f' % middle)+algo_end for middle in algo_middle]

exp.add_suite(DIR, SUITE)
for index, algorithm in enumerate(algos):
    exp.add_algorithm(
        algos_name[index], REPO, REV, ['--search',algorithm], 
        driver_options=DRIVER_OPTIONS)
exp.add_algorithm(
    'astar_'+algo, REPO, REV, ['--search', 'astar('+algo+')'], 
    driver_options=DRIVER_OPTIONS)

##Add step that writes experiment files to disk.
exp.add_step('build', exp.build)

# Add step that executes all runs.
exp.add_step('start', exp.start_runs)

# Add step that collects properties from run directories and
# writes them to *-eval/properties.
exp.add_fetcher(name='fetch')

# Make a report.
exp.add_report(
    AbsoluteReport(attributes=ATTRIBUTES),
    outfile='report.html')

exp.add_report(
    FmmPlotReport(attributes=ATTRIBUTES, filter_algorithm=algos_name),
    outfile='plot.html')

# Parse the commandline and run the specified steps.
exp.run_steps()
