def WriteConnectionsFromDictAndDeps(sPredDictFile, sDepsFile, sExactConnectionsFile, sLooseConnectionsFile):
    dDeps = data.file_to_obj_with_comments('dep.json');
    lPreds = predicate.PredDictFileToPredList(sPredDictFile);
    mConnDepthLoose = GenConnectionMatrix(dDeps, lPreds, bLoose = True);
    mConnDepthExact = GenConnectionMatrix(dDeps, lPreds, bLoose = False);
    #ComputeShortestPath(mConnDepth);
    WriteConnectionsFileFromMatrix(lPreds, mConnDepthLoose, sLooseConnectionsFile);
    WriteConnectionsFileFromMatrix(lPreds, mConnDepthExact, sExactConnectionsFile);
def LoadGoldStringConnSet():
    sGoldDepFile = config.get_string('GOLD_DEP_FILE');
    assert(sGoldDepFile != '');
    dGoldDeps = data.file_to_obj_with_comments(sGoldDepFile);
    setGoldConns = set();
    for sTo, dFrom in dGoldDeps.items():
        for sFrom in dFrom:
            if sFrom == 'num':
                continue;
            setGoldConns.add((sFrom, sTo));
    return setGoldConns;
    iNumGold = len(setGoldConns);
def GenAllGranularSamplesFromList(lSentences, sLogFileName):
    sSentenceLogFile = config.get_string('SENTENCE_LOG_FILE');
    fLog = open(sSentenceLogFile, 'w');
    lSamples = [];
    iNumLoopy = 0;
    for sentence in lSentences:
        iCurNumLoopy, lCurSamples = sentence.GenAllGranularSamples(fLog);
        lSamples.extend(lCurSamples);
        iNumLoopy += iCurNumLoopy;

    if iNumLoopy > 0:
        print "NUM LOOPY:", iNumLoopy;
    assert iNumLoopy < config.get_int('NUM_ALLOWED_LOOPY'), 'Too Many Loopy: ' + str(iNumLoopy) + ' NonLoopy: ' + str(len(lSamples));

    sGoldDepFile = config.get_string('GOLD_DEP_FILE');
    if sGoldDepFile != '':
        dGoldDeps = data.file_to_obj_with_comments(sGoldDepFile);
        # add the gold dep info
        for sample in lSamples:
            if (sample.pddlconn.sPddlTo in dGoldDeps) and (sample.pddlconn.sPddlFrom in dGoldDeps[sample.pddlconn.sPddlTo]):
                sample.bGoldPos = True;

    sPredDictFile = config.get_string('PRED_DICT_FILE');
    if sPredDictFile != '':
        lPredicates = predicate.PredDictFileToPredList(sPredDictFile);
        dObjToPredList = collections.defaultdict(lambda:[]);
        for predCur in lPredicates:
            dObjToPredList[predCur.GetObject()].append(predCur);
        for sample in lSamples:
            sample.pddlconn.lFromPreds = dObjToPredList[sample.pddlconn.sPddlFrom];
            sample.pddlconn.lToPreds = dObjToPredList[sample.pddlconn.sPddlTo];
    else:
        assert False;
    #prune the unecessary features
    dFeatureCounts = collections.defaultdict(lambda:0);
    for sample in lSamples:
        for iFeature in sample.features.GetFeatureIndexList():
            dFeatureCounts[iFeature] += 1;
    iMinFeatureCount = config.get_int('MIN_FEATURE_OCCURANCE_COUNT');
    for sample in lSamples:
        for iFeature in sample.features.GetFeatureIndexList():
            if dFeatureCounts[iFeature] < iMinFeatureCount:
                sample.features.RemoveFeature(iFeature);
    return lSamples;
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 Run3():
    dDeps = data.file_to_obj_with_comments('dep.json');
    lSubgoals = GenSubgoals(dDeps, "fish", 1);
    data.print_obj(lSubgoals);