def FixObjectives(sInFile, sOutFile):
    lOldObjectives = data.file_to_obj(sInFile);
    lNewObjectives = [];
    for tObjective in lOldObjectives:
        lObjective = [tObjective[0], tObjective[1]+1];
        lNewObjectives.append(lObjective);
    data.obj_to_file(lNewObjectives,sOutFile);
def test_json():
    dNate = {};

    dNate[0] = [{1:3, 2:4},2,3];
    dNate[5] = [4,5,6];

    data.obj_to_file(dNate, "test.json");
def ExtractInitGoalPreds(sProblemPath, sDomainFile, sGoldSubgoalsFile, sAugmentedGoldSubgoalsFile):
    lGold = data.file_to_obj(sGoldSubgoalsFile);
    for dGold in lGold[:min(len(lGold), MAXNUMPROBLEMS)]:
        sProblemFile = sProblemPath + dGold['file'];
        domain = pddl.Domain(sDomainFile, sProblemFile, 0);
        lInitPreds = Predicate.GetInitPredList(domain);
        lGoalPreds = Predicate.GetGoalPredList(domain);
        dGold['init'] = lInitPreds;
        dGold['goal'] = lGoalPreds;
    data.obj_to_file(lGold, sAugmentedGoldSubgoalsFile);
def GetAllObjectives(sDomainFile, sDir):
    #dir = open(sDir);
    lObjectives = [];
    for item in os.listdir(sDir):
        if (item.startswith('test.0.pddl') or
            item.startswith('tallgrass.34.pddl')):
            continue;
        sFullPath = sDir+item;
        print "objective for:", item;
        if os.path.isfile(sFullPath):
            tObjective = GetOneObjective(sDomainFile, sFullPath);
            print item, tObjective;
            lObjectives.append([item, tObjective[0], tObjective[1]]);
    data.obj_to_file(lObjectives, 'objectives.json');
def GetSubgoalsForAll(bValidate, bRebuildObjectives):
    sDomainFile = '../subgoal_learning/data/domain-no-stone-iron-tools-simple-furnace.v120.pddl';
    sProblemPath = '../subgoal_learning/data/problems/no-stone-iron-tools-and-extra-resources-rand2/'
    sFFPath = '/home/nkushman/hierarchical_planning/ff/metric-ff-recompiled-2011-11-24-2';
    sMainPath = '/home/nkushman/hierarchical_planning/model/subgoal_learning/lhla_v4 /home/nkushman/hierarchical_planning/model/subgoal_learning/run_compute_end_state.cfg';
    sNumSubgoalsFile = 'thing-available_max5.gold_num_subgoals';
    dDeps = data.file_to_obj_with_comments('dep.json');
    if bRebuildObjectives:
        GetAllObjectives(sDomainFile, sProblemPath);
    lObjectives = data.file_to_obj('objectives.json');
    setObjectives = set();
    lOutput = [];
    for iIndex, tObjective in enumerate(lObjectives):
        dCurOutput = {'file':tObjective[0], 'thing':tObjective[1], 'num':tObjective[2]}
        if iIndex < 0:
            print "Skipping:", iIndex, tObjective;
            continue;
        sys.stdout.flush();
        print "****Objective:", iIndex, tObjective;
        lSubgoals = GenSubgoals(dDeps, tObjective[1], tObjective[2]);
        lSubgoalsToValidate = TransformToValidateFormat(lSubgoals);
        print "Plan:";
        data.print_obj(lSubgoalsToValidate);
        sTempProblemPath = 'tmp-no-shovel/test.' + tObjective[0] + '.subgoals';
        if bValidate:
            bSuccess = SubgoalValidator.TestSubgoals(sDomainFile, sProblemPath + tObjective[0], lSubgoalsToValidate,
                                                     sFFPath, sMainPath, sTempProblemPath, bOptimize = False);
            dCurOutput['success'] = bSuccess;
            print "Success:", bSuccess;
        # include them all
        for dSubgoal in lSubgoals:
            setObjectives.add(FormatSubgoal(dSubgoal));

        dCurOutput['subgoals'] = lSubgoals;
        dCurOutput['subgoals-formatted'] = lSubgoalsToValidate;
        dCurOutput['index'] = iIndex;
        lOutput.append(dCurOutput);
        #data.print_obj(lSubgoals);
    data.obj_to_file(lOutput, 'subgoals_gold.json');
    dIndexToPred, dPredToIndex = GenerateIndexesFromPredDict('pred_gold.txt', setObjectives);
    WriteConnectionsFile(dDeps, dPredToIndex, dIndexToPred, 'pred_gold_connections.txt');
    WriteNumSubgoalsFile(lOutput, sNumSubgoalsFile);
def CalcEasyHardConnections():
    sPartialDir = '/nfs2/hierarchical_planning/branavan/output/atdi1_fw_';
    dCounts = collections.defaultdict(lambda:[0,0]);
    for i in range(1, 201):
        bStartedRelevant = False;
        for sLine in open(sPartialDir + str(i) + '/run.log'):
            sLine = sLine.strip();
            if not bStartedRelevant:
                if sLine.startswith('List of solved problems'):
                    bStartedRelevant = True;
                continue;
            else:
                if sLine.startswith('----'):
                    continue;
                sName = sLine.split('.')[0];
                bNotSolved = (sLine.split()[1].startswith('[NOT'));
                if bNotSolved:
                    dCounts[sName][0] += 1;
                else:
                    #print "Not Solved:" + sLine.split()[1] + ":";
                    dCounts[sName][1] += 1;

    lFirst30 = [];
    for sName, (iFailedCount, iSolvedCount) in dCounts.items():
        if iSolvedCount == 400:
            lFirst30.append(sName);
    data.obj_to_file(lFirst30, 'first30.json');

    # sorted 
    lSortedTups = sorted(dCounts.items(), key=lambda tup:tup[1][1]);
    lSortedObjs = map(lambda x:x[0], lSortedTups);
    data.obj_to_file(lSortedObjs, 'sorted.json');
    iSplit = int(len(lSortedObjs)/2.0);
    lEasy = lSortedObjs[iSplit:];
    data.obj_to_file(lEasy, 'easy.json');
def LoadFullRewardsDict():
    lRewards = LoadAllFullRewardsFiles();
    data.obj_to_file(lRewards, 'debug.json');
    print "LenRewards:", len(lRewards);
    dConnRewards = Reward.ComputeCompressedRewardFromList(lRewards);
    return dConnRewards;